1
0
mirror of https://github.com/go-task/task.git synced 2025-08-10 22:42:19 +02:00

feat: better versioning (#2131)

This commit is contained in:
Pete Davison
2025-04-05 23:09:27 +01:00
committed by GitHub
parent 88fdbd13cf
commit 55790be6ad
9 changed files with 72 additions and 44 deletions

View File

@@ -67,6 +67,10 @@ func release() error {
return err return err
} }
if err := setVersionFile("internal/version/version.txt", version); err != nil {
return err
}
if err := setJSONVersion("package.json", version); err != nil { if err := setJSONVersion("package.json", version); err != nil {
return err return err
} }
@@ -144,6 +148,10 @@ func changelog(version *semver.Version) error {
return os.WriteFile(changelogTarget, []byte(changelog), 0o644) return os.WriteFile(changelogTarget, []byte(changelog), 0o644)
} }
func setVersionFile(fileName string, version *semver.Version) error {
return os.WriteFile(fileName, []byte(version.String()+"\n"), 0o644)
}
func setJSONVersion(fileName string, version *semver.Version) error { func setJSONVersion(fileName string, version *semver.Version) error {
// Read the JSON file // Read the JSON file
b, err := os.ReadFile(fileName) b, err := os.ReadFile(fileName)

View File

@@ -17,7 +17,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/flags" "github.com/go-task/task/v3/internal/flags"
"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/internal/version"
"github.com/go-task/task/v3/taskfile" "github.com/go-task/task/v3/taskfile"
"github.com/go-task/task/v3/taskfile/ast" "github.com/go-task/task/v3/taskfile/ast"
) )
@@ -57,7 +57,7 @@ func run() error {
} }
if flags.Version { if flags.Version {
fmt.Printf("Task version: %s\n", ver.GetVersionWithSum()) fmt.Println(version.GetVersionWithBuildInfo())
return nil return nil
} }

View File

@@ -10,7 +10,6 @@ import (
"testing" "testing"
"github.com/sebdah/goldie/v2" "github.com/sebdah/goldie/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/go-task/task/v3" "github.com/go-task/task/v3"
@@ -343,41 +342,31 @@ func TestSpecialVars(t *testing.T) {
const dir = "testdata/special_vars" const dir = "testdata/special_vars"
const subdir = "testdata/special_vars/subdir" const subdir = "testdata/special_vars/subdir"
toAbs := func(rel string) string {
abs, err := filepath.Abs(rel)
assert.NoError(t, err)
return abs
}
tests := []struct { tests := []string{
target string
expected string
}{
// Root // Root
{target: "print-task", expected: "print-task"}, "print-task",
{target: "print-root-dir", expected: toAbs(dir)}, "print-root-dir",
{target: "print-taskfile", expected: toAbs(dir) + "/Taskfile.yml"}, "print-taskfile",
{target: "print-taskfile-dir", expected: toAbs(dir)}, "print-taskfile-dir",
{target: "print-task-version", expected: "unknown"}, "print-task-dir",
{target: "print-task-dir", expected: toAbs(dir) + "/foo"},
// Included // Included
{target: "included:print-task", expected: "included:print-task"}, "included:print-task",
{target: "included:print-root-dir", expected: toAbs(dir)}, "included:print-root-dir",
{target: "included:print-taskfile", expected: toAbs(dir) + "/included/Taskfile.yml"}, "included:print-taskfile",
{target: "included:print-taskfile-dir", expected: toAbs(dir) + "/included"}, "included:print-taskfile-dir",
{target: "included:print-task-version", expected: "unknown"},
} }
for _, dir := range []string{dir, subdir} { for _, dir := range []string{dir, subdir} {
for _, test := range tests { for _, test := range tests {
NewExecutorTest(t, NewExecutorTest(t,
WithName(fmt.Sprintf("%s-%s", dir, test.target)), WithName(fmt.Sprintf("%s-%s", dir, test)),
WithExecutorOptions( WithExecutorOptions(
task.WithDir(dir), task.WithDir(dir),
task.WithSilent(true), task.WithSilent(true),
task.WithVersionCheck(true), task.WithVersionCheck(true),
), ),
WithTask(test.target), WithTask(test),
WithPostProcessFn(PPRemoveAbsolutePaths), WithPostProcessFn(PPRemoveAbsolutePaths),
) )
} }

View File

@@ -1,33 +1,67 @@
package version package version
import ( import (
"fmt" _ "embed"
"runtime/debug" "runtime/debug"
"strings"
) )
var ( var (
version = "" //go:embed version.txt
sum = "" version string
commit string
dirty bool
) )
func init() { func init() {
info, ok := debug.ReadBuildInfo() version = strings.TrimSpace(version)
if !ok || info.Main.Version == "(devel)" || info.Main.Version == "" { // Attempt to get build info from the Go runtime. We only use this if not
version = "unknown" // built from a tagged version.
} else { if info, ok := debug.ReadBuildInfo(); ok && info.Main.Version == "(devel)" {
if version == "" { commit = getCommit(info)
version = info.Main.Version dirty = getDirty(info)
}
if sum == "" {
sum = info.Main.Sum
}
} }
} }
func getDirty(info *debug.BuildInfo) bool {
for _, setting := range info.Settings {
if setting.Key == "vcs.modified" {
return setting.Value == "true"
}
}
return false
}
func getCommit(info *debug.BuildInfo) string {
for _, setting := range info.Settings {
if setting.Key == "vcs.revision" {
return setting.Value[:7]
}
}
return ""
}
// GetVersion returns the version of Task. By default, this is retrieved from
// the embedded version.txt file which is kept up-to-date by our release script.
// However, it can also be overridden at build time using:
// -ldflags="-X 'github.com/go-task/task/v3/internal/version.version=vX.X.X'".
func GetVersion() string { func GetVersion() string {
return version return version
} }
func GetVersionWithSum() string { // GetVersionWithBuildInfo is the same as [GetVersion], but it also includes
return fmt.Sprintf("%s (%s)", version, sum) // the commit hash and dirty status if available. This will only work when built
// within inside of a Git checkout.
func GetVersionWithBuildInfo() string {
var buildInfo string
if commit != "" {
buildInfo += commit
}
if dirty {
buildInfo += "-dirty"
}
if buildInfo != "" {
return version + "-" + buildInfo
}
return version
} }

View File

@@ -0,0 +1 @@
3.42.1