-
Notifications
You must be signed in to change notification settings - Fork 6
/
codegen.go
384 lines (342 loc) · 11 KB
/
codegen.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
package generator
import (
"bytes"
"fmt"
"github.com/goradd/gofile/pkg/sys"
"github.com/goradd/goradd/pkg/config"
"github.com/goradd/goradd/pkg/orm/db"
"github.com/goradd/goradd/pkg/stringmap"
"github.com/goradd/goradd/pkg/strings"
"log"
"os"
"os/exec"
"path"
"path/filepath"
)
// BuildingExamples turns on the building of the templates for the examples code.
// This is specific to automating the build of the examples database code.
// You do not normally need to set this.
var BuildingExamples bool
type CodeGenerator struct {
// Tables is a map of the tables by database
Tables map[string]map[string]TableType
// EnumTables is a map of the enum tables by database
EnumTables map[string]map[string]EnumTableType
// importAliasesByPath stores import paths by package name to help correctly manage packages with the same name
importAliasesByPath map[string]string
}
type TableType struct {
*db.Table
controlDescriptions map[interface{}]*ControlDescription
Imports []ImportType
}
type EnumTableType struct {
*db.EnumTable
}
// ImportType represents an import path required for a control. This is analyzed per-table.
type ImportType struct {
Path string
Alias string // blank if not needing an alias
}
// ControlDescription is matched with a Column below and provides additional information regarding
// how information in a column can be used to generate a default control to edit that information.
// It is specifically for code generation.
type ControlDescription struct {
Path string
// Package is the package alias to be used when referring to the package the control is in. It is generated on a per-file basis.
Package string
// Imports is the list of imported packages that the control uses
Imports []string
ControlType string
ControlName string
ControlID string // default id to generate
DefaultLabel string
Generator ControlGenerator
Connector string
}
func (cd *ControlDescription) ControlIDConst() string {
if cd.ControlID == "" {
return ""
}
return strings.KebabToCamel(cd.ControlID) + "Id"
}
func (t *TableType) GetColumnByDbName(name string) *db.Column {
for _, col := range t.Columns {
if col.DbName == name {
return col
}
}
return nil
}
func (t *TableType) ControlDescription(ref interface{}) *ControlDescription {
return t.controlDescriptions[ref]
}
func Generate() {
codegen := CodeGenerator{
Tables: make(map[string]map[string]TableType),
EnumTables: make(map[string]map[string]EnumTableType),
}
databases := db.GetDatabases()
if len(databases) == 0 {
log.Println("There are no databases to use for code generation. Setup databases in the db.cfg file.")
}
if BuildingExamples {
databases = []db.DatabaseI{db.GetDatabase("goradd")}
}
// map object names to tables, making sure there are no duplicates
for _, database := range databases {
if database.Model() == nil {
panic("Missing model. Did you forget to call Analyze on the database?")
}
dbKey := database.Model().DbKey
codegen.Tables[dbKey] = make(map[string]TableType)
codegen.EnumTables[dbKey] = make(map[string]EnumTableType)
dd := database.Model()
// Create wrappers for the tables with extra analysis required for form generation
stringmap.Range(dd.EnumTables, func(k string, enumTable *db.EnumTable) bool {
if _, ok := codegen.EnumTables[dbKey][enumTable.GoName]; ok {
log.Println("Error: enum table " + enumTable.GoName + " is defined more than once.")
} else {
tt := EnumTableType{
enumTable,
}
codegen.EnumTables[dbKey][enumTable.GoName] = tt
}
return true
})
stringmap.Range(dd.Tables, func(k string, table *db.Table) bool {
if _, ok := codegen.Tables[dbKey][table.GoName]; ok {
log.Println("Error: table " + table.GoName + " is defined more than once.")
} else {
descriptions := make(map[interface{}]*ControlDescription)
importAliases := make(map[string]string)
matchColumnsWithControls(database, table, descriptions, importAliases)
matchReverseReferencesWithControls(table, descriptions, importAliases)
matchManyManyReferencesWithControls(table, descriptions, importAliases)
var i []ImportType
for _, k := range stringmap.SortedKeys(importAliases) {
i = append(i, ImportType{k, importAliases[k]})
}
t := TableType{
table,
descriptions,
i,
}
codegen.Tables[dbKey][table.GoName] = t
}
return true
})
}
buf := new(bytes.Buffer)
// Generate the templates.
for _, database := range databases {
dd := database.Model()
dbKey := dd.DbKey
for _, tableKey := range stringmap.SortedKeys(codegen.EnumTables[dbKey]) {
enumTable := codegen.EnumTables[dbKey][tableKey]
for _, enumTableTemplate := range EnumTableTemplates {
buf.Reset()
// the template generator function in each template, by convention
enumTableTemplate.GenerateEnumTable(codegen, dd, enumTable, buf)
fileName := enumTableTemplate.FileName(dbKey, enumTable)
fp := filepath.Dir(fileName)
// If the file already exists, and we are not over-writing, skip it
if _, err := os.Stat(fileName); err == nil {
if !enumTableTemplate.Overwrite() {
continue
}
}
if err := os.MkdirAll(fp, 0777); err != nil {
log.Print(err)
}
if err := os.WriteFile(fileName, buf.Bytes(), 0644); err != nil {
log.Print(err)
} else {
if Verbose {
log.Printf("Writing %s", fileName)
}
}
RunGoImports(fileName)
}
}
for _, tableKey := range stringmap.SortedKeys(codegen.Tables[dbKey]) {
table := codegen.Tables[dbKey][tableKey]
if table.PrimaryKeyColumn() == nil {
log.Println("*** Skipping table " + table.DbName + " since it has no primary key column")
continue
}
for _, tableTemplate := range TableTemplates {
buf.Reset()
tableTemplate.GenerateTable(codegen, dd, table, buf)
fileName := tableTemplate.FileName(dbKey, table)
fp := filepath.Dir(fileName)
// If the file already exists, and we are not over-writing, skip it
if _, err := os.Stat(fileName); err == nil {
if !tableTemplate.Overwrite() {
continue
}
}
if err := os.MkdirAll(fp, 0777); err != nil {
log.Print(err)
}
if err := os.WriteFile(fileName, buf.Bytes(), 0644); err != nil {
log.Print(err)
} else {
if Verbose {
log.Printf("Writing %s", fileName)
}
}
RunGoImports(fileName)
}
}
for _, dbTemplate := range DatabaseTemplates {
buf.Reset()
// the template generator function in each template, by convention
dbTemplate.GenerateDatabase(codegen, dd, buf)
fileName := dbTemplate.FileName(dbKey)
fp := filepath.Dir(fileName)
// If the file already exists, and we are not over-writing, skip it
if _, err := os.Stat(fileName); err == nil {
if !dbTemplate.Overwrite() {
continue
}
}
if err := os.MkdirAll(fp, 0777); err != nil {
log.Print(err)
}
if err := os.WriteFile(fileName, buf.Bytes(), 0644); err != nil {
log.Print(err)
} else {
if Verbose {
log.Printf("Writing %s", fileName)
}
}
RunGoImports(fileName)
}
}
for _, onceTemplate := range OneTimeTemplates {
buf.Reset()
onceTemplate.GenerateOnce(codegen, databases, buf)
fileName := onceTemplate.FileName()
fp := filepath.Dir(fileName)
// If the file already exists, and we are not over-writing, skip it
if _, err := os.Stat(fileName); err == nil {
if !onceTemplate.Overwrite() {
continue
}
}
if err := os.MkdirAll(fp, 0777); err != nil {
log.Print(err)
}
if err := os.WriteFile(fileName, buf.Bytes(), 0644); err != nil {
log.Print(err)
} else {
if Verbose {
log.Printf("Writing %s", fileName)
}
}
RunGoImports(fileName)
}
}
func RunGoImports(fileName string) {
// run imports on all generated go files
if strings.EndsWith(fileName, ".go") {
curDir, _ := os.Getwd()
_ = os.Chdir(filepath.Dir(fileName)) // run it from the file's directory to pick up the correct go.mod file if there is one
_, err := sys.ExecuteShellCommand("goimports -w " + filepath.Base(fileName))
_ = os.Chdir(curDir)
if err != nil {
if e, ok := err.(*exec.Error); ok {
panic("error running goimports: " + e.Error()) // perhaps goimports is not installed?
} else if e2, ok2 := err.(*exec.ExitError); ok2 {
// Likely a syntax error in the resulting file
log.Print(string(e2.Stderr))
}
}
}
}
// ResetImports resets the internal information of the code generator. Call this just before generating a file.
func (c *CodeGenerator) ResetImports() {
c.importAliasesByPath = make(map[string]string)
}
// AddImportPaths adds an import path to the import path list. In particular, it will help manage the package aliases
// so the path can be referred to using the correct package name or package alias. Call this on all
// paths used by the file before calling ImportString.
func (c *CodeGenerator) AddImportPaths(paths ...string) {
for _, p := range paths {
if p == "" {
return
}
if _, ok := c.importAliasesByPath[p]; ok {
return
}
alias := path.Base(p)
count := 2
newAlias := alias
Found:
for _, a := range c.importAliasesByPath {
if a == newAlias {
newAlias = fmt.Sprint(alias, count)
count++
continue Found
}
}
c.importAliasesByPath[p] = newAlias
}
}
// AddObjectPath adds an object path to the import path list. In particular, it will help manage the package list
// so the object can referred to using the correct package name or package alias. Call this on all object
// paths used by the form before calling ImportString.
func (c *CodeGenerator) AddObjectPath(p string) {
c.AddImportPaths(path.Dir(p))
}
// ImportStrings returns strings to use in an import statement for all of the objects and imports entered
func (c *CodeGenerator) ImportStrings() (ret string) {
for p, a := range c.importAliasesByPath {
pkg := path.Base(p)
if a == pkg {
ret += fmt.Sprintf("%#v\n", p)
} else {
ret += fmt.Sprintf("%s %#v\n", a, p)
}
}
return
}
// ObjectType returns the string that should be used for an object type given its module path
func (c *CodeGenerator) ObjectType(p string) string {
imp, t := path.Split(p)
imp = path.Clean(imp)
if a := c.importAliasesByPath[imp]; a == "" {
panic("unknown object path: " + p)
} else {
return a + "." + t
}
}
func (c *CodeGenerator) ImportPackage(imp string) string {
if a := c.importAliasesByPath[imp]; a == "" {
panic("unknown import path: " + imp)
} else {
return a
}
}
func (c *CodeGenerator) ObjectPackage(imp string) string {
imp = path.Dir(imp)
if a := c.importAliasesByPath[imp]; a == "" {
panic("unknown import path: " + imp)
} else {
return a
}
}
// WrapFormField returns a creator template for a field wrapper with type wrapperType.
//
// child should be the creator template for the control that will be wrapped.
func (c *CodeGenerator) WrapFormField(wrapperType string, label string, forId string, child string) string {
return fmt.Sprintf(
`%sCreator{
ID: p.ID() + "-%s%s",
For: p.ID() + "-%s",
Label: "%s",
Child: %s,
}
`, wrapperType, forId, config.DefaultFormFieldWrapperIdSuffix, forId, label, child)
}