/
generator.go
308 lines (280 loc) · 8.55 KB
/
generator.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
package main // import "github.com/azr/generators/recycler"
import (
"bytes"
"flag"
"fmt"
"go/ast"
"go/build"
"go/format"
"go/importer"
"go/parser"
"go/token"
"go/types"
"io/ioutil"
"log"
"os"
"path/filepath"
"runtime"
"strings"
"text/template"
"github.com/azr/generators/utils"
)
// Usage is a replacement usage function for the flags package.
func Usage() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
fmt.Fprintf(os.Stderr, "\trecycler [flags] -type T [directory]\n")
fmt.Fprintf(os.Stderr, "\trecycler [flags] -type T files... # Must be a single package\n")
fmt.Fprintf(os.Stderr, "\trecycler [flags] -template pool.gotpl -type T files... # To generate a basic pool\n")
fmt.Fprintf(os.Stderr, "\trecycler [flags] -template freelist.gotpl -type T files... # To generate a basic freelist\n")
fmt.Fprintf(os.Stderr, "\trecycler [flags] -template /path/to/template -type T files... # To generate your own freelist\n")
fmt.Fprintf(os.Stderr, "For more information, see:\n")
fmt.Fprintf(os.Stderr, "\thttp://godoc.org/github.com/azr/recycler\n")
fmt.Fprintf(os.Stderr, "Flags:\n")
flag.PrintDefaults()
}
func main() {
var (
typeNames = flag.String("type", "", "comma-separated list of type names; must be set")
output = flag.String("output", "", "output file name; default srcdir/<type[0]>_recycler.go")
tpl = flag.String("template", "pool.gotpl", "go template to generate your recycler with. Defined ones are pool and freelists. Full path also works.\nAvailable template vars:\n\t*Type: type to recycle\n\t*Size: size of the freelist. Not used in pool.")
size = flag.Int("size", 50, "Max number of items kept. used for freelist")
importSync = flag.Bool("sync", false, "Should the generated file import the sync pkg ?")
)
log.SetFlags(0)
log.SetPrefix("recycler: ")
flag.Usage = Usage
flag.Parse()
if len(*typeNames) == 0 {
flag.Usage()
os.Exit(2)
}
types := strings.Split(*typeNames, ",")
// We accept either one directory or a list of files. Which do we have?
args := flag.Args()
if len(args) == 0 {
// Default: process whole package in current directory.
args = []string{"."}
}
// copy file utils
_, currFile, _, ok := runtime.Caller(0)
if !ok {
log.Fatalf("No caller information")
}
// Parse the package once.
var (
dir string
g Generator
err error
)
if *tpl == "pool.gotpl" {
*importSync = true
}
templatePath, err := utils.GetExistingPathFor(*tpl, filepath.Dir(currFile))
if err != nil {
log.Fatalf("Could not find template: %s", err)
}
g.tpl, err = template.ParseFiles(templatePath)
if err != nil {
log.Fatalf("Could not parse template: %s", err)
}
if len(args) == 1 && isDirectory(args[0]) {
dir = args[0]
g.parsePackageDir(args[0])
} else {
dir = filepath.Dir(args[0])
g.parsePackageFiles(args)
}
// Print the header and package clause.
g.Printf("// Code generated by \"github.com/azr/generators/recycler %s\"; DO NOT EDIT\n", strings.Join(os.Args[1:], " "))
g.Printf("\n")
g.Printf("package %s", g.pkg.name)
g.Printf("\n")
if *importSync {
g.Printf("import \"sync\"\n")
}
// Run generate for each type.
for _, typeName := range types {
g.generate(typeName, *size)
}
// Format the output.
src := g.format()
// Write to file.
outputName := *output
if outputName == "" {
baseName := fmt.Sprintf("%s_recycler.go", types[0])
outputName = filepath.Join(dir, strings.ToLower(baseName))
}
err = ioutil.WriteFile(outputName, src, 0644)
if err != nil {
log.Fatalf("writing output: %s", err)
}
}
// isDirectory reports whether the named file is a directory.
func isDirectory(name string) bool {
info, err := os.Stat(name)
if err != nil {
log.Fatal(err)
}
return info.IsDir()
}
// Generator holds the state of the analysis. Primarily used to buffer
// the output for format.Source.
type Generator struct {
buf bytes.Buffer // Accumulated output.
pkg *Package // Package we are scanning.
tpl *template.Template // Template used for writing file.
}
func (g *Generator) Printf(format string, args ...interface{}) {
fmt.Fprintf(&g.buf, format, args...)
}
// File holds a single parsed file and associated data.
type File struct {
pkg *Package // Package to which this file belongs.
file *ast.File // Parsed AST.
// These fields are reset for each type being generated.
typeName string // Name of the type.
found bool
}
type Package struct {
dir string
name string
defs map[*ast.Ident]types.Object
files []*File
typesPkg *types.Package
}
// parsePackageDir parses the package residing in the directory.
func (g *Generator) parsePackageDir(directory string) {
pkg, err := build.Default.ImportDir(directory, 0)
if err != nil {
log.Fatalf("cannot process directory %s: %s", directory, err)
}
var names []string
names = append(names, pkg.GoFiles...)
names = append(names, pkg.CgoFiles...)
// TODO: Need to think about constants in test files. Maybe write type_string_test.go
// in a separate pass? For later.
// names = append(names, pkg.TestGoFiles...) // These are also in the "foo" package.
names = append(names, pkg.SFiles...)
names = prefixDirectory(directory, names)
g.parsePackage(directory, names, nil)
}
// parsePackageFiles parses the package occupying the named files.
func (g *Generator) parsePackageFiles(names []string) {
g.parsePackage(".", names, nil)
}
// prefixDirectory places the directory name on the beginning of each name in the list.
func prefixDirectory(directory string, names []string) []string {
if directory == "." {
return names
}
ret := make([]string, len(names))
for i, name := range names {
ret[i] = filepath.Join(directory, name)
}
return ret
}
// parsePackage analyzes the single package constructed from the named files.
// If text is non-nil, it is a string to be used instead of the content of the file,
// to be used for testing. parsePackage exits if there is an error.
func (g *Generator) parsePackage(directory string, names []string, text interface{}) {
var files []*File
var astFiles []*ast.File
g.pkg = new(Package)
fs := token.NewFileSet()
for _, name := range names {
if !strings.HasSuffix(name, ".go") {
continue
}
parsedFile, err := parser.ParseFile(fs, name, text, 0)
if err != nil {
log.Fatalf("parsing package: %s: %s", name, err)
}
astFiles = append(astFiles, parsedFile)
files = append(files, &File{
file: parsedFile,
pkg: g.pkg,
})
}
if len(astFiles) == 0 {
log.Fatalf("%s: no buildable Go files", directory)
}
g.pkg.name = astFiles[0].Name.Name
g.pkg.files = files
g.pkg.dir = directory
// Type check the package.
g.pkg.check(fs, astFiles)
}
// check type-checks the package. The package must be OK to proceed.
func (pkg *Package) check(fs *token.FileSet, astFiles []*ast.File) {
pkg.defs = make(map[*ast.Ident]types.Object)
config := types.Config{
FakeImportC: true,
Importer: importer.Default(),
}
info := &types.Info{
Defs: pkg.defs,
}
typesPkg, err := config.Check(pkg.dir, fs, astFiles, info)
if err != nil {
log.Fatalf("checking package: %s", err)
}
pkg.typesPkg = typesPkg
}
// generate produces the String method for the named type.
func (g *Generator) generate(typeName string, size int) {
found := false
for _, file := range g.pkg.files {
// Set the state for this run of the walker.
file.typeName = typeName
if file.file != nil {
ast.Inspect(file.file, file.genDecl)
if file.found {
found = true
}
}
}
if found {
g.build(typeName, size)
} else {
fmt.Printf("Type not found: %s", typeName)
}
}
// format returns the gofmt-ed contents of the Generator's buffer.
func (g *Generator) format() []byte {
src, err := format.Source(g.buf.Bytes())
if err != nil {
// Should never happen, but can arise when developing this code.
// The user can compile the output to see the error.
log.Printf("warning: internal error: invalid Go generated: %s", err)
log.Printf("warning: compile the package to analyze the error")
return g.buf.Bytes()
}
return src
}
// genDecl processes one declaration clause.
func (f *File) genDecl(node ast.Node) bool {
decl, ok := node.(*ast.GenDecl)
if !ok || decl.Tok != token.TYPE {
// We only care about type declarations.
return true
}
for _, spec := range decl.Specs {
vspec := spec.(*ast.TypeSpec) // Guaranteed to succeed as this is Type.
if vspec.Name.String() != f.typeName {
continue
}
f.found = true
}
return false
}
// build generates the variables and String method for a single run of contiguous values.
func (g *Generator) build(typeName string, size int) {
g.Printf("\n")
g.tpl.Execute(&g.buf, struct {
Type string
Size int
}{
Type: typeName,
Size: size,
})
}