From 1dec956e993b39e5c416520a94944ffff7ecd772 Mon Sep 17 00:00:00 2001 From: Andrey Nering Date: Sat, 2 Feb 2019 21:12:57 -0200 Subject: [PATCH] Allow calling a task of the root Taskfile from within an included Taskfile Fixes #161 --- CHANGELOG.md | 6 ++++++ docs/usage.md | 4 ++++ internal/taskfile/merge.go | 3 +++ task_test.go | 12 ++++++++++++ testdata/includes_call_root_task/.gitignore | 1 + testdata/includes_call_root_task/Taskfile.yml | 9 +++++++++ testdata/includes_call_root_task/Taskfile2.yml | 6 ++++++ 7 files changed, 41 insertions(+) create mode 100644 testdata/includes_call_root_task/.gitignore create mode 100644 testdata/includes_call_root_task/Taskfile.yml create mode 100644 testdata/includes_call_root_task/Taskfile2.yml diff --git a/CHANGELOG.md b/CHANGELOG.md index a70ce149..ff5a6576 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## Unreleased + +- Allow calling a task of the root Taskfile from an included Taskfile + by prefixing it with `:` + ([#161](https://github.com/go-task/task/issues/161), [#172](https://github.com/go-task/task/issues/172)). + ## v2.3.0 - 2019-01-02 - On Windows, Task can now be installed using [Scoop](https://scoop.sh/) diff --git a/docs/usage.md b/docs/usage.md index ab291428..b334595c 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -258,6 +258,10 @@ tasks: The above syntax is also supported in `deps`. +> NOTE: If you want to call a task declared in the root Taskfile from within an +> [included Taskfile](#including-other-taskfiles), add a leading `:` like this: +> `task: :task-name`. + ## Prevent unnecessary work If a task generates something, you can inform Task the source and generated diff --git a/internal/taskfile/merge.go b/internal/taskfile/merge.go index ac4dbd77..b9f415ba 100644 --- a/internal/taskfile/merge.go +++ b/internal/taskfile/merge.go @@ -66,5 +66,8 @@ func Merge(t1, t2 *Taskfile, namespaces ...string) error { } func taskNameWithNamespace(taskName string, namespaces ...string) string { + if strings.HasPrefix(taskName, ":") { + return strings.TrimPrefix(taskName, ":") + } return strings.Join(append(namespaces, taskName), NamespaceSeparator) } diff --git a/task_test.go b/task_test.go index 02f7b0ff..76dcdb46 100644 --- a/task_test.go +++ b/task_test.go @@ -511,3 +511,15 @@ func TestIncludesDependencies(t *testing.T) { } tt.Run(t) } + +func TestIncludesCallingRoot(t *testing.T) { + tt := fileContentTest{ + Dir: "testdata/includes_call_root_task", + Target: "included:call-root", + TrimSpace: true, + Files: map[string]string{ + "root_task.txt": "root task", + }, + } + tt.Run(t) +} diff --git a/testdata/includes_call_root_task/.gitignore b/testdata/includes_call_root_task/.gitignore new file mode 100644 index 00000000..2211df63 --- /dev/null +++ b/testdata/includes_call_root_task/.gitignore @@ -0,0 +1 @@ +*.txt diff --git a/testdata/includes_call_root_task/Taskfile.yml b/testdata/includes_call_root_task/Taskfile.yml new file mode 100644 index 00000000..41e60ad0 --- /dev/null +++ b/testdata/includes_call_root_task/Taskfile.yml @@ -0,0 +1,9 @@ +version: '2' + +includes: + included: Taskfile2.yml + +tasks: + root-task: + cmds: + - echo "root task" > root_task.txt diff --git a/testdata/includes_call_root_task/Taskfile2.yml b/testdata/includes_call_root_task/Taskfile2.yml new file mode 100644 index 00000000..99d5cb3a --- /dev/null +++ b/testdata/includes_call_root_task/Taskfile2.yml @@ -0,0 +1,6 @@ +version: '2' + +tasks: + call-root: + cmds: + - task: :root-task