diff --git a/CHANGELOG.md b/CHANGELOG.md index f6ee94b5..8619777a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +# Unreleased + +- Expose `.TASK` variable in templates with the task name + ([#252](https://github.com/go-task/task/issues/252)). + # v3.0.0 - Preview 2 - Taskfiles in version 1 are not supported anymore diff --git a/docs/usage.md b/docs/usage.md index 9e79d060..42012840 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -439,6 +439,8 @@ Example of sending parameters with environment variables: $ TASK_VARIABLE=a-value task do-something ``` +> TIP: A special variable `.TASK` is always available containg the task name. + Since some shells don't support above syntax to set environment variables (Windows) tasks also accepts a similar style when not in the beginning of the command. Variables given in this form are only visible to the task called diff --git a/internal/compiler/v2/compiler_v2.go b/internal/compiler/v2/compiler_v2.go index e2399f33..f42b5fbe 100644 --- a/internal/compiler/v2/compiler_v2.go +++ b/internal/compiler/v2/compiler_v2.go @@ -38,6 +38,7 @@ type CompilerV2 struct { // 5. Environment variables func (c *CompilerV2) GetVariables(t *taskfile.Task, call taskfile.Call) (taskfile.Vars, error) { vr := varResolver{c: c, vars: compiler.GetEnviron()} + vr.vars["TASK"] = taskfile.Var{Static: t.Task} for _, vars := range []taskfile.Vars{c.Taskvars, c.TaskfileVars, call.Vars, t.Vars} { for i := 0; i < c.Expansions; i++ { vr.merge(vars) diff --git a/task_test.go b/task_test.go index fe78744a..27128f33 100644 --- a/task_test.go +++ b/task_test.go @@ -98,6 +98,7 @@ func TestVarsV2(t *testing.T) { "nestedtmpl2_foo2.txt": "", "override.txt": "bar", "nested.txt": "Taskvars-TaskfileVars-TaskVars", + "task_name.txt": "hello", }, } tt.Run(t) diff --git a/testdata/vars/v2/Taskfile.yml b/testdata/vars/v2/Taskfile.yml index 657a464c..90f36993 100644 --- a/testdata/vars/v2/Taskfile.yml +++ b/testdata/vars/v2/Taskfile.yml @@ -32,6 +32,7 @@ tasks: - echo '{{.NESTEDTMPL2_FOO2}}' > nestedtmpl2_foo2.txt - echo {{.OVERRIDE}} > override.txt - echo '{{.NESTED3}}' > nested.txt + - echo '{{.TASK}}' > task_name.txt vars: FOO: foo BAR: $echo bar