diff --git a/help.go b/help.go index 6ee1f7e371..e7a024fb89 100644 --- a/help.go +++ b/help.go @@ -132,16 +132,10 @@ func (e *Executor) ListTaskNames(allTasks bool) error { if e.TaskSorter == nil { e.TaskSorter = sort.AlphaNumericWithRootTasksFirst } - keys := e.Taskfile.Tasks.Keys() - e.TaskSorter(keys, nil) // Create a list of task names taskNames := make([]string, 0, e.Taskfile.Tasks.Len()) - for _, key := range keys { - task, ok := e.Taskfile.Tasks.Get(key) - if !ok { - continue - } + for task := range e.Taskfile.Tasks.Values(e.TaskSorter) { if (allTasks || task.Desc != "") && !task.Internal { taskNames = append(taskNames, strings.TrimRight(task.Task, ":")) for _, alias := range task.Aliases { diff --git a/internal/compiler/compiler.go b/internal/compiler/compiler.go index 527163acb9..3afe9b6e8a 100644 --- a/internal/compiler/compiler.go +++ b/internal/compiler/compiler.go @@ -103,18 +103,26 @@ func (c *Compiler) getVariables(t *ast.Task, call *ast.Call, evaluateShVars bool taskRangeFunc = getRangeFunc(dir) } - if err := c.TaskfileEnv.Range(rangeFunc); err != nil { - return nil, err + for k, v := range c.TaskfileEnv.All() { + if err := rangeFunc(k, v); err != nil { + return nil, err + } } - if err := c.TaskfileVars.Range(rangeFunc); err != nil { - return nil, err + for k, v := range c.TaskfileVars.All() { + if err := rangeFunc(k, v); err != nil { + return nil, err + } } if t != nil { - if err := t.IncludeVars.Range(rangeFunc); err != nil { - return nil, err + for k, v := range t.IncludeVars.All() { + if err := rangeFunc(k, v); err != nil { + return nil, err + } } - if err := t.IncludedTaskfileVars.Range(taskRangeFunc); err != nil { - return nil, err + for k, v := range t.IncludedTaskfileVars.All() { + if err := taskRangeFunc(k, v); err != nil { + return nil, err + } } } @@ -122,11 +130,15 @@ func (c *Compiler) getVariables(t *ast.Task, call *ast.Call, evaluateShVars bool return result, nil } - if err := call.Vars.Range(rangeFunc); err != nil { - return nil, err + for k, v := range call.Vars.All() { + if err := rangeFunc(k, v); err != nil { + return nil, err + } } - if err := t.Vars.Range(taskRangeFunc); err != nil { - return nil, err + for k, v := range t.Vars.All() { + if err := taskRangeFunc(k, v); err != nil { + return nil, err + } } return result, nil diff --git a/internal/templater/templater.go b/internal/templater/templater.go index 3f3ab20e18..e5265b8117 100644 --- a/internal/templater/templater.go +++ b/internal/templater/templater.go @@ -141,10 +141,9 @@ func ReplaceVarsWithExtra(vars *ast.Vars, cache *Cache, extra map[string]any) *a } newVars := ast.NewVars() - _ = vars.Range(func(k string, v ast.Var) error { + for k, v := range vars.All() { newVars.Set(k, ReplaceVarWithExtra(v, cache, extra)) - return nil - }) + } return newVars } diff --git a/setup.go b/setup.go index d8d1ffb718..70cfa5e480 100644 --- a/setup.go +++ b/setup.go @@ -92,12 +92,9 @@ func (e *Executor) setupFuzzyModel() { model.SetThreshold(1) // because we want to build grammar based on every task name var words []string - for _, taskName := range e.Taskfile.Tasks.Keys() { - words = append(words, taskName) - - for _, task := range e.Taskfile.Tasks.Values() { - words = slices.Concat(words, task.Aliases) - } + for name, task := range e.Taskfile.Tasks.All(nil) { + words = append(words, name) + words = slices.Concat(words, task.Aliases) } model.Train(words) @@ -212,12 +209,11 @@ func (e *Executor) readDotEnvFiles() error { return err } - err = env.Range(func(key string, value ast.Var) error { - if _, ok := e.Taskfile.Env.Get(key); !ok { - e.Taskfile.Env.Set(key, value) + for k, v := range env.All() { + if _, ok := e.Taskfile.Env.Get(k); !ok { + e.Taskfile.Env.Set(k, v) } - return nil - }) + } return err } @@ -235,7 +231,7 @@ func (e *Executor) setupConcurrencyState() { e.taskCallCount = make(map[string]*int32, e.Taskfile.Tasks.Len()) e.mkdirMutexMap = make(map[string]*sync.Mutex, e.Taskfile.Tasks.Len()) - for _, k := range e.Taskfile.Tasks.Keys() { + for k := range e.Taskfile.Tasks.Keys(nil) { e.taskCallCount[k] = new(int32) e.mkdirMutexMap[k] = &sync.Mutex{} } diff --git a/task.go b/task.go index f572738e55..8cba61bb49 100644 --- a/task.go +++ b/task.go @@ -470,7 +470,7 @@ func (e *Executor) GetTask(call *ast.Call) (*ast.Task, error) { // If didn't find one, search for a task with a matching alias var matchingTask *ast.Task var aliasedTasks []string - for _, task := range e.Taskfile.Tasks.Values() { + for task := range e.Taskfile.Tasks.Values(nil) { if slices.Contains(task.Aliases, call.Task) { aliasedTasks = append(aliasedTasks, task.Task) matchingTask = task @@ -510,15 +510,9 @@ func (e *Executor) GetTaskList(filters ...FilterFunc) ([]*ast.Task, error) { if e.TaskSorter == nil { e.TaskSorter = sort.AlphaNumericWithRootTasksFirst } - keys := e.Taskfile.Tasks.Keys() - e.TaskSorter(keys, nil) // Filter tasks based on the given filter functions - for _, key := range keys { - task, ok := e.Taskfile.Tasks.Get(key) - if !ok { - continue - } + for task := range e.Taskfile.Tasks.Values(e.TaskSorter) { var shouldFilter bool for _, filter := range filters { if filter(task) { diff --git a/taskfile/ast/graph.go b/taskfile/ast/graph.go index 31d189e0aa..2f52b19b3a 100644 --- a/taskfile/ast/graph.go +++ b/taskfile/ast/graph.go @@ -116,14 +116,14 @@ func (tfg *TaskfileGraph) Merge() (*Taskfile, error) { return nil, err } - _ = rootVertex.Taskfile.Tasks.Range(func(name string, task *Task) error { + // TODO: I don't think this is necessary anymore + for name, task := range rootVertex.Taskfile.Tasks.All(nil) { if task == nil { task = &Task{} rootVertex.Taskfile.Tasks.Set(name, task) } task.Task = name - return nil - }) + } return rootVertex.Taskfile, nil } diff --git a/taskfile/ast/include.go b/taskfile/ast/include.go index cba35cb741..894fd23d1d 100644 --- a/taskfile/ast/include.go +++ b/taskfile/ast/include.go @@ -1,6 +1,7 @@ package ast import ( + "iter" "sync" "github.com/elliotchance/orderedmap/v3" @@ -84,19 +85,31 @@ func (includes *Includes) Set(key string, value *Include) bool { return includes.om.Set(key, value) } +// All returns an iterator that loops over all task key-value pairs. // Range calls the provided function for each include in the map. The function // receives the include's key and value as arguments. If the function returns // an error, the iteration stops and the error is returned. -func (includes *Includes) Range(f func(k string, v *Include) error) error { +func (includes *Includes) All() iter.Seq2[string, *Include] { if includes == nil || includes.om == nil { - return nil + return func(yield func(string, *Include) bool) {} } - for pair := includes.om.Front(); pair != nil; pair = pair.Next() { - if err := f(pair.Key, pair.Value); err != nil { - return err - } + return includes.om.AllFromFront() +} + +// Keys returns an iterator that loops over all task keys. +func (includes *Includes) Keys() iter.Seq[string] { + if includes == nil || includes.om == nil { + return func(yield func(string) bool) {} + } + return includes.om.Keys() +} + +// Values returns an iterator that loops over all task values. +func (includes *Includes) Values() iter.Seq[*Include] { + if includes == nil || includes.om == nil { + return func(yield func(*Include) bool) {} } - return nil + return includes.om.Values() } // UnmarshalYAML implements the yaml.Unmarshaler interface. diff --git a/taskfile/ast/matrix.go b/taskfile/ast/matrix.go index 92ff590fb3..e6b9f1737e 100644 --- a/taskfile/ast/matrix.go +++ b/taskfile/ast/matrix.go @@ -1,6 +1,8 @@ package ast import ( + "iter" + "github.com/elliotchance/orderedmap/v3" "gopkg.in/yaml.v3" @@ -48,16 +50,28 @@ func (matrix *Matrix) Set(key string, value []any) bool { return matrix.om.Set(key, value) } -func (matrix *Matrix) Range(f func(k string, v []any) error) error { +// All returns an iterator that loops over all task key-value pairs. +func (matrix *Matrix) All() iter.Seq2[string, []any] { if matrix == nil || matrix.om == nil { - return nil + return func(yield func(string, []any) bool) {} } - for pair := matrix.om.Front(); pair != nil; pair = pair.Next() { - if err := f(pair.Key, pair.Value); err != nil { - return err - } + return matrix.om.AllFromFront() +} + +// Keys returns an iterator that loops over all task keys. +func (matrix *Matrix) Keys() iter.Seq[string] { + if matrix == nil || matrix.om == nil { + return func(yield func(string) bool) {} + } + return matrix.om.Keys() +} + +// Values returns an iterator that loops over all task values. +func (matrix *Matrix) Values() iter.Seq[[]any] { + if matrix == nil || matrix.om == nil { + return func(yield func([]any) bool) {} } - return nil + return matrix.om.Values() } func (matrix *Matrix) DeepCopy() *Matrix { diff --git a/taskfile/ast/tasks.go b/taskfile/ast/tasks.go index d9ad1ccd71..6fc84f46ca 100644 --- a/taskfile/ast/tasks.go +++ b/taskfile/ast/tasks.go @@ -2,6 +2,7 @@ package ast import ( "fmt" + "iter" "slices" "strings" "sync" @@ -11,6 +12,7 @@ import ( "github.com/go-task/task/v3/errors" "github.com/go-task/task/v3/internal/filepathext" + "github.com/go-task/task/v3/internal/sort" ) type ( @@ -79,47 +81,47 @@ func (tasks *Tasks) Set(key string, value *Task) bool { return tasks.om.Set(key, value) } -// Range calls the provided function for each task in the map. The function -// receives the task's key and value as arguments. If the function returns an -// error, the iteration stops and the error is returned. -func (tasks *Tasks) Range(f func(k string, v *Task) error) error { - if tasks == nil || tasks.om == nil { - return nil +// All returns an iterator that loops over all task key-value pairs in the order +// specified by the sorter. +func (t *Tasks) All(sorter sort.Sorter) iter.Seq2[string, *Task] { + if t == nil || t.om == nil { + return func(yield func(string, *Task) bool) {} + } + if sorter == nil { + return t.om.AllFromFront() } - for pair := tasks.om.Front(); pair != nil; pair = pair.Next() { - if err := f(pair.Key, pair.Value); err != nil { - return err + return func(yield func(string, *Task) bool) { + for _, key := range sorter(slices.Collect(t.om.Keys()), nil) { + el := t.om.GetElement(key) + if !yield(el.Key, el.Value) { + return + } } } - return nil } -// Keys returns a slice of all the keys in the Tasks map. -func (tasks *Tasks) Keys() []string { - if tasks == nil { - return nil - } - defer tasks.mutex.RUnlock() - tasks.mutex.RLock() - var keys []string - for pair := tasks.om.Front(); pair != nil; pair = pair.Next() { - keys = append(keys, pair.Key) +// Keys returns an iterator that loops over all task keys in the order specified +// by the sorter. +func (t *Tasks) Keys(sorter sort.Sorter) iter.Seq[string] { + return func(yield func(string) bool) { + for k := range t.All(sorter) { + if !yield(k) { + return + } + } } - return keys } -// Values returns a slice of all the values in the Tasks map. -func (tasks *Tasks) Values() []*Task { - if tasks == nil { - return nil - } - defer tasks.mutex.RUnlock() - tasks.mutex.RLock() - var values []*Task - for pair := tasks.om.Front(); pair != nil; pair = pair.Next() { - values = append(values, pair.Value) +// Values returns an iterator that loops over all task values in the order +// specified by the sorter. +func (t *Tasks) Values(sorter sort.Sorter) iter.Seq[*Task] { + return func(yield func(*Task) bool) { + for _, v := range t.All(sorter) { + if !yield(v) { + return + } + } } - return values } // FindMatchingTasks returns a list of tasks that match the given call. A task @@ -138,22 +140,21 @@ func (t *Tasks) FindMatchingTasks(call *Call) []*MatchingTask { } // Attempt a wildcard match // For now, we can just nil check the task before each loop - _ = t.Range(func(key string, value *Task) error { + for _, value := range t.All(nil) { if match, wildcards := value.WildcardMatch(call.Task); match { matchingTasks = append(matchingTasks, &MatchingTask{ Task: value, Wildcards: wildcards, }) } - return nil - }) + } return matchingTasks } func (t1 *Tasks) Merge(t2 *Tasks, include *Include, includedTaskfileVars *Vars) error { defer t2.mutex.RUnlock() t2.mutex.RLock() - err := t2.Range(func(name string, v *Task) error { + for name, v := range t2.All(nil) { // We do a deep copy of the task struct here to ensure that no data can // be changed elsewhere once the taskfile is merged. task := v.DeepCopy() @@ -219,9 +220,7 @@ func (t1 *Tasks) Merge(t2 *Tasks, include *Include, includedTaskfileVars *Vars) } // Add the task to the merged taskfile t1.Set(taskName, task) - - return nil - }) + } // If the included Taskfile has a default task, is not flattened and the // parent namespace has no task with a matching name, we can add an alias so @@ -239,7 +238,7 @@ func (t1 *Tasks) Merge(t2 *Tasks, include *Include, includedTaskfileVars *Vars) } } - return err + return nil } func (t *Tasks) UnmarshalYAML(node *yaml.Node) error { diff --git a/taskfile/ast/var.go b/taskfile/ast/var.go index 8dfe85662f..c84d30effc 100644 --- a/taskfile/ast/var.go +++ b/taskfile/ast/var.go @@ -1,6 +1,7 @@ package ast import ( + "iter" "strings" "sync" @@ -72,19 +73,28 @@ func (vars *Vars) Set(key string, value Var) bool { return vars.om.Set(key, value) } -// Range calls the provided function for each variable in the map. The function -// receives the variable's key and value as arguments. If the function returns -// an error, the iteration stops and the error is returned. -func (vars *Vars) Range(f func(k string, v Var) error) error { +// All returns an iterator that loops over all task key-value pairs. +func (vars *Vars) All() iter.Seq2[string, Var] { if vars == nil || vars.om == nil { - return nil + return func(yield func(string, Var) bool) {} } - for pair := vars.om.Front(); pair != nil; pair = pair.Next() { - if err := f(pair.Key, pair.Value); err != nil { - return err - } + return vars.om.AllFromFront() +} + +// Keys returns an iterator that loops over all task keys. +func (vars *Vars) Keys() iter.Seq[string] { + if vars == nil || vars.om == nil { + return func(yield func(string) bool) {} + } + return vars.om.Keys() +} + +// Values returns an iterator that loops over all task values. +func (vars *Vars) Values() iter.Seq[Var] { + if vars == nil || vars.om == nil { + return func(yield func(Var) bool) {} } - return nil + return vars.om.Values() } // ToCacheMap converts Vars to an unordered map containing only the static @@ -93,16 +103,16 @@ func (vars *Vars) ToCacheMap() (m map[string]any) { defer vars.mutex.RUnlock() vars.mutex.RLock() m = make(map[string]any, vars.Len()) - for pair := vars.om.Front(); pair != nil; pair = pair.Next() { - if pair.Value.Sh != nil && *pair.Value.Sh != "" { + for k, v := range vars.All() { + if v.Sh != nil && *v.Sh != "" { // Dynamic variable is not yet resolved; trigger // to be used in templates. return nil } - if pair.Value.Live != nil { - m[pair.Key] = pair.Value.Live + if v.Live != nil { + m[k] = v.Live } else { - m[pair.Key] = pair.Value.Value + m[k] = v.Value } } return diff --git a/taskfile/reader.go b/taskfile/reader.go index d6157067d9..4e02a93299 100644 --- a/taskfile/reader.go +++ b/taskfile/reader.go @@ -100,7 +100,7 @@ func (r *Reader) include(node Node) error { var g errgroup.Group // Loop over each included taskfile - _ = vertex.Taskfile.Includes.Range(func(namespace string, include *ast.Include) error { + for _, include := range vertex.Taskfile.Includes.All() { vars := compiler.GetEnviron() vars.Merge(vertex.Taskfile.Vars, nil) // Start a goroutine to process each included Taskfile @@ -177,8 +177,7 @@ func (r *Reader) include(node Node) error { } return err }) - return nil - }) + } // Wait for all the go routines to finish return g.Wait() @@ -207,7 +206,7 @@ func (r *Reader) readNode(node Node) (*ast.Taskfile, error) { // Set the taskfile/task's locations tf.Location = node.Location() - for _, task := range tf.Tasks.Values() { + for task := range tf.Tasks.Values(nil) { // If the task is not defined, create a new one if task == nil { task = &ast.Task{} diff --git a/variables.go b/variables.go index e92ad34912..1aded11a1e 100644 --- a/variables.go +++ b/variables.go @@ -110,21 +110,17 @@ func (e *Executor) compiledTask(call *ast.Call, evaluateShVars bool) (*ast.Task, new.Env.Merge(templater.ReplaceVars(dotenvEnvs, cache), nil) new.Env.Merge(templater.ReplaceVars(origTask.Env, cache), nil) if evaluateShVars { - err = new.Env.Range(func(k string, v ast.Var) error { + for k, v := range new.Env.All() { // If the variable is not dynamic, we can set it and return if v.Value != nil || v.Sh == nil { new.Env.Set(k, ast.Var{Value: v.Value}) - return nil + continue } static, err := e.Compiler.HandleDynamicVar(v, new.Dir, env.GetFromVars(new.Env)) if err != nil { - return err + return nil, err } new.Env.Set(k, ast.Var{Value: static}) - return nil - }) - if err != nil { - return nil, err } } @@ -303,7 +299,7 @@ func itemsFromFor( // If the variable is dynamic, then it hasn't been resolved yet // and we can't use it as a list. This happens when fast compiling a task // for use in --list or --list-all etc. - if ok && v.Sh == nil { + if ok && v.Value != nil && v.Sh == nil { switch value := v.Value.(type) { case string: if f.Split != "" { @@ -344,7 +340,7 @@ func product(inputMap *ast.Matrix) []map[string]any { result := []map[string]any{{}} // Iterate over each slice in the slices - _ = inputMap.Range(func(key string, slice []any) error { + for key, slice := range inputMap.All() { var newResult []map[string]any // For each combination in the current result @@ -364,8 +360,7 @@ func product(inputMap *ast.Matrix) []map[string]any { // Update result with the new combinations result = newResult - return nil - }) + } return result }