-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbootstrap.go
437 lines (378 loc) · 12.7 KB
/
bootstrap.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
package compiler
import (
"fmt"
"go/ast"
"go/build"
"go/format"
"go/parser"
"go/token"
"os"
"path/filepath"
"github.com/mh-cbon/export-funcmap/export"
)
// this is only for the tests.
var emptyFunc map[string]interface{}
// GenerateProgramBootstrap generates the bootstrap program that handles
// the compilation of the templates from the source file imports
// and the configuration variable declaration ast.
func GenerateProgramBootstrap(
importsContext []*ast.ImportSpec,
confNode *ast.GenDecl,
varName string,
) (string, error) {
programImport := export.NewImportDecl()
programImport.Lparen = token.Pos(1)
export.InjectImportPaths([]string{
"github.com/mh-cbon/template-compiler/compiler",
"fmt",
}, programImport)
newImports, err := prepareConfiguration(importsContext, confNode)
if err != nil {
return "", err
}
for _, i := range newImports {
programImport.Specs = append(programImport.Specs, i)
}
return makeProgram(programImport, confNode, varName), nil
}
// GenerateProgramBootstrapFromAstFile generates the bootstrap program that handles
// the compilation of the templates from a parsed go source.
func GenerateProgramBootstrapFromAstFile(
parsedFile *ast.File,
varName string,
) (string, error) {
var configurationVar *ast.GenDecl
configurationVar = extractVar(parsedFile, varName)
if configurationVar == nil {
return "", fmt.Errorf("Configuration variable %v not found in %v", varName, parsedFile.Name.String())
}
importsContext := extractImports(parsedFile)
return GenerateProgramBootstrap(importsContext, configurationVar, varName)
}
// GenerateProgramBootstrapFromFile generates the bootstrap program that handles
// the compilation of the templates from a file path.
func GenerateProgramBootstrapFromFile(
file string,
varName string,
) (string, error) {
parsedFile, err := parseGoFilePath(file)
if err != nil {
return "", err
}
return GenerateProgramBootstrapFromAstFile(parsedFile, varName)
}
// GenerateProgramBootstrapFromString generates the bootstrap program that handles
// the compilation of the templates from a go code string.
func GenerateProgramBootstrapFromString(
content string,
varName string,
) (string, error) {
parsedFile, err := parseGoString(content)
if err != nil {
return "", err
}
return GenerateProgramBootstrapFromAstFile(parsedFile, varName)
}
func makeProgram(imports *ast.GenDecl, confNode *ast.GenDecl, varName string) string {
programMain := fmt.Sprintf(`package main
%v
%v
func main () {
compiler := compiler.NewCompiledTemplatesProgram(%q)
if err := compiler.CompileAndWrite(%v); err != nil {
panic(fmt.Errorf("Failed to compile the templates: %%v", err))
}
}
`,
astNodeToString(imports),
astNodeToString(confNode),
varName,
varName,
)
return formatGoCode(programMain)
}
// prepareConfiguration takes the configuration ast.Node and completes it
func prepareConfiguration(importsContext []*ast.ImportSpec, confNode *ast.GenDecl) ([]*ast.ImportSpec, error) {
newImports := []*ast.ImportSpec{}
compiledNew := confNode.Specs[0].(*ast.ValueSpec).Values[0].(*ast.CallExpr)
// need to check the Fun Call is compiled.New,
// it might not be if the callexpr compiled.New is followed
// byt a SetPkg(...) call,
// in such case callExpr.Fun.X is not an ast.ident.
if _, ok := compiledNew.Fun.(*ast.SelectorExpr).X.(*ast.Ident); ok == false {
// not compiled.New
compiledNew = compiledNew.Fun.(*ast.SelectorExpr).X.(*ast.CallExpr)
}
// in the original program, we know compiled package is imported,
// but it may use an alias, lets grab this now.
compiledAlias := compiledNew.Fun.(*ast.SelectorExpr).X.(*ast.Ident).Name
if compiledAlias == "compiled" {
compiledAlias = ""
}
s := export.NewImportSpec("github.com/mh-cbon/template-compiler/compiled", compiledAlias)
newImports = append(newImports, s)
// in New(outpath string, templates []TemplateConfiguration, funcsmap ...string) *Configuration
// find funcsmap arguments and add them to allTemplatesFuncs
allTemplatesFuncs := []string{}
if len(compiledNew.Args) > 2 {
for _, a := range compiledNew.Args[2:] {
arg := a.(*ast.BasicLit)
value := arg.Value[1 : len(arg.Value)-1] // remove quotes
if value != "" {
allTemplatesFuncs = append(allTemplatesFuncs, value)
}
}
}
// ensure the template configuration contains text template std funcs
if containsStr(allTemplatesFuncs, "text/template:builtins") == false {
allTemplatesFuncs = append(allTemplatesFuncs, "text/template:builtins")
}
// ensure the template configuration contains template-tree-simplifier funcs
if containsStr(allTemplatesFuncs, "github.com/mh-cbon/template-tree-simplifier/funcmap:tplFunc") == false {
allTemplatesFuncs = append(allTemplatesFuncs, "github.com/mh-cbon/template-tree-simplifier/funcmap:tplFunc")
}
// in New(outpath string, templates []TemplateConfiguration, funcsmap ...string) *Configuration
// find templates argument, then look for each compiled.TemplateConfiguration{},
// then browse each key / value expression,
// - for an HTML key, check if it says true/false
// - for a Data key, creates and adds a DataConfiguration{}
// - for a TemplatesData key, searches for all related package and import them
// - for a FuncsMap key, exports them to their symbolic version, and their public idents,
// add those new data to the configuration of the template.
templatesConf := compiledNew.Args[1].(*ast.CompositeLit)
for _, t := range templatesConf.Elts {
templateConf := t.(*ast.CompositeLit)
// manage HTML key
isHTML := isAnHTMLTemplateConf(templateConf)
// search for data packages and import them
for _, i := range getDataImports(importsContext, templateConf) {
if containsImportSpec(newImports, i) == false {
newImports = append(newImports, i)
}
}
// manage FuncsMap key
var varToExport []string
if funcsMapKey := getKeyValue(templateConf, "FuncsMap"); funcsMapKey != nil {
varToExport = getFuncsMapKeyValues(funcsMapKey.Value.(*ast.CompositeLit))
}
varToExport = append(varToExport, allTemplatesFuncs...)
// ensure the template configuration contains html template std funcs
if isHTML {
if containsStr(varToExport, "github.com/mh-cbon/template-compiler/std/html/template:publicFuncMap") == false {
varToExport = append(varToExport, "github.com/mh-cbon/template-compiler/std/html/template:publicFuncMap")
}
}
if len(varToExport) > 0 {
funcsMapValue, publicIdentValue, imports, err := exportFuncsMap(varToExport)
if err != nil {
return newImports, err
}
kvFuncsExport := &ast.KeyValueExpr{
Key: &ast.Ident{Name: "FuncsExport"},
Value: funcsMapValue,
}
templateConf.Elts = append(templateConf.Elts, kvFuncsExport)
kvPublicIdents := &ast.KeyValueExpr{
Key: &ast.Ident{Name: "PublicIdents"},
Value: publicIdentValue,
}
templateConf.Elts = append(templateConf.Elts, kvPublicIdents)
newImports = append(newImports, imports...)
}
}
return newImports, nil
}
// isAnHTMLTemplateConf tells if a TemplateConfiguration contains an HTML key and its value is true
func isAnHTMLTemplateConf(templateConf *ast.CompositeLit) bool {
isHTML := false
if isHTMLKey := getKeyValue(templateConf, "HTML"); isHTMLKey != nil {
if x, ok := isHTMLKey.Value.(*ast.Ident); ok == false {
panic(fmt.Errorf("Unexpected node\n%v\n%#v", isHTMLKey.Value, isHTMLKey.Value))
} else {
isHTML = x.Name == "true"
}
}
return isHTML
}
// from a ast.CompositeLit such
// TypeOfData{Key: value}, extract the key/value
// matching search
func getKeyValue(templateConf *ast.CompositeLit, search string) *ast.KeyValueExpr {
for _, keyValue := range templateConf.Elts {
keyValueExpr := keyValue.(*ast.KeyValueExpr)
if ident, ok := keyValueExpr.Key.(*ast.Ident); ok {
if ident.Name == search {
return keyValueExpr
}
}
}
return nil
}
// transforms the ast.Node of a value
// []string{"",""...} into a slice of string values.
func getFuncsMapKeyValues(value *ast.CompositeLit) []string {
var varToExport []string
for _, v := range value.Elts {
value := v.(*ast.BasicLit).Value
value = value[1 : len(value)-1]
if value != "" {
varToExport = append(varToExport, value)
}
}
return varToExport
}
func exportFuncsMap(funcExports []string) (*ast.CompositeLit, *ast.CompositeLit, []*ast.ImportSpec, error) {
imports := []*ast.ImportSpec{}
targets := export.Targets{}
if err := targets.Parse(funcExports); err != nil {
return nil, nil, nil, err
}
resFile, err := export.Export(targets, "gen.go", "main", "funcsMap")
if err != nil {
return nil, nil, nil, err
}
funcsMapDecl := export.GetVarDecl(resFile, "funcsMap")
publicIdentsDecl := export.GetVarDecl(resFile, "funcsMapPublic")
importDecl := export.MustGetImportDecl(resFile)
funcsMapValue := funcsMapDecl.Specs[0].(*ast.ValueSpec).Values[0].(*ast.CompositeLit)
publicIdentValue := publicIdentsDecl.Specs[0].(*ast.ValueSpec).Values[0].(*ast.CompositeLit)
for _, i := range importDecl.Specs {
if ii, ok := i.(*ast.ImportSpec); ok {
imports = append(imports, ii)
}
}
return funcsMapValue, publicIdentValue, imports, nil
}
// getDataImports browses all TemplatesData keyValues and extracts related package path.
func getDataImports(importsContext []*ast.ImportSpec, templateConf *ast.CompositeLit) []*ast.ImportSpec {
ret := []*ast.ImportSpec{}
kv := getKeyValue(templateConf, "TemplatesData")
if kv != nil {
values := kv.Value.(*ast.CompositeLit).Elts
for _, v := range values {
switch x := v.(*ast.KeyValueExpr).Value.(type) {
case *ast.CompositeLit:
// case where the data is defined as pkgName.DataType{}
if sel, ok := x.Type.(*ast.SelectorExpr); ok {
dataImportSpec := getPkgPath(importsContext, sel.X.(*ast.Ident).Name)
ret = append(ret, dataImportSpec)
// case where the data is defined as DataType{}
// this case means that the DataType is declared into the same
// package as the configuration.
} else if ident, ok := x.Type.(*ast.Ident); ok {
wd, _ := os.Getwd()
// try to detect the pkgpath of the configuration variable.
// that may work because the bootstrap is invoked in the directory
// of the configuration.
pkg, err := build.Default.ImportDir(wd, 0)
if err != nil {
panic(err)
}
if pkg.IsCommand() {
panic(
fmt.Errorf(
"Impossible to consume the datatype %v located in the main package of the GO program %v",
ident.Name, pkg.ImportPath,
),
)
}
dataImportSpec := export.NewImportSpec(pkg.ImportPath, "")
ret = append(ret, dataImportSpec)
}
case *ast.Ident:
// assume its a nil.
default:
panic(
fmt.Errorf("getDataImports: Unhandled node type\n%v\n%#v\n", x, x),
)
}
}
}
return ret
}
func getPkgPath(importsContext []*ast.ImportSpec, name string) *ast.ImportSpec {
for _, i := range importsContext {
if i.Name != nil && i.Name.Name == name {
return i
}
}
for _, i := range importsContext {
pkgPath := i.Path.Value[1 : len(i.Path.Value)-1] // remove quotes
if filepath.Base(pkgPath) == name {
return i
}
}
return nil
}
func strIndex(list []string, search string) int {
for i, l := range list {
if l == search {
return i
}
}
return -1
}
func containsStr(list []string, search string) bool {
return strIndex(list, search) > -1
}
func containsImportSpec(list []*ast.ImportSpec, search *ast.ImportSpec) bool {
for _, i := range list {
if search.Name != nil {
if i.Name != nil && i.Name.Name == search.Name.Name {
return true
}
} else {
if search.Path.Value == i.Path.Value {
return true
}
}
}
return false
}
func parseGoFilePath(file string) (*ast.File, error) {
fset := token.NewFileSet()
return parser.ParseFile(fset, file, nil, 0)
}
func parseGoString(content string) (*ast.File, error) {
fset := token.NewFileSet()
return parser.ParseFile(fset, "whatever.go", content, 0)
}
// given a go file ast.Node, extracts a (top-level) variable declaration.
func extractVar(file *ast.File, varName string) *ast.GenDecl {
var found *ast.GenDecl
ast.Inspect(file, func(n ast.Node) bool {
if d, ok := n.(*ast.GenDecl); ok {
if v, okk := d.Specs[0].(*ast.ValueSpec); okk {
if len(v.Names) > 0 && v.Names[0].Name == varName {
found = d
}
}
}
return found == nil
})
return found
}
// given a go file ast.Node, extract its import statements.
func extractImports(file *ast.File) []*ast.ImportSpec {
var found []*ast.ImportSpec
ast.Inspect(file, func(n ast.Node) bool {
if d, ok := n.(*ast.GenDecl); ok {
for _, s := range d.Specs {
if v, okk := s.(*ast.ImportSpec); okk {
found = append(found, v)
}
}
}
return true
})
return found
}
func formatGoCode(s string) string {
fmtExpected, err := format.Source([]byte(s))
if err != nil {
panic(
fmt.Errorf("%v\n%v", err, s),
)
}
return string(fmtExpected)
}