diff --git a/CHANGELOG.md b/CHANGELOG.md index a70ce149fb..ff5a657655 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 95483e1eba..2570c79105 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -298,6 +298,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/task.go b/internal/taskfile/task.go index 445d608a8d..e52d9a206b 100644 --- a/internal/taskfile/task.go +++ b/internal/taskfile/task.go @@ -49,14 +49,21 @@ func (t *Task) ApplyNamespace(taskName string, ns ...string) []*Task { } for _, cmd := range t.Cmds { if cmd.Task != "" { - cmd.Task = strings.Join(append(ns, cmd.Task), NamespaceSeparator) + cmd.Task = taskWithNamespace(ns, cmd.Task) } } for _, dep := range t.Deps { if dep.Task != "" { - dep.Task = strings.Join(append(ns, dep.Task), NamespaceSeparator) + dep.Task = taskWithNamespace(ns, dep.Task) } } - t.Task = strings.Join(append(ns, taskName), NamespaceSeparator) + t.Task = taskWithNamespace(ns, taskName) return append(tasks, t) } + +func taskWithNamespace(ns []string, taskName string) string { + if strings.HasPrefix(taskName, ":") { + return strings.TrimPrefix(taskName, ":") + } + return strings.Join(append(ns, taskName), NamespaceSeparator) +} diff --git a/task_test.go b/task_test.go index 02f7b0ff86..76dcdb4685 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 0000000000..2211df63dd --- /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 0000000000..41e60ad03e --- /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 0000000000..99d5cb3a07 --- /dev/null +++ b/testdata/includes_call_root_task/Taskfile2.yml @@ -0,0 +1,6 @@ +version: '2' + +tasks: + call-root: + cmds: + - task: :root-task