From eaba1b9cc8b4c38914330de4e2b5cdcf968a8ddb Mon Sep 17 00:00:00 2001 From: Evgeny Abramovich Date: Wed, 29 Jan 2020 10:02:22 +0300 Subject: [PATCH 1/9] Added structure for storage information about included tasks --- internal/taskfile/included_taskfile.go | 38 ++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 internal/taskfile/included_taskfile.go diff --git a/internal/taskfile/included_taskfile.go b/internal/taskfile/included_taskfile.go new file mode 100644 index 00000000..3b9c7008 --- /dev/null +++ b/internal/taskfile/included_taskfile.go @@ -0,0 +1,38 @@ +package taskfile + +import "errors" + +var ( + // ErrCantUnmarshalIncludedTaskFile is returned for invalid var YAML. + ErrCantUnmarshalIncludedTaskFile = errors.New("task: can't unmarshal included value") +) + +// IncludedTaskFile represents information about included tasksfile +type IncludedTaskFile struct { + Taskfile string + Dir string +} + +// IncludedTaskFiles represents information about included tasksfiles +type IncludedTaskFiles = map[string]IncludedTaskFile + +// UnmarshalYAML implements yaml.Unmarshaler interface +func (it *IncludedTaskFile) UnmarshalYAML(unmarshal func(interface{}) error) error { + var str string + if err := unmarshal(&str); err == nil { + it.Taskfile = str + return nil + } + + var includedTaskfile struct { + Taskfile string + Dir string + } + if err := unmarshal(&includedTaskfile); err == nil { + it.Dir = includedTaskfile.Dir + it.Taskfile = includedTaskfile.Taskfile + return nil + } + + return ErrCantUnmarshalIncludedTaskFile +} From 7a8142ed9275283704998191b4aeec70087ac3f4 Mon Sep 17 00:00:00 2001 From: Evgeny Abramovich Date: Wed, 29 Jan 2020 10:03:06 +0300 Subject: [PATCH 2/9] Added included taskfile directory resolving --- internal/taskfile/merge.go | 2 +- internal/taskfile/read/taskfile.go | 9 +++++++-- internal/taskfile/taskfile.go | 4 ++-- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/internal/taskfile/merge.go b/internal/taskfile/merge.go index b9f415ba..6a545df3 100644 --- a/internal/taskfile/merge.go +++ b/internal/taskfile/merge.go @@ -22,7 +22,7 @@ func Merge(t1, t2 *Taskfile, namespaces ...string) error { } if t1.Includes == nil { - t1.Includes = make(map[string]string) + t1.Includes = make(IncludedTaskFiles) } for k, v := range t2.Includes { t1.Includes[k] = v diff --git a/internal/taskfile/read/taskfile.go b/internal/taskfile/read/taskfile.go index a08360d3..1a310eda 100644 --- a/internal/taskfile/read/taskfile.go +++ b/internal/taskfile/read/taskfile.go @@ -28,8 +28,8 @@ func Taskfile(dir string, entrypoint string) (*taskfile.Taskfile, error) { return nil, err } - for namespace, path := range t.Includes { - path = filepath.Join(dir, path) + for namespace, includedTask := range t.Includes { + path = filepath.Join(dir, includedTask.Taskfile) info, err := os.Stat(path) if err != nil { return nil, err @@ -44,6 +44,11 @@ func Taskfile(dir string, entrypoint string) (*taskfile.Taskfile, error) { if len(includedTaskfile.Includes) > 0 { return nil, ErrIncludedTaskfilesCantHaveIncludes } + + for _, task := range includedTaskfile.Tasks { + task.Dir = filepath.Join(includedTask.Dir, task.Dir) + } + if err = taskfile.Merge(t, includedTaskfile, namespace); err != nil { return nil, err } diff --git a/internal/taskfile/taskfile.go b/internal/taskfile/taskfile.go index 52b707c8..c0fc5a17 100644 --- a/internal/taskfile/taskfile.go +++ b/internal/taskfile/taskfile.go @@ -6,7 +6,7 @@ type Taskfile struct { Expansions int Output string Method string - Includes map[string]string + Includes IncludedTaskFiles Vars Vars Env Vars Tasks Tasks @@ -20,7 +20,7 @@ func (tf *Taskfile) UnmarshalYAML(unmarshal func(interface{}) error) error { Expansions int Output string Method string - Includes map[string]string + Includes IncludedTaskFiles Vars Vars Env Vars Tasks Tasks From 8ff81562d27d69642a829ce2d80af4e4c0c19a86 Mon Sep 17 00:00:00 2001 From: Evgeny Abramovich Date: Wed, 29 Jan 2020 10:39:43 +0300 Subject: [PATCH 3/9] Added os-related files for included taskfiles --- internal/taskfile/read/taskfile.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/internal/taskfile/read/taskfile.go b/internal/taskfile/read/taskfile.go index 1a310eda..d7ed4b9e 100644 --- a/internal/taskfile/read/taskfile.go +++ b/internal/taskfile/read/taskfile.go @@ -6,6 +6,7 @@ import ( "os" "path/filepath" "runtime" + "strings" "github.com/go-task/task/v2/internal/taskfile" @@ -45,6 +46,19 @@ func Taskfile(dir string, entrypoint string) (*taskfile.Taskfile, error) { return nil, ErrIncludedTaskfilesCantHaveIncludes } + includedTaskDirectory := filepath.Dir(path) + includedTaskFileName := strings.TrimSuffix(filepath.Base(path), filepath.Ext(path)) + path = filepath.Join(includedTaskDirectory, fmt.Sprintf("%s_%s.yml", includedTaskFileName, runtime.GOOS)) + if _, err = os.Stat(path); err == nil { + osIncludedTaskfile, err := readTaskfile(path) + if err != nil { + return nil, err + } + if err = taskfile.Merge(includedTaskfile, osIncludedTaskfile); err != nil { + return nil, err + } + } + for _, task := range includedTaskfile.Tasks { task.Dir = filepath.Join(includedTask.Dir, task.Dir) } From d567e23e5046246b9226b06071d76e7b3c65375b Mon Sep 17 00:00:00 2001 From: Evgeny Abramovich Date: Wed, 29 Jan 2020 11:25:11 +0300 Subject: [PATCH 4/9] Added tests for new inport taskfile logic --- task_test.go | 10 +++++++--- testdata/includes/Taskfile.yml | 18 ++++++++++++++++++ testdata/includes/module1/Taskfile.yml | 9 +++++++++ testdata/includes/module2/Taskfile.yml | 9 +++++++++ testdata/includes/module3/CustomTaskfile.yml | 6 ++++++ .../includes/module3/CustomTaskfile_darwin.yml | 6 ++++++ .../includes/module3/CustomTaskfile_linux.yml | 6 ++++++ .../module3/CustomTaskfile_windows.yml | 6 ++++++ 8 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 testdata/includes/module1/Taskfile.yml create mode 100644 testdata/includes/module2/Taskfile.yml create mode 100644 testdata/includes/module3/CustomTaskfile.yml create mode 100644 testdata/includes/module3/CustomTaskfile_darwin.yml create mode 100644 testdata/includes/module3/CustomTaskfile_linux.yml create mode 100644 testdata/includes/module3/CustomTaskfile_windows.yml diff --git a/task_test.go b/task_test.go index 5877088d..ce31ff1b 100644 --- a/task_test.go +++ b/task_test.go @@ -540,9 +540,13 @@ func TestIncludes(t *testing.T) { Target: "default", TrimSpace: true, Files: map[string]string{ - "main.txt": "main", - "included_directory.txt": "included_directory", - "included_taskfile.txt": "included_taskfile", + "main.txt": "main", + "included_directory.txt": "included_directory", + "included_directory_without_dir.txt": "included_directory_without_dir", + "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", + "./module3/os_related.txt": runtime.GOOS, }, } tt.Run(t) diff --git a/testdata/includes/Taskfile.yml b/testdata/includes/Taskfile.yml index 6b7f29ef..144116fb 100644 --- a/testdata/includes/Taskfile.yml +++ b/testdata/includes/Taskfile.yml @@ -3,6 +3,19 @@ version: '2' includes: included: ./included included_taskfile: ./Taskfile2.yml + included_without_dir: + taskfile: ./module1 + included_taskfile_without_dir: + taskfile: ./module1/Taskfile.yml + included_with_dir: + taskfile: ./module2 + dir: ./module2 + included_taskfile_with_dir: + taskfile: ./module2/Taskfile.yml + dir: ./module2 + os_related: + taskfile: ./module3/CustomTaskfile.yml + dir: ./module3 tasks: default: @@ -10,6 +23,11 @@ tasks: - task: gen - task: included:gen - task: included_taskfile:gen + - task: included_without_dir:gen_file + - task: included_taskfile_without_dir:gen_dir + - task: included_with_dir:gen_file + - task: included_taskfile_with_dir:gen_dir + - task: os_related:gen gen: cmds: diff --git a/testdata/includes/module1/Taskfile.yml b/testdata/includes/module1/Taskfile.yml new file mode 100644 index 00000000..e5d5126b --- /dev/null +++ b/testdata/includes/module1/Taskfile.yml @@ -0,0 +1,9 @@ +version: '2' + +tasks: + gen_dir: + cmds: + - echo included_directory_without_dir > included_directory_without_dir.txt + gen_file: + cmds: + - echo included_taskfile_without_dir > included_taskfile_without_dir.txt diff --git a/testdata/includes/module2/Taskfile.yml b/testdata/includes/module2/Taskfile.yml new file mode 100644 index 00000000..e746d559 --- /dev/null +++ b/testdata/includes/module2/Taskfile.yml @@ -0,0 +1,9 @@ +version: '2' + +tasks: + gen_dir: + cmds: + - echo included_directory_with_dir > included_directory_with_dir.txt + gen_file: + cmds: + - echo included_taskfile_with_dir > included_taskfile_with_dir.txt diff --git a/testdata/includes/module3/CustomTaskfile.yml b/testdata/includes/module3/CustomTaskfile.yml new file mode 100644 index 00000000..a9a954a5 --- /dev/null +++ b/testdata/includes/module3/CustomTaskfile.yml @@ -0,0 +1,6 @@ +version: '2' + +tasks: + gen: + cmds: + - echo "INVALID" > os_related.txt diff --git a/testdata/includes/module3/CustomTaskfile_darwin.yml b/testdata/includes/module3/CustomTaskfile_darwin.yml new file mode 100644 index 00000000..5b9ed323 --- /dev/null +++ b/testdata/includes/module3/CustomTaskfile_darwin.yml @@ -0,0 +1,6 @@ +version: '2' + +tasks: + gen: + cmds: + - echo "darwin" > os_related.txt diff --git a/testdata/includes/module3/CustomTaskfile_linux.yml b/testdata/includes/module3/CustomTaskfile_linux.yml new file mode 100644 index 00000000..2fc40546 --- /dev/null +++ b/testdata/includes/module3/CustomTaskfile_linux.yml @@ -0,0 +1,6 @@ +version: '2' + +tasks: + gen: + cmds: + - echo "linux" > os_related.txt diff --git a/testdata/includes/module3/CustomTaskfile_windows.yml b/testdata/includes/module3/CustomTaskfile_windows.yml new file mode 100644 index 00000000..5307e81f --- /dev/null +++ b/testdata/includes/module3/CustomTaskfile_windows.yml @@ -0,0 +1,6 @@ +version: '2' + +tasks: + gen: + cmds: + - echo "windows" > os_related.txt From a3464068bd31aefa96c79602f0e4d6c0e79d62d3 Mon Sep 17 00:00:00 2001 From: Evgeny Abramovich Date: Wed, 12 Feb 2020 10:42:00 +0300 Subject: [PATCH 5/9] Rename TaskFile to Taskfile --- internal/taskfile/included_taskfile.go | 16 ++++++++-------- internal/taskfile/merge.go | 2 +- internal/taskfile/read/taskfile.go | 4 ++-- internal/taskfile/taskfile.go | 4 ++-- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/internal/taskfile/included_taskfile.go b/internal/taskfile/included_taskfile.go index 3b9c7008..21170e4d 100644 --- a/internal/taskfile/included_taskfile.go +++ b/internal/taskfile/included_taskfile.go @@ -3,21 +3,21 @@ package taskfile import "errors" var ( - // ErrCantUnmarshalIncludedTaskFile is returned for invalid var YAML. - ErrCantUnmarshalIncludedTaskFile = errors.New("task: can't unmarshal included value") + // ErrCantUnmarshalIncludedTaskfile is returned for invalid var YAML. + ErrCantUnmarshalIncludedTaskfile = errors.New("task: can't unmarshal included value") ) -// IncludedTaskFile represents information about included tasksfile -type IncludedTaskFile struct { +// IncludedTaskfile represents information about included tasksfile +type IncludedTaskfile struct { Taskfile string Dir string } -// IncludedTaskFiles represents information about included tasksfiles -type IncludedTaskFiles = map[string]IncludedTaskFile +// IncludedTaskfiles represents information about included tasksfiles +type IncludedTaskfiles = map[string]IncludedTaskfile // UnmarshalYAML implements yaml.Unmarshaler interface -func (it *IncludedTaskFile) UnmarshalYAML(unmarshal func(interface{}) error) error { +func (it *IncludedTaskfile) UnmarshalYAML(unmarshal func(interface{}) error) error { var str string if err := unmarshal(&str); err == nil { it.Taskfile = str @@ -34,5 +34,5 @@ func (it *IncludedTaskFile) UnmarshalYAML(unmarshal func(interface{}) error) err return nil } - return ErrCantUnmarshalIncludedTaskFile + return ErrCantUnmarshalIncludedTaskfile } diff --git a/internal/taskfile/merge.go b/internal/taskfile/merge.go index 6a545df3..d350e4b9 100644 --- a/internal/taskfile/merge.go +++ b/internal/taskfile/merge.go @@ -22,7 +22,7 @@ func Merge(t1, t2 *Taskfile, namespaces ...string) error { } if t1.Includes == nil { - t1.Includes = make(IncludedTaskFiles) + t1.Includes = make(IncludedTaskfiles) } for k, v := range t2.Includes { t1.Includes[k] = v diff --git a/internal/taskfile/read/taskfile.go b/internal/taskfile/read/taskfile.go index d7ed4b9e..cd80ca8b 100644 --- a/internal/taskfile/read/taskfile.go +++ b/internal/taskfile/read/taskfile.go @@ -47,8 +47,8 @@ func Taskfile(dir string, entrypoint string) (*taskfile.Taskfile, error) { } includedTaskDirectory := filepath.Dir(path) - includedTaskFileName := strings.TrimSuffix(filepath.Base(path), filepath.Ext(path)) - path = filepath.Join(includedTaskDirectory, fmt.Sprintf("%s_%s.yml", includedTaskFileName, runtime.GOOS)) + includedTaskfileName := strings.TrimSuffix(filepath.Base(path), filepath.Ext(path)) + path = filepath.Join(includedTaskDirectory, fmt.Sprintf("%s_%s.yml", includedTaskfileName, runtime.GOOS)) if _, err = os.Stat(path); err == nil { osIncludedTaskfile, err := readTaskfile(path) if err != nil { diff --git a/internal/taskfile/taskfile.go b/internal/taskfile/taskfile.go index c0fc5a17..bcb7653c 100644 --- a/internal/taskfile/taskfile.go +++ b/internal/taskfile/taskfile.go @@ -6,7 +6,7 @@ type Taskfile struct { Expansions int Output string Method string - Includes IncludedTaskFiles + Includes IncludedTaskfiles Vars Vars Env Vars Tasks Tasks @@ -20,7 +20,7 @@ func (tf *Taskfile) UnmarshalYAML(unmarshal func(interface{}) error) error { Expansions int Output string Method string - Includes IncludedTaskFiles + Includes IncludedTaskfiles Vars Vars Env Vars Tasks Tasks From f38ba7fcd31dd2d1ba0a09154990516bb4948705 Mon Sep 17 00:00:00 2001 From: Evgeny Abramovich Date: Sat, 15 Feb 2020 16:40:42 +0300 Subject: [PATCH 6/9] Removed automatic inclusion of Taskfiles by OS and update tests --- internal/taskfile/read/taskfile.go | 18 +++--------------- task_test.go | 1 - testdata/includes/Taskfile.yml | 6 +----- testdata/includes/Taskfile2.yml | 2 +- testdata/includes/included/Taskfile.yml | 2 +- testdata/includes/module1/Taskfile.yml | 3 ++- testdata/includes/module2/Taskfile.yml | 3 ++- testdata/includes/module3/CustomTaskfile.yml | 6 ------ .../includes/module3/CustomTaskfile_darwin.yml | 6 ------ .../includes/module3/CustomTaskfile_linux.yml | 6 ------ .../module3/CustomTaskfile_windows.yml | 6 ------ 11 files changed, 10 insertions(+), 49 deletions(-) delete mode 100644 testdata/includes/module3/CustomTaskfile.yml delete mode 100644 testdata/includes/module3/CustomTaskfile_darwin.yml delete mode 100644 testdata/includes/module3/CustomTaskfile_linux.yml delete mode 100644 testdata/includes/module3/CustomTaskfile_windows.yml diff --git a/internal/taskfile/read/taskfile.go b/internal/taskfile/read/taskfile.go index cd80ca8b..8cf149d2 100644 --- a/internal/taskfile/read/taskfile.go +++ b/internal/taskfile/read/taskfile.go @@ -6,7 +6,6 @@ import ( "os" "path/filepath" "runtime" - "strings" "github.com/go-task/task/v2/internal/taskfile" @@ -46,21 +45,10 @@ func Taskfile(dir string, entrypoint string) (*taskfile.Taskfile, error) { return nil, ErrIncludedTaskfilesCantHaveIncludes } - includedTaskDirectory := filepath.Dir(path) - includedTaskfileName := strings.TrimSuffix(filepath.Base(path), filepath.Ext(path)) - path = filepath.Join(includedTaskDirectory, fmt.Sprintf("%s_%s.yml", includedTaskfileName, runtime.GOOS)) - if _, err = os.Stat(path); err == nil { - osIncludedTaskfile, err := readTaskfile(path) - if err != nil { - return nil, err - } - if err = taskfile.Merge(includedTaskfile, osIncludedTaskfile); err != nil { - return nil, err - } - } - for _, task := range includedTaskfile.Tasks { - task.Dir = filepath.Join(includedTask.Dir, task.Dir) + if !filepath.IsAbs(task.Dir) { + task.Dir = filepath.Join(includedTask.Dir, task.Dir) + } } if err = taskfile.Merge(t, includedTaskfile, namespace); err != nil { diff --git a/task_test.go b/task_test.go index ce31ff1b..9bda7b93 100644 --- a/task_test.go +++ b/task_test.go @@ -546,7 +546,6 @@ 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", - "./module3/os_related.txt": runtime.GOOS, }, } tt.Run(t) diff --git a/testdata/includes/Taskfile.yml b/testdata/includes/Taskfile.yml index 144116fb..e94099e1 100644 --- a/testdata/includes/Taskfile.yml +++ b/testdata/includes/Taskfile.yml @@ -1,4 +1,4 @@ -version: '2' +version: '3' includes: included: ./included @@ -13,9 +13,6 @@ includes: included_taskfile_with_dir: taskfile: ./module2/Taskfile.yml dir: ./module2 - os_related: - taskfile: ./module3/CustomTaskfile.yml - dir: ./module3 tasks: default: @@ -27,7 +24,6 @@ tasks: - task: included_taskfile_without_dir:gen_dir - task: included_with_dir:gen_file - task: included_taskfile_with_dir:gen_dir - - task: os_related:gen gen: cmds: diff --git a/testdata/includes/Taskfile2.yml b/testdata/includes/Taskfile2.yml index dbb4a34c..858fb38b 100644 --- a/testdata/includes/Taskfile2.yml +++ b/testdata/includes/Taskfile2.yml @@ -1,4 +1,4 @@ -version: '2' +version: '3' tasks: gen: diff --git a/testdata/includes/included/Taskfile.yml b/testdata/includes/included/Taskfile.yml index e8fe2ad2..93b82347 100644 --- a/testdata/includes/included/Taskfile.yml +++ b/testdata/includes/included/Taskfile.yml @@ -1,4 +1,4 @@ -version: '2' +version: '3' tasks: gen: diff --git a/testdata/includes/module1/Taskfile.yml b/testdata/includes/module1/Taskfile.yml index e5d5126b..3659073e 100644 --- a/testdata/includes/module1/Taskfile.yml +++ b/testdata/includes/module1/Taskfile.yml @@ -1,9 +1,10 @@ -version: '2' +version: '3' tasks: gen_dir: cmds: - echo included_directory_without_dir > included_directory_without_dir.txt + gen_file: cmds: - echo included_taskfile_without_dir > included_taskfile_without_dir.txt diff --git a/testdata/includes/module2/Taskfile.yml b/testdata/includes/module2/Taskfile.yml index e746d559..09bbdb60 100644 --- a/testdata/includes/module2/Taskfile.yml +++ b/testdata/includes/module2/Taskfile.yml @@ -1,9 +1,10 @@ -version: '2' +version: '3' tasks: gen_dir: cmds: - echo included_directory_with_dir > included_directory_with_dir.txt + gen_file: cmds: - echo included_taskfile_with_dir > included_taskfile_with_dir.txt diff --git a/testdata/includes/module3/CustomTaskfile.yml b/testdata/includes/module3/CustomTaskfile.yml deleted file mode 100644 index a9a954a5..00000000 --- a/testdata/includes/module3/CustomTaskfile.yml +++ /dev/null @@ -1,6 +0,0 @@ -version: '2' - -tasks: - gen: - cmds: - - echo "INVALID" > os_related.txt diff --git a/testdata/includes/module3/CustomTaskfile_darwin.yml b/testdata/includes/module3/CustomTaskfile_darwin.yml deleted file mode 100644 index 5b9ed323..00000000 --- a/testdata/includes/module3/CustomTaskfile_darwin.yml +++ /dev/null @@ -1,6 +0,0 @@ -version: '2' - -tasks: - gen: - cmds: - - echo "darwin" > os_related.txt diff --git a/testdata/includes/module3/CustomTaskfile_linux.yml b/testdata/includes/module3/CustomTaskfile_linux.yml deleted file mode 100644 index 2fc40546..00000000 --- a/testdata/includes/module3/CustomTaskfile_linux.yml +++ /dev/null @@ -1,6 +0,0 @@ -version: '2' - -tasks: - gen: - cmds: - - echo "linux" > os_related.txt diff --git a/testdata/includes/module3/CustomTaskfile_windows.yml b/testdata/includes/module3/CustomTaskfile_windows.yml deleted file mode 100644 index 5307e81f..00000000 --- a/testdata/includes/module3/CustomTaskfile_windows.yml +++ /dev/null @@ -1,6 +0,0 @@ -version: '2' - -tasks: - gen: - cmds: - - echo "windows" > os_related.txt From 17ad7060b338aa3b0780be93304206d19e96335c Mon Sep 17 00:00:00 2001 From: Evgeny Abramovich Date: Sat, 15 Feb 2020 17:24:06 +0300 Subject: [PATCH 7/9] Added version validation and updated tests --- internal/taskfile/included_taskfile.go | 6 ++++-- internal/taskfile/read/taskfile.go | 8 +++++--- task.go | 8 ++++++++ task_test.go | 15 +++++++++++++++ testdata/incorrect_includes/Taskfile.yml | 10 ++++++++++ testdata/incorrect_includes/included/Taskfile.yml | 6 ++++++ 6 files changed, 48 insertions(+), 5 deletions(-) create mode 100644 testdata/incorrect_includes/Taskfile.yml create mode 100644 testdata/incorrect_includes/included/Taskfile.yml diff --git a/internal/taskfile/included_taskfile.go b/internal/taskfile/included_taskfile.go index 21170e4d..346744f9 100644 --- a/internal/taskfile/included_taskfile.go +++ b/internal/taskfile/included_taskfile.go @@ -9,8 +9,9 @@ var ( // IncludedTaskfile represents information about included tasksfile type IncludedTaskfile struct { - Taskfile string - Dir string + Taskfile string + Dir string + AdvancedImport bool } // IncludedTaskfiles represents information about included tasksfiles @@ -31,6 +32,7 @@ func (it *IncludedTaskfile) UnmarshalYAML(unmarshal func(interface{}) error) err if err := unmarshal(&includedTaskfile); err == nil { it.Dir = includedTaskfile.Dir it.Taskfile = includedTaskfile.Taskfile + it.AdvancedImport = true return nil } diff --git a/internal/taskfile/read/taskfile.go b/internal/taskfile/read/taskfile.go index 8cf149d2..5f12446e 100644 --- a/internal/taskfile/read/taskfile.go +++ b/internal/taskfile/read/taskfile.go @@ -45,9 +45,11 @@ func Taskfile(dir string, entrypoint string) (*taskfile.Taskfile, error) { return nil, ErrIncludedTaskfilesCantHaveIncludes } - for _, task := range includedTaskfile.Tasks { - if !filepath.IsAbs(task.Dir) { - task.Dir = filepath.Join(includedTask.Dir, task.Dir) + if includedTask.AdvancedImport { + for _, task := range includedTaskfile.Tasks { + if !filepath.IsAbs(task.Dir) { + task.Dir = filepath.Join(includedTask.Dir, task.Dir) + } } } diff --git a/task.go b/task.go index 8c1b14b7..5c4f07d6 100644 --- a/task.go +++ b/task.go @@ -207,6 +207,14 @@ func (e *Executor) Setup() error { } } + if v < 3 { + for _, taskfile := range e.Taskfile.Includes { + if taskfile.AdvancedImport { + return errors.New(`task: Import with additional parameters is only available starting on Taskfile version v3`) + } + } + } + e.taskCallCount = make(map[string]*int32, len(e.Taskfile.Tasks)) e.mkdirMutexMap = make(map[string]*sync.Mutex, len(e.Taskfile.Tasks)) for k := range e.Taskfile.Tasks { diff --git a/task_test.go b/task_test.go index 9bda7b93..c752e95b 100644 --- a/task_test.go +++ b/task_test.go @@ -551,6 +551,21 @@ func TestIncludes(t *testing.T) { tt.Run(t) } +func TestIncorrectVersionIncludes(t *testing.T) { + const dir = "testdata/incorrect_includes" + expectedError := "task: Import with additional parameters is only available starting on Taskfile version v3" + + var buff bytes.Buffer + e := task.Executor{ + Dir: dir, + Stdout: &buff, + Stderr: &buff, + Silent: true, + } + + assert.EqualError(t, e.Setup(), expectedError) +} + func TestIncludesEmptyMain(t *testing.T) { tt := fileContentTest{ Dir: "testdata/includes_empty", diff --git a/testdata/incorrect_includes/Taskfile.yml b/testdata/incorrect_includes/Taskfile.yml new file mode 100644 index 00000000..63f4745b --- /dev/null +++ b/testdata/incorrect_includes/Taskfile.yml @@ -0,0 +1,10 @@ +version: '2.6' + +includes: + included: + taskfile: ./included + +tasks: + default: + cmds: + - task: gen diff --git a/testdata/incorrect_includes/included/Taskfile.yml b/testdata/incorrect_includes/included/Taskfile.yml new file mode 100644 index 00000000..a50cc0ba --- /dev/null +++ b/testdata/incorrect_includes/included/Taskfile.yml @@ -0,0 +1,6 @@ +version: '2.6' + +tasks: + gen: + cmds: + - echo incorrect includes test From 0acb911d6a1f2567a0f1e26b22ca474867c440f6 Mon Sep 17 00:00:00 2001 From: Evgeny Abramovich Date: Sat, 15 Feb 2020 18:07:09 +0300 Subject: [PATCH 8/9] Fixed absolute path resolving for included tasksfile --- internal/taskfile/read/taskfile.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/internal/taskfile/read/taskfile.go b/internal/taskfile/read/taskfile.go index 5f12446e..7b1db509 100644 --- a/internal/taskfile/read/taskfile.go +++ b/internal/taskfile/read/taskfile.go @@ -29,7 +29,12 @@ func Taskfile(dir string, entrypoint string) (*taskfile.Taskfile, error) { } for namespace, includedTask := range t.Includes { - path = filepath.Join(dir, includedTask.Taskfile) + if filepath.IsAbs(includedTask.Taskfile) { + path = includedTask.Taskfile + } else { + path = filepath.Join(dir, includedTask.Taskfile) + } + info, err := os.Stat(path) if err != nil { return nil, err From cb2cd3e10f24bc6d102c8367693e898c85ecfffb Mon Sep 17 00:00:00 2001 From: Evgeny Abramovich Date: Sat, 15 Feb 2020 18:07:27 +0300 Subject: [PATCH 9/9] Updated CHANGELOG.md --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d81487a9..6ebab2b7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ ([#252](https://github.com/go-task/task/issues/252)). - Implement short task syntax ([#194](https://github.com/go-task/task/issues/194), [#240](https://github.com/go-task/task/pull/240)). +- Added option to make included Taskfile run commands on its own directory + ([#260](https://github.com/go-task/task/issues/260), [#144](https://github.com/go-task/task/issues/144)) # v3.0.0 - Preview 2