-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvariables.go
More file actions
630 lines (592 loc) · 18.2 KB
/
variables.go
File metadata and controls
630 lines (592 loc) · 18.2 KB
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
package task
import (
"fmt"
"maps"
"os"
"path/filepath"
"strings"
"github.com/joho/godotenv"
"github.com/wallix/task/v3/errors"
"github.com/wallix/task/v3/internal/deepcopy"
"github.com/wallix/task/v3/internal/env"
"github.com/wallix/task/v3/internal/execext"
"github.com/wallix/task/v3/internal/filepathext"
"github.com/wallix/task/v3/internal/fingerprint"
"github.com/wallix/task/v3/internal/templater"
"github.com/wallix/task/v3/taskfile/ast"
)
// CompiledTask returns a copy of a task, but replacing variables in almost all
// properties using the Go template package.
func (e *Executor) CompiledTask(call *Call) (*ast.Task, error) {
return e.compiledTask(call, true)
}
// FastCompiledTask is like CompiledTask, but it skippes dynamic variables.
func (e *Executor) FastCompiledTask(call *Call) (*ast.Task, error) {
return e.compiledTask(call, false)
}
func (e *Executor) CompiledTaskForTaskList(call *Call) (*ast.Task, error) {
origTask, err := e.GetTask(call)
if err != nil {
return nil, err
}
vars, err := e.Compiler.FastGetVariables(origTask, call)
if err != nil {
return nil, err
}
cache := &templater.Cache{Vars: vars}
return &ast.Task{
Task: origTask.Task,
Label: templater.Replace(origTask.Label, cache),
Desc: templater.Replace(origTask.Desc, cache),
Prompt: templater.Replace(origTask.Prompt, cache),
Summary: templater.Replace(origTask.Summary, cache),
Aliases: origTask.Aliases,
Sources: origTask.Sources,
Generates: origTask.Generates,
Dirs: origTask.Dirs,
Set: origTask.Set,
Shopt: origTask.Shopt,
Vars: vars,
Env: nil,
Dotenv: origTask.Dotenv,
Silent: deepcopy.Scalar(origTask.Silent),
Interactive: origTask.Interactive,
Internal: origTask.Internal,
Prefix: origTask.Prefix,
IgnoreError: origTask.IgnoreError,
Run: origTask.Run,
IncludeVars: origTask.IncludeVars,
IncludedTaskfileVars: origTask.IncludedTaskfileVars,
Platforms: origTask.Platforms,
Location: origTask.Location,
Requires: origTask.Requires,
Watch: origTask.Watch,
Namespace: origTask.Namespace,
Failfast: origTask.Failfast,
}, nil
}
func (e *Executor) compiledTask(call *Call, evaluateShVars bool) (*ast.Task, error) {
origTask, err := e.GetTask(call)
if err != nil {
return nil, err
}
var vars *ast.Vars
if evaluateShVars {
vars, err = e.Compiler.GetVariables(origTask, call)
} else {
vars, err = e.Compiler.FastGetVariables(origTask, call)
}
if err != nil {
return nil, err
}
fullName := origTask.Task
if matches, exists := vars.Get("MATCH"); exists {
for _, match := range matches.Value.([]string) {
fullName = strings.Replace(fullName, "*", match, 1)
}
}
cache := &templater.Cache{Vars: vars}
new := ast.Task{
Task: origTask.Task,
Label: templater.Replace(origTask.Label, cache),
Desc: templater.Replace(origTask.Desc, cache),
Prompt: templater.Replace(origTask.Prompt, cache),
Summary: templater.Replace(origTask.Summary, cache),
Aliases: origTask.Aliases,
Sources: templater.ReplaceGlobs(origTask.Sources, cache),
Generates: templater.ReplaceGlobs(origTask.Generates, cache),
Dirs: templater.Replace(origTask.Dirs, cache),
Set: origTask.Set,
Shopt: origTask.Shopt,
Vars: vars,
Env: nil,
Dotenv: templater.Replace(origTask.Dotenv, cache),
Silent: deepcopy.Scalar(origTask.Silent),
Interactive: origTask.Interactive,
Internal: origTask.Internal,
Prefix: templater.Replace(origTask.Prefix, cache),
IgnoreError: origTask.IgnoreError,
Run: templater.Replace(origTask.Run, cache),
IncludeVars: origTask.IncludeVars,
IncludedTaskfileVars: origTask.IncludedTaskfileVars,
RawCmds: origTask.Cmds,
Cache: nil, // resolved below after CHECKSUM is available
Platforms: origTask.Platforms,
If: templater.Replace(origTask.If, cache),
Location: origTask.Location,
Requires: origTask.Requires,
Watch: origTask.Watch,
Failfast: origTask.Failfast,
Namespace: origTask.Namespace,
FullName: fullName,
}
for i := range new.Dirs {
new.Dirs[i], err = execext.ExpandLiteral(new.Dirs[i])
if err != nil {
return nil, err
}
}
if e.Dir != "" {
new.Dirs = append([]string{e.Dir}, new.Dirs...)
}
if new.Prefix == "" {
new.Prefix = new.Task
}
dotenvEnvs := ast.NewVars()
if len(new.Dotenv) > 0 {
for _, dotEnvPath := range new.Dotenv {
dotEnvPath = filepathext.JoinDirs(append(new.Dirs, dotEnvPath))
if _, err := os.Stat(dotEnvPath); os.IsNotExist(err) {
continue
}
envs, err := godotenv.Read(dotEnvPath)
if err != nil {
return nil, err
}
for key, value := range envs {
if _, ok := dotenvEnvs.Get(key); !ok {
dotenvEnvs.Set(key, ast.Var{Value: value})
}
}
}
}
new.Env = ast.NewVars()
new.Env.Merge(templater.ReplaceVars(e.Taskfile.Env, cache), nil)
new.Env.Merge(templater.ReplaceVars(dotenvEnvs, cache), nil)
new.Env.Merge(templater.ReplaceVars(origTask.Env, cache), nil)
if evaluateShVars {
for k, v := range new.Env.All() {
// If the variable is not dynamic, we can set it and return
if v.Value != nil || v.Sh == nil {
new.Env.Set(k, ast.Var{Value: v.Value})
continue
}
static, err := e.Compiler.HandleDynamicVar(v, new.ComputeDir(), env.GetFromVars(new.Env))
if err != nil {
return nil, err
}
new.Env.Set(k, ast.Var{Value: static})
}
}
// Compute source hash from RawCmds (unresolved templates) for stable
// checksumming. CHECKSUM must be available before cmd/cache resolution.
if len(origTask.Sources) > 0 {
checker := fingerprint.NewChecksumChecker(e.TempDir.Fingerprint, &new)
new.SourceHash = checker.SourceValue()
vars.Set("CHECKSUM", ast.Var{Live: new.SourceHash})
cache.ResetCache()
}
if len(origTask.Cmds) > 0 {
new.Cmds = make([]*ast.Cmd, 0, len(origTask.Cmds))
for _, cmd := range origTask.Cmds {
if cmd == nil {
continue
}
if cmd.For != nil {
list, keys, err := itemsFromFor(cmd.For, new.ComputeDir(), new.Sources, new.Generates, vars, origTask.Location, cache)
if err != nil {
return nil, err
}
// Name the iterator variable
var as string
if cmd.For.As != "" {
as = cmd.For.As
} else {
as = "ITEM"
}
// Create a new command for each item in the list
for i, loopValue := range list {
extra := map[string]any{
as: loopValue,
}
if len(keys) > 0 {
extra["KEY"] = keys[i]
}
newCmd := cmd.DeepCopy()
newCmd.Cmd = templater.ReplaceWithExtra(cmd.Cmd, cache, extra)
newCmd.Task = templater.ReplaceWithExtra(cmd.Task, cache, extra)
newCmd.If = templater.ReplaceWithExtra(cmd.If, cache, extra)
newCmd.Vars = templater.ReplaceVarsWithExtra(cmd.Vars, cache, extra)
new.Cmds = append(new.Cmds, newCmd)
}
continue
}
// Defer commands are replaced in a lazy manner because
// we need to include EXIT_CODE.
if cmd.Defer {
new.Cmds = append(new.Cmds, cmd.DeepCopy())
continue
}
newCmd := cmd.DeepCopy()
newCmd.Cmd = templater.Replace(cmd.Cmd, cache)
newCmd.Task = templater.Replace(cmd.Task, cache)
newCmd.If = templater.Replace(cmd.If, cache)
newCmd.Vars = templater.ReplaceVars(cmd.Vars, cache)
new.Cmds = append(new.Cmds, newCmd)
}
}
new.Setup, err = compileDeps(origTask.Setup, &new, vars, origTask.Location, cache)
if err != nil {
return nil, err
}
new.Deps, err = compileDeps(origTask.Deps, &new, vars, origTask.Location, cache)
if err != nil {
return nil, err
}
// Resolve "from:" entries in sources and generates by expanding them
// into the sources/generates of the referenced tasks.
if err := e.resolveGlobsFrom(&new, "sources"); err != nil {
return nil, err
}
if err := e.resolveGlobsFrom(&new, "generates"); err != nil {
return nil, err
}
// Recompute source hash if sources were extended by from: resolution,
// so that the CHECKSUM reflects the full set of inputs.
if hasFromEntries(origTask.Sources) && len(new.Sources) > 0 {
checker := fingerprint.NewChecksumChecker(e.TempDir.Fingerprint, &new)
new.SourceHash = checker.SourceValue()
vars.Set("CHECKSUM", ast.Var{Live: new.SourceHash})
cache.ResetCache()
}
if len(origTask.Preconditions) > 0 {
new.Preconditions = make([]*ast.Precondition, 0, len(origTask.Preconditions))
for _, precondition := range origTask.Preconditions {
if precondition == nil {
continue
}
newPrecondition := precondition.DeepCopy()
newPrecondition.Sh = templater.Replace(precondition.Sh, cache)
newPrecondition.Msg = templater.Replace(precondition.Msg, cache)
new.Preconditions = append(new.Preconditions, newPrecondition)
}
}
// Resolve cache fields — CHECKSUM is already available from above.
if origTask.Cache != nil {
resolved := origTask.Cache.DeepCopy()
if resolved.Inherit != "" {
if model, ok := e.Taskfile.Caches[resolved.Inherit]; ok && model != nil {
merged := model.DeepCopy()
if resolved.URL != "" {
merged.URL = resolved.URL
}
if resolved.Lock != "" {
merged.Lock = resolved.Lock
}
if resolved.If != "" {
merged.If = resolved.If
}
if resolved.Enabled != nil {
merged.Enabled = resolved.Enabled
}
if resolved.LockTimeout != "" {
merged.LockTimeout = resolved.LockTimeout
}
resolved = merged
}
}
resolved.Inherit = ""
resolved.URL = templater.Replace(resolved.URL, cache)
resolved.Lock = templater.Replace(resolved.Lock, cache)
resolved.If = templater.Replace(resolved.If, cache)
resolved.LockTimeout = templater.Replace(resolved.LockTimeout, cache)
new.Cache = resolved
}
// Validate that cached tasks don't reference generates outside the
// project root — such paths would escape the archive on extraction.
if e.cacheEnabled(&new) && e.Dir != "" {
taskDir := new.ComputeDir()
for _, g := range new.Generates {
resolved := filepathext.SmartJoin(taskDir, g.Glob)
rel, err := filepath.Rel(e.Dir, resolved)
if err != nil || strings.HasPrefix(rel, "..") {
return nil, fmt.Errorf("task: %s: generates path %q is outside project root %q; caching requires all outputs to be within the project directory", new.Task, g.Glob, e.Dir)
}
}
}
// We only care about templater errors if we are evaluating shell variables
if evaluateShVars && cache.Err() != nil {
return &new, cache.Err()
}
return &new, nil
}
// hasFromEntries reports whether any Glob in the slice has a From directive.
func hasFromEntries(globs []*ast.Glob) bool {
for _, g := range globs {
if g.From != "" {
return true
}
}
return false
}
// resolveGlobsFrom expands "from:" entries in a task's sources or generates
// list (selected by field). Supported from: values:
// - "deps": replaced by the same field of every direct dependency
// - "cmds": replaced by the same field of every cmd task-call
//
// This allows wrapper tasks to inherit their children's sources/generates
// without duplicating glob patterns. Chaining works: if a child also uses
// "from:", its globs are resolved first via CompiledTask.
func (e *Executor) resolveGlobsFrom(t *ast.Task, field string) error {
var globs *[]*ast.Glob
switch field {
case "sources":
globs = &t.Sources
case "generates":
globs = &t.Generates
default:
return fmt.Errorf("task: %s: resolveGlobsFrom: unknown field %q", t.Task, field)
}
if !hasFromEntries(*globs) {
return nil
}
getField := func(ct *ast.Task) []*ast.Glob {
if field == "sources" {
return ct.Sources
}
return ct.Generates
}
resolved := make([]*ast.Glob, 0, len(*globs))
seen := make(map[string]bool)
add := func(g *ast.Glob) {
key := g.Glob
if g.Negate {
key = "!" + key
}
if g.Fingerprint != "" {
key += "\x00fp:" + g.Fingerprint
}
if seen[key] {
return
}
seen[key] = true
resolved = append(resolved, g)
}
addFromTask := func(taskName string, vars *ast.Vars, fromKind string) error {
ct, err := e.CompiledTask(&Call{Task: taskName, Vars: vars, Indirect: true})
if err != nil {
return fmt.Errorf("task: %s: %s: from: %s: resolving %q: %w", t.Task, field, fromKind, taskName, err)
}
for _, g := range getField(ct) {
if g.From != "" {
continue
}
add(g)
}
return nil
}
for _, g := range *globs {
if g.From == "" {
add(g)
continue
}
switch g.From {
case "deps":
for _, dep := range t.Deps {
if dep == nil {
continue
}
if err := addFromTask(dep.Task, dep.Vars, "deps"); err != nil {
return err
}
}
case "cmds":
for _, cmd := range t.Cmds {
if cmd == nil || cmd.Task == "" {
continue
}
if err := addFromTask(cmd.Task, cmd.Vars, "cmds"); err != nil {
return err
}
}
default:
return fmt.Errorf("task: %s: %s: unsupported from: %q (expected \"deps\" or \"cmds\")", t.Task, field, g.From)
}
}
*globs = resolved
return nil
}
func asAnySlice[T any](slice []T) []any {
ret := make([]any, len(slice))
for i, v := range slice {
ret[i] = v
}
return ret
}
func itemsFromFor(
f *ast.For,
dir string,
sources []*ast.Glob,
generates []*ast.Glob,
vars *ast.Vars,
location *ast.Location,
cache *templater.Cache,
) ([]any, []string, error) {
var keys []string // The list of keys to loop over (only if looping over a map)
var values []any // The list of values to loop over
// Get the list from a matrix
if f.Matrix.Len() != 0 {
if err := resolveMatrixRefs(f.Matrix, cache); err != nil {
return nil, nil, errors.TaskfileInvalidError{
URI: location.Taskfile,
Err: err,
}
}
return asAnySlice(product(f.Matrix)), nil, nil
}
// Get the list from the explicit for list
if len(f.List) > 0 {
return f.List, nil, nil
}
// Get the list from the task sources
if f.From == "sources" {
glist, err := fingerprint.Globs(dir, sources)
if err != nil {
return nil, nil, err
}
// Make the paths relative to the task dir
for i, v := range glist {
if glist[i], err = filepath.Rel(dir, v); err != nil {
return nil, nil, err
}
}
values = asAnySlice(glist)
}
// Get the list from the task generates
if f.From == "generates" {
glist, err := fingerprint.Globs(dir, generates)
if err != nil {
return nil, nil, err
}
// Make the paths relative to the task dir
for i, v := range glist {
if glist[i], err = filepath.Rel(dir, v); err != nil {
return nil, nil, err
}
}
values = asAnySlice(glist)
}
// Get the list from a variable and split it up
if f.Var != "" {
if vars != nil {
v, ok := vars.Get(f.Var)
// If the variable is dynamic, then it hasn't been resolved yet
// and we can't use it as a list. This happens when fast compiling a task
// for use in --list or --list-all etc.
if ok && v.Value != nil && v.Sh == nil {
switch value := v.Value.(type) {
case string:
if f.Split != "" {
values = asAnySlice(strings.Split(value, f.Split))
} else {
values = asAnySlice(strings.Fields(value))
}
case []string:
values = asAnySlice(value)
case []int:
values = asAnySlice(value)
case []any:
values = value
case map[string]any:
for k, v := range value {
keys = append(keys, k)
values = append(values, v)
}
default:
return nil, nil, errors.TaskfileInvalidError{
URI: location.Taskfile,
Err: errors.New("loop var must be a delimiter-separated string, list or a map"),
}
}
}
}
}
return values, keys, nil
}
func resolveMatrixRefs(matrix *ast.Matrix, cache *templater.Cache) error {
if matrix.Len() == 0 {
return nil
}
for _, row := range matrix.All() {
if row.Ref != "" {
v := templater.ResolveRef(row.Ref, cache)
switch value := v.(type) {
case []any:
row.Value = value
default:
return fmt.Errorf("matrix reference %q must resolve to a list", row.Ref)
}
}
}
return nil
}
// product generates the cartesian product of the input map of slices.
func product(matrix *ast.Matrix) []map[string]any {
if matrix.Len() == 0 {
return nil
}
// Start with an empty product result
result := []map[string]any{{}}
// Iterate over each slice in the slices
for key, row := range matrix.All() {
var newResult []map[string]any
// For each combination in the current result
for _, combination := range result {
// Append each element from the current slice to the combinations
for _, item := range row.Value {
newComb := make(map[string]any, len(combination))
// Copy the existing combination
maps.Copy(newComb, combination)
// Add the current item with the corresponding key
newComb[key] = item
newResult = append(newResult, newComb)
}
}
// Update result with the new combinations
result = newResult
}
return result
}
// compileDeps resolves templates and for-loops in a list of deps.
// Used for both Setup and Deps fields.
func compileDeps(deps []*ast.Dep, t *ast.Task, vars *ast.Vars, location *ast.Location, cache *templater.Cache) ([]*ast.Dep, error) {
if len(deps) == 0 {
return nil, nil
}
result := make([]*ast.Dep, 0, len(deps))
for _, dep := range deps {
if dep == nil {
continue
}
if dep.For != nil {
list, keys, err := itemsFromFor(dep.For, t.ComputeDir(), t.Sources, t.Generates, vars, location, cache)
if err != nil {
return nil, err
}
var as string
if dep.For.As != "" {
as = dep.For.As
} else {
as = "ITEM"
}
for i, loopValue := range list {
extra := map[string]any{
as: loopValue,
}
if len(keys) > 0 {
extra["KEY"] = keys[i]
}
newDep := dep.DeepCopy()
newDep.Task = templater.ReplaceWithExtra(dep.Task, cache, extra)
newDep.Vars = templater.ReplaceVarsWithExtra(dep.Vars, cache, extra)
result = append(result, newDep)
}
continue
}
newDep := dep.DeepCopy()
newDep.Task = templater.Replace(dep.Task, cache)
newDep.Vars = templater.ReplaceVars(dep.Vars, cache)
result = append(result, newDep)
}
return result, nil
}