-
Notifications
You must be signed in to change notification settings - Fork 5
/
knit.go
416 lines (366 loc) · 8.58 KB
/
knit.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
package knit
import (
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"sync"
"unicode"
"unicode/utf8"
"github.com/adrg/xdg"
lua "github.com/zyedidia/gopher-lua"
"github.com/zyedidia/knit/rules"
)
// Flags for modifying the behavior of Knit.
type Flags struct {
Knitfile string
Ncpu int
DryRun bool
RunDir string
Always bool
Quiet bool
Style string
CacheDir string
Hash bool
Updated []string
Shell string
KeepGoing bool
Tool string
ToolArgs []string
}
// Flags that may be automatically set in a .knit.toml file.
type UserFlags struct {
Knitfile *string
Ncpu *int
DryRun *bool
RunDir *string `toml:"directory"`
Always *bool
Quiet *bool
Style *string
CacheDir *string `toml:"cache"`
Hash *bool
Updated *[]string
Shell *string
KeepGoing *bool
}
// Capitalize the first rune of a string.
func title(s string) string {
r, size := utf8.DecodeRuneInString(s)
return string(unicode.ToTitle(r)) + s[size:]
}
func rel(basepath, targpath string) (string, error) {
slash := strings.HasSuffix(targpath, "/")
rel, err := filepath.Rel(basepath, targpath)
if err != nil {
return filepath.Join(basepath, targpath), nil
}
if slash {
rel += "/"
}
return rel, err
}
type assign struct {
name string
value string
}
// Parses 'args' for expressions of the form 'key=value'. Assignments that are
// found are returned, along with the remaining arguments.
func makeAssigns(args []string) ([]assign, []string) {
assigns := make([]assign, 0, len(args))
other := make([]string, 0)
for _, a := range args {
before, after, found := strings.Cut(a, "=")
if found {
assigns = append(assigns, assign{
name: before,
value: after,
})
} else {
other = append(other, a)
}
}
return assigns, other
}
func pathJoin(dir, target string) string {
if filepath.IsAbs(target) {
return target
}
p := filepath.Join(dir, target)
if strings.HasSuffix(target, "/") {
p += "/"
}
return p
}
// Changes the working directory to 'dir' and changes all targets to be
// relative to that directory.
func goToKnitfile(vm *LuaVM, dir string, targets []string) error {
wd, err := os.Getwd()
if err != nil {
return err
}
adir, err := filepath.Abs(dir)
if err != nil {
return err
}
for i, t := range targets {
r, err := rel(adir, wd)
if err != nil {
return err
}
if r != "" && r != "." {
targets[i] = pathJoin(r, t)
}
}
return os.Chdir(dir)
}
var ErrNothingToDo = errors.New("nothing to be done")
var ErrQuiet = errors.New("quiet")
type ErrMessage struct {
msg string
}
func (e *ErrMessage) Error() string {
return e.msg
}
// Given the return value from the Lua evaluation of the Knitfile, returns all
// the buildsets and a list of the directories in order of priority. If the
// Knitfile requested to return an error (with a string), or quietly (with
// nil), returns an appropriate error.
func getBuildSets(lval lua.LValue) (map[string]*LBuildSet, error) {
bsets := map[string]*LBuildSet{
".": {
Dir: ".",
rset: LRuleSet{},
},
}
var addBuildSet func(bs LBuildSet)
addBuildSet = func(bs LBuildSet) {
if b, ok := bsets[bs.Dir]; ok {
b.rset = append(b.rset, bs.rset...)
} else {
bsets[bs.Dir] = &bs
}
// TODO: can there be a buildset cycle?
for _, bset := range bs.bsets {
addBuildSet(bset)
}
}
switch v := lval.(type) {
case lua.LString:
return nil, &ErrMessage{msg: string(v)}
case *lua.LNilType:
return nil, ErrQuiet
case *lua.LUserData:
switch u := v.Value.(type) {
case LBuildSet:
addBuildSet(u)
default:
return nil, fmt.Errorf("invalid return value: %v", lval)
}
default:
return nil, fmt.Errorf("invalid return value: %v", lval)
}
return bsets, nil
}
// Run searches for a Knitfile and executes it, according to args (a list of
// targets and assignments), and the flags. All output is written to 'out'. The
// path of the executed knitfile is returned, along with a possible error.
func Run(out io.Writer, args []string, flags Flags) (string, error) {
if flags.RunDir != "" {
err := os.Chdir(flags.RunDir)
if err != nil {
return "", err
}
}
vm := NewLuaVM(flags.Shell, flags)
cliAssigns, targets := makeAssigns(args)
envAssigns, _ := makeAssigns(os.Environ())
vm.MakeTable("cli", cliAssigns)
vm.MakeTable("env", envAssigns)
file, dir, err := FindBuildFile(flags.Knitfile)
if err != nil {
return "", err
}
knitpath := filepath.Join(dir, file)
if file == "" {
def, ok := DefaultBuildFile()
if ok {
file = def
}
} else if dir != "" {
for i, u := range flags.Updated {
p, err := rel(dir, u)
if err != nil {
return knitpath, err
}
flags.Updated[i] = p
}
err = goToKnitfile(vm, dir, targets)
if err != nil {
return knitpath, err
}
}
if file == "" {
return knitpath, fmt.Errorf("%s does not exist", flags.Knitfile)
}
lval, err := vm.DoFile(file)
if err != nil {
return knitpath, err
}
bsets, err := getBuildSets(lval)
if err != nil {
return knitpath, err
}
var rulesets []*rules.RuleSet
var main *rules.RuleSet
for k, v := range bsets {
rs := rules.NewRuleSet(k)
for _, lr := range v.rset {
err := rules.ParseInto(lr.Contents, rs, lr.File, lr.Line)
if err != nil {
return knitpath, err
}
}
if k == "." {
main = rs
} else {
rulesets = append(rulesets, rs)
}
}
if main == nil {
return knitpath, fmt.Errorf("no buildset for the root directory found")
}
rs := rules.MergeRuleSets(main, rulesets)
alltargets := rs.AllTargets()
if len(targets) == 0 {
targets = []string{rs.MainTarget()}
}
rootTargets := make([]string, 0, len(targets))
if len(targets) == 0 {
return knitpath, errors.New("no targets")
}
for _, t := range targets {
if t != "" {
rootTargets = append(rootTargets, filepath.Base(t))
}
}
rs.Add(rules.NewDirectRuleBase([]string{":build"}, targets, nil, rules.AttrSet{
Virtual: true,
NoMeta: true,
Rebuild: true,
}))
rs.Add(rules.NewDirectRuleBase([]string{":build-root"}, rootTargets, nil, rules.AttrSet{
Virtual: true,
NoMeta: true,
Rebuild: true,
}))
rs.Add(rules.NewDirectRuleBase([]string{":all"}, alltargets, nil, rules.AttrSet{
Virtual: true,
NoMeta: true,
Rebuild: true,
}))
updated := make(map[string]bool)
for _, u := range flags.Updated {
updated[u] = true
}
graph, err := rules.NewGraph(rs, ":build", updated)
if err != nil {
g, rerr := rules.NewGraph(rs, ":build-root", updated)
if rerr != nil {
return knitpath, err
}
graph = g
}
err = graph.ExpandRecipes(vm)
if err != nil {
return knitpath, err
}
var db *rules.Database
if flags.CacheDir == "." || flags.CacheDir == "" {
db = rules.NewDatabase(filepath.Join(".knit", file))
} else {
wd, err := os.Getwd()
if err != nil {
return knitpath, err
}
dir := flags.CacheDir
if dir == "$cache" {
dir = filepath.Join(xdg.CacheHome, "knit")
}
db = rules.NewCacheDatabase(dir, filepath.Join(wd, file))
}
var w io.Writer = out
if flags.Quiet {
w = io.Discard
}
if flags.Tool != "" {
var t rules.Tool
switch flags.Tool {
case "list":
t = &rules.ListTool{W: w}
case "graph":
t = &rules.GraphTool{W: w}
case "clean":
t = &rules.CleanTool{W: w, NoExec: flags.DryRun, Db: db}
case "targets":
t = &rules.TargetsTool{W: w}
case "compdb":
t = &rules.CompileDbTool{W: w}
case "commands":
t = &rules.CommandsTool{W: w}
case "status":
t = &rules.StatusTool{W: w, Db: db, Hash: flags.Hash}
case "path":
t = &rules.PathTool{W: w, Path: knitpath}
case "db":
t = &rules.DbTool{W: w, Db: db}
default:
return knitpath, fmt.Errorf("unknown tool: %s", flags.Tool)
}
err = t.Run(graph, flags.ToolArgs)
if err != nil {
return knitpath, err
}
return knitpath, db.Save()
}
if flags.Ncpu <= 0 {
return knitpath, errors.New("you must enable at least 1 core")
}
var printer rules.Printer
switch flags.Style {
case "steps":
printer = &StepPrinter{w: w}
case "progress":
printer = &ProgressPrinter{
w: w,
tasks: make(map[string]string),
}
default:
printer = &BasicPrinter{w: w}
}
lock := sync.Mutex{}
ex := rules.NewExecutor(".", db, flags.Ncpu, printer, func(msg string) {
lock.Lock()
fmt.Fprintln(out, msg)
lock.Unlock()
}, rules.Options{
NoExec: flags.DryRun,
Shell: flags.Shell,
AbortOnError: !flags.KeepGoing,
BuildAll: flags.Always,
Hash: flags.Hash,
})
rebuilt, execerr := ex.Exec(graph)
err = db.Save()
if err != nil {
return knitpath, err
}
if execerr != nil {
return knitpath, execerr
}
if !rebuilt {
return knitpath, fmt.Errorf("'%s': %w", strings.Join(targets, " "), ErrNothingToDo)
}
return knitpath, nil
}