-
Notifications
You must be signed in to change notification settings - Fork 3
/
process.go
120 lines (104 loc) · 2.9 KB
/
process.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
package codegen
import (
"fmt"
"go/token"
"go/types"
"strings"
"github.com/pkg/errors"
"golang.org/x/tools/go/packages"
)
func ProcessFile(filePaths ...string) error {
patterns := make([]string, len(filePaths), len(filePaths))
for i, filePath := range filePaths {
if !strings.HasSuffix(filePath, ".go") {
return errors.New(filePath + " does not reference a go file")
}
patterns[i] = fmt.Sprint("file=", filePath)
}
fset := token.NewFileSet()
cfg := &packages.Config{
Fset: fset,
Mode: packages.NeedName |
packages.NeedTypes |
packages.NeedDeps |
packages.NeedFiles,
}
pkgs, err := packages.Load(cfg, patterns...)
if err != nil {
return errors.Wrap(err, "parsing file")
}
filePathToPkg, err := generatePathToPackageMap(filePaths, pkgs)
if err != nil {
return errors.Wrapf(err, "failed to map file paths to packages")
}
for filePath, pkg := range filePathToPkg {
structs := findStructsInFile(filePath, pkg, fset)
ctx := NewGenContext(fset, pkg.Types)
for _, s := range structs {
if err := processStruct(s, ctx); err != nil {
return errors.Wrapf(err, "processing struct %s", s.Obj().Name())
}
}
if len(ctx.Generated()) == 0 {
return errors.New("No codegen tags detected in file " + filePath)
}
base := filePath[:len(filePath)-len(".go")]
genPath := base + "_generated.go"
if err := Output(ctx, genPath); err != nil {
return errors.Wrap(err, "writing generated code to "+genPath)
}
fmt.Printf("Wrote %s.\n", genPath)
}
return nil
}
func processStruct(aStruct *types.Named, ctx *GenContext) error {
invocations, err := InvocationsForStruct(aStruct.Underlying().(*types.Struct))
if err != nil {
return errors.Wrap(err, "extracting template invocations")
}
for _, invocation := range invocations {
if err := ctx.RunTemplate(invocation, aStruct); err != nil {
return errors.Wrap(err, "running template")
}
}
return nil
}
func findStructsInFile(
filePath string,
pkg *packages.Package,
fset *token.FileSet,
) []*types.Named {
var structs []*types.Named
scope := pkg.Types.Scope()
for _, name := range scope.Names() {
object := scope.Lookup(name)
namedType, ok := object.Type().(*types.Named)
if !ok {
continue
}
if _, ok = namedType.Underlying().(*types.Struct); !ok {
continue
}
fpath := fset.Position(object.Pos()).Filename
if fpath == filePath {
structs = append(structs, namedType)
}
}
return structs
}
func generatePathToPackageMap(filePaths []string, pkgs []*packages.Package) (map[string]*packages.Package, error) {
filePathToPkg := make(map[string]*packages.Package, len(filePaths))
FilePathLoop:
for _, filePath := range filePaths {
for _, pkg := range pkgs {
for _, pkgFile := range pkg.GoFiles {
if filePath == pkgFile {
filePathToPkg[filePath] = pkg
continue FilePathLoop
}
}
}
return nil, errors.New("could not find package for file, " + filePath)
}
return filePathToPkg, nil
}