1
0
mirror of https://github.com/go-task/task.git synced 2024-12-16 10:59:23 +02:00
task/internal/summary/summary.go

104 lines
1.9 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"
2019-02-24 17:25:27 +02:00
"github.com/go-task/task/v2/internal/logger"
"github.com/go-task/task/v2/internal/taskfile"
)
2019-03-04 14:03:13 +02:00
func PrintTasks(l *logger.Logger, t *taskfile.Taskfile, c []taskfile.Call) {
2019-03-04 13:25:42 +02:00
for i, call := range c {
2019-03-04 14:03:13 +02:00
printSpaceBetweenSummaries(l, i)
PrintTask(l, t.Tasks[call.Task])
2019-03-04 13:25:42 +02:00
}
}
2019-03-04 14:03:13 +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("")
l.Outf("")
}
2019-03-04 14:03:13 +02:00
func PrintTask(l *logger.Logger, t *taskfile.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)
printTaskCommands(l, t)
}
func printTaskDescribingText(t *taskfile.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
}
}
2019-02-24 20:14:15 +02:00
func hasSummary(t *taskfile.Task) bool {
return t.Summary != ""
2019-02-24 17:26:46 +02:00
}
2019-02-24 20:14:15 +02:00
func printTaskSummary(l *logger.Logger, t *taskfile.Task) {
lines := strings.Split(t.Summary, "\n")
for i, line := range lines {
notLastLine := i+1 < len(lines)
if notLastLine || line != "" {
2019-02-24 20:14:15 +02:00
l.Outf(line)
}
}
}
2019-02-24 20:14:15 +02:00
func printTaskName(l *logger.Logger, t *taskfile.Task) {
l.Outf("task: %s", t.Task)
2019-02-24 20:14:15 +02:00
l.Outf("")
2019-02-24 17:25:27 +02:00
}
2019-02-24 20:14:15 +02:00
func hasDescription(t *taskfile.Task) bool {
return t.Desc != ""
}
2019-02-24 20:14:15 +02:00
func printTaskDescription(l *logger.Logger, t *taskfile.Task) {
l.Outf(t.Desc)
}
2019-02-24 20:14:15 +02:00
func printNoDescriptionOrSummary(l *logger.Logger) {
l.Outf("(task does not have description or summary)")
2019-02-24 17:25:27 +02:00
}
2019-02-24 20:14:15 +02:00
func printTaskDependencies(l *logger.Logger, t *taskfile.Task) {
if len(t.Deps) == 0 {
return
}
2019-02-24 17:25:27 +02:00
l.Outf("")
l.Outf("dependencies:")
for _, d := range t.Deps {
l.Outf(" - %s", d.Task)
2019-02-24 17:25:27 +02:00
}
}
2019-02-24 20:14:15 +02:00
func printTaskCommands(l *logger.Logger, t *taskfile.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("")
l.Outf("commands:")
for _, c := range t.Cmds {
isCommand := c.Cmd != ""
if isCommand {
l.Outf(" - %s", c.Cmd)
} else {
l.Outf(" - Task: %s", c.Task)
2019-02-24 17:25:27 +02:00
}
}
}