Skip to content

Commit

Permalink
feat: add TASK_DIR special variable
Browse files Browse the repository at this point in the history
  • Loading branch information
vmaerten committed Dec 15, 2024
1 parent 36ff00e commit da30452
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 1 deletion.
6 changes: 5 additions & 1 deletion internal/compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
1 change: 1 addition & 0 deletions task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)},
Expand Down
3 changes: 3 additions & 0 deletions testdata/special_vars/Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ tasks:
cmds:
- echo "{{.ALIAS}}"
print-task-alias-default: echo "{{.ALIAS}}"
print-task-dir:
dir: 'foo'
cmd: echo {{.TASK_DIR}}
1 change: 1 addition & 0 deletions website/docs/reference/templating.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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`. |
Expand Down

0 comments on commit da30452

Please sign in to comment.