1
0
mirror of https://github.com/go-task/task.git synced 2025-03-17 21:08:01 +02:00

Add new TASK_VERSION special variable

Closes 
Closes 
This commit is contained in:
João Pedro 2023-02-19 22:28:17 -03:00 committed by Andrey Nering
parent 12a1cd6f62
commit ec2110e58f
10 changed files with 45 additions and 28 deletions

@ -6,15 +6,15 @@ build:
- darwin - darwin
- linux - linux
goarch: goarch:
- 386 - '386'
- amd64 - amd64
- arm - arm
- arm64 - arm64
goarm: goarm:
- 6 - '6'
ignore: ignore:
- goos: darwin - goos: darwin
goarch: 386 goarch: '386'
env: env:
- CGO_ENABLED=0 - CGO_ENABLED=0
mod_timestamp: '{{ .CommitTimestamp }}' mod_timestamp: '{{ .CommitTimestamp }}'

@ -2,6 +2,8 @@
## Unreleased ## 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). - 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) - 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) - 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)

@ -21,8 +21,9 @@ tasks:
sources: sources:
- './**/*.go' - './**/*.go'
cmds: 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: vars:
VERSION_VAR: github.com/go-task/task/v3/internal/version.version
GIT_COMMIT: GIT_COMMIT:
sh: git log -n 1 --format=%h sh: git log -n 1 --format=%h
@ -112,3 +113,7 @@ tasks:
GO_PACKAGES: GO_PACKAGES:
sh: go list ./... sh: go list ./...
silent: true silent: true
foo:
cmds:
- echo "{{.TASK_VERSION}}"

@ -6,7 +6,6 @@ import (
"log" "log"
"os" "os"
"path/filepath" "path/filepath"
"runtime/debug"
"strings" "strings"
"time" "time"
@ -16,13 +15,10 @@ import (
"github.com/go-task/task/v3" "github.com/go-task/task/v3"
"github.com/go-task/task/v3/args" "github.com/go-task/task/v3/args"
"github.com/go-task/task/v3/internal/logger" "github.com/go-task/task/v3/internal/logger"
ver "github.com/go-task/task/v3/internal/version"
"github.com/go-task/task/v3/taskfile" "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...] 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 Runs the specified task(s). Falls back to the "default" task if no task name
@ -104,7 +100,7 @@ func main() {
pflag.Parse() pflag.Parse()
if versionFlag { if versionFlag {
fmt.Printf("Task version: %s\n", getVersion()) fmt.Printf("Task version: %s\n", ver.GetVersion())
return return
} }
@ -255,21 +251,3 @@ func getArgs() ([]string, string, error) {
} }
return args[:doubleDashPos], strings.Join(quotedCliArgs, " "), nil 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
}

@ -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. | | `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`. | | `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`. | | `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 ## ENV

@ -12,6 +12,7 @@ import (
"github.com/go-task/task/v3/internal/filepathext" "github.com/go-task/task/v3/internal/filepathext"
"github.com/go-task/task/v3/internal/logger" "github.com/go-task/task/v3/internal/logger"
"github.com/go-task/task/v3/internal/templater" "github.com/go-task/task/v3/internal/templater"
"github.com/go-task/task/v3/internal/version"
"github.com/go-task/task/v3/taskfile" "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, "ROOT_DIR": c.Dir,
"TASKFILE_DIR": taskfileDir, "TASKFILE_DIR": taskfileDir,
"USER_WORKING_DIR": c.UserWorkingDir, "USER_WORKING_DIR": c.UserWorkingDir,
"TASK_VERSION": version.GetVersion(),
}, nil }, nil
} }

@ -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
}

@ -190,11 +190,13 @@ func TestSpecialVars(t *testing.T) {
assert.Contains(t, output, "root/TASK=print") assert.Contains(t, output, "root/TASK=print")
assert.Contains(t, output, "root/ROOT_DIR="+toAbs("testdata/special_vars")) 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/TASKFILE_DIR="+toAbs("testdata/special_vars"))
assert.Contains(t, output, "root/TASK_VERSION=unknown")
// Included Taskfile // Included Taskfile
assert.Contains(t, output, "included/TASK=included:print") assert.Contains(t, output, "included/TASK=included:print")
assert.Contains(t, output, "included/ROOT_DIR="+toAbs("testdata/special_vars")) 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/TASKFILE_DIR="+toAbs("testdata/special_vars/included"))
assert.Contains(t, output, "included/TASK_VERSION=unknown")
} }
func TestVarsInvalidTmpl(t *testing.T) { func TestVarsInvalidTmpl(t *testing.T) {

@ -16,3 +16,4 @@ tasks:
- echo root/TASK={{.TASK}} - echo root/TASK={{.TASK}}
- echo root/ROOT_DIR={{.ROOT_DIR}} - echo root/ROOT_DIR={{.ROOT_DIR}}
- echo root/TASKFILE_DIR={{.TASKFILE_DIR}} - echo root/TASKFILE_DIR={{.TASKFILE_DIR}}
- echo root/TASK_VERSION={{.TASK_VERSION}}

@ -6,3 +6,4 @@ tasks:
- echo included/TASK={{.TASK}} - echo included/TASK={{.TASK}}
- echo included/ROOT_DIR={{.ROOT_DIR}} - echo included/ROOT_DIR={{.ROOT_DIR}}
- echo included/TASKFILE_DIR={{.TASKFILE_DIR}} - echo included/TASKFILE_DIR={{.TASKFILE_DIR}}
- echo included/TASK_VERSION={{.TASK_VERSION}}