forked from ncw/gotemplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.go
401 lines (362 loc) · 10.5 KB
/
template.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
// Reads the templates and writes the substituted templates
package main
import (
"bytes"
"fmt"
"go/ast"
"go/build"
"go/format"
"go/parser"
"go/token"
"io/ioutil"
"path"
"regexp"
"strings"
)
const (
genHeader = "// Code generated by gotemplate. DO NOT EDIT.\n\n"
)
// Holds the desired template
type template struct {
Package string
Name string
Args []string
NewPackage string
Dir string
importPaths []string
templateName string
templateArgs []string
mappings map[string]string
newIsPublic bool
inputFile string
}
// findPackageName reads all the go packages in the curent directory
// and finds which package they are in
func findPackageName() string {
p, err := build.Default.Import(".", ".", build.ImportMode(0))
if err != nil {
fatalf("Failed to read packages in current directory: %v", err)
}
return p.Name
}
// init the template instantiation
func newTemplate(dir, pkg, templateArgsString string) *template {
name, templateArgs := parseTemplateAndArgs(templateArgsString)
return &template{
Package: pkg,
Name: name,
Args: templateArgs,
Dir: dir,
mappings: make(map[string]string),
NewPackage: findPackageName(),
}
}
// Add import paths
func (t *template) addImports(paths []string) {
t.importPaths = paths
}
// Add a mapping for identifier
func (t *template) addMapping(name string) {
replacementName := ""
if !strings.Contains(name, t.templateName) {
// If name doesn't contain template name then just prefix it
innerName := strings.ToUpper(t.Name[:1]) + t.Name[1:]
replacementName = name + innerName
debugf("Top level definition '%s' doesn't contain template name '%s', using '%s'", name, t.templateName, replacementName)
} else {
// make sure the new identifier will follow
// Go casing style (newMySet not newmySet).
innerName := t.Name
if strings.Index(name, t.templateName) != 0 {
innerName = strings.ToUpper(innerName[:1]) + innerName[1:]
}
replacementName = strings.Replace(name, t.templateName, innerName, 1)
}
// If new template name is not public then make sure
// the exported name is not public too
if !t.newIsPublic && ast.IsExported(replacementName) {
replacementName = strings.ToLower(replacementName[:1]) + replacementName[1:]
}
t.mappings[name] = replacementName
}
// Parse the arguments string Template(A, B, C)
func parseTemplateAndArgs(s string) (name string, args []string) {
expr, err := parser.ParseExpr(s)
if err != nil {
fatalf("Failed to parse %q: %v", s, err)
}
debugf("expr = %#v\n", expr)
callExpr, ok := expr.(*ast.CallExpr)
if !ok {
fatalf("Failed to parse %q: expecting Identifier(...)", s)
}
debugf("fun = %#v", callExpr.Fun)
fn, ok := callExpr.Fun.(*ast.Ident)
if !ok {
fatalf("Failed to parse %q: expecting Identifier(...)", s)
}
name = fn.Name
for i, arg := range callExpr.Args {
var buf bytes.Buffer
debugf("arg[%d] = %#v", i, arg)
err = format.Node(&buf, token.NewFileSet(), arg)
if err != nil {
fatalf("Failed to format %q: %v", s, err)
}
s := buf.String()
debugf("parsed = %q", s)
args = append(args, s)
}
return
}
// "template type Set(A)"
var matchTemplateType = regexp.MustCompile(`^//\s*template\s+type\s+(\w+\s*.*?)\s*$`)
func (t *template) findTemplateDefinition(f *ast.File) {
// Inspect the comments
t.templateName = ""
t.templateArgs = nil
for _, cg := range f.Comments {
for _, x := range cg.List {
matches := matchTemplateType.FindStringSubmatch(x.Text)
if matches != nil {
if t.templateName != "" {
fatalf("Found multiple template definitions in %s", t.inputFile)
}
t.templateName, t.templateArgs = parseTemplateAndArgs(matches[1])
}
}
}
if t.templateName == "" {
fatalf("Didn't find template definition in %s", t.inputFile)
}
if len(t.templateArgs) != len(t.Args) {
fatalf("Wrong number of arguments - template is expecting %d but %d supplied", len(t.Args), len(t.templateArgs))
}
debugf("templateName = %v, templateArgs = %v", t.templateName, t.templateArgs)
}
// Parses a file into a Fileset and Ast
//
// Dies with a fatal error on error
func parseFile(path string, src interface{}) (*token.FileSet, *ast.File) {
fset := token.NewFileSet() // positions are relative to fset
f, err := parser.ParseFile(fset, path, src, parser.ParseComments)
if err != nil {
fatalf("Failed to parse file: %s", err)
}
return fset, f
}
// Replace the identifers in f
func replaceIdentifier(f *ast.File, old, new string) {
// Inspect the AST and print all identifiers and literals.
ast.Inspect(f, func(n ast.Node) bool {
switch x := n.(type) {
case *ast.Ident:
// We replace the identifier name
// which is a bit untidy if we weren't
// replacing with an identifier
if x.Name == old {
x.Name = new
}
}
return true
})
}
// Return true if name is a template argument
func (t *template) isTemplateArgument(name string) bool {
for _, item := range t.templateArgs {
if item == name {
return true
}
}
return false
}
// Parses the template file
func (t *template) parse(inputFile string) {
t.inputFile = inputFile
// Make the name mappings
t.newIsPublic = ast.IsExported(t.Name)
fset, f := parseFile(inputFile, nil)
t.findTemplateDefinition(f)
// debugf("Decls = %#v", f.Decls)
// Find names which need to be adjusted
namesToMangle := []string{}
newDecls := []ast.Decl{}
// Insert additional imports
if len(t.importPaths) != 0 {
// Use a fake position close to the package declaration to make sure
// the import clause is near the beginning of the file.
pos := f.Package + 10
specs := make([]ast.Spec, len(t.importPaths))
for i, path := range t.importPaths {
debugf("Adding import path %q", path)
spec := &ast.ImportSpec{
Path: &ast.BasicLit{
Kind: token.STRING,
Value: fmt.Sprintf("%q", path),
},
EndPos: pos,
}
specs[i] = spec
f.Imports = append(f.Imports, spec)
}
decl := &ast.GenDecl{
Tok: token.IMPORT,
Lparen: pos,
Specs: specs,
Rparen: pos,
}
newDecls = append(newDecls, decl)
}
for _, Decl := range f.Decls {
remove := false
switch d := Decl.(type) {
case *ast.GenDecl:
// A general definition
switch d.Tok {
case token.IMPORT:
// Ignore imports
case token.CONST, token.VAR:
// Find and remove identifiers found in template
// params
emptySpecs := []int{}
for i, spec := range d.Specs {
namesToRemove := []int{}
v := spec.(*ast.ValueSpec)
for j, name := range v.Names {
debugf("VAR or CONST %v", name.Name)
namesToMangle = append(namesToMangle, name.Name)
if t.isTemplateArgument(name.Name) {
namesToRemove = append(namesToRemove, j)
}
}
// Shuffle the names to remove out of v.Names and v.Values
for i := len(namesToRemove) - 1; i >= 0; i-- {
p := namesToRemove[i]
v.Names = append(v.Names[:p], v.Names[p+1:]...)
v.Values = append(v.Values[:p], v.Values[p+1:]...)
}
// If empty then add to slice to remove later
if len(v.Names) == 0 {
emptySpecs = append(emptySpecs, i)
}
}
// Remove now-empty specs
for i := len(emptySpecs) - 1; i >= 0; i-- {
p := emptySpecs[i]
d.Specs = append(d.Specs[:p], d.Specs[p+1:]...)
}
remove = len(d.Specs) == 0
case token.TYPE:
namesToRemove := []int{}
for i, spec := range d.Specs {
typeSpec := spec.(*ast.TypeSpec)
debugf("Type %v", typeSpec.Name.Name)
namesToMangle = append(namesToMangle, typeSpec.Name.Name)
// Remove type A if it is a template definition
if t.isTemplateArgument(typeSpec.Name.Name) {
namesToRemove = append(namesToRemove, i)
}
}
for i := len(namesToRemove) - 1; i >= 0; i-- {
p := namesToRemove[i]
d.Specs = append(d.Specs[:p], d.Specs[p+1:]...)
}
remove = len(d.Specs) == 0
default:
logf("Unknown type %s", d.Tok)
}
debugf("GenDecl = %#v", d)
case *ast.FuncDecl:
// A function definition
if d.Recv != nil {
// Has receiver so is a method - ignore this function
} else {
//debugf("FuncDecl = %#v", d)
debugf("FuncDecl = %s", d.Name.Name)
namesToMangle = append(namesToMangle, d.Name.Name)
// Remove func A() if it is a template definition
remove = t.isTemplateArgument(d.Name.Name)
}
default:
fatalf("Unknown Decl %#v", Decl)
}
if !remove {
newDecls = append(newDecls, Decl)
}
}
debugf("Names to mangle = %#v", namesToMangle)
// Remove the stub type definitions "type A int" from the package
f.Decls = newDecls
// Map the type definitions A -> string, B -> int
for i := range t.Args {
t.mappings[t.templateArgs[i]] = t.Args[i]
}
found := false
for _, name := range namesToMangle {
if name == t.templateName {
found = true
t.addMapping(name)
} else if _, found := t.mappings[name]; !found {
t.addMapping(name)
}
}
if !found {
fatalf("No definition for template type '%s'", t.templateName)
}
debugf("mappings = %#v", t.mappings)
// Replace the identifiers
for name, replacement := range t.mappings {
replaceIdentifier(f, name, replacement)
}
// Change the package to the local package name
f.Name.Name = t.NewPackage
// Output but only if contents have changed from existing file
b := new(bytes.Buffer)
outputFileName := fmt.Sprintf(*outfile+".go", t.Name)
format := func() {
b.Reset()
if err := format.Node(b, fset, f); err != nil {
fatalf("Failed to format output: %s", err)
}
}
format()
// bit gross to inject the header this way... but in the spirit of
// minimal changes et al...
fset, f = parseFile(outputFileName, genHeader+b.String())
format()
write := true
curr, err := ioutil.ReadFile(outputFileName)
if err == nil {
if bytes.Equal(curr, b.Bytes()) {
write = false
}
}
if write {
err := ioutil.WriteFile(outputFileName, b.Bytes(), 0666)
if err != nil {
fatalf("unable to write to %q: %v", outputFileName, err)
}
}
debugf("Written '%s'", outputFileName)
}
// Instantiate the template package
func (t *template) instantiate() {
debugf("Substituting %q with %s(%s) into package %s", t.Package, t.Name, strings.Join(t.Args, ","), t.NewPackage)
p, err := build.Default.Import(t.Package, t.Dir, build.ImportMode(0))
if err != nil {
fatalf("Import %s failed: %s", t.Package, err)
}
//debugf("package = %#v", p)
debugf("Dir = %#v", p.Dir)
// FIXME CgoFiles ?
debugf("Go files = %#v", p.GoFiles)
if len(p.GoFiles) == 0 {
fatalf("No go files found for package '%s'", t.Package)
}
// FIXME
if len(p.GoFiles) != 1 {
fatalf("Found more than one go file in '%s' - can only cope with 1 for the moment, sorry", t.Package)
}
templateFilePath := path.Join(p.Dir, p.GoFiles[0])
t.parse(templateFilePath)
}