Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add TASK_DIR special variable #1961

Merged
merged 3 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions internal/compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"fmt"
"maps"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -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
}
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
Loading