-
Notifications
You must be signed in to change notification settings - Fork 420
/
entry_v3.go
146 lines (121 loc) · 3.67 KB
/
entry_v3.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
"flag"
"github.com/davyxu/tabtoy/build"
"github.com/davyxu/tabtoy/v3/compiler"
"github.com/davyxu/tabtoy/v3/gen"
"github.com/davyxu/tabtoy/v3/gen/bindata"
"github.com/davyxu/tabtoy/v3/gen/cssrc"
"github.com/davyxu/tabtoy/v3/gen/gosrc"
"github.com/davyxu/tabtoy/v3/gen/javasrc"
"github.com/davyxu/tabtoy/v3/gen/jsondata"
"github.com/davyxu/tabtoy/v3/gen/jsontype"
"github.com/davyxu/tabtoy/v3/gen/luasrc"
"github.com/davyxu/tabtoy/v3/gen/pbdata"
"github.com/davyxu/tabtoy/v3/gen/pbsrc"
"github.com/davyxu/tabtoy/v3/helper"
"github.com/davyxu/tabtoy/v3/model"
"github.com/davyxu/tabtoy/v3/report"
"os"
)
type V3GenEntry struct {
name string
genSingleFile gen.GenSingleFile
genCustom gen.GenCustom
param *string
}
// v3新增
var (
paramIndexFile = flag.String("index", "", "input multi-files configs")
paramTagAction = flag.String("tag_action", "", "do action by tag selected target, format: action1:tag1+tag2|action2:tag1+tag3")
v3GenList = []V3GenEntry{
{name: "gosrc", genSingleFile: gosrc.Generate, param: paramGoOut},
{name: "jsondata", genSingleFile: jsondata.Generate, param: paramJsonOut},
{name: "jsontype", genSingleFile: jsontype.Generate, param: paramJsonTypeOut},
{name: "luasrc", genSingleFile: luasrc.Generate, param: paramLuaOut},
{name: "cssrc", genSingleFile: cssrc.Generate, param: paramCSharpOut},
{name: "bindata", genSingleFile: bindata.Generate, param: paramBinaryOut},
{name: "javasrc", genSingleFile: javasrc.Generate, param: paramJavaOut},
{name: "pbsrc", genSingleFile: pbsrc.Generate, param: paramProtoOut},
{name: "pbdata", genSingleFile: pbdata.Generate, param: paramPbBinaryOut},
{name: "jsondir", genCustom: jsondata.Output, param: paramJsonDir},
{name: "luadir", genCustom: luasrc.Output, param: paramLuaDir},
{name: "binarydir", genCustom: bindata.Output, param: paramBinaryDir},
{name: "pbdatadir", genCustom: pbdata.Output, param: paramPbBinaryDir},
}
)
func genFile(globals *model.Globals, entry V3GenEntry, c chan error) {
filename := *entry.param
if entry.genSingleFile != nil {
if data, err := entry.genSingleFile(globals); err != nil {
c <- err
} else {
report.Log.Infof(" [%s] %s", entry.name, filename)
err = helper.WriteFile(filename, data)
if err != nil {
c <- err
}
}
}
if entry.genCustom != nil {
if err := entry.genCustom(globals, *entry.param); err != nil {
c <- err
} else {
report.Log.Infof(" [%s] %s", entry.name, filename)
}
}
c <- nil
}
func GenFileByList(globals *model.Globals) error {
var errList []chan error
for _, entry := range v3GenList {
if *entry.param == "" {
continue
}
c := make(chan error)
errList = append(errList, c)
go genFile(globals, entry, c)
}
for _, c := range errList {
err := <-c
if err != nil {
return err
}
}
return nil
}
func V3Entry() {
globals := model.NewGlobals()
globals.Version = build.Version
globals.ParaLoading = *paramPara
if *paramUseCache {
globals.CacheDir = *paramCacheDir
os.Mkdir(globals.CacheDir, 0666)
}
globals.IndexFile = *paramIndexFile
globals.PackageName = *paramPackageName
globals.CombineStructName = *paramCombineStructName
globals.GenBinary = *paramBinaryOut != "" || *paramBinaryDir != ""
idxloader := helper.NewFileLoader(true, globals.CacheDir)
globals.IndexGetter = idxloader
var err error
if *paramTagAction != "" {
globals.TagActions, err = model.ParseTagAction(*paramTagAction)
if err != nil {
goto Exit
}
}
err = compiler.Compile(globals)
if err != nil {
goto Exit
}
report.Log.Debugln("Generate files...")
err = GenFileByList(globals)
if err != nil {
goto Exit
}
return
Exit:
report.Log.Errorln(err)
os.Exit(1)
}