mirror of
https://github.com/go-task/task.git
synced 2025-01-22 05:10:17 +02:00
feat: support refs in dependencies
This commit is contained in:
parent
25b1966506
commit
dbc120c970
@ -225,7 +225,8 @@ tasks:
|
||||
```
|
||||
|
||||
This means that the type of the variable is maintained when it is passed to
|
||||
another Task. This also works when defining a variable:
|
||||
another Task. This also works the same way when calling `deps` and when defining
|
||||
a variable and can be used in any combination:
|
||||
|
||||
```yaml
|
||||
version: 3
|
||||
@ -236,8 +237,14 @@ tasks:
|
||||
FOO: [A, B, C] # <-- FOO is defined as an array
|
||||
BAR:
|
||||
ref: FOO # <-- BAR is defined as a reference to FOO
|
||||
deps:
|
||||
- task: bar
|
||||
vars:
|
||||
BAR:
|
||||
ref: BAR # <-- BAR gets passed by reference to bar and maintains its type
|
||||
bar:
|
||||
cmds:
|
||||
- 'echo {{index .BAR 0}}' # <-- BAR refers to FOO so the task outputs 'A'
|
||||
- 'echo {{index .BAR 0}}' # <-- BAR still refers to FOO so the task outputs 'A'
|
||||
```
|
||||
|
||||
</TabItem></Tabs>
|
||||
|
17
testdata/vars/any2/Taskfile.yml
vendored
17
testdata/vars/any2/Taskfile.yml
vendored
@ -3,6 +3,9 @@ version: '3'
|
||||
tasks:
|
||||
default:
|
||||
- task: map
|
||||
- task: ref
|
||||
- task: ref-sh
|
||||
- task: ref-dep
|
||||
- task: json
|
||||
- task: yaml
|
||||
|
||||
@ -16,7 +19,7 @@ tasks:
|
||||
VAR:
|
||||
ref: MAP
|
||||
|
||||
map-ref:
|
||||
ref:
|
||||
vars:
|
||||
MAP:
|
||||
map: {"name":"Alice","age":30,"children":[{"name":"Bob","age":5},{"name":"Charlie","age":3},{"name":"Diane","age":1}]}
|
||||
@ -28,7 +31,7 @@ tasks:
|
||||
VAR:
|
||||
ref: MAP_REF
|
||||
|
||||
map-ref-sh:
|
||||
ref-sh:
|
||||
vars:
|
||||
JSON_STRING:
|
||||
sh: echo '{"name":"Alice","age":30,"children":[{"name":"Bob","age":5},{"name":"Charlie","age":3},{"name":"Diane","age":1}]}'
|
||||
@ -42,6 +45,16 @@ tasks:
|
||||
VAR:
|
||||
ref: MAP_REF
|
||||
|
||||
ref-dep:
|
||||
vars:
|
||||
MAP:
|
||||
map: {"name":"Alice","age":30,"children":[{"name":"Bob","age":5},{"name":"Charlie","age":3},{"name":"Diane","age":1}]}
|
||||
deps:
|
||||
- task: print-var
|
||||
vars:
|
||||
VAR:
|
||||
ref: MAP
|
||||
|
||||
json:
|
||||
vars:
|
||||
JSON_STRING:
|
||||
|
11
variables.go
11
variables.go
@ -234,6 +234,17 @@ func (e *Executor) compiledTask(call ast.Call, evaluateShVars bool) (*ast.Task,
|
||||
newDep := dep.DeepCopy()
|
||||
newDep.Task = r.Replace(dep.Task)
|
||||
newDep.Vars = r.ReplaceVars(dep.Vars)
|
||||
// Loop over the dep's variables and resolve any references to other variables
|
||||
err := dep.Vars.Range(func(k string, v ast.Var) error {
|
||||
if v.Ref != "" {
|
||||
refVal := vars.Get(v.Ref)
|
||||
newDep.Vars.Set(k, refVal)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
new.Deps = append(new.Deps, newDep)
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user