-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
233 lines (192 loc) · 5.01 KB
/
main.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
package core
import (
"encoding/json"
"fmt"
"io/ioutil"
"path"
"regexp"
"sort"
"unicode"
)
const inputFileName = "input.json"
const outputFileName = "output.json"
type mode uint
const (
modeBuild mode = iota
modeList
modeRun
modeTest
modeReport
modeFlags
)
type targetInfo struct {
Description string
Runnable bool
Testable bool
Report bool
Selected bool
}
type generatorInput struct {
DbtVersion version
SourceDir string
WorkingDir string
OutputDir string
CmdlineFlags map[string]string
WorkspaceFlags map[string]string
CompletionsOnly bool
RunArgs []string
TestArgs []string
Layout string
SelectedTargets []string
PersistFlags bool
PositivePatterns []string
NegativePatterns []string
Mode mode
}
type generatorOutput struct {
NinjaFile string
Targets map[string]targetInfo
Flags map[string]flagInfo
CompDbRules []string
SelectedTargets []string
}
var input = loadInput()
// Determine the set of targets to be built.
type targetFilter struct {
positiveRegexps []*regexp.Regexp
negativeRegexps []*regexp.Regexp
}
func makeFilter() targetFilter {
filter := targetFilter{}
for _, pattern := range input.PositivePatterns {
re, err := regexp.Compile(fmt.Sprintf("^%s$", pattern))
if err != nil {
Fatal("Positive target pattern '%s' is not a valid regular expression: %s.\n", pattern, err)
}
filter.positiveRegexps = append(filter.positiveRegexps, re)
}
for _, pattern := range input.NegativePatterns {
re, err := regexp.Compile(fmt.Sprintf("^%s$", pattern))
if err != nil {
Fatal("Negative target pattern '%s' is not a valid regular expression: %s.\n", pattern, err)
}
filter.negativeRegexps = append(filter.negativeRegexps, re)
}
return filter
}
func (f targetFilter) isSelected(targetPath string, info targetInfo) bool {
// Negative patterns have precedence
for _, re := range f.negativeRegexps {
if re.MatchString(targetPath) {
return false
}
}
for _, re := range f.positiveRegexps {
if re.MatchString(targetPath) {
return true
}
}
return false
}
func skipTarget(info targetInfo) bool {
if input.Mode == modeRun && !info.Runnable {
return true
}
if input.Mode == modeTest && !info.Testable {
return true
}
if input.Mode != modeReport && info.Report {
return true
}
return false
}
func GeneratorMain(vars map[string]interface{}) {
output := generatorOutput{
Targets: map[string]targetInfo{},
Flags: lockAndGetFlags(input.PersistFlags),
}
filter := makeFilter()
var selectedTargets = []interface{}{}
for targetPath, variable := range vars {
targetName := path.Base(targetPath)
if !unicode.IsUpper([]rune(targetName)[0]) {
continue
}
if _, ok := variable.(BuildInterface); !ok {
continue
}
info := targetInfo{}
if descriptionIface, ok := variable.(descriptionInterface); ok {
info.Description = descriptionIface.Description()
}
if _, ok := variable.(RunInterface); ok {
info.Runnable = true
}
if _, ok := variable.(TestInterface); ok {
info.Testable = true
}
if _, ok := variable.(ReportInterface); ok {
info.Report = true
}
if skipTarget(info) {
continue
}
info.Selected = filter.isSelected(targetPath, info)
if info.Selected {
selectedTargets = append(selectedTargets, variable)
if _, ok := variable.(BuildInterface); ok {
if input.Mode != modeReport || info.Report {
// In report mode do not pass non-report targets to ninja
output.SelectedTargets = append(output.SelectedTargets, targetPath)
}
}
}
output.Targets[targetPath] = info
}
// Create build files.
if !input.CompletionsOnly {
ctx := newContext(vars)
// Making sure targets are processed in a deterministic order
targetPaths := []string{}
for targetPath := range vars {
targetPaths = append(targetPaths, targetPath)
}
sort.Strings(targetPaths)
var allTargets = []interface{}{}
var reportTargets = []ReportInterface{}
for _, targetPath := range targetPaths {
tgt := vars[targetPath]
allTargets = append(allTargets, tgt)
if rep, ok := tgt.(ReportInterface); ok {
reportTargets = append(reportTargets, rep)
}
}
for _, targetPath := range targetPaths {
tgt := vars[targetPath]
if rep, ok := tgt.(ReportInterface); ok {
if info, iok := output.Targets[targetPath]; !iok || !info.Selected {
continue
}
tgt = rep.Report(allTargets, selectedTargets)
}
if build, ok := tgt.(BuildInterface); ok {
ctx.handleTarget(targetPath, build)
}
}
output.NinjaFile = ctx.ninjaFile()
output.CompDbRules = []string{}
for name := range ctx.compDbBuildRules {
output.CompDbRules = append(output.CompDbRules, name)
}
sort.Strings(output.CompDbRules)
}
// Serialize generator output.
data, err := json.MarshalIndent(output, "", " ")
if err != nil {
Fatal("failed to marshal generator output: %s", err)
}
err = ioutil.WriteFile(outputFileName, data, fileMode)
if err != nil {
Fatal("failed to write generator output: %s", err)
}
}