Skip to content

Commit ef9ffda

Browse files
committed
feat: add TASK_SOURCES special variable
This commit adds the `TASK_SOURCES` variable to tasks. If a task defines the `sources` key, `TASK_SOURCES` contains the list of paths referenced by `sources`. Globs are expanded. This was initially implemented by @hernandanielg in #1146 to fix #948.
1 parent b5b1524 commit ef9ffda

File tree

7 files changed

+35
-3
lines changed

7 files changed

+35
-3
lines changed

internal/compiler/compiler.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/go-task/task/v3/internal/env"
1313
"github.com/go-task/task/v3/internal/execext"
1414
"github.com/go-task/task/v3/internal/filepathext"
15+
"github.com/go-task/task/v3/internal/fingerprint"
1516
"github.com/go-task/task/v3/internal/logger"
1617
"github.com/go-task/task/v3/internal/templater"
1718
"github.com/go-task/task/v3/internal/version"
@@ -184,19 +185,27 @@ func (c *Compiler) ResetCache() {
184185
c.dynamicCache = nil
185186
}
186187

187-
func (c *Compiler) getSpecialVars(t *ast.Task, call *ast.Call) (map[string]string, error) {
188-
allVars := map[string]string{
188+
func (c *Compiler) getSpecialVars(t *ast.Task, call *ast.Call) (map[string]any, error) {
189+
allVars := map[string]any{
189190
"TASK_EXE": filepath.ToSlash(os.Args[0]),
190191
"ROOT_TASKFILE": filepathext.SmartJoin(c.Dir, c.Entrypoint),
191192
"ROOT_DIR": c.Dir,
192193
"USER_WORKING_DIR": c.UserWorkingDir,
193194
"TASK_VERSION": version.GetVersion(),
194195
}
195196
if t != nil {
197+
dir := filepathext.SmartJoin(c.Dir, t.Dir)
198+
199+
taskSources, err := fingerprint.Globs(dir, t.Sources)
200+
if err != nil {
201+
return allVars, fmt.Errorf("expand globs: %v", err)
202+
}
203+
196204
allVars["TASK"] = t.Task
197-
allVars["TASK_DIR"] = filepathext.SmartJoin(c.Dir, t.Dir)
205+
allVars["TASK_DIR"] = dir
198206
allVars["TASKFILE"] = t.Location.Taskfile
199207
allVars["TASKFILE_DIR"] = filepath.Dir(t.Location.Taskfile)
208+
allVars["TASK_SOURCES"] = taskSources
200209
}
201210
if call != nil {
202211
allVars["ALIAS"] = call.Task

task_test.go

+11
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,15 @@ func TestSpecialVars(t *testing.T) {
231231
{target: "print-taskfile-dir", expected: toAbs(dir)},
232232
{target: "print-task-version", expected: "unknown"},
233233
{target: "print-task-dir", expected: toAbs(dir) + "/foo"},
234+
{
235+
target: "print-task-sources",
236+
expected: "[" +
237+
toAbs(dir) + "/sources-as-var/file.txt, " +
238+
toAbs(dir) + "/sources-as-var/subdir1/file.txt, " +
239+
toAbs(dir) + "/sources-as-var/subdir2/file.txt" +
240+
"]",
241+
},
242+
{target: "print-task-sources-empty", expected: "[]"},
234243
// Included
235244
{target: "included:print-task", expected: "included:print-task"},
236245
{target: "included:print-root-dir", expected: toAbs(dir)},
@@ -251,6 +260,8 @@ func TestSpecialVars(t *testing.T) {
251260
Stderr: &buff,
252261
Silent: true,
253262
EnableVersionCheck: true,
263+
// NOTE needed to enable Force to ignore fingerprints generated
264+
Force: true,
254265
}
255266
require.NoError(t, e.Setup())
256267
require.NoError(t, e.Run(context.Background(), &ast.Call{Task: test.target}))

testdata/special_vars/Taskfile.yml

+11
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,14 @@ tasks:
2222
print-task-dir:
2323
dir: 'foo'
2424
cmd: echo {{.TASK_DIR}}
25+
26+
print-task-sources:
27+
sources:
28+
- sources-as-var/**/*.txt
29+
cmds:
30+
- echo "[{{.TASK_SOURCES | sortAlpha | join ", "}}]"
31+
32+
print-task-sources-empty:
33+
# No sources defined, yet we try to use TASK_SOURCES.
34+
cmds:
35+
- echo "{{.TASK_SOURCES}}"

testdata/special_vars/sources-as-var/file.txt

Whitespace-only changes.

testdata/special_vars/sources-as-var/subdir1/file.txt

Whitespace-only changes.

testdata/special_vars/sources-as-var/subdir2/file.txt

Whitespace-only changes.

website/docs/reference/templating.mdx

+1
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ special variable will be overridden.
115115
| `TASKFILE` | The absolute path of the included Taskfile. |
116116
| `TASKFILE_DIR` | The absolute path of the included Taskfile directory. |
117117
| `TASK_DIR` | The absolute path of the directory where the task is executed. |
118+
| `TASK_SOURCES` | List of files referenced by task `sources` key. Empty if task does not define `sources`. |
118119
| `USER_WORKING_DIR` | The absolute path of the directory `task` was called from. |
119120
| `CHECKSUM` | The checksum of the files listed in `sources`. Only available within the `status` prop and if method is set to `checksum`. |
120121
| `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`. |

0 commit comments

Comments
 (0)