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

feat: for supports variables and lists of any type

This commit is contained in:
Pete Davison
2023-11-30 11:32:53 +00:00
parent 1a12b94bd3
commit 4a0414274f
2 changed files with 25 additions and 8 deletions

View File

@@ -10,7 +10,7 @@ import (
type For struct { type For struct {
From string From string
List []string List []any
Var string Var string
Split string Split string
As string As string
@@ -28,7 +28,7 @@ func (f *For) UnmarshalYAML(node *yaml.Node) error {
return nil return nil
case yaml.SequenceNode: case yaml.SequenceNode:
var list []string var list []any
if err := node.Decode(&list); err != nil { if err := node.Decode(&list); err != nil {
return err return err
} }

View File

@@ -7,6 +7,7 @@ import (
"github.com/joho/godotenv" "github.com/joho/godotenv"
"github.com/go-task/task/v3/errors"
"github.com/go-task/task/v3/internal/execext" "github.com/go-task/task/v3/internal/execext"
"github.com/go-task/task/v3/internal/filepathext" "github.com/go-task/task/v3/internal/filepathext"
"github.com/go-task/task/v3/internal/fingerprint" "github.com/go-task/task/v3/internal/fingerprint"
@@ -132,23 +133,24 @@ func (e *Executor) compiledTask(call taskfile.Call, evaluateShVars bool) (*taskf
continue continue
} }
if cmd.For != nil { if cmd.For != nil {
var list []string var list []any
// Get the list from the explicit for list // Get the list from the explicit for list
if cmd.For.List != nil && len(cmd.For.List) > 0 { if cmd.For.List != nil && len(cmd.For.List) > 0 {
list = cmd.For.List list = cmd.For.List
} }
// Get the list from the task sources // Get the list from the task sources
if cmd.For.From == "sources" { if cmd.For.From == "sources" {
list, err = fingerprint.Globs(new.Dir, new.Sources) glist, err := fingerprint.Globs(new.Dir, new.Sources)
if err != nil { if err != nil {
return nil, err return nil, err
} }
// Make the paths relative to the task dir // Make the paths relative to the task dir
for i, v := range list { for i, v := range glist {
if list[i], err = filepath.Rel(new.Dir, v); err != nil { if glist[i], err = filepath.Rel(new.Dir, v); err != nil {
return nil, err return nil, err
} }
} }
list = asAnySlice(glist)
} }
// Get the list from a variable and split it up // Get the list from a variable and split it up
if cmd.For.Var != "" { if cmd.For.Var != "" {
@@ -157,9 +159,16 @@ func (e *Executor) compiledTask(call taskfile.Call, evaluateShVars bool) (*taskf
switch value := v.Value.(type) { switch value := v.Value.(type) {
case string: case string:
if cmd.For.Split != "" { if cmd.For.Split != "" {
list = strings.Split(value, cmd.For.Split) list = asAnySlice(strings.Split(value, cmd.For.Split))
} else { } else {
list = strings.Fields(value) list = asAnySlice(strings.Fields(value))
}
case []any:
list = value
default:
return nil, errors.TaskfileInvalidError{
URI: origTask.Location.Taskfile,
Err: errors.New("var must be a delimiter-separated string or a list"),
} }
} }
} }
@@ -251,3 +260,11 @@ func (e *Executor) compiledTask(call taskfile.Call, evaluateShVars bool) (*taskf
return &new, r.Err() return &new, r.Err()
} }
func asAnySlice[T any](slice []T) []any {
ret := make([]any, len(slice))
for i, v := range slice {
ret[i] = v
}
return ret
}