Skip to content

Add sources special var #1146

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

- Added new 'SOURCES' special variable [#1146](https://github.com/go-task/task/pull/1146) by @hernandanielg).
- Starting on this release, official binaries for FreeBSD will be available to
download (#1068).
- Fix some errors being unintendedly supressed (#1134 by @clintmod).
Expand Down
1 change: 1 addition & 0 deletions docs/docs/api_reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ There are some special variables that is available on the templating system:
| `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 listes in `sources`. Only available within the `status` prop and if method is set to `timestamp`. |
| `TASK_VERSION` | The current version of task. |
| `SOURCES` | Comma-separated string that contains the list of sources files. |

## ENV

Expand Down
7 changes: 7 additions & 0 deletions internal/compiler/v3/compiler_v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/go-task/task/v3/internal/compiler"
"github.com/go-task/task/v3/internal/execext"
"github.com/go-task/task/v3/internal/filepathext"
glob "github.com/go-task/task/v3/internal/fingerprint"
"github.com/go-task/task/v3/internal/logger"
"github.com/go-task/task/v3/internal/templater"
"github.com/go-task/task/v3/internal/version"
Expand Down Expand Up @@ -180,12 +181,18 @@ func (c *CompilerV3) getSpecialVars(t *taskfile.Task) (map[string]string, error)
return nil, err
}

sources, err := glob.Globs(t.Dir, t.Sources)
if err != nil {
return nil, err
}

return map[string]string{
"TASK": t.Task,
"ROOT_DIR": c.Dir,
"TASKFILE_DIR": taskfileDir,
"USER_WORKING_DIR": c.UserWorkingDir,
"TASK_VERSION": version.GetVersion(),
"SOURCES": strings.Join(sources, ", "),
}, nil
}

Expand Down
4 changes: 4 additions & 0 deletions internal/fingerprint/glob.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,7 @@ func Glob(dir string, g string) ([]string, error) {
}
return files, nil
}

func Globs(dir string, g []string) ([]string, error) {
return globs(dir, g)
}
2 changes: 2 additions & 0 deletions task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,14 @@ func TestSpecialVars(t *testing.T) {
assert.Contains(t, output, "root/ROOT_DIR="+toAbs("testdata/special_vars"))
assert.Contains(t, output, "root/TASKFILE_DIR="+toAbs("testdata/special_vars"))
assert.Contains(t, output, "root/TASK_VERSION=unknown")
assert.Contains(t, output, "root/SOURCES=install-task.sh")

// Included Taskfile
assert.Contains(t, output, "included/TASK=included:print")
assert.Contains(t, output, "included/ROOT_DIR="+toAbs("testdata/special_vars"))
assert.Contains(t, output, "included/TASKFILE_DIR="+toAbs("testdata/special_vars/included"))
assert.Contains(t, output, "included/TASK_VERSION=unknown")
assert.Contains(t, output, "root/SOURCES=install-task.sh")
}

func TestVarsInvalidTmpl(t *testing.T) {
Expand Down
5 changes: 4 additions & 1 deletion testdata/special_vars/Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ tasks:
- echo root/TASK={{.TASK}}
- echo root/ROOT_DIR={{.ROOT_DIR}}
- echo root/TASKFILE_DIR={{.TASKFILE_DIR}}
- echo root/TASK_VERSION={{.TASK_VERSION}}
- echo root/TASK_VERSION="{{.TASK_VERSION}}"
- echo root/SOURCES="{{.SOURCES}}"
sources:
- '*.sh'