1
0
mirror of https://github.com/go-task/task.git synced 2024-12-14 10:52:43 +02:00
task/internal/summary/summary.go

119 lines
2.5 KiB
Go
Raw Normal View History

2019-02-24 17:25:27 +02:00
package summary
import (
2019-03-04 13:03:28 +02:00
"strings"
"github.com/go-task/task/v3/internal/logger"
"github.com/go-task/task/v3/taskfile/ast"
2019-02-24 17:25:27 +02:00
)
2024-01-26 16:34:18 +02:00
func PrintTasks(l *logger.Logger, t *ast.Taskfile, c []*ast.Call) {
2019-03-04 13:25:42 +02:00
for i, call := range c {
PrintSpaceBetweenSummaries(l, i)
PrintTask(l, t.Tasks.Get(call.Task))
2019-03-04 13:25:42 +02:00
}
}
func PrintSpaceBetweenSummaries(l *logger.Logger, i int) {
2019-03-04 13:27:10 +02:00
spaceRequired := i > 0
if !spaceRequired {
return
}
l.Outf(logger.Default, "\n")
l.Outf(logger.Default, "\n")
2019-03-04 13:27:10 +02:00
}
func PrintTask(l *logger.Logger, t *ast.Task) {
2019-02-24 19:20:59 +02:00
printTaskName(l, t)
2019-03-04 14:04:04 +02:00
printTaskDescribingText(t, l)
printTaskDependencies(l, t)
printTaskAliases(l, t)
2019-03-04 14:04:04 +02:00
printTaskCommands(l, t)
}
func printTaskDescribingText(t *ast.Task, l *logger.Logger) {
2019-02-24 19:20:59 +02:00
if hasSummary(t) {
printTaskSummary(l, t)
} else if hasDescription(t) {
2019-02-24 19:20:59 +02:00
printTaskDescription(l, t)
} else {
2019-02-24 20:14:15 +02:00
printNoDescriptionOrSummary(l)
2019-02-24 19:20:59 +02:00
}
}
func hasSummary(t *ast.Task) bool {
2019-02-24 20:14:15 +02:00
return t.Summary != ""
2019-02-24 17:26:46 +02:00
}
func printTaskSummary(l *logger.Logger, t *ast.Task) {
2019-02-24 20:14:15 +02:00
lines := strings.Split(t.Summary, "\n")
for i, line := range lines {
notLastLine := i+1 < len(lines)
if notLastLine || line != "" {
l.Outf(logger.Default, "%s\n", line)
}
}
}
func printTaskName(l *logger.Logger, t *ast.Task) {
l.Outf(logger.Default, "task: ")
l.Outf(logger.Green, "%s\n", t.Name())
l.Outf(logger.Default, "\n")
2019-02-24 17:25:27 +02:00
}
func printTaskAliases(l *logger.Logger, t *ast.Task) {
if len(t.Aliases) == 0 {
return
}
l.Outf(logger.Default, "\n")
l.Outf(logger.Default, "aliases:\n")
for _, alias := range t.Aliases {
l.Outf(logger.Default, " - ")
l.Outf(logger.Cyan, "%s\n", alias)
}
}
func hasDescription(t *ast.Task) bool {
2019-02-24 20:14:15 +02:00
return t.Desc != ""
}
func printTaskDescription(l *logger.Logger, t *ast.Task) {
l.Outf(logger.Default, "%s\n", t.Desc)
}
2019-02-24 20:14:15 +02:00
func printNoDescriptionOrSummary(l *logger.Logger) {
l.Outf(logger.Default, "(task does not have description or summary)\n")
2019-02-24 17:25:27 +02:00
}
func printTaskDependencies(l *logger.Logger, t *ast.Task) {
if len(t.Deps) == 0 {
return
}
2019-02-24 17:25:27 +02:00
l.Outf(logger.Default, "\n")
l.Outf(logger.Default, "dependencies:\n")
for _, d := range t.Deps {
l.Outf(logger.Default, " - %s\n", d.Task)
2019-02-24 17:25:27 +02:00
}
}
func printTaskCommands(l *logger.Logger, t *ast.Task) {
2019-03-04 13:28:26 +02:00
if len(t.Cmds) == 0 {
2019-03-04 13:28:11 +02:00
return
}
l.Outf(logger.Default, "\n")
l.Outf(logger.Default, "commands:\n")
2019-03-04 13:28:11 +02:00
for _, c := range t.Cmds {
isCommand := c.Cmd != ""
l.Outf(logger.Default, " - ")
2019-03-04 13:28:11 +02:00
if isCommand {
l.Outf(logger.Yellow, "%s\n", c.Cmd)
2019-03-04 13:28:11 +02:00
} else {
l.Outf(logger.Green, "Task: %s\n", c.Task)
2019-02-24 17:25:27 +02:00
}
}
}