1
0
mirror of https://github.com/go-task/task.git synced 2025-03-19 21:17:46 +02:00

Expose .TASK variable with the task name

Closes #252
This commit is contained in:
Andrey Nering 2019-12-07 19:43:10 -03:00
parent d718527a1f
commit 7373639f57
5 changed files with 10 additions and 0 deletions

View File

@ -1,5 +1,10 @@
# Changelog # 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 # v3.0.0 - Preview 2
- Taskfiles in version 1 are not supported anymore - Taskfiles in version 1 are not supported anymore

View File

@ -439,6 +439,8 @@ Example of sending parameters with environment variables:
$ TASK_VARIABLE=a-value task do-something $ 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 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 (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 the command. Variables given in this form are only visible to the task called

View File

@ -38,6 +38,7 @@ type CompilerV2 struct {
// 5. Environment variables // 5. Environment variables
func (c *CompilerV2) GetVariables(t *taskfile.Task, call taskfile.Call) (taskfile.Vars, error) { func (c *CompilerV2) GetVariables(t *taskfile.Task, call taskfile.Call) (taskfile.Vars, error) {
vr := varResolver{c: c, vars: compiler.GetEnviron()} 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 _, vars := range []taskfile.Vars{c.Taskvars, c.TaskfileVars, call.Vars, t.Vars} {
for i := 0; i < c.Expansions; i++ { for i := 0; i < c.Expansions; i++ {
vr.merge(vars) vr.merge(vars)

View File

@ -98,6 +98,7 @@ func TestVarsV2(t *testing.T) {
"nestedtmpl2_foo2.txt": "<no value>", "nestedtmpl2_foo2.txt": "<no value>",
"override.txt": "bar", "override.txt": "bar",
"nested.txt": "Taskvars-TaskfileVars-TaskVars", "nested.txt": "Taskvars-TaskfileVars-TaskVars",
"task_name.txt": "hello",
}, },
} }
tt.Run(t) tt.Run(t)

View File

@ -32,6 +32,7 @@ tasks:
- echo '{{.NESTEDTMPL2_FOO2}}' > nestedtmpl2_foo2.txt - echo '{{.NESTEDTMPL2_FOO2}}' > nestedtmpl2_foo2.txt
- echo {{.OVERRIDE}} > override.txt - echo {{.OVERRIDE}} > override.txt
- echo '{{.NESTED3}}' > nested.txt - echo '{{.NESTED3}}' > nested.txt
- echo '{{.TASK}}' > task_name.txt
vars: vars:
FOO: foo FOO: foo
BAR: $echo bar BAR: $echo bar