From 9d8c4ba7e6fc6093fd214cf1d6a3445b56c3658d Mon Sep 17 00:00:00 2001 From: Valentin Maerten Date: Mon, 30 Dec 2024 11:45:25 +0100 Subject: [PATCH] feat: add TASK_DIR special variable (#1961) Co-authored-by: Pete Davison --- internal/compiler/compiler.go | 9 ++++++--- task_test.go | 1 + testdata/special_vars/Taskfile.yml | 3 +++ website/docs/reference/templating.mdx | 1 + 4 files changed, 11 insertions(+), 3 deletions(-) diff --git a/internal/compiler/compiler.go b/internal/compiler/compiler.go index 420bed8e..fe3bb4b4 100644 --- a/internal/compiler/compiler.go +++ b/internal/compiler/compiler.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "fmt" - "maps" "os" "path/filepath" "strings" @@ -192,10 +191,14 @@ func (c *Compiler) getSpecialVars(t *ast.Task, call *ast.Call) (map[string]strin "TASK_VERSION": version.GetVersion(), } if t != nil { - maps.Copy(allVars, map[string]string{"TASK": t.Task, "TASKFILE": t.Location.Taskfile, "TASKFILE_DIR": filepath.Dir(t.Location.Taskfile)}) + allVars["TASK"] = t.Task + allVars["TASK_DIR"] = filepathext.SmartJoin(c.Dir, t.Dir) + allVars["TASKFILE"] = t.Location.Taskfile + allVars["TASKFILE_DIR"] = filepath.Dir(t.Location.Taskfile) } if call != nil { - maps.Copy(allVars, map[string]string{"ALIAS": call.Task}) + allVars["ALIAS"] = call.Task } + return allVars, nil } diff --git a/task_test.go b/task_test.go index f8af6399..c54424e7 100644 --- a/task_test.go +++ b/task_test.go @@ -226,6 +226,7 @@ func TestSpecialVars(t *testing.T) { {target: "print-taskfile", expected: toAbs(dir) + "/Taskfile.yml"}, {target: "print-taskfile-dir", expected: toAbs(dir)}, {target: "print-task-version", expected: "unknown"}, + {target: "print-task-dir", expected: toAbs(dir) + "/foo"}, // Included {target: "included:print-task", expected: "included:print-task"}, {target: "included:print-root-dir", expected: toAbs(dir)}, diff --git a/testdata/special_vars/Taskfile.yml b/testdata/special_vars/Taskfile.yml index d46e9346..3a650f50 100644 --- a/testdata/special_vars/Taskfile.yml +++ b/testdata/special_vars/Taskfile.yml @@ -19,3 +19,6 @@ tasks: cmds: - echo "{{.ALIAS}}" print-task-alias-default: echo "{{.ALIAS}}" + print-task-dir: + dir: 'foo' + cmd: echo {{.TASK_DIR}} diff --git a/website/docs/reference/templating.mdx b/website/docs/reference/templating.mdx index 468a4333..63321756 100644 --- a/website/docs/reference/templating.mdx +++ b/website/docs/reference/templating.mdx @@ -114,6 +114,7 @@ special variable will be overridden. | `ROOT_DIR` | The absolute path of the root Taskfile directory. | | `TASKFILE` | The absolute path of the included Taskfile. | | `TASKFILE_DIR` | The absolute path of the included Taskfile directory. | +| `TASK_DIR` | The absolute path of the directory where the task is executed. | | `USER_WORKING_DIR` | The absolute path of the directory `task` was called from. | | `CHECKSUM` | The checksum of the files listed in `sources`. Only available within the `status` prop and if method is set to `checksum`. | | `TIMESTAMP` | The date object of the greatest timestamp of the files listed in `sources`. Only available within the `status` prop and if method is set to `timestamp`. |