-
Notifications
You must be signed in to change notification settings - Fork 1
/
generator.go
371 lines (349 loc) · 9.22 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
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
package gen
import (
"bytes"
"fmt"
"os"
"path/filepath"
"strings"
"text/template"
"time"
"github.com/apigear-io/cli/pkg/gen/filters"
"github.com/apigear-io/cli/pkg/helper"
"github.com/apigear-io/cli/pkg/model"
"github.com/apigear-io/cli/pkg/spec"
)
// Generator parses documents and applies
// template transformation on a set of files.
type DataMap map[string]any
type GeneratorStats struct {
FilesWritten int `json:"files_written"`
FilesSkipped int `json:"files_skipped"`
FilesCopied int `json:"files_copied"`
FilesTouched []string `json:"files_touched"`
RunStart time.Time `json:"run_start"`
RunEnd time.Time `json:"run_end"`
Duration time.Duration `json:"duration"`
}
func (g *GeneratorStats) Start() {
g.RunStart = time.Now()
}
func (g *GeneratorStats) Stop() {
g.RunEnd = time.Now()
g.Duration = g.RunEnd.Sub(g.RunStart).Truncate(time.Millisecond)
log.Info().Msgf("generated %d files in %s. (%d write, %d skip, %d copy)", g.TotalFiles(), g.Duration, g.FilesWritten, g.FilesSkipped, g.FilesCopied)
}
func (s *GeneratorStats) TotalFiles() int {
return s.FilesWritten + s.FilesSkipped + s.FilesCopied
}
type GeneratorOptions struct {
OutputDir string
TemplatesDir string
System *model.System
UserFeatures []string
UserForce bool
Output Output
DryRun bool
}
// generator applies template transformation on a set of files define in rules
type generator struct {
Template *template.Template
System *model.System
UserFeatures []string // features defined by user
ComputedFeatures map[string]bool
UserForce bool // force overwrite
TemplatesDir string
OutputDir string
DryRun bool
Stats GeneratorStats
Output Output
}
func New(o GeneratorOptions) (*generator, error) {
if o.Output == nil {
o.Output = &FileOutput{}
}
if o.System == nil {
return nil, fmt.Errorf("system is required")
}
if len(o.UserFeatures) == 0 {
o.UserFeatures = []string{"all"}
}
g := &generator{
OutputDir: o.OutputDir,
Template: template.New(""),
UserForce: o.UserForce,
System: o.System,
TemplatesDir: o.TemplatesDir,
UserFeatures: o.UserFeatures,
DryRun: o.DryRun,
Output: o.Output,
}
g.Template.Funcs(filters.PopulateFuncMap())
err := g.ParseTemplatesDir(o.TemplatesDir)
if err != nil {
return nil, err
}
return g, nil
}
func (g *generator) ParseTemplate(path string) error {
b, err := os.ReadFile(path)
if err != nil {
return err
}
tplName, err := filepath.Rel(g.TemplatesDir, path)
if err != nil {
return err
}
_, err = g.Template.New(tplName).Parse(string(b))
return err
}
func (g *generator) ParseTemplatesDir(dir string) error {
log.Debug().Msgf("parsing templates dir: %s", dir)
err := filepath.WalkDir(dir, func(path string, d os.DirEntry, err error) error {
if err != nil {
return err
}
// ignore all dirs
if d.IsDir() {
return nil
}
// ignore files start starting with .
if strings.HasPrefix(filepath.Base(path), ".") {
return nil
}
if !strings.HasSuffix(filepath.Base(path), ".tpl") {
return nil
}
return g.ParseTemplate(path)
})
if err != nil {
return err
}
return nil
}
// ProcessRules processes a set of rules from a rules document
func (g *generator) ProcessRules(doc *spec.RulesDoc) error {
log.Debug().Msgf("processing rules: %s", doc.Name)
g.Stats = GeneratorStats{}
g.Stats.Start()
defer func() {
g.Stats.Stop()
}()
if g.System == nil {
return fmt.Errorf("system is nil")
}
err := doc.ComputeFeatures(g.UserFeatures)
if err != nil {
return err
}
g.ComputedFeatures = doc.FeatureNamesMap()
for _, f := range doc.Features {
if f.Skip {
continue
}
err := g.processFeature(f)
if err != nil {
return err
}
}
return nil
}
// processFeature processes a feature rule
func (g *generator) processFeature(f *spec.FeatureRule) error {
log.Debug().Msgf("processing feature %s", f.Name)
// process system
ctx := model.SystemScope{
System: g.System,
Features: g.ComputedFeatures,
}
scopes := f.FindScopesByMatch(spec.ScopeSystem)
for _, scope := range scopes {
err := g.processScope(scope, ctx)
if err != nil {
return err
}
}
for _, module := range g.System.Modules {
// process module
scopes := f.FindScopesByMatch(spec.ScopeModule)
ctx := model.ModuleScope{
System: g.System,
Module: module,
Features: g.ComputedFeatures,
}
for _, scope := range scopes {
err := g.processScope(scope, ctx)
if err != nil {
return err
}
}
for _, iface := range module.Interfaces {
// process interface
ctx := model.InterfaceScope{
System: g.System,
Module: module,
Interface: iface,
Features: g.ComputedFeatures,
}
scopes := f.FindScopesByMatch(spec.ScopeInterface)
for _, scope := range scopes {
err := g.processScope(scope, ctx)
if err != nil {
return err
}
}
}
for _, struct_ := range module.Structs {
// process struct
ctx := model.StructScope{
System: g.System,
Module: module,
Struct: struct_,
Features: g.ComputedFeatures,
}
scopes := f.FindScopesByMatch(spec.ScopeStruct)
for _, scope := range scopes {
err := g.processScope(scope, ctx)
if err != nil {
return err
}
}
}
for _, enum := range module.Enums {
// process enum
ctx := model.EnumScope{
System: g.System,
Module: module,
Enum: enum,
Features: g.ComputedFeatures,
}
scopes := f.FindScopesByMatch(spec.ScopeEnum)
for _, scope := range scopes {
err := g.processScope(scope, ctx)
if err != nil {
return err
}
}
}
}
return nil
}
// processScope processes a scope rule (e.g. system, modules, ...) with the given context
func (g *generator) processScope(scope *spec.ScopeRule, ctx any) error {
prefix := scope.Prefix
for _, doc := range scope.Documents {
// clean doc target
if doc.Target == "" {
doc.Target = doc.Source
}
// apply prefix if set
if prefix != "" {
doc.Target = prefix + doc.Target
}
err := g.processDocument(doc, ctx)
if err != nil {
return err
}
}
return nil
}
// processDocument processes a document rule with the given context
func (g *generator) processDocument(doc spec.DocumentRule, ctx any) error {
log.Debug().Msgf("processing document %s", doc.Source)
// the source file to render
var source = filepath.Clean(doc.Source)
// the docTarget destination file
var docTarget = filepath.Clean(doc.Target)
// either user can force an overwrite or the target or the rules document
force := doc.Force || g.UserForce
// transform the target name using the context
target, err := g.RenderString(docTarget, ctx)
if err != nil {
return fmt.Errorf("render rules target %s: %s", docTarget, err)
}
// TODO: when doc.Raw is set, we should just copy it to the target
if doc.Raw {
// copy the source to the target
err := g.CopyFile(source, target, force)
if err != nil {
log.Warn().Msgf("copy file %s to %s: %s", source, target, err)
return err
}
} else {
// render the source file to the target
err := g.RenderFile(source, target, ctx, force)
if err != nil {
return err
}
}
return nil
}
// Renders a string using the given context
func (g *generator) RenderString(s string, ctx any) (string, error) {
var buf = bytes.NewBuffer(nil)
t := template.New("target")
t.Funcs(filters.PopulateFuncMap())
_, err := t.Parse(s)
if err != nil {
log.Warn().Msgf("render string: %s: %s", s, err)
return "", err
}
err = t.Execute(buf, ctx)
if err != nil {
log.Warn().Msgf("exec template %s: %s", s, err)
return "", err
}
return buf.String(), nil
}
func (g *generator) CopyFile(source, target string, force bool) error {
g.Stats.FilesCopied++
if g.DryRun {
log.Info().Msgf("dry run: copying file %s to %s", source, target)
g.Stats.FilesTouched = append(g.Stats.FilesTouched, target)
return nil
}
source = helper.Join(g.TemplatesDir, source)
target = helper.Join(g.OutputDir, target)
return g.Output.Copy(source, target, force)
}
func (g *generator) RenderFile(source, target string, ctx any, force bool) error {
// var force = doc.Force
// var transform = doc.Transform
log.Debug().Msgf("render %s -> %s", source, target)
// render the template using the context
buf := bytes.NewBuffer(nil)
err := g.Template.ExecuteTemplate(buf, source, ctx)
if err != nil {
log.Warn().Msgf("exec template %s: %s", source, err)
return fmt.Errorf("render template %s: %w", source, err)
}
// write the file
log.Debug().Msgf("write %s", target)
err = g.WriteFile(buf.Bytes(), target, force)
if err != nil {
log.Warn().Msgf("write file %s: %s", target, err)
return fmt.Errorf("write file %s: %w", target, err)
}
return nil
}
func (g *generator) WriteFile(input []byte, target string, force bool) error {
target = helper.Join(g.OutputDir, target)
if !force {
same, err := g.Output.Compare(input, target)
if err != nil {
return err
}
if same {
g.Stats.FilesSkipped++
log.Info().Msgf("skipping file %s", target)
return nil
}
}
log.Debug().Msgf("write file %s", target)
g.Stats.FilesTouched = append(g.Stats.FilesTouched, target)
g.Stats.FilesWritten++
if g.DryRun {
log.Info().Msgf("dry run: writing file %s", target)
return nil
}
return g.Output.Write(input, target, force)
}