-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
gen.go
220 lines (177 loc) · 4.47 KB
/
gen.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
package gen
import (
"fmt"
"sync"
"time"
"github.com/hofstadter-io/hof/cmd/hof/flags"
"github.com/hofstadter-io/hof/lib/yagu"
)
func Gen(args []string, rootflags flags.RootPflagpole, cmdflags flags.GenFlagpole) error {
// return GenLast(args, rootflags, cmdflags)
verystart := time.Now()
err := runGen(args, rootflags, cmdflags)
if err != nil {
return err
}
// final timing
veryend := time.Now()
elapsed := veryend.Sub(verystart).Round(time.Millisecond)
if cmdflags.Stats {
fmt.Printf("\nGrand Total Elapsed Time: %s\n\n", elapsed)
}
return nil
}
func runGen(args []string, rootflags flags.RootPflagpole, cmdflags flags.GenFlagpole) error {
// This is our runtime for codegeneration
R := NewRuntime(args, cmdflags)
R.Verbosity = rootflags.Verbose
// b/c shorter names
LT := len(cmdflags.Template)
LG := len(cmdflags.Generator)
globs := cmdflags.WatchGlobs
xcue := cmdflags.WatchXcue
// determine watch mode
// excplicit: -w
// implicit: -W/-X
watch := cmdflags.Watch
if len(globs) > 0 || len(xcue) > 0 {
watch = true
}
// generally there are more messages when in watch mode
if watch {
fmt.Println("Loading CUE from", R.Entrypoints)
}
// this is the first time we create a runtime and load cue
err := R.LoadCue()
if err != nil {
return err
}
/* We will run a generator if either
not adhoc or is adhoc with the -G flag
So let's load them early, there is some helpful info in them
*/
if LT == 0 || LG > 0 {
// load generators just so we can search for watch lists
err := R.ExtractGenerators()
if err != nil {
return err
}
errsL := R.LoadGenerators()
if len(errsL) > 0 {
for _, e := range errsL {
fmt.Println(e)
// cuetils.PrintCueError(e)
}
return fmt.Errorf("\nErrors while loading generators\n")
}
}
if LT > 0 {
err = R.CreateAdhocGenerator()
if err != nil {
return err
}
}
/* Build up watch list
We need to buildup the watch list from flags
and any generator we might run, which might have watch settings
*/
for _, G := range R.Generators {
if G.Disabled {
continue
}
globs = append(globs, G.WatchGlobs...)
xcue = append(xcue, G.WatchXcue...)
}
// this might be empty, we calc anyway for ease and sharing
wfiles, err := yagu.FilesFromGlobs(globs)
if err != nil {
return err
}
xfiles, err := yagu.FilesFromGlobs(xcue)
if err != nil {
return err
}
// if we are in watch mode, let the user know what is being watched
if watch {
fmt.Printf("found %d glob files from %v\n", len(wfiles), globs)
fmt.Printf("found %d xcue files from %v\n", len(xfiles), xcue)
}
// code gen func
doGen := func(fast bool) (chan bool, error) {
if R.Verbosity > 0 {
fmt.Println("runGen.doGen: fast:", fast)
}
return R.genOnce(fast, watch, xfiles)
}
// no watch, we can now exit 0
if !watch {
_, err := doGen(true)
if err != nil {
return err
}
return nil
}
fmt.Println("watching for changes...")
// we are in watch mode, this loop does a complete reload
var wg sync.WaitGroup
// this is our main executor around full regen
wg.Add(1)
go func() {
defer wg.Done()
err = DoWatch(doGen, false, true, wfiles, "full", make(chan bool, 2))
}()
// main process waits here for ctrl-c
wg.Wait()
if err != nil {
return err
}
return nil
}
func (R *Runtime) genOnce(fast, watch bool, files []string) (chan bool, error) {
verystart := time.Now()
doGen := func(fast bool) (chan bool, error) {
if R.Verbosity > 0 {
fmt.Println("genOnce.doGen: fast:", fast)
}
err := R.Reload(fast)
if err != nil {
return nil, err
}
// issue #20 - Don't print and exit on error here, wait until after we have written, so we can still write good files
errsG := R.RunGenerators()
errsW := R.WriteOutput()
// final timing
veryend := time.Now()
elapsed := veryend.Sub(verystart).Round(time.Millisecond)
if R.Flagpole.Stats {
R.PrintStats()
fmt.Printf("\nTotal Elapsed Time: %s\n\n", elapsed)
}
if len(errsG) > 0 {
for _, e := range errsG {
fmt.Println(e)
}
return nil, fmt.Errorf("\nErrors while generating output\n")
}
if len(errsW) > 0 {
for _, e := range errsW {
fmt.Println(e)
}
return nil, fmt.Errorf("\nErrors while writing output\n")
}
R.PrintMergeConflicts()
return nil, nil
} // end doGen
// run code gen
_, err := doGen(fast)
if err != nil {
return nil, err
}
// return if not watching
if !watch {
return nil, nil
}
quit := make(chan bool, 2)
go DoWatch(doGen, true, false, files, "xcue", quit)
return quit, err
}