Skip to content

Commit b7aab34

Browse files
fhofherrCuckooEXE
authored andcommitted
Generates TASK_SOURCES and TASK_GENERATES variables, as well as allows you to loop over tasks and see their definitions.
1 parent 8dfafe5 commit b7aab34

File tree

21 files changed

+260
-6
lines changed

21 files changed

+260
-6
lines changed

compiler.go

Lines changed: 84 additions & 3 deletions
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"
@@ -197,19 +198,99 @@ func (c *Compiler) ResetCache() {
197198
c.dynamicCache = nil
198199
}
199200

200-
func (c *Compiler) getSpecialVars(t *ast.Task, call *Call) (map[string]string, error) {
201-
allVars := map[string]string{
201+
func (c *Compiler) getSpecialVars(t *ast.Task, call *Call) (map[string]any, error) {
202+
allVars := map[string]any{
202203
"TASK_EXE": filepath.ToSlash(os.Args[0]),
203204
"ROOT_TASKFILE": filepathext.SmartJoin(c.Dir, c.Entrypoint),
204205
"ROOT_DIR": c.Dir,
205206
"USER_WORKING_DIR": c.UserWorkingDir,
206207
"TASK_VERSION": version.GetVersion(),
207208
}
208209
if t != nil {
210+
taskDir := filepathext.SmartJoin(c.Dir, t.Dir)
211+
209212
allVars["TASK"] = t.Task
210-
allVars["TASK_DIR"] = filepathext.SmartJoin(c.Dir, t.Dir)
213+
allVars["TASK_DIR"] = taskDir
211214
allVars["TASKFILE"] = t.Location.Taskfile
212215
allVars["TASKFILE_DIR"] = filepath.Dir(t.Location.Taskfile)
216+
217+
// Template and expand shell variables for both Sources and Generates
218+
envVars := env.GetEnviron()
219+
// Add the special vars we've built so far to the cache
220+
for k, v := range allVars {
221+
envVars.Set(k, ast.Var{Value: v})
222+
}
223+
// Add taskfile variables to the cache so they're available for templating
224+
// Note: Variables that reference TASK_SOURCES or TASK_GENERATES won't work here,
225+
// but simple string variables like C_EXT: c will work fine
226+
for k, v := range c.TaskfileEnv.All() {
227+
envVars.Set(k, v)
228+
}
229+
for k, v := range c.TaskfileVars.All() {
230+
envVars.Set(k, v)
231+
}
232+
for k, v := range t.IncludeVars.All() {
233+
envVars.Set(k, v)
234+
}
235+
for k, v := range t.IncludedTaskfileVars.All() {
236+
envVars.Set(k, v)
237+
}
238+
for k, v := range t.Vars.All() {
239+
envVars.Set(k, v)
240+
}
241+
if call != nil {
242+
for k, v := range call.Vars.All() {
243+
envVars.Set(k, v)
244+
}
245+
}
246+
cache := &templater.Cache{Vars: envVars}
247+
248+
// Template Sources
249+
templatedSources := templater.ReplaceGlobs(t.Sources, cache)
250+
if err := cache.Err(); err != nil {
251+
return allVars, fmt.Errorf("template sources: %v", err)
252+
}
253+
// Expand shell variables in Sources and then expand globs to actual file paths
254+
expandedSources := make([]*ast.Glob, len(templatedSources))
255+
for i, g := range templatedSources {
256+
expanded, err := execext.ExpandLiteral(g.Glob)
257+
if err != nil {
258+
return allVars, fmt.Errorf("expand shell variables in source glob %q: %v", g.Glob, err)
259+
}
260+
expandedSources[i] = &ast.Glob{Glob: expanded, Negate: g.Negate}
261+
}
262+
taskSources, err := fingerprint.Globs(taskDir, expandedSources)
263+
if err != nil {
264+
return allVars, fmt.Errorf("expand globs: %v", err)
265+
}
266+
// Convert absolute paths to relative paths (relative to root directory)
267+
relativeSources := make([]string, len(taskSources))
268+
for i, source := range taskSources {
269+
rel, err := filepath.Rel(c.Dir, source)
270+
if err != nil {
271+
// If relative path calculation fails, use the original path
272+
relativeSources[i] = source
273+
} else {
274+
relativeSources[i] = filepath.ToSlash(rel)
275+
}
276+
}
277+
allVars["TASK_SOURCES"] = relativeSources
278+
279+
// Template Generates
280+
templatedGenerates := templater.ReplaceGlobs(t.Generates, cache)
281+
if err := cache.Err(); err != nil {
282+
return allVars, fmt.Errorf("template generates: %v", err)
283+
}
284+
// Expand shell variables in Generates, but keep glob patterns intact
285+
taskGenerates := make([]string, len(templatedGenerates))
286+
for i, g := range templatedGenerates {
287+
expanded, err := execext.ExpandLiteral(g.Glob)
288+
if err != nil {
289+
return allVars, fmt.Errorf("expand shell variables in generate glob %q: %v", g.Glob, err)
290+
}
291+
taskGenerates[i] = expanded
292+
}
293+
allVars["TASK_GENERATES"] = taskGenerates
213294
} else {
214295
allVars["TASK"] = ""
215296
allVars["TASK_DIR"] = ""

executor_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,8 @@ func TestSpecialVars(t *testing.T) {
367367
"print-taskfile",
368368
"print-taskfile-dir",
369369
"print-task-dir",
370+
"print-task-sources",
371+
"print-task-sources-empty",
370372
// Included
371373
"included:print-task",
372374
"included:print-root-dir",
@@ -382,6 +384,7 @@ func TestSpecialVars(t *testing.T) {
382384
task.WithDir(dir),
383385
task.WithSilent(true),
384386
task.WithVersionCheck(true),
387+
task.WithForce(true),
385388
),
386389
WithTask(test),
387390
WithFixtureTemplating(),

testdata/special_vars/Taskfile.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,25 @@ 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+
- printf "{{.TASK_SOURCES | sortAlpha | join "\n"}}\n"
31+
32+
print-task-sources-empty:
33+
# No sources defined, yet we try to use TASK_SOURCES.
34+
cmds:
35+
- echo "{{.TASK_SOURCES}}"
36+
37+
print-task-generates:
38+
generates:
39+
- generates-as-var/**/*.txt
40+
cmds:
41+
- printf "{{.TASK_GENERATES | sortAlpha | join "\n"}}\n"
42+
43+
print-task-generates-empty:
44+
# No generates defined, yet we try to use TASK_GENERATES.
45+
cmds:
46+
- echo "{{.TASK_GENERATES}}"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
test file
2+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
test file
2+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
test file
2+

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.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]

0 commit comments

Comments
 (0)