/
missing-fixer.go
402 lines (329 loc) · 9.85 KB
/
missing-fixer.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
package gen
import (
"bufio"
"bytes"
"fmt"
"go/ast"
"go/parser"
"go/token"
"io"
"io/ioutil"
"os"
"path/filepath"
"sort"
"strings"
"unicode"
)
// missingFixer handles generating various missing types and methods.
// Looks at file structure and scans for `//vugugen:` comments
// See https://github.com/vugu/vugu/issues/128 for more explanation and background.
type missingFixer struct {
pkgPath string // absolute path to package
pkgName string // short name of package from the `package` statement
vuguComps map[string]string // map of comp.vugu -> comp_vgen.go (all just relative base name of file, no dir)
outfile string // file name of output file (relative), 0_missing_vgen.go by default
}
func newMissingFixer(pkgPath, pkgName string, vuguComps map[string]string) *missingFixer {
return &missingFixer{
pkgPath: pkgPath,
pkgName: pkgName,
vuguComps: vuguComps,
}
}
// run does work for this one package
func (mf *missingFixer) run() error {
// remove the output file if it doesn't exist,
// and then below we re-create it if it turns out
// we need it
_ = mf.removeOutfile()
// parse the package
var fset token.FileSet
pkgMap, err := parser.ParseDir(&fset, mf.pkgPath, nil, 0)
if err != nil {
return err
}
pkg := pkgMap[mf.pkgName]
if pkg == nil {
return fmt.Errorf("unable to find package %q after parsing dir %s", mf.pkgName, mf.pkgPath)
}
// log.Printf("pkg: %#v", pkg)
var fout *os.File
// read each _vgen.go file
for _, goFile := range mf.vuguComps {
// var ffset token.FileSet
// file, err := parser.ParseFile(&ffset, filepath.Join(mf.pkgPath, goFile), nil, 0)
// if err != nil {
// return fmt.Errorf("error while reading %s: %w", goFile, err)
// }
// ast.Print(&ffset, file.Decls)
file := fileInPackage(pkg, goFile)
if file == nil {
return fmt.Errorf("unable to find file %q in package (i.e. parse.ParseDir did not give us this file)", goFile)
}
compTypeName := findFileBuildMethodType(file)
// if we didn't find a build method, we don't need to do anything
if compTypeName == "" {
continue
}
// log.Printf("found compTypeName=%s", compTypeName)
// see if the type is already declared somewhere in the package
compTypeDecl := findTypeDecl(&fset, pkg, compTypeName)
// the type exists, we don't need to emit a declaration for it
if compTypeDecl != nil {
continue
}
// open outfile if it doesn't exist
if fout == nil {
fout, err = mf.createOutfile()
if err != nil {
return err
}
defer fout.Close()
}
fmt.Fprintf(fout, `// %s is a Vugu component and implements the vugu.Builder interface.
type %s struct {}
`, compTypeName, compTypeName)
// log.Printf("aaa compTypeName=%s, compTypeDecl=%v", compTypeName, compTypeDecl)
}
// scan all .go files for known vugugen comments
gcomments, err := readVugugenComments(mf.pkgPath)
if err != nil {
return err
}
// open outputfile if not already done above
if len(gcomments) > 0 {
if fout == nil {
fout, err = mf.createOutfile()
if err != nil {
return err
}
defer fout.Close()
}
}
gcommentFnames := make([]string, 0, len(gcomments))
for fname := range gcomments {
gcommentFnames = append(gcommentFnames, fname)
}
sort.Strings(gcommentFnames) // try to get deterministic output
// for each file with vugugen comments
for _, fname := range gcommentFnames {
commentList := gcomments[fname]
sort.Strings(commentList) // try to get deterministic output
for _, c := range commentList {
c := strings.TrimSpace(c)
c = strings.TrimPrefix(c, "//vugugen:")
cparts := strings.Fields(c) // split by whitespace
if len(cparts) == 0 {
return fmt.Errorf("error parsing %s vugugen comment with no type found %q", fname, c)
}
switch cparts[0] {
case "event":
args := cparts[1:]
if len(args) < 1 {
return fmt.Errorf("error parsing %s vugugen event comment with no args %q", fname, c)
}
eventName := args[0]
if !unicode.IsUpper(rune(eventName[0])) {
return fmt.Errorf("error parsing %s vugugen event comment, event name must start with a capital letter: %q", fname, c)
}
opts := args[1:]
// isInterface := false
// try to keep the option parsing very strict, especially for now before we get this
// all figured out
/*if len(opts) == 1 && opts[0] == "interface" {
isInterface = true
} else */
if len(opts) == 0 {
// no opts is fine
} else {
return fmt.Errorf("error parsing %s vugugen event comment unexpected options %q", fname, c)
}
// check for NameEvent
decl := findTypeDecl(&fset, pkg, eventName+"Event")
// emit type if missing as a struct wrapper around a DOMEvent
if decl == nil {
fmt.Fprintf(fout, `// %sEvent is a component event.
type %sEvent struct {
vugu.DOMEvent
}
`, eventName, eventName)
}
// check for NameHandler type, emit if missing
decl = findTypeDecl(&fset, pkg, eventName+"Handler")
if decl == nil {
fmt.Fprintf(fout, `// %sHandler is the interface for things that can handle %sEvent.
type %sHandler interface {
%sHandle(event %sEvent)
}
`, eventName, eventName, eventName, eventName, eventName)
}
// check for NameFunc type, emit if missing along with method and type check
decl = findTypeDecl(&fset, pkg, eventName+"Func")
if decl == nil {
fmt.Fprintf(fout, `// %sFunc implements %sHandler as a function.
type %sFunc func(event %sEvent)
// %sHandle implements the %sHandler interface.
func (f %sFunc) %sHandle(event %sEvent) { f(event) }
// assert %sFunc implements %sHandler
var _ %sHandler = %sFunc(nil)
`, eventName, eventName, eventName, eventName, eventName, eventName, eventName, eventName, eventName, eventName, eventName, eventName, eventName)
}
default:
return fmt.Errorf("error parsing %s vugugen comment with unknown type %q", fname, c)
}
}
}
return nil
}
func (mf *missingFixer) fullOutfilePath() string {
if mf.outfile == "" {
return filepath.Join(mf.pkgPath, "0_missing_vgen.go")
}
return filepath.Join(mf.pkgPath, mf.outfile)
}
func (mf *missingFixer) removeOutfile() error {
return os.Remove(mf.fullOutfilePath())
}
func (mf *missingFixer) createOutfile() (*os.File, error) {
p := mf.fullOutfilePath()
fout, err := os.Create(p)
if err != nil {
return nil, fmt.Errorf("failed to create missingFixer outfile %s: %w", p, err)
}
fmt.Fprintf(fout, "package %s\n\nimport \"github.com/vugu/vugu\"\n\nvar _ vugu.DOMEvent // import fixer\n\n", mf.pkgName)
return fout, nil
}
// readVugugenComments will look in every .go file for a //vugugen: comment
// and return a map with file name keys and a slice of the comments found as the values.
// vugugen comment lines that are exactly identical will be deduplicated (even across files)
// as it will never be correct to generate two of the same thing in one package
func readVugugenComments(pkgPath string) (map[string][]string, error) {
fis, err := ioutil.ReadDir(pkgPath)
if err != nil {
return nil, err
}
foundLines := make(map[string]bool)
ret := make(map[string][]string, len(fis))
for _, fi := range fis {
if fi.IsDir() {
continue
}
bname := filepath.Base(fi.Name())
if !strings.HasSuffix(bname, ".go") {
continue
}
f, err := os.Open(filepath.Join(pkgPath, bname))
if err != nil {
return ret, err
}
defer f.Close()
br := bufio.NewReader(f)
var fc []string
pfx := []byte("//vugugen:")
for {
line, err := br.ReadBytes('\n')
if err == io.EOF {
if len(line) == 0 {
break
}
} else if err != nil {
return ret, fmt.Errorf("missingFixer error while reading %s: %w", bname, err)
}
// ignoring whitespace
line = bytes.TrimSpace(line)
// line must start with prefix exactly
if !bytes.HasPrefix(line, pfx) {
continue
}
// and not be a duplicate
lineStr := string(line)
if foundLines[lineStr] {
continue
}
foundLines[lineStr] = true
fc = append(fc, lineStr)
}
if fc != nil {
ret[bname] = fc
}
}
return ret, nil
}
// fileInPackage given pkg and "blah.go" will return the file whose base name is "blah.go"
// (i.e. it ignores the directory part of the map key in pkg.Files)
// Will return nil if not found.
func fileInPackage(pkg *ast.Package, fileName string) *ast.File {
for fpath, file := range pkg.Files {
if filepath.Base(fpath) == fileName {
return file
}
}
return nil
}
// findTypeDecl looks through the package for the given type and returns
// the declaraction or nil if not found
func findTypeDecl(fset *token.FileSet, pkg *ast.Package, typeName string) ast.Decl {
for _, file := range pkg.Files {
for _, decl := range file.Decls {
// ast.Print(fset, decl)
// looking for genDecl
genDecl, ok := decl.(*ast.GenDecl)
if !ok {
continue
}
// which is a type declaration
if genDecl.Tok != token.TYPE {
continue
}
// with one TypeSpec
if len(genDecl.Specs) != 1 {
continue
}
spec, ok := genDecl.Specs[0].(*ast.TypeSpec)
if !ok {
continue
}
// with a name
if spec.Name == nil {
continue
}
// that matches the one we're looking for
if spec.Name.Name == typeName {
return genDecl
}
}
}
return nil
}
// findFileBuildMethodType will return "Comp" given `func (c *Root) Comp` exists in the file.
func findFileBuildMethodType(file *ast.File) string {
for _, decl := range file.Decls {
// only care about a function declaration
funcDecl, ok := decl.(*ast.FuncDecl)
if !ok {
continue
}
// named Build
if funcDecl.Name.Name != "Build" {
continue
}
// with exactly one receiver
if !(funcDecl.Recv != nil && len(funcDecl.Recv.List) == 1) {
continue
}
// which is a pointer
recv := funcDecl.Recv.List[0]
starExpr, ok := recv.Type.(*ast.StarExpr)
if !ok {
continue
}
// to an identifier
xident, ok := starExpr.X.(*ast.Ident)
if !ok {
continue
}
// whose name is the component type we're after
return xident.Name
}
return ""
}