/
gen.go
343 lines (313 loc) Β· 8.31 KB
/
gen.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
package main
import (
"errors"
"fmt"
"go/build"
"go/parser"
"go/token"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strconv"
"strings"
"goa.design/goa/v3/codegen"
"golang.org/x/tools/go/packages"
)
// Generator is the code generation management data structure.
type Generator struct {
// Command is the name of the command to run.
Command string
// DesignPath is the Go import path to the design package.
DesignPath string
// Output is the absolute path to the output directory.
Output string
// DesignVersion is the major component of the Goa version used by the design DSL.
// DesignVersion is either 2 or 3.
DesignVersion int
// bin is the filename of the generated generator.
bin string
// tmpDir is the temporary directory used to compile the generator.
tmpDir string
// hasVendorDirectory is a flag to indicate whether the project uses vendoring
hasVendorDirectory bool
}
// NewGenerator creates a Generator.
func NewGenerator(cmd string, path, output string) *Generator {
bin := "goa"
if runtime.GOOS == "windows" {
bin += ".exe"
}
var version int
var hasVendorDirectory bool
{
version = 2
matched := false
pkgs, _ := packages.Load(&packages.Config{Mode: packages.NeedFiles | packages.NeedModule}, path)
fset := token.NewFileSet()
p := regexp.MustCompile(`goa.design/goa/v(\d+)/dsl`)
for _, pkg := range pkgs {
// Nil check in case packages.Load can't get module info
if pkg.Module != nil {
if _, err := os.Stat(filepath.Join(pkg.Module.Dir, "vendor")); !os.IsNotExist(err) {
hasVendorDirectory = true
}
}
for _, gof := range pkg.GoFiles {
if bs, err := os.ReadFile(gof); err == nil {
if f, err := parser.ParseFile(fset, "", string(bs), parser.ImportsOnly); err == nil {
for _, s := range f.Imports {
matches := p.FindStringSubmatch(s.Path.Value)
if len(matches) == 2 {
matched = true
version, _ = strconv.Atoi(matches[1]) // We know it's an integer
}
}
}
}
if matched {
break
}
}
if matched {
break
}
}
}
return &Generator{
Command: cmd,
DesignPath: path,
Output: output,
DesignVersion: version,
hasVendorDirectory: hasVendorDirectory,
bin: bin,
}
}
// Write writes the main file.
func (g *Generator) Write(_ bool) error {
var tmpDir string
{
wd := "."
if cwd, err := os.Getwd(); err != nil {
wd = cwd
}
tmp, err := os.MkdirTemp(wd, "goa")
if err != nil {
return err
}
tmpDir = tmp
}
g.tmpDir = tmpDir
var sections []*codegen.SectionTemplate
{
data := map[string]any{
"Command": g.Command,
"CleanupDirs": cleanupDirs(g.Command, g.Output),
"DesignVersion": g.DesignVersion,
}
ver := ""
if g.DesignVersion > 2 {
ver = "v" + strconv.Itoa(g.DesignVersion) + "/"
}
imports := []*codegen.ImportSpec{
codegen.SimpleImport("flag"),
codegen.SimpleImport("fmt"),
codegen.SimpleImport("os"),
codegen.SimpleImport("path/filepath"),
codegen.SimpleImport("sort"),
codegen.SimpleImport("strconv"),
codegen.SimpleImport("strings"),
codegen.SimpleImport("goa.design/goa/" + ver + "codegen"),
codegen.SimpleImport("goa.design/goa/" + ver + "codegen/generator"),
codegen.SimpleImport("goa.design/goa/" + ver + "eval"),
codegen.NewImport("goa", "goa.design/goa/"+ver+"pkg"),
codegen.NewImport("_", g.DesignPath),
}
sections = []*codegen.SectionTemplate{
codegen.Header("Code Generator", "main", imports),
{
Name: "main",
Source: mainT,
Data: data,
},
}
}
f := &codegen.File{Path: "main.go", SectionTemplates: sections}
_, err := f.Render(tmpDir)
return err
}
// Compile compiles the generator.
func (g *Generator) Compile() error {
// We first need to go get the generated package to make sure that all
// dependencies are added to go.sum prior to compiling.
pkgs, err := packages.Load(&packages.Config{Mode: packages.NeedName}, fmt.Sprintf(".%c%s", filepath.Separator, g.tmpDir))
if err != nil {
return err
}
if len(pkgs) != 1 {
return fmt.Errorf("expected to find one package in %s", g.tmpDir)
}
if !g.hasVendorDirectory {
if err := g.runGoCmd("get", pkgs[0].PkgPath); err != nil {
return err
}
}
err = g.runGoCmd("build", "-o", g.bin)
// If we're in vendor context we check the error string to see if it's an issue of unsatisfied dependencies
if err != nil && g.hasVendorDirectory {
if strings.Contains(err.Error(), "cannot find package") && strings.Contains(err.Error(), "/goa.design/goa/v3/codegen/generator") {
return errors.New("generated code expected `goa.design/goa/v3/codegen/generator` to be present in the vendor directory, see documentation for more details")
}
}
return err
}
// Run runs the compiled binary and return the output lines.
func (g *Generator) Run() ([]string, error) {
var cmdl string
{
args := make([]string, len(os.Args)-1)
gopaths := filepath.SplitList(os.Getenv("GOPATH"))
if len(gopaths) == 0 {
gopaths = []string{build.Default.GOPATH}
}
for i, a := range os.Args[1:] {
for _, p := range gopaths {
if strings.HasPrefix(a, p) {
args[i] = strings.Replace(a, p, "$(GOPATH)", 1)
break
}
}
if args[i] == "" {
args[i] = a
}
}
cmdl = " " + strings.Join(args, " ")
rawcmd := filepath.Base(os.Args[0])
// Remove .exe suffix to avoid different output on Windows.
rawcmd = strings.TrimSuffix(rawcmd, ".exe")
cmdl = fmt.Sprintf("$ %s%s", rawcmd, cmdl)
}
args := []string{"--version=" + strconv.Itoa(g.DesignVersion), "--output=" + g.Output, "--cmd=" + cmdl}
cmd := exec.Command(filepath.Join(g.tmpDir, g.bin), 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
}
// Remove deletes the package files.
func (g *Generator) Remove() {
if g.tmpDir != "" {
os.RemoveAll(g.tmpDir)
g.tmpDir = ""
}
}
func (g *Generator) runGoCmd(args ...string) error {
gobin, err := exec.LookPath("go")
if err != nil {
return fmt.Errorf(`failed to find a go compiler, looked in "%s"`, os.Getenv("PATH"))
}
if g.DesignVersion > 2 {
os.Setenv("GO111MODULE", "on")
}
c := exec.Cmd{
Path: gobin,
Args: append([]string{gobin}, args...),
Dir: g.tmpDir,
}
out, err := c.CombinedOutput()
if err != nil {
if len(out) > 0 {
return fmt.Errorf(string(out))
}
return fmt.Errorf("failed to compile generator: %s", err)
}
return nil
}
// cleanupDirs returns the paths of the subdirectories under gendir to delete
// before generating code.
func cleanupDirs(cmd, output string) []string {
if cmd == "gen" {
gendirPath := filepath.Join(output, codegen.Gendir)
gendir, err := os.Open(gendirPath)
if err != nil {
return nil
}
defer func() {
err := gendir.Close()
if err != nil {
fmt.Fprintf(os.Stderr, "failed to close gendir: %s", err)
}
}()
finfos, err := gendir.Readdir(-1)
if err != nil {
return []string{gendirPath}
}
var dirs []string
for _, fi := range finfos {
if fi.IsDir() {
dirs = append(dirs, filepath.Join(gendirPath, fi.Name()))
}
}
return dirs
}
return nil
}
// mainT is the template for the generator main.
const mainT = `func main() {
var (
out = flag.String("output", "", "")
version = flag.String("version", "", "")
cmdl = flag.String("cmd", "", "")
ver int
)
{
flag.Parse()
if *out == "" {
fail("missing output flag")
}
if *version == "" {
fail("missing version flag")
}
if *cmdl == "" {
fail("missing cmd flag")
}
v, err := strconv.Atoi(*version)
if err != nil {
fail("invalid version %s", *version)
}
ver = v
}
if ver > goa.Major {
fail("cannot run goa %s on design using goa v%s\n", goa.Version(), *version)
}
if err := eval.Context.Errors; err != nil {
fail(err.Error())
}
if err := eval.RunDSL(); err != nil {
fail(err.Error())
}
{{- range .CleanupDirs }}
if err := os.RemoveAll({{ printf "%q" . }}); err != nil {
fail(err.Error())
}
{{- end }}
{{- if gt .DesignVersion 2 }}
codegen.DesignVersion = ver
{{- end }}
outputs, err := generator.Generate(*out, {{ printf "%q" .Command }})
if err != nil {
fail(err.Error())
}
fmt.Println(strings.Join(outputs, "\n"))
}
func fail(msg string, vals ...any) {
fmt.Fprintf(os.Stderr, msg, vals...)
os.Exit(1)
}
`