2017-03-19 20:18:18 +02:00
|
|
|
package task
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
"text/tabwriter"
|
2018-02-17 18:22:18 +02:00
|
|
|
|
2020-08-16 20:48:19 +02:00
|
|
|
"github.com/go-task/task/v3/internal/logger"
|
2020-08-19 10:59:58 +02:00
|
|
|
"github.com/go-task/task/v3/taskfile"
|
2017-03-19 20:18:18 +02:00
|
|
|
)
|
|
|
|
|
2022-01-04 22:16:21 +02:00
|
|
|
// ListTasksWithDesc reports tasks that have a description spec.
|
|
|
|
func (e *Executor) ListTasksWithDesc() {
|
2022-01-04 22:19:38 +02:00
|
|
|
e.printTasks(false)
|
2022-01-04 22:16:21 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListAllTasks reports all tasks, with or without a description spec.
|
|
|
|
func (e *Executor) ListAllTasks() {
|
2022-01-04 22:19:38 +02:00
|
|
|
e.printTasks(true)
|
2022-01-04 22:16:21 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-04 22:19:38 +02:00
|
|
|
func (e *Executor) printTasks(listAll bool) {
|
2020-11-13 22:27:03 +02:00
|
|
|
var tasks []*taskfile.Task
|
2022-01-04 22:16:21 +02:00
|
|
|
if listAll {
|
2022-01-03 19:12:18 +02:00
|
|
|
tasks = e.allTaskNames()
|
2020-11-13 22:27:03 +02:00
|
|
|
} else {
|
|
|
|
tasks = e.tasksWithDesc()
|
|
|
|
}
|
|
|
|
|
2017-03-19 20:18:18 +02:00
|
|
|
if len(tasks) == 0 {
|
2022-01-04 22:16:21 +02:00
|
|
|
if listAll {
|
|
|
|
e.Logger.Outf(logger.Yellow, "task: No tasks available")
|
|
|
|
} else {
|
|
|
|
e.Logger.Outf(logger.Yellow, "task: No tasks with description available. Try --list-all to list all tasks")
|
|
|
|
}
|
2017-03-19 20:18:18 +02:00
|
|
|
return
|
|
|
|
}
|
2019-05-26 23:36:39 +02:00
|
|
|
e.Logger.Outf(logger.Default, "task: Available tasks for this project:")
|
2017-03-19 20:18:18 +02:00
|
|
|
|
|
|
|
// Format in tab-separated columns with a tab stop of 8.
|
2017-07-01 20:05:51 +02:00
|
|
|
w := tabwriter.NewWriter(e.Stdout, 0, 8, 0, '\t', 0)
|
2017-03-19 20:18:18 +02:00
|
|
|
for _, task := range tasks {
|
2020-06-03 22:19:12 +02:00
|
|
|
fmt.Fprintf(w, "* %s: \t%s\n", task.Name(), task.Desc)
|
2017-03-19 20:18:18 +02:00
|
|
|
}
|
|
|
|
w.Flush()
|
|
|
|
}
|
|
|
|
|
2022-01-03 19:12:18 +02:00
|
|
|
func (e *Executor) allTaskNames() (tasks []*taskfile.Task) {
|
2020-11-13 22:27:03 +02:00
|
|
|
tasks = make([]*taskfile.Task, 0, len(e.Taskfile.Tasks))
|
|
|
|
for _, task := range e.Taskfile.Tasks {
|
|
|
|
tasks = append(tasks, task)
|
|
|
|
}
|
|
|
|
sort.Slice(tasks, func(i, j int) bool { return tasks[i].Task < tasks[j].Task })
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-02-17 18:22:18 +02:00
|
|
|
func (e *Executor) tasksWithDesc() (tasks []*taskfile.Task) {
|
|
|
|
tasks = make([]*taskfile.Task, 0, len(e.Taskfile.Tasks))
|
2017-12-29 22:27:32 +02:00
|
|
|
for _, task := range e.Taskfile.Tasks {
|
2017-03-19 20:18:18 +02:00
|
|
|
if task.Desc != "" {
|
2021-01-12 17:03:04 +02:00
|
|
|
compiledTask, err := e.FastCompiledTask(taskfile.Call{Task: task.Task})
|
2020-01-20 15:38:18 +02:00
|
|
|
if err == nil {
|
|
|
|
task = compiledTask
|
|
|
|
}
|
2017-12-29 22:27:32 +02:00
|
|
|
tasks = append(tasks, task)
|
2017-03-19 20:18:18 +02:00
|
|
|
}
|
|
|
|
}
|
2017-12-29 22:27:32 +02:00
|
|
|
sort.Slice(tasks, func(i, j int) bool { return tasks[i].Task < tasks[j].Task })
|
2017-03-19 20:18:18 +02:00
|
|
|
return
|
|
|
|
}
|