forked from goadesign/goa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.go
108 lines (97 loc) · 1.98 KB
/
generate.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
package generator
import (
"go/build"
"os"
"path/filepath"
"sort"
"goa.design/goa/codegen"
"goa.design/goa/eval"
)
// Generate runs the code generation algorithms.
func Generate(dir, cmd string) ([]string, error) {
// 1. Compute design roots.
var roots []eval.Root
{
rs, err := eval.Context.Roots()
if err != nil {
return nil, err
}
roots = rs
}
// 2. Compute "gen" package import path.
var genpkg string
{
base, err := filepath.Abs(dir)
if err != nil {
return nil, err
}
path := filepath.Join(base, codegen.Gendir)
if err := os.MkdirAll(path, 0777); err != nil {
return nil, err
}
pkg, err := build.ImportDir(path, build.FindOnly)
if err != nil {
return nil, err
}
genpkg = pkg.ImportPath
}
// 3. Retrieve goa generators for given command.
var genfuncs []Genfunc
{
gs, err := Generators(cmd)
if err != nil {
return nil, err
}
genfuncs = gs
}
// 4. Run the code pre generation plugins.
err := codegen.RunPluginsPrepare(cmd, genpkg, roots)
if err != nil {
return nil, err
}
// 5. Generate initial set of files produced by goa code generators.
var genfiles []*codegen.File
for _, gen := range genfuncs {
fs, err := gen(genpkg, roots)
if err != nil {
return nil, err
}
genfiles = append(genfiles, fs...)
}
// 6. Run the code generation plugins.
genfiles, err = codegen.RunPlugins(cmd, genpkg, roots, genfiles)
if err != nil {
return nil, err
}
// 7. Write the files.
written := make(map[string]struct{})
for _, f := range genfiles {
filename, err := f.Render(dir)
if err != nil {
return nil, err
}
if filename != "" {
written[filename] = struct{}{}
}
}
// 8. Compute all output filenames.
var outputs []string
{
outputs = make([]string, len(written))
cwd, err := os.Getwd()
if err != nil {
cwd = "."
}
i := 0
for o := range written {
rel, err := filepath.Rel(cwd, o)
if err != nil {
rel = o
}
outputs[i] = rel
i++
}
}
sort.Strings(outputs)
return outputs, nil
}