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 #1014
Closes #990
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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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) {

View File

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

View File

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