forked from goadesign/goa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.go
208 lines (185 loc) · 5.05 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
package meta
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"sort"
"strconv"
"strings"
"text/template"
"github.com/goadesign/goa/goagen/codegen"
"github.com/goadesign/goa/version"
)
// Generator generates the code of, compiles and runs generators.
// This extra step is necessary to compile in the end user design package so
// that generator code can iterate through it.
type Generator struct {
// Genfunc contains the name of the generator entry point function.
// The function signature must be:
//
// func <Genfunc>([]dslengine.Root) ([]string, error)
Genfunc string
// Imports list the imports that are specific for that generator that
// should be added to the main Go file.
Imports []*codegen.ImportSpec
// Flags is the list of flags to be used when invoking the final
// generator on the command line.
Flags map[string]string
// OutDir is the final output directory.
OutDir string
// DesignPkgPath is the Go import path to the design package.
DesignPkgPath string
debug bool
}
// NewGenerator returns a meta generator that can run an actual Generator
// given its factory method and command line flags.
func NewGenerator(genfunc string, imports []*codegen.ImportSpec, flags map[string]string) (*Generator, error) {
var (
outDir, designPkgPath string
debug bool
)
if o, ok := flags["out"]; ok {
outDir = o
}
if d, ok := flags["design"]; ok {
designPkgPath = d
}
if d, ok := flags["debug"]; ok {
var err error
debug, err = strconv.ParseBool(d)
if err != nil {
return nil, fmt.Errorf("failed to parse debug flag: %s", err)
}
}
return &Generator{
Genfunc: genfunc,
Imports: imports,
Flags: flags,
OutDir: outDir,
DesignPkgPath: designPkgPath,
debug: debug,
}, nil
}
// Generate compiles and runs the generator and returns the generated filenames.
func (m *Generator) Generate() ([]string, error) {
// Sanity checks
if os.Getenv("GOPATH") == "" {
return nil, fmt.Errorf("GOPATH not set")
}
if m.OutDir == "" {
return nil, fmt.Errorf("missing output directory flag")
}
if m.DesignPkgPath == "" {
return nil, fmt.Errorf("missing design package flag")
}
// Create output directory
if err := os.MkdirAll(m.OutDir, 0755); err != nil {
return nil, err
}
// Create temporary workspace used for generation
wd, err := os.Getwd()
if err != nil {
return nil, err
}
tmpDir, err := ioutil.TempDir(wd, "goagen")
if err != nil {
if _, ok := err.(*os.PathError); ok {
err = fmt.Errorf(`invalid output directory path "%s"`, m.OutDir)
}
return nil, err
}
defer func() {
if !m.debug {
os.RemoveAll(tmpDir)
}
}()
if m.debug {
fmt.Printf("** Code generator source dir: %s\n", tmpDir)
}
pkgSourcePath, err := codegen.PackageSourcePath(m.DesignPkgPath)
if err != nil {
return nil, fmt.Errorf("invalid design package import path: %s", err)
}
pkgName, err := codegen.PackageName(pkgSourcePath)
if err != nil {
return nil, err
}
// Generate tool source code.
pkgPath := filepath.Join(tmpDir, pkgName)
p, err := codegen.PackageFor(pkgPath)
if err != nil {
return nil, err
}
m.generateToolSourceCode(p)
// Compile and run generated tool.
if m.debug {
fmt.Printf("** Compiling with:\n%s", strings.Join(os.Environ(), "\n"))
}
genbin, err := p.Compile("goagen")
if err != nil {
return nil, err
}
return m.spawn(genbin)
}
func (m *Generator) generateToolSourceCode(pkg *codegen.Package) {
file := pkg.CreateSourceFile("main.go")
imports := append(m.Imports,
codegen.SimpleImport("fmt"),
codegen.SimpleImport("strings"),
codegen.SimpleImport("github.com/goadesign/goa/dslengine"),
codegen.NewImport("_", filepath.ToSlash(m.DesignPkgPath)),
)
file.WriteHeader("Code Generator", "main", imports)
tmpl, err := template.New("generator").Parse(mainTmpl)
if err != nil {
panic(err) // bug
}
pkgName, err := codegen.PackageName(pkg.Abs())
if err != nil {
panic(err)
}
context := map[string]string{
"Genfunc": m.Genfunc,
"DesignPackage": m.DesignPkgPath,
"PkgName": pkgName,
}
err = tmpl.Execute(file, context)
if err != nil {
panic(err) // bug
}
}
// spawn runs the compiled generator using the arguments initialized by Kingpin
// when parsing the command line.
func (m *Generator) spawn(genbin string) ([]string, error) {
args := make([]string, len(m.Flags))
i := 0
for k, v := range m.Flags {
args[i] = fmt.Sprintf("--%s=%s", k, v)
i++
}
sort.Strings(args)
args = append(args, "--version="+version.String())
cmd := exec.Command(genbin, args...)
out, err := cmd.CombinedOutput()
if err != nil {
return nil, fmt.Errorf("%s\n%s", err, string(out))
}
res := strings.Split(string(out), "\n")
for (len(res) > 0) && (res[len(res)-1] == "") {
res = res[:len(res)-1]
}
return res, nil
}
const mainTmpl = `
func main() {
// Check if there were errors while running the first DSL pass
dslengine.FailOnError(dslengine.Errors)
// Now run the secondary DSLs
dslengine.FailOnError(dslengine.Run())
files, err := {{.Genfunc}}()
dslengine.FailOnError(err)
// We're done
fmt.Println(strings.Join(files, "\n"))
}`