-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
gen.cue
157 lines (138 loc) · 3.85 KB
/
gen.cue
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
package cmds
import (
"github.com/hofstadter-io/hofmod-cli/schema"
)
#GenCommand: schema.#Command & {
// TBD: "✓"
Name: "gen"
Usage: "gen [files...]"
Aliases: ["G"]
Short: "create arbitrary files from data with templates and generators"
Long: GenLongHelp
Flags: [...schema.#Flag] & [
{
Name: "list"
Type: "bool"
Default: "false"
Help: "list available generators"
Long: "list"
Short: "l"
},
{
Name: "stats"
Type: "bool"
Default: "false"
Help: "print generator statistics"
Long: "stats"
Short: "s"
},
{
Name: "generator"
Type: "[]string"
Default: "nil"
Help: "generator tags to run, default is all"
Long: "generator"
Short: "G"
},
{
Name: "template"
Type: "[]string"
Default: "nil"
Help: "template mappings to render as '<filepath>;<?cuepath>;<?outpath>'"
Long: "template"
Short: "T"
},
{
Name: "partial"
Type: "[]string"
Default: "nil"
Help: "file globs to partial templates to register with the templates"
Long: "partial"
Short: "P"
},
{
Name: "diff3"
Type: "bool"
Default: "false"
Help: "enable diff3 support for adhoc render, generators are configured in code"
Long: "diff3"
Short: "D"
},
{
Name: "watch"
Type: "bool"
Default: "false"
Help: "run in watch mode, regenerating when files change"
Long: "watch"
Short: "w"
},
{
Name: "WatchGlobs"
Type: "[]string"
Default: "nil"
Help: "filepath globs to watch for changes and regen"
Long: "watch-globs"
Short: "W"
},
{
Name: "WatchXcue"
Type: "[]string"
Default: "nil"
Help: "like watch, but skips CUE reload, useful when working on templates, can be used with watch"
Long: "watch-xcue"
Short: "X"
},
{
Name: "AsModule"
Type: "string"
Default: ""
Help: "<name> in the printed output, for the given flags as a generator module"
Long: "as-module"
},
]
}
GenLongHelp: """
hof gen joins CUE with Go's text/template system and diff3
create on-liners to generate any file from any data
build reusable and modular generators
edit and regenerate those files while keeping changes
If no generator is specified, hof gen runs in adhoc mode.
# Render a template
hof gen data.cue -T template.txt
hof gen data.yaml schema.cue -T template.txt > output.txt
# Add partials to the template context
hof gen data.cue -T template.txt -P partial.txt
# The template flag as code gen mappings
hof gen data.cue ...
# Generate multiple templates at once
-T templateA.txt -T templateB.txt
# Select a sub-input value by CUEpath
-T 'templateA.txt:foo'
-T 'templateB.txt:sub.val'
# Choose a schema with @
-T 'templateA.txt:foo@#foo'
-T 'templateB.txt:sub.val@schemas.val'
# Writing to file with ; (semicolon)
-T 'templateA.txt;a.txt'
-T 'templateB.txt:sub.val@schema;b.txt'
# Templated output path
-T 'templateA.txt:;{{ .name | lower }}.txt'
# Repeated templates are used when
# 1. the output has a '[]' prefix
# 2. the input is a list or array
# The template will be processed per entry
# This also requires using a templated outpath
-T 'template.txt:items;[]out/{{ .filepath }}.txt'
# Learn about writing templates, with extra functions and helpers
https://docs.hofstadter.io/code-generation/template-writing/
# Check the tests for complete examples
https://github.com/hofstadter-io/hof/tree/_dev/test/render
# Turn any hof gen flags into a reusable generator module
hof gen [entrypoints] flags... --as-module [name]
hof gen [entrypoints] -G [name]
# Compose code gen mappings into reusable modules with
hof gen app.cue -G frontend -G backend -G migrations
https://docs.hofstadter.io/first-example/
# You can mix adhof with generators by using
# both the -G and -T/-P flags
"""