1
0
mirror of https://github.com/go-task/task.git synced 2025-02-09 13:47:06 +02:00

Use semver for Taskfile versions

This commit is contained in:
Andrey Nering 2018-03-03 18:54:42 -03:00
parent afe6744e97
commit 2d4ca37226
7 changed files with 70 additions and 15 deletions

View File

@ -38,3 +38,7 @@
[[constraint]]
branch = "master"
name = "github.com/mitchellh/go-homedir"
[[constraint]]
branch = "master"
name = "github.com/Masterminds/semver"

View File

@ -2,8 +2,7 @@ package taskfile
// Taskfile represents a Taskfile.yml
type Taskfile struct {
// TODO: version is still not used
Version int
Version string
Tasks Tasks
}
@ -14,7 +13,7 @@ func (tf *Taskfile) UnmarshalYAML(unmarshal func(interface{}) error) error {
}
var taskfile struct {
Version int
Version string
Tasks Tasks
}
if err := unmarshal(&taskfile); err != nil {

View 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
View File

@ -13,7 +13,9 @@ import (
"github.com/go-task/task/internal/execext"
"github.com/go-task/task/internal/logger"
"github.com/go-task/task/internal/taskfile"
"github.com/go-task/task/internal/taskfile/version"
"github.com/Masterminds/semver"
"golang.org/x/sync/errgroup"
)
@ -76,9 +78,14 @@ func (e *Executor) Run(calls ...taskfile.Call) error {
}
func (e *Executor) setup() error {
if e.Taskfile.Version == 0 {
e.Taskfile.Version = 1
if e.Taskfile.Version == "" {
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 {
e.Context = context.Background()
}
@ -96,14 +103,14 @@ func (e *Executor) setup() error {
Stderr: e.Stderr,
Verbose: e.Verbose,
}
switch e.Taskfile.Version {
case 1:
switch {
case version.IsV1(v):
e.Compiler = &compilerv1.CompilerV1{
Dir: e.Dir,
Vars: e.taskvars,
Logger: e.Logger,
}
case 2:
case version.IsV2(v):
e.Compiler = &compilerv2.CompilerV2{
Dir: e.Dir,
Vars: e.taskvars,
@ -113,8 +120,8 @@ func (e *Executor) setup() error {
if !e.Silent {
e.Logger.Errf(`task: warning: Taskfile "version: 2" is experimental and implementation can change before v2.0.0 release`)
}
default:
return fmt.Errorf(`task: Unrecognized Taskfile version "%d"`, e.Taskfile.Version)
case version.IsV21(v):
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))

View File

@ -392,10 +392,10 @@ func TestCyclicDep(t *testing.T) {
func TestTaskVersion(t *testing.T) {
tests := []struct {
Dir string
Version int
Version string
}{
{"testdata/version/v1", 1},
{"testdata/version/v2", 2},
{"testdata/version/v1", "1"},
{"testdata/version/v2", "2"},
}
for _, test := range tests {

View File

@ -1,4 +1,4 @@
version: 2
version: '2'
tasks:
default:
deps: [hello]

View File

@ -1,4 +1,4 @@
version: 2
version: '2'
tasks:
default:
vars: