mirror of
https://github.com/go-task/task.git
synced 2025-12-16 11:47:44 +01:00
chore: optimize task filtering (#982)
This commit is contained in:
9
help.go
9
help.go
@@ -51,10 +51,10 @@ func (o ListOptions) Validate() error {
|
|||||||
// Filters returns the slice of FilterFunc which filters a list
|
// Filters returns the slice of FilterFunc which filters a list
|
||||||
// of taskfile.Task according to the given ListOptions
|
// of taskfile.Task according to the given ListOptions
|
||||||
func (o ListOptions) Filters() []FilterFunc {
|
func (o ListOptions) Filters() []FilterFunc {
|
||||||
filters := []FilterFunc{FilterOutInternal()}
|
filters := []FilterFunc{FilterOutInternal}
|
||||||
|
|
||||||
if o.ListOnlyTasksWithDescriptions {
|
if o.ListOnlyTasksWithDescriptions {
|
||||||
filters = append(filters, FilterOutNoDesc())
|
filters = append(filters, FilterOutNoDesc)
|
||||||
}
|
}
|
||||||
|
|
||||||
return filters
|
return filters
|
||||||
@@ -65,7 +65,10 @@ func (o ListOptions) Filters() []FilterFunc {
|
|||||||
// The function returns a boolean indicating whether tasks were found
|
// The function returns a boolean indicating whether tasks were found
|
||||||
// and an error if one was encountered while preparing the output.
|
// and an error if one was encountered while preparing the output.
|
||||||
func (e *Executor) ListTasks(o ListOptions) (bool, error) {
|
func (e *Executor) ListTasks(o ListOptions) (bool, error) {
|
||||||
tasks := e.GetTaskList(o.Filters()...)
|
tasks, err := e.GetTaskList(o.Filters()...)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
if o.FormatTaskListAsJSON {
|
if o.FormatTaskListAsJSON {
|
||||||
output, err := e.ToEditorOutput(tasks)
|
output, err := e.ToEditorOutput(tasks)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
71
task.go
71
task.go
@@ -400,23 +400,39 @@ func (e *Executor) GetTask(call taskfile.Call) (*taskfile.Task, error) {
|
|||||||
return matchingTask, nil
|
return matchingTask, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type FilterFunc func(tasks []*taskfile.Task) []*taskfile.Task
|
type FilterFunc func(task *taskfile.Task) bool
|
||||||
|
|
||||||
func (e *Executor) GetTaskList(filters ...FilterFunc) []*taskfile.Task {
|
func (e *Executor) GetTaskList(filters ...FilterFunc) ([]*taskfile.Task, error) {
|
||||||
tasks := make([]*taskfile.Task, 0, len(e.Taskfile.Tasks))
|
tasks := make([]*taskfile.Task, 0, len(e.Taskfile.Tasks))
|
||||||
|
|
||||||
|
// Create an error group to wait for each task to be compiled
|
||||||
|
var g errgroup.Group
|
||||||
|
|
||||||
// Fetch and compile the list of tasks
|
// Fetch and compile the list of tasks
|
||||||
for _, task := range e.Taskfile.Tasks {
|
for key := range e.Taskfile.Tasks {
|
||||||
compiledTask, err := e.FastCompiledTask(taskfile.Call{Task: task.Task})
|
task := e.Taskfile.Tasks[key]
|
||||||
if err == nil {
|
g.Go(func() error {
|
||||||
task = compiledTask
|
|
||||||
}
|
// Check if we should filter the task
|
||||||
tasks = append(tasks, task)
|
for _, filter := range filters {
|
||||||
|
if filter(task) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Compile the task
|
||||||
|
compiledTask, err := e.FastCompiledTask(taskfile.Call{Task: task.Task})
|
||||||
|
if err == nil {
|
||||||
|
task = compiledTask
|
||||||
|
}
|
||||||
|
tasks = append(tasks, task)
|
||||||
|
return nil
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Filter the tasks
|
// Wait for all the go routines to finish
|
||||||
for _, filter := range filters {
|
if err := g.Wait(); err != nil {
|
||||||
tasks = filter(tasks)
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sort the tasks.
|
// Sort the tasks.
|
||||||
@@ -434,40 +450,17 @@ func (e *Executor) GetTaskList(filters ...FilterFunc) []*taskfile.Task {
|
|||||||
return false
|
return false
|
||||||
})
|
})
|
||||||
|
|
||||||
return tasks
|
return tasks, nil
|
||||||
}
|
|
||||||
|
|
||||||
// Filter is a generic task filtering function. It will remove each task in the
|
|
||||||
// slice where the result of the given function is true.
|
|
||||||
func Filter(f func(task *taskfile.Task) bool) FilterFunc {
|
|
||||||
return func(tasks []*taskfile.Task) []*taskfile.Task {
|
|
||||||
shift := 0
|
|
||||||
for _, task := range tasks {
|
|
||||||
if !f(task) {
|
|
||||||
tasks[shift] = task
|
|
||||||
shift++
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// This loop stops any memory leaks
|
|
||||||
for j := shift; j < len(tasks); j++ {
|
|
||||||
tasks[j] = nil
|
|
||||||
}
|
|
||||||
return slices.Clip(tasks[:shift])
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// FilterOutNoDesc removes all tasks that do not contain a description.
|
// FilterOutNoDesc removes all tasks that do not contain a description.
|
||||||
func FilterOutNoDesc() FilterFunc {
|
func FilterOutNoDesc(task *taskfile.Task) bool {
|
||||||
return Filter(func(task *taskfile.Task) bool {
|
return task.Desc == ""
|
||||||
return task.Desc == ""
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// FilterOutInternal removes all tasks that are marked as internal.
|
// FilterOutInternal removes all tasks that are marked as internal.
|
||||||
func FilterOutInternal() FilterFunc {
|
func FilterOutInternal(task *taskfile.Task) bool {
|
||||||
return Filter(func(task *taskfile.Task) bool {
|
return task.Internal
|
||||||
return task.Internal
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func shouldRunOnCurrentPlatform(platforms []*taskfile.Platform) bool {
|
func shouldRunOnCurrentPlatform(platforms []*taskfile.Platform) bool {
|
||||||
|
|||||||
Reference in New Issue
Block a user