From da30452c653549a0c22cbea1e2a0c99427e18877 Mon Sep 17 00:00:00 2001 From: Valentin Maerten Date: Sun, 15 Dec 2024 16:12:47 +0100 Subject: [PATCH] feat: add TASK_DIR special variable --- internal/compiler/compiler.go | 6 +++++- task_test.go | 1 + testdata/special_vars/Taskfile.yml | 3 +++ website/docs/reference/templating.mdx | 1 + 4 files changed, 10 insertions(+), 1 deletion(-) diff --git a/internal/compiler/compiler.go b/internal/compiler/compiler.go index 420bed8ead..d85eeff267 100644 --- a/internal/compiler/compiler.go +++ b/internal/compiler/compiler.go @@ -192,7 +192,11 @@ 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)}) + TaskDir := t.Dir + if !filepath.IsAbs(t.Dir) { + TaskDir = filepathext.SmartJoin(c.Dir, t.Dir) + } + maps.Copy(allVars, map[string]string{"TASK": t.Task, "TASKFILE": t.Location.Taskfile, "TASKFILE_DIR": filepath.Dir(t.Location.Taskfile), "TASK_DIR": TaskDir}) } if call != nil { maps.Copy(allVars, map[string]string{"ALIAS": call.Task}) diff --git a/task_test.go b/task_test.go index ef039d3bfc..cce507dbc0 100644 --- a/task_test.go +++ b/task_test.go @@ -222,6 +222,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 d46e934640..3a650f5098 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 468a4333b3..63321756fa 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`. |