diff --git a/.goreleaser.yml b/.goreleaser.yml index 60a03fc0..41aebbd9 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -6,15 +6,15 @@ build: - darwin - linux goarch: - - 386 + - '386' - amd64 - arm - arm64 goarm: - - 6 + - '6' ignore: - goos: darwin - goarch: 386 + goarch: '386' env: - CGO_ENABLED=0 mod_timestamp: '{{ .CommitTimestamp }}' diff --git a/CHANGELOG.md b/CHANGELOG.md index 636eb378..3d7146fd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Unreleased +- Added new `TASK_VERSION` special variable + ([#990](https://github.com/go-task/task/issues/990), [#1014](https://github.com/go-task/task/pull/1014) by @ja1code). - Fixed a bug where tasks were sometimes incorrectly marked as internal ([#1007](https://github.com/go-task/task/pull/1007) by @pd93). - Update to Go 1.20 (bump minimum version to 1.19) ([#1010](https://github.com/go-task/task/pull/1010) by @pd93) - Added environment variable `FORCE_COLOR` support to force color output. Usefull for environments without TTY ([#1003](https://github.com/go-task/task/pull/1003) by @automation-stack) diff --git a/Taskfile.yml b/Taskfile.yml index 1a78f0b2..86834c31 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -21,8 +21,9 @@ tasks: sources: - './**/*.go' cmds: - - go install -v -ldflags="-w -s -X main.version={{.GIT_COMMIT}}" ./cmd/task + - go install -v -ldflags="-w -s -X '{{.VERSION_VAR}}={{.GIT_COMMIT}}'" ./cmd/task vars: + VERSION_VAR: github.com/go-task/task/v3/internal/version.version GIT_COMMIT: sh: git log -n 1 --format=%h @@ -112,3 +113,7 @@ tasks: GO_PACKAGES: sh: go list ./... silent: true + + foo: + cmds: + - echo "{{.TASK_VERSION}}" diff --git a/cmd/task/task.go b/cmd/task/task.go index 31cef824..2b472f55 100644 --- a/cmd/task/task.go +++ b/cmd/task/task.go @@ -6,7 +6,6 @@ import ( "log" "os" "path/filepath" - "runtime/debug" "strings" "time" @@ -16,13 +15,10 @@ import ( "github.com/go-task/task/v3" "github.com/go-task/task/v3/args" "github.com/go-task/task/v3/internal/logger" + ver "github.com/go-task/task/v3/internal/version" "github.com/go-task/task/v3/taskfile" ) -var ( - version = "" -) - const usage = `Usage: task [-ilfwvsd] [--init] [--list] [--force] [--watch] [--verbose] [--silent] [--dir] [--taskfile] [--dry] [--summary] [task...] Runs the specified task(s). Falls back to the "default" task if no task name @@ -104,7 +100,7 @@ func main() { pflag.Parse() if versionFlag { - fmt.Printf("Task version: %s\n", getVersion()) + fmt.Printf("Task version: %s\n", ver.GetVersion()) return } @@ -255,21 +251,3 @@ func getArgs() ([]string, string, error) { } return args[:doubleDashPos], strings.Join(quotedCliArgs, " "), nil } - -func getVersion() string { - if version != "" { - return version - } - - info, ok := debug.ReadBuildInfo() - if !ok || info.Main.Version == "" { - return "unknown" - } - - version = info.Main.Version - if info.Main.Sum != "" { - version += fmt.Sprintf(" (%s)", info.Main.Sum) - } - - return version -} diff --git a/docs/docs/api_reference.md b/docs/docs/api_reference.md index 6347db0a..43b580df 100644 --- a/docs/docs/api_reference.md +++ b/docs/docs/api_reference.md @@ -58,6 +58,7 @@ There are some special variables that is available on the templating system: | `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 listes in `sources`. Only available within the `status` prop and if method is set to `timestamp`. | +| `TASK_VERSION` | The current version of task. | ## ENV diff --git a/internal/compiler/v3/compiler_v3.go b/internal/compiler/v3/compiler_v3.go index 71e6c977..ae01636b 100644 --- a/internal/compiler/v3/compiler_v3.go +++ b/internal/compiler/v3/compiler_v3.go @@ -12,6 +12,7 @@ import ( "github.com/go-task/task/v3/internal/filepathext" "github.com/go-task/task/v3/internal/logger" "github.com/go-task/task/v3/internal/templater" + "github.com/go-task/task/v3/internal/version" "github.com/go-task/task/v3/taskfile" ) @@ -184,6 +185,7 @@ func (c *CompilerV3) getSpecialVars(t *taskfile.Task) (map[string]string, error) "ROOT_DIR": c.Dir, "TASKFILE_DIR": taskfileDir, "USER_WORKING_DIR": c.UserWorkingDir, + "TASK_VERSION": version.GetVersion(), }, nil } diff --git a/internal/version/version.go b/internal/version/version.go new file mode 100644 index 00000000..0fbbcab6 --- /dev/null +++ b/internal/version/version.go @@ -0,0 +1,25 @@ +package version + +import ( + "fmt" + "runtime/debug" +) + +var version = "" + +func GetVersion() string { + if version != "" { + return version + } + + info, ok := debug.ReadBuildInfo() + if !ok || info.Main.Version == "" { + return "unknown" + } + + ver := info.Main.Version + if info.Main.Sum != "" { + ver += fmt.Sprintf(" (%s)", info.Main.Sum) + } + return ver +} diff --git a/task_test.go b/task_test.go index 9ba87e0a..46ac069b 100644 --- a/task_test.go +++ b/task_test.go @@ -190,11 +190,13 @@ func TestSpecialVars(t *testing.T) { assert.Contains(t, output, "root/TASK=print") 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") // 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") } func TestVarsInvalidTmpl(t *testing.T) { diff --git a/testdata/special_vars/Taskfile.yml b/testdata/special_vars/Taskfile.yml index e72d593e..61623074 100644 --- a/testdata/special_vars/Taskfile.yml +++ b/testdata/special_vars/Taskfile.yml @@ -16,3 +16,4 @@ tasks: - echo root/TASK={{.TASK}} - echo root/ROOT_DIR={{.ROOT_DIR}} - echo root/TASKFILE_DIR={{.TASKFILE_DIR}} + - echo root/TASK_VERSION={{.TASK_VERSION}} diff --git a/testdata/special_vars/included/Taskfile.yml b/testdata/special_vars/included/Taskfile.yml index 6000abbc..881a9c3e 100644 --- a/testdata/special_vars/included/Taskfile.yml +++ b/testdata/special_vars/included/Taskfile.yml @@ -6,3 +6,4 @@ tasks: - echo included/TASK={{.TASK}} - echo included/ROOT_DIR={{.ROOT_DIR}} - echo included/TASKFILE_DIR={{.TASKFILE_DIR}} + - echo included/TASK_VERSION={{.TASK_VERSION}}