-
Notifications
You must be signed in to change notification settings - Fork 14
/
generator.go
240 lines (219 loc) · 7.61 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
package codegen
import (
"bytes"
"path/filepath"
"strings"
"text/template"
"github.com/solo-io/solo-kit/pkg/code-generator/codegen/templates/kube"
"github.com/solo-io/solo-kit/pkg/errors"
"github.com/iancoleman/strcase"
"github.com/solo-io/go-utils/log"
code_generator "github.com/solo-io/solo-kit/pkg/code-generator"
"github.com/solo-io/solo-kit/pkg/code-generator/codegen/templates"
"github.com/solo-io/solo-kit/pkg/code-generator/model"
)
const fileHeader = `// Code generated by solo-kit. DO NOT EDIT.
`
func GenerateFiles(project *model.Project, skipOutOfPackageFiles, skipGeneratedTests, genKubeTypes bool) (code_generator.Files, error) {
files, err := generateFilesForProject(project)
if err != nil {
return nil, err
}
if genKubeTypes {
kubeFiles, err := generateKubeFilesForProject(project)
if err != nil {
return nil, err
}
files = append(files, kubeFiles...)
}
for _, res := range project.Resources {
// only generate files for the resources in our group, otherwise we import
if !project.ProjectConfig.IsOurProto(res.Filename) && !res.IsCustom {
log.Printf("not generating solo-kit "+
"clients for resource %v.%v, "+
"resource proto package must match project proto package %v", res.ProtoPackage, res.Name, project.ProtoPackage)
continue
} else if res.IsCustom && res.CustomResource.Imported {
log.Printf("not generating solo-kit "+
"clients for resource %v.%v, "+
"custom resources from a different project (%v) are not generated", res.GoPackage, res.Name, project.ProjectConfig.GoPackage)
continue
}
fs, err := generateFilesForResource(res)
if err != nil {
return nil, err
}
files = append(files, fs...)
}
for _, grp := range project.ResourceGroups {
if skipOutOfPackageFiles && !(strings.HasSuffix(grp.Name, "."+project.ProtoPackage) || grp.Name == project.ProtoPackage) {
continue
}
fs, err := generateFilesForResourceGroup(grp)
if err != nil {
return nil, err
}
files = append(files, fs...)
}
for _, res := range project.XDSResources {
if skipOutOfPackageFiles && !project.ProjectConfig.IsOurProto(res.Filename) {
continue
}
fs, err := generateFilesForXdsResource(res)
if err != nil {
return nil, err
}
files = append(files, fs...)
}
for i, v := range files {
if strings.HasSuffix(v.Filename, ".go") {
files[i].Content = fileHeader + files[i].Content
}
}
if skipGeneratedTests {
var filesWithoutTests code_generator.Files
for _, file := range files {
if strings.HasSuffix(file.Filename, "_test.go") {
continue
}
filesWithoutTests = append(filesWithoutTests, file)
}
files = filesWithoutTests
}
return files, nil
}
func generateFilesForXdsResource(resource *model.XDSResource) (code_generator.Files, error) {
var v code_generator.Files
for suffix, tmpl := range map[string]*template.Template{
"_xds.sk.sk.go": templates.XdsTemplate,
} {
content, err := generateXdsResourceFile(resource, tmpl)
if err != nil {
return nil, err
}
v = append(v, code_generator.File{
Filename: strcase.ToSnake(resource.Name) + suffix,
Content: content,
})
}
return v, nil
}
func generateFilesForResource(resource *model.Resource) (code_generator.Files, error) {
var v code_generator.Files
for suffix, tmpl := range map[string]*template.Template{
".sk.go": templates.ResourceTemplate,
"_client.sk.go": templates.ResourceClientTemplate,
"_client_test.go": templates.ResourceClientTestTemplate,
"_reconciler.sk.go": templates.ResourceReconcilerTemplate,
} {
content, err := generateResourceFile(resource, tmpl)
if err != nil {
return nil, errors.Wrapf(err, "internal error: processing template '%v' for resource %v failed", tmpl.ParseName, resource.Name)
}
v = append(v, code_generator.File{
Filename: strcase.ToSnake(resource.Name) + suffix,
Content: content,
})
}
return v, nil
}
func generateFilesForResourceGroup(rg *model.ResourceGroup) (code_generator.Files, error) {
var v code_generator.Files
for suffix, tmpl := range map[string]*template.Template{
"_snapshot.sk.go": templates.ResourceGroupSnapshotTemplate,
"_snapshot_simple_emitter.sk.go": templates.SimpleEmitterTemplate,
"_snapshot_emitter.sk.go": templates.ResourceGroupEmitterTemplate,
"_snapshot_emitter_test.go": templates.ResourceGroupEmitterTestTemplate,
"_event_loop.sk.go": templates.ResourceGroupEventLoopTemplate,
"_simple_event_loop.sk.go": templates.SimpleEventLoopTemplate,
"_event_loop_test.go": templates.ResourceGroupEventLoopTestTemplate,
} {
content, err := generateResourceGroupFile(rg, tmpl)
if err != nil {
return nil, errors.Wrapf(err, "internal error: processing template '%s' for resource group %v failed", tmpl.ParseName, rg.Name)
}
v = append(v, code_generator.File{
Filename: strcase.ToSnake(rg.GoName) + suffix,
Content: content,
})
}
return v, nil
}
func generateFilesForProject(project *model.Project) (code_generator.Files, error) {
var v code_generator.Files
for suffix, tmpl := range map[string]*template.Template{
"_suite_test.go": templates.ProjectTestSuiteTemplate,
} {
content, err := generateProjectFile(project, tmpl)
if err != nil {
return nil, errors.Wrapf(err, "internal error: processing template '%v' for project %v failed", tmpl.ParseName, project.ProjectConfig.Name)
}
v = append(v, code_generator.File{
Filename: strcase.ToSnake(project.ProjectConfig.Name) + suffix,
Content: content,
})
}
return v, nil
}
func kubeProjectPrefix(projectName, version string) string {
return filepath.Join("kube", "apis", projectName, version)
}
func generateKubeFilesForProject(project *model.Project) (code_generator.Files, error) {
var v code_generator.Files
for suffix, tmpl := range map[string]*template.Template{
"doc.go": kube.DocTemplate,
"register.go": kube.RegisterTemplate,
"types.go": kube.TypesTemplate,
} {
content, err := generateProjectFile(project, tmpl)
if err != nil {
return nil, errors.Wrapf(err, "internal error: processing template '%v' for project %v failed", tmpl.ParseName, project.ProjectConfig.Name)
}
v = append(v, code_generator.File{
Filename: filepath.Join(kubeProjectPrefix(project.ProjectConfig.Name, project.ProjectConfig.Version), suffix),
Content: content,
})
}
for suffix, tmpl := range map[string]*template.Template{
"hack/update-codegen.sh": kube.GenerateScriptTemplate,
"hack/verify-codegen.sh": kube.VerifyGenerateTemplate,
} {
content, err := generateProjectFile(project, tmpl)
if err != nil {
return nil, errors.Wrapf(err, "internal error: processing template '%v' for project %v failed", tmpl.ParseName, project.ProjectConfig.Name)
}
v = append(v, code_generator.File{
Filename: filepath.Join("kube", suffix),
Content: content,
})
}
return v, nil
}
func generateXdsResourceFile(resource *model.XDSResource, tmpl *template.Template) (string, error) {
buf := &bytes.Buffer{}
if err := tmpl.Execute(buf, resource); err != nil {
return "", err
}
return buf.String(), nil
}
func generateResourceFile(resource *model.Resource, tmpl *template.Template) (string, error) {
buf := &bytes.Buffer{}
if err := tmpl.Execute(buf, resource); err != nil {
return "", err
}
return buf.String(), nil
}
func generateResourceGroupFile(rg *model.ResourceGroup, tmpl *template.Template) (string, error) {
buf := &bytes.Buffer{}
if err := tmpl.Execute(buf, rg); err != nil {
return "", err
}
return buf.String(), nil
}
func generateProjectFile(project *model.Project, tmpl *template.Template) (string, error) {
buf := &bytes.Buffer{}
if err := tmpl.Execute(buf, project); err != nil {
return "", err
}
return buf.String(), nil
}