Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,9 @@ func TestForCmds(t *testing.T) {
{name: "loop-vars"},
{name: "loop-vars-sh"},
{name: "loop-task"},
{name: "loop-task-ref"},
{name: "loop-task-as"},
{name: "loop-task-ref-as"},
{name: "loop-different-tasks"},
}

Expand Down Expand Up @@ -915,7 +917,9 @@ func TestForDeps(t *testing.T) {
{name: "loop-vars"},
{name: "loop-vars-sh"},
{name: "loop-task"},
{name: "loop-task-ref"},
{name: "loop-task-as"},
{name: "loop-task-ref-as"},
{name: "loop-different-tasks"},
}

Expand Down
16 changes: 13 additions & 3 deletions internal/templater/templater.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ func (r *Cache) Err() error {
}

func ResolveRef(ref string, cache *Cache) any {
return ResolveRefWithExtra(ref, cache, nil)
}

func ResolveRefWithExtra(ref string, cache *Cache, extra map[string]any) any {
// If there is already an error, do nothing
if cache.err != nil {
return nil
Expand All @@ -42,15 +46,21 @@ func ResolveRef(ref string, cache *Cache) any {
cache.cacheMap = cache.Vars.ToCacheMap()
}

data := cache.cacheMap
if extra != nil {
data = maps.Clone(cache.cacheMap)
maps.Copy(data, extra)
}

if ref == "." {
return cache.cacheMap
return data
}
t, err := template.New("resolver").Funcs(templateFuncs).Parse(fmt.Sprintf("{{%s}}", ref))
if err != nil {
cache.err = err
return nil
}
val, err := t.Resolve(cache.cacheMap)
val, err := t.Resolve(data)
if err != nil {
cache.err = err
return nil
Expand Down Expand Up @@ -132,7 +142,7 @@ func ReplaceVar(v ast.Var, cache *Cache) ast.Var {

func ReplaceVarWithExtra(v ast.Var, cache *Cache, extra map[string]any) ast.Var {
if v.Ref != "" {
return ast.Var{Value: ResolveRef(v.Ref, cache)}
return ast.Var{Value: ResolveRefWithExtra(v.Ref, cache, extra)}
}
return ast.Var{
Value: ReplaceWithExtra(v.Value, cache, extra),
Expand Down
25 changes: 25 additions & 0 deletions testdata/for/cmds/Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,18 @@ tasks:
vars:
FILE: "{{.ITEM}}"

# Loop over another task using ref to the default loop variable
loop-task-ref:
vars:
FOO: foo.txt bar.txt
cmds:
- for:
var: FOO
task: looped-task
vars:
FILE:
ref: .ITEM

# Loop over another task with the variable named differently
loop-task-as:
vars:
Expand All @@ -117,6 +129,19 @@ tasks:
vars:
FILE: "{{.FILE}}"

# Loop over another task using ref to a named loop variable
loop-task-ref-as:
vars:
FOO: foo.txt bar.txt
cmds:
- for:
var: FOO
as: FILE
task: looped-task
vars:
FILE:
ref: .FILE

# Loop over different tasks using the variable
loop-different-tasks:
vars:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
foo
bar
2 changes: 2 additions & 0 deletions testdata/for/cmds/testdata/TestForCmds-loop-task-ref.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
foo
bar
25 changes: 25 additions & 0 deletions testdata/for/deps/Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,18 @@ tasks:
vars:
FILE: "{{.ITEM}}"

# Loop over another task using ref to the default loop variable
loop-task-ref:
vars:
FOO: foo.txt bar.txt
deps:
- for:
var: FOO
task: looped-task
vars:
FILE:
ref: .ITEM

# Loop over another task with the variable named differently
loop-task-as:
vars:
Expand All @@ -137,6 +149,19 @@ tasks:
vars:
FILE: "{{.FILE}}"

# Loop over another task using ref to a named loop variable
loop-task-ref-as:
vars:
FOO: foo.txt bar.txt
deps:
- for:
var: FOO
as: FILE
task: looped-task
vars:
FILE:
ref: .FILE

# Loop over different tasks using the variable
loop-different-tasks:
vars:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bar
foo
2 changes: 2 additions & 0 deletions testdata/for/deps/testdata/TestForDeps-loop-task-ref.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bar
foo