1
0
mirror of https://github.com/go-task/task.git synced 2025-01-04 03:48:02 +02:00

v3: Allow interpolation on "includes"

The idea is to allow manual inclusion of a OS-dependant Taskfile, since it's
not automatically included anymore.
This commit is contained in:
Andrey Nering 2020-05-17 16:03:03 -03:00
parent 191c34c9c4
commit 9f0f18c5c4
6 changed files with 33 additions and 4 deletions

View File

@ -8,6 +8,7 @@ import (
"runtime"
"github.com/go-task/task/v2/internal/taskfile"
"github.com/go-task/task/v2/internal/templater"
"gopkg.in/yaml.v3"
)
@ -28,7 +29,24 @@ func Taskfile(dir string, entrypoint string) (*taskfile.Taskfile, error) {
return nil, err
}
v, err := t.ParsedVersion()
if err != nil {
return nil, err
}
for namespace, includedTask := range t.Includes {
if v >= 3.0 {
tr := templater.Templater{Vars: &taskfile.Vars{}, RemoveNoValue: true}
includedTask = taskfile.IncludedTaskfile{
Taskfile: tr.Replace(includedTask.Taskfile),
Dir: tr.Replace(includedTask.Dir),
AdvancedImport: includedTask.AdvancedImport,
}
if err := tr.Err(); err != nil {
return nil, err
}
}
if filepath.IsAbs(includedTask.Taskfile) {
path = includedTask.Taskfile
} else {
@ -63,10 +81,6 @@ func Taskfile(dir string, entrypoint string) (*taskfile.Taskfile, error) {
}
}
v, err := t.ParsedVersion()
if err != nil {
return nil, err
}
if v < 3.0 {
path = filepath.Join(dir, fmt.Sprintf("Taskfile_%s.yml", runtime.GOOS))
if _, err = os.Stat(path); err == nil {

View File

@ -560,6 +560,7 @@ func TestIncludes(t *testing.T) {
"included_taskfile_without_dir.txt": "included_taskfile_without_dir",
"./module2/included_directory_with_dir.txt": "included_directory_with_dir",
"./module2/included_taskfile_with_dir.txt": "included_taskfile_with_dir",
"os_include.txt": "os",
},
}
tt.Run(t)

View File

@ -13,6 +13,7 @@ includes:
included_taskfile_with_dir:
taskfile: ./module2/Taskfile.yml
dir: ./module2
included_os: ./Taskfile_{{OS}}.yml
tasks:
default:
@ -24,6 +25,7 @@ tasks:
- task: included_taskfile_without_dir:gen_dir
- task: included_with_dir:gen_file
- task: included_taskfile_with_dir:gen_dir
- task: included_os:gen
gen:
cmds:

4
testdata/includes/Taskfile_darwin.yml vendored Normal file
View File

@ -0,0 +1,4 @@
version: '3'
tasks:
gen: echo 'os' > os_include.txt

4
testdata/includes/Taskfile_linux.yml vendored Normal file
View File

@ -0,0 +1,4 @@
version: '3'
tasks:
gen: echo 'os' > os_include.txt

View File

@ -0,0 +1,4 @@
version: '3'
tasks:
gen: echo 'os' > os_include.txt