Skip to content
Open
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
14 changes: 14 additions & 0 deletions executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1072,6 +1072,20 @@ func TestIncludeSilent(t *testing.T) {
)
}

func TestGeneratesArray(t *testing.T) {
t.Parallel()

NewExecutorTest(t,
WithName("generates-array"),
WithExecutorOptions(
task.WithDir("testdata/generates-array"),
task.WithSilent(true),
task.WithForce(true),
),
WithTask("default"),
)
}

func TestFailfast(t *testing.T) {
t.Parallel()

Expand Down
22 changes: 17 additions & 5 deletions internal/templater/templater.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,23 @@ func ReplaceGlobs(globs []*ast.Glob, cache *Cache) []*ast.Glob {
return nil
}

new := make([]*ast.Glob, len(globs))
for i, g := range globs {
new[i] = &ast.Glob{
Glob: Replace(g.Glob, cache),
Negate: g.Negate,
new := []*ast.Glob{}
for _, g := range globs {
v := Replace(g.Glob, cache)
if strings.HasPrefix(v, "[") && strings.HasSuffix(v, "]") {
// A templated var of an array will have form: [a b c], extract that.
for _, glob := range strings.Fields(v[1 : len(v)-1]) {
new = append(new, &ast.Glob{
Glob: glob,
Negate: g.Negate,
})
}
} else {
// Otherwise take the glob as provided.
new = append(new, &ast.Glob{
Glob: v,
Negate: g.Negate,
})
}
}
return new
Expand Down
2 changes: 2 additions & 0 deletions testdata/generates-array/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
foo.*
bar.*
83 changes: 83 additions & 0 deletions testdata/generates-array/Taskfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
version: '3'

method: none

vars:
GLOB_SINGLE: 'foo.*'
GLOB_ARRAY: ['*.txt', '*.yml']
GLOB_STRING: '*.txt:*.yml'
GLOB_SPLIT: '{{splitList ":" .GLOB_STRING}}'
GLOB_DYNAMIC:
sh: 'echo "[*.txt]"'
GLOB_DYNAMIC_DIR:
sh: 'echo "./"'
tasks:
default:
cmds:
- cmd: touch foo.txt
- cmd: touch bar.txt
- cmd: touch foo.yml
- cmd: touch bar.yml
- task: direct
- task: single
- task: array
- task: string
- task: split
- task: dynamic
- task: dynamic-dir

direct:
sources:
- '*.txt'
cmds:
- echo "== {{.TASK}} =="
- for: sources
cmd: echo {{.ITEM}}
single:
sources:
- '{{.GLOB_SINGLE}}'
cmds:
- echo "== {{.TASK}} =="
- for: sources
cmd: echo {{.ITEM}}
array:
sources:
- '{{.GLOB_ARRAY}}'
- exclude: Taskfile.yml
cmds:
- echo "== {{.TASK}} =="
- for: sources
cmd: echo {{.ITEM}}
string:
sources:
- '{{splitList ":" .GLOB_STRING}}'
- exclude: Taskfile.yml
cmds:
- echo "== {{.TASK}} =="
- for: sources
cmd: echo {{.ITEM}}
split:
sources:
- '{{.GLOB_SPLIT}}'
- exclude: Taskfile.yml
cmds:
- echo "== {{.TASK}} =="
- for: sources
cmd: echo {{.ITEM}}
dynamic:
sources:
- '{{.GLOB_DYNAMIC}}'
- exclude: Taskfile.yml
cmds:
- echo "== {{.TASK}} =="
- for: sources
cmd: echo {{.ITEM}}
dynamic-dir:
sources:
- "{{.GLOB_DYNAMIC_DIR}}/foo*"
- exclude: Taskfile.yml
cmds:
- echo "== {{.TASK}} =="
- for: sources
cmd: echo {{.ITEM}}

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
== direct ==
bar.txt
foo.txt
== single ==
foo.txt
foo.yml
== array ==
bar.txt
bar.yml
foo.txt
foo.yml
== string ==
bar.txt
bar.yml
foo.txt
foo.yml
== split ==
bar.txt
bar.yml
foo.txt
foo.yml
== dynamic ==
bar.txt
foo.txt
== dynamic-dir ==
foo.txt
foo.yml
12 changes: 10 additions & 2 deletions variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,23 @@ func (e *Executor) compiledTask(call *Call, evaluateShVars bool) (*ast.Task, err
}

cache := &templater.Cache{Vars: vars}
globber := func(globs []*ast.Glob) []*ast.Glob {
// Delays globbing until dynamic variables are available.
if evaluateShVars {
return templater.ReplaceGlobs(globs, cache)
} else {
return origTask.Sources
}
}
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),
Sources: globber(origTask.Sources),
Generates: globber(origTask.Generates),
Dir: templater.Replace(origTask.Dir, cache),
Set: origTask.Set,
Shopt: origTask.Shopt,
Expand Down
Loading