-
Notifications
You must be signed in to change notification settings - Fork 329
/
codegen.go
304 lines (244 loc) · 7.23 KB
/
codegen.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package codegen
import (
"flag"
"fmt"
"os"
"path/filepath"
"strings"
"text/template"
"github.com/Masterminds/sprig"
"github.com/fsnotify/fsnotify"
)
func Proc() {
const (
docGenBase = "/generated/partials"
)
var (
err error
watchChanges bool
beVerbose bool
docPath string
genCode = true
genDocs = false
fileList []string
watcher *fsnotify.Watcher
templatesPath = filepath.Join("pkg", "codegen", "assets", "*.tpl")
templatesSrc []string
actionSrcPath = filepath.Join("*", "service", "*_actions.yaml")
actionSrc []string
actionDefs []*actionsDef
eventSrcPath = filepath.Join("*", "service", "event", "events.yaml")
eventSrc []string
eventDefs []*eventsDef
// workaround because
// filepath.Join merges "*","*" into "**" instead of "*/*"
pkgTypeSrcPath = filepath.Join("*"+string(filepath.Separator)+"*"+string(filepath.Separator)+"*", "types.yaml")
typeSrcPath = filepath.Join("*"+string(filepath.Separator)+"*", "types.yaml")
typeSrc []string
typeDefs []*typesDef
// workaround because
// filepath.Join merges "*","*" into "**" instead of "*/*"
exprTypeSrcPath = filepath.Join("*"+string(filepath.Separator)+"*", "expr_types.yaml")
exprTypeSrc []string
exprTypeDefs []*exprTypesDef
restSrcPath = filepath.Join("*", "rest.yaml")
restSrc []string
restDefs []*restDef
aFuncsSrcPath = filepath.Join("*", "automation", "*_handler.yaml")
aFuncsSrc []string
aFuncsDefs []*aFuncDefs
tpls *template.Template
tplBase = template.New("").
Funcs(map[string]interface{}{
"camelCase": camelCase,
"kebabCase": kebabCase,
"export": export,
"unexport": unexport,
"removePtr": removePtr,
"hasPtr": hasPtr,
"toggleExport": toggleExport,
"toLower": strings.ToLower,
"toUpper": strings.ToUpper,
"cc2underscore": cc2underscore,
"normalizeImport": normalizeImport,
"comment": func(text string, skip1st bool) string {
ll := strings.Split(text, "\n")
s := 0
out := ""
if skip1st {
s = 1
out = ll[0] + "\n"
}
for ; s < len(ll); s++ {
out += "// " + ll[s] + "\n"
}
return out
},
}).
Funcs(sprig.TxtFuncMap())
output = func(format string, aa ...interface{}) {
if beVerbose {
fmt.Fprintf(os.Stdout, format, aa...)
}
}
outputErr = func(err error, format string, aa ...interface{}) bool {
if err != nil {
fmt.Fprintf(os.Stdout, format, aa...)
fmt.Fprintf(os.Stdout, "%v\n", err)
return true
}
return false
}
)
flag.BoolVar(&watchChanges, "w", false, "regenerate on change of template or definition files")
flag.BoolVar(&beVerbose, "v", false, "output loaded definitions, templates and outputs")
flag.StringVar(&docPath, "d", "", "generate docs on template or definition change")
flag.Parse()
defer func() {
if watcher != nil {
watcher.Close()
}
}()
if len(docPath) > 0 {
docPath = strings.TrimRight(docPath, "/") + "/src/modules"
if i, err := os.Stat(docPath); err != nil {
handleError(err)
} else if !i.IsDir() {
handleError(fmt.Errorf("expecting directory: %q", docPath))
}
genDocs = true
}
for {
fileList = make([]string, 0, 100)
templatesSrc = glob(templatesPath)
output("loaded %d templates from %s\n", len(templatesSrc), templatesPath)
actionSrc = glob(actionSrcPath)
output("loaded %d action definitions from %s\n", len(actionSrc), actionSrcPath)
eventSrc = glob(eventSrcPath)
output("loaded %d event definitions from %s\n", len(eventSrc), eventSrcPath)
typeSrc = glob(typeSrcPath)
typeSrc = append(typeSrc, glob(pkgTypeSrcPath)...)
output("loaded %d type definitions from %s\n", len(typeSrc), typeSrcPath)
exprTypeSrc = glob(exprTypeSrcPath)
output("loaded %d exprType definitions from %s\n", len(exprTypeSrc), exprTypeSrcPath)
restSrc = glob(restSrcPath)
output("loaded %d rest definitions from %s\n", len(restSrc), restSrcPath)
aFuncsSrc = glob(aFuncsSrcPath)
output("loaded %d function definitions from %s\n", len(aFuncsSrc), aFuncsSrcPath)
if watchChanges {
if watcher != nil {
watcher.Close()
}
watcher, err = fsnotify.NewWatcher()
handleError(err)
fileList = append(fileList, templatesSrc...)
fileList = append(fileList, actionSrc...)
fileList = append(fileList, eventSrc...)
fileList = append(fileList, typeSrc...)
fileList = append(fileList, exprTypeSrc...)
fileList = append(fileList, restSrc...)
fileList = append(fileList, aFuncsSrc...)
for _, d := range fileList {
handleError(watcher.Add(d))
}
}
func() {
tpls, err = tplBase.ParseFiles(templatesSrc...)
if outputErr(err, "could not parse templates:\n") {
return
}
if actionDefs, err = procActions(actionSrc...); err == nil {
if genCode {
err = genActions(tpls, actionDefs...)
}
}
if outputErr(err, "failed to process actions:\n") {
return
}
if exprTypeDefs, err = procExprTypes(exprTypeSrc...); err == nil {
if genCode {
err = genExprTypes(tpls, exprTypeDefs...)
}
if genDocs && err == nil {
err = genExprTypeDocs(tpls, docPath+docGenBase, exprTypeDefs...)
}
}
if outputErr(err, "failed to process expr types:\n") {
return
}
if eventDefs, err = procEvents(eventSrc...); err == nil {
if genCode {
expandEventTypes(eventDefs, exprTypeDefs)
err = genEvents(tpls, eventDefs...)
}
if genDocs && err == nil {
err = genEventsDocs(tpls, docPath+docGenBase, eventDefs...)
}
}
if outputErr(err, "failed to process events:\n") {
return
}
if typeDefs, err = procTypes(typeSrc...); err == nil {
if genCode {
err = genTypes(tpls, typeDefs...)
}
}
if outputErr(err, "failed to process types:\n") {
return
}
if restDefs, err = procRest(restSrc...); err == nil {
if genCode {
err = genRest(tpls, restDefs...)
}
}
if outputErr(err, "failed to process rest:\n") {
return
}
if outputErr(err, "failed to process store:\n") {
return
}
if outputErr(err, "fail to process options:\n") {
return
}
if aFuncsDefs, err = procAutomationFunctions(aFuncsSrc...); err == nil {
if genCode {
expandAutomationFunctionTypes(aFuncsDefs, exprTypeDefs)
err = genAutomationFunctions(tpls, aFuncsDefs...)
}
if genDocs && err == nil {
err = genAutomationFunctionDocs(tpls, docPath+docGenBase, aFuncsDefs...)
}
}
if outputErr(err, "failed to process automation functions:\n") {
return
}
}()
if !watchChanges {
break
}
// @todo fix this (without causing too many "too-many-files" issues :)
output("waiting for changes (if you add a new file, restart codegen manually)\n")
select {
case <-watcher.Events:
case err = <-watcher.Errors:
handleError(err)
}
}
}
func glob(path string) []string {
src, err := filepath.Glob(path)
if err != nil {
handleError(fmt.Errorf("failed to glob %q: %w", path, err))
}
return src
}
// Similar to cli.HandleError but without cli pkg dependencies
//
// pkg/cli deps can give us issues when generating go files with invalid code
func handleError(err error) {
if err == nil {
return
}
_, _ = fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}