diff --git a/CHANGELOG.md b/CHANGELOG.md index c81ce277c7..8d226c74ed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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). diff --git a/docs/docs/api_reference.md b/docs/docs/api_reference.md index c0128848c1..4226a94943 100644 --- a/docs/docs/api_reference.md +++ b/docs/docs/api_reference.md @@ -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 diff --git a/internal/compiler/v3/compiler_v3.go b/internal/compiler/v3/compiler_v3.go index a5c6a48a9b..013878fa0b 100644 --- a/internal/compiler/v3/compiler_v3.go +++ b/internal/compiler/v3/compiler_v3.go @@ -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" @@ -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 } diff --git a/internal/fingerprint/glob.go b/internal/fingerprint/glob.go index 0b7fc83715..b305553121 100644 --- a/internal/fingerprint/glob.go +++ b/internal/fingerprint/glob.go @@ -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) +} diff --git a/task_test.go b/task_test.go index 0def5c0ca9..bd2c48a40b 100644 --- a/task_test.go +++ b/task_test.go @@ -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) { diff --git a/testdata/special_vars/Taskfile.yml b/testdata/special_vars/Taskfile.yml index 61623074b7..dcddc17627 100644 --- a/testdata/special_vars/Taskfile.yml +++ b/testdata/special_vars/Taskfile.yml @@ -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'