From 9f0f18c5c4f59a2b76650c747c8a789b0b1f6a2d Mon Sep 17 00:00:00 2001 From: Andrey Nering Date: Sun, 17 May 2020 16:03:03 -0300 Subject: [PATCH] v3: Allow interpolation on "includes" The idea is to allow manual inclusion of a OS-dependant Taskfile, since it's not automatically included anymore. --- internal/taskfile/read/taskfile.go | 22 ++++++++++++++++++---- task_test.go | 1 + testdata/includes/Taskfile.yml | 2 ++ testdata/includes/Taskfile_darwin.yml | 4 ++++ testdata/includes/Taskfile_linux.yml | 4 ++++ testdata/includes/Taskfile_windows.yml | 4 ++++ 6 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 testdata/includes/Taskfile_darwin.yml create mode 100644 testdata/includes/Taskfile_linux.yml create mode 100644 testdata/includes/Taskfile_windows.yml diff --git a/internal/taskfile/read/taskfile.go b/internal/taskfile/read/taskfile.go index e6fd1bc7..8bbe919f 100644 --- a/internal/taskfile/read/taskfile.go +++ b/internal/taskfile/read/taskfile.go @@ -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 { diff --git a/task_test.go b/task_test.go index ef985361..03185bcb 100644 --- a/task_test.go +++ b/task_test.go @@ -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) diff --git a/testdata/includes/Taskfile.yml b/testdata/includes/Taskfile.yml index e94099e1..8ed9e416 100644 --- a/testdata/includes/Taskfile.yml +++ b/testdata/includes/Taskfile.yml @@ -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: diff --git a/testdata/includes/Taskfile_darwin.yml b/testdata/includes/Taskfile_darwin.yml new file mode 100644 index 00000000..731c7d0c --- /dev/null +++ b/testdata/includes/Taskfile_darwin.yml @@ -0,0 +1,4 @@ +version: '3' + +tasks: + gen: echo 'os' > os_include.txt diff --git a/testdata/includes/Taskfile_linux.yml b/testdata/includes/Taskfile_linux.yml new file mode 100644 index 00000000..731c7d0c --- /dev/null +++ b/testdata/includes/Taskfile_linux.yml @@ -0,0 +1,4 @@ +version: '3' + +tasks: + gen: echo 'os' > os_include.txt diff --git a/testdata/includes/Taskfile_windows.yml b/testdata/includes/Taskfile_windows.yml new file mode 100644 index 00000000..731c7d0c --- /dev/null +++ b/testdata/includes/Taskfile_windows.yml @@ -0,0 +1,4 @@ +version: '3' + +tasks: + gen: echo 'os' > os_include.txt