Skip to content

Commit 9f0f18c

Browse files
committed
v3: Allow interpolation on "includes"
The idea is to allow manual inclusion of a OS-dependant Taskfile, since it's not automatically included anymore.
1 parent 191c34c commit 9f0f18c

File tree

6 files changed

+33
-4
lines changed

6 files changed

+33
-4
lines changed

internal/taskfile/read/taskfile.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"runtime"
99

1010
"github.com/go-task/task/v2/internal/taskfile"
11+
"github.com/go-task/task/v2/internal/templater"
1112

1213
"gopkg.in/yaml.v3"
1314
)
@@ -28,7 +29,24 @@ func Taskfile(dir string, entrypoint string) (*taskfile.Taskfile, error) {
2829
return nil, err
2930
}
3031

32+
v, err := t.ParsedVersion()
33+
if err != nil {
34+
return nil, err
35+
}
36+
3137
for namespace, includedTask := range t.Includes {
38+
if v >= 3.0 {
39+
tr := templater.Templater{Vars: &taskfile.Vars{}, RemoveNoValue: true}
40+
includedTask = taskfile.IncludedTaskfile{
41+
Taskfile: tr.Replace(includedTask.Taskfile),
42+
Dir: tr.Replace(includedTask.Dir),
43+
AdvancedImport: includedTask.AdvancedImport,
44+
}
45+
if err := tr.Err(); err != nil {
46+
return nil, err
47+
}
48+
}
49+
3250
if filepath.IsAbs(includedTask.Taskfile) {
3351
path = includedTask.Taskfile
3452
} else {
@@ -63,10 +81,6 @@ func Taskfile(dir string, entrypoint string) (*taskfile.Taskfile, error) {
6381
}
6482
}
6583

66-
v, err := t.ParsedVersion()
67-
if err != nil {
68-
return nil, err
69-
}
7084
if v < 3.0 {
7185
path = filepath.Join(dir, fmt.Sprintf("Taskfile_%s.yml", runtime.GOOS))
7286
if _, err = os.Stat(path); err == nil {

task_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,7 @@ func TestIncludes(t *testing.T) {
560560
"included_taskfile_without_dir.txt": "included_taskfile_without_dir",
561561
"./module2/included_directory_with_dir.txt": "included_directory_with_dir",
562562
"./module2/included_taskfile_with_dir.txt": "included_taskfile_with_dir",
563+
"os_include.txt": "os",
563564
},
564565
}
565566
tt.Run(t)

testdata/includes/Taskfile.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ includes:
1313
included_taskfile_with_dir:
1414
taskfile: ./module2/Taskfile.yml
1515
dir: ./module2
16+
included_os: ./Taskfile_{{OS}}.yml
1617

1718
tasks:
1819
default:
@@ -24,6 +25,7 @@ tasks:
2425
- task: included_taskfile_without_dir:gen_dir
2526
- task: included_with_dir:gen_file
2627
- task: included_taskfile_with_dir:gen_dir
28+
- task: included_os:gen
2729

2830
gen:
2931
cmds:
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
version: '3'
2+
3+
tasks:
4+
gen: echo 'os' > os_include.txt
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
version: '3'
2+
3+
tasks:
4+
gen: echo 'os' > os_include.txt
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
version: '3'
2+
3+
tasks:
4+
gen: echo 'os' > os_include.txt

0 commit comments

Comments
 (0)