mirror of
https://github.com/go-task/task.git
synced 2025-06-17 00:17:51 +02:00
Use semver for Taskfile versions
This commit is contained in:
@ -38,3 +38,7 @@
|
|||||||
[[constraint]]
|
[[constraint]]
|
||||||
branch = "master"
|
branch = "master"
|
||||||
name = "github.com/mitchellh/go-homedir"
|
name = "github.com/mitchellh/go-homedir"
|
||||||
|
|
||||||
|
[[constraint]]
|
||||||
|
branch = "master"
|
||||||
|
name = "github.com/Masterminds/semver"
|
||||||
|
@ -2,8 +2,7 @@ package taskfile
|
|||||||
|
|
||||||
// Taskfile represents a Taskfile.yml
|
// Taskfile represents a Taskfile.yml
|
||||||
type Taskfile struct {
|
type Taskfile struct {
|
||||||
// TODO: version is still not used
|
Version string
|
||||||
Version int
|
|
||||||
Tasks Tasks
|
Tasks Tasks
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -14,7 +13,7 @@ func (tf *Taskfile) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var taskfile struct {
|
var taskfile struct {
|
||||||
Version int
|
Version string
|
||||||
Tasks Tasks
|
Tasks Tasks
|
||||||
}
|
}
|
||||||
if err := unmarshal(&taskfile); err != nil {
|
if err := unmarshal(&taskfile); err != nil {
|
||||||
|
45
internal/taskfile/version/version.go
Normal file
45
internal/taskfile/version/version.go
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
package version
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/Masterminds/semver"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
v1 = mustVersion("1")
|
||||||
|
v2 = mustVersion("2")
|
||||||
|
|
||||||
|
isV1 = mustConstraint("= 1")
|
||||||
|
isV2 = mustConstraint(">= 2")
|
||||||
|
isV21 = mustConstraint(">= 2.1")
|
||||||
|
)
|
||||||
|
|
||||||
|
// IsV1 returns if is a given Taskfile version is version 1
|
||||||
|
func IsV1(v *semver.Version) bool {
|
||||||
|
return isV1.Check(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsV2 returns if is a given Taskfile version is at least version 2
|
||||||
|
func IsV2(v *semver.Version) bool {
|
||||||
|
return isV2.Check(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsV21 returns if is a given Taskfile version is at least version 2
|
||||||
|
func IsV21(v *semver.Version) bool {
|
||||||
|
return isV21.Check(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
func mustVersion(s string) *semver.Version {
|
||||||
|
v, err := semver.NewVersion(s)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
|
func mustConstraint(s string) *semver.Constraints {
|
||||||
|
c, err := semver.NewConstraint(s)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return c
|
||||||
|
}
|
21
task.go
21
task.go
@ -13,7 +13,9 @@ import (
|
|||||||
"github.com/go-task/task/internal/execext"
|
"github.com/go-task/task/internal/execext"
|
||||||
"github.com/go-task/task/internal/logger"
|
"github.com/go-task/task/internal/logger"
|
||||||
"github.com/go-task/task/internal/taskfile"
|
"github.com/go-task/task/internal/taskfile"
|
||||||
|
"github.com/go-task/task/internal/taskfile/version"
|
||||||
|
|
||||||
|
"github.com/Masterminds/semver"
|
||||||
"golang.org/x/sync/errgroup"
|
"golang.org/x/sync/errgroup"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -76,9 +78,14 @@ func (e *Executor) Run(calls ...taskfile.Call) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (e *Executor) setup() error {
|
func (e *Executor) setup() error {
|
||||||
if e.Taskfile.Version == 0 {
|
if e.Taskfile.Version == "" {
|
||||||
e.Taskfile.Version = 1
|
e.Taskfile.Version = "1"
|
||||||
}
|
}
|
||||||
|
v, err := semver.NewVersion(e.Taskfile.Version)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf(`task: could not parse taskfile version "%s": %v`, e.Taskfile.Version, err)
|
||||||
|
}
|
||||||
|
|
||||||
if e.Context == nil {
|
if e.Context == nil {
|
||||||
e.Context = context.Background()
|
e.Context = context.Background()
|
||||||
}
|
}
|
||||||
@ -96,14 +103,14 @@ func (e *Executor) setup() error {
|
|||||||
Stderr: e.Stderr,
|
Stderr: e.Stderr,
|
||||||
Verbose: e.Verbose,
|
Verbose: e.Verbose,
|
||||||
}
|
}
|
||||||
switch e.Taskfile.Version {
|
switch {
|
||||||
case 1:
|
case version.IsV1(v):
|
||||||
e.Compiler = &compilerv1.CompilerV1{
|
e.Compiler = &compilerv1.CompilerV1{
|
||||||
Dir: e.Dir,
|
Dir: e.Dir,
|
||||||
Vars: e.taskvars,
|
Vars: e.taskvars,
|
||||||
Logger: e.Logger,
|
Logger: e.Logger,
|
||||||
}
|
}
|
||||||
case 2:
|
case version.IsV2(v):
|
||||||
e.Compiler = &compilerv2.CompilerV2{
|
e.Compiler = &compilerv2.CompilerV2{
|
||||||
Dir: e.Dir,
|
Dir: e.Dir,
|
||||||
Vars: e.taskvars,
|
Vars: e.taskvars,
|
||||||
@ -113,8 +120,8 @@ func (e *Executor) setup() error {
|
|||||||
if !e.Silent {
|
if !e.Silent {
|
||||||
e.Logger.Errf(`task: warning: Taskfile "version: 2" is experimental and implementation can change before v2.0.0 release`)
|
e.Logger.Errf(`task: warning: Taskfile "version: 2" is experimental and implementation can change before v2.0.0 release`)
|
||||||
}
|
}
|
||||||
default:
|
case version.IsV21(v):
|
||||||
return fmt.Errorf(`task: Unrecognized Taskfile version "%d"`, e.Taskfile.Version)
|
return fmt.Errorf(`task: Taskfile versions greater than v2 not implemented in the version of Task`)
|
||||||
}
|
}
|
||||||
|
|
||||||
e.taskCallCount = make(map[string]*int32, len(e.Taskfile.Tasks))
|
e.taskCallCount = make(map[string]*int32, len(e.Taskfile.Tasks))
|
||||||
|
@ -392,10 +392,10 @@ func TestCyclicDep(t *testing.T) {
|
|||||||
func TestTaskVersion(t *testing.T) {
|
func TestTaskVersion(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
Dir string
|
Dir string
|
||||||
Version int
|
Version string
|
||||||
}{
|
}{
|
||||||
{"testdata/version/v1", 1},
|
{"testdata/version/v1", "1"},
|
||||||
{"testdata/version/v2", 2},
|
{"testdata/version/v2", "2"},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
|
2
testdata/vars/v2/Taskfile.yml
vendored
2
testdata/vars/v2/Taskfile.yml
vendored
@ -1,4 +1,4 @@
|
|||||||
version: 2
|
version: '2'
|
||||||
tasks:
|
tasks:
|
||||||
default:
|
default:
|
||||||
deps: [hello]
|
deps: [hello]
|
||||||
|
2
testdata/vars/v2/multiline/Taskfile.yml
vendored
2
testdata/vars/v2/multiline/Taskfile.yml
vendored
@ -1,4 +1,4 @@
|
|||||||
version: 2
|
version: '2'
|
||||||
tasks:
|
tasks:
|
||||||
default:
|
default:
|
||||||
vars:
|
vars:
|
||||||
|
Reference in New Issue
Block a user