refactor: taskfile/ast package (#1450)

* refactor: ast package

* feat: read -> taskfile

* refactor: taskfile.Taskfile -> taskfile.Read

* refactor: move merge function back into taskfile package

* refactor: rename taskfile.go to read.go
This commit is contained in:
Pete Davison
2023-12-29 20:32:03 +00:00
committed by GitHub
parent 2b67d05b9d
commit 247c2586c2
61 changed files with 471 additions and 468 deletions

View File

@@ -4,10 +4,10 @@ import (
"strings"
"github.com/go-task/task/v3/internal/logger"
"github.com/go-task/task/v3/taskfile"
"github.com/go-task/task/v3/taskfile/ast"
)
func PrintTasks(l *logger.Logger, t *taskfile.Taskfile, c []taskfile.Call) {
func PrintTasks(l *logger.Logger, t *ast.Taskfile, c []ast.Call) {
for i, call := range c {
PrintSpaceBetweenSummaries(l, i)
PrintTask(l, t.Tasks.Get(call.Task))
@@ -24,7 +24,7 @@ func PrintSpaceBetweenSummaries(l *logger.Logger, i int) {
l.Outf(logger.Default, "\n")
}
func PrintTask(l *logger.Logger, t *taskfile.Task) {
func PrintTask(l *logger.Logger, t *ast.Task) {
printTaskName(l, t)
printTaskDescribingText(t, l)
printTaskDependencies(l, t)
@@ -32,7 +32,7 @@ func PrintTask(l *logger.Logger, t *taskfile.Task) {
printTaskCommands(l, t)
}
func printTaskDescribingText(t *taskfile.Task, l *logger.Logger) {
func printTaskDescribingText(t *ast.Task, l *logger.Logger) {
if hasSummary(t) {
printTaskSummary(l, t)
} else if hasDescription(t) {
@@ -42,11 +42,11 @@ func printTaskDescribingText(t *taskfile.Task, l *logger.Logger) {
}
}
func hasSummary(t *taskfile.Task) bool {
func hasSummary(t *ast.Task) bool {
return t.Summary != ""
}
func printTaskSummary(l *logger.Logger, t *taskfile.Task) {
func printTaskSummary(l *logger.Logger, t *ast.Task) {
lines := strings.Split(t.Summary, "\n")
for i, line := range lines {
notLastLine := i+1 < len(lines)
@@ -56,13 +56,13 @@ func printTaskSummary(l *logger.Logger, t *taskfile.Task) {
}
}
func printTaskName(l *logger.Logger, t *taskfile.Task) {
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")
}
func printTaskAliases(l *logger.Logger, t *taskfile.Task) {
func printTaskAliases(l *logger.Logger, t *ast.Task) {
if len(t.Aliases) == 0 {
return
}
@@ -74,11 +74,11 @@ func printTaskAliases(l *logger.Logger, t *taskfile.Task) {
}
}
func hasDescription(t *taskfile.Task) bool {
func hasDescription(t *ast.Task) bool {
return t.Desc != ""
}
func printTaskDescription(l *logger.Logger, t *taskfile.Task) {
func printTaskDescription(l *logger.Logger, t *ast.Task) {
l.Outf(logger.Default, "%s\n", t.Desc)
}
@@ -86,7 +86,7 @@ func printNoDescriptionOrSummary(l *logger.Logger) {
l.Outf(logger.Default, "(task does not have description or summary)\n")
}
func printTaskDependencies(l *logger.Logger, t *taskfile.Task) {
func printTaskDependencies(l *logger.Logger, t *ast.Task) {
if len(t.Deps) == 0 {
return
}
@@ -99,7 +99,7 @@ func printTaskDependencies(l *logger.Logger, t *taskfile.Task) {
}
}
func printTaskCommands(l *logger.Logger, t *taskfile.Task) {
func printTaskCommands(l *logger.Logger, t *ast.Task) {
if len(t.Cmds) == 0 {
return
}

View File

@@ -9,13 +9,13 @@ import (
"github.com/go-task/task/v3/internal/logger"
"github.com/go-task/task/v3/internal/summary"
"github.com/go-task/task/v3/taskfile"
"github.com/go-task/task/v3/taskfile/ast"
)
func TestPrintsDependenciesIfPresent(t *testing.T) {
buffer, l := createDummyLogger()
task := &taskfile.Task{
Deps: []*taskfile.Dep{
task := &ast.Task{
Deps: []*ast.Dep{
{Task: "dep1"},
{Task: "dep2"},
{Task: "dep3"},
@@ -39,8 +39,8 @@ func createDummyLogger() (*bytes.Buffer, logger.Logger) {
func TestDoesNotPrintDependenciesIfMissing(t *testing.T) {
buffer, l := createDummyLogger()
task := &taskfile.Task{
Deps: []*taskfile.Dep{},
task := &ast.Task{
Deps: []*ast.Dep{},
}
summary.PrintTask(&l, task)
@@ -50,7 +50,7 @@ func TestDoesNotPrintDependenciesIfMissing(t *testing.T) {
func TestPrintTaskName(t *testing.T) {
buffer, l := createDummyLogger()
task := &taskfile.Task{
task := &ast.Task{
Task: "my-task-name",
}
@@ -61,8 +61,8 @@ func TestPrintTaskName(t *testing.T) {
func TestPrintTaskCommandsIfPresent(t *testing.T) {
buffer, l := createDummyLogger()
task := &taskfile.Task{
Cmds: []*taskfile.Cmd{
task := &ast.Task{
Cmds: []*ast.Cmd{
{Cmd: "command-1"},
{Cmd: "command-2"},
{Task: "task-1"},
@@ -79,8 +79,8 @@ func TestPrintTaskCommandsIfPresent(t *testing.T) {
func TestDoesNotPrintCommandIfMissing(t *testing.T) {
buffer, l := createDummyLogger()
task := &taskfile.Task{
Cmds: []*taskfile.Cmd{},
task := &ast.Task{
Cmds: []*ast.Cmd{},
}
summary.PrintTask(&l, task)
@@ -90,13 +90,13 @@ func TestDoesNotPrintCommandIfMissing(t *testing.T) {
func TestLayout(t *testing.T) {
buffer, l := createDummyLogger()
task := &taskfile.Task{
task := &ast.Task{
Task: "sample-task",
Summary: "line1\nline2\nline3\n",
Deps: []*taskfile.Dep{
Deps: []*ast.Dep{
{Task: "dependency"},
},
Cmds: []*taskfile.Cmd{
Cmds: []*ast.Cmd{
{Cmd: "command"},
},
}
@@ -124,15 +124,15 @@ commands:
func TestPrintDescriptionAsFallback(t *testing.T) {
buffer, l := createDummyLogger()
taskWithoutSummary := &taskfile.Task{
taskWithoutSummary := &ast.Task{
Desc: "description",
}
taskWithSummary := &taskfile.Task{
taskWithSummary := &ast.Task{
Desc: "description",
Summary: "summary",
}
taskWithoutSummaryOrDescription := &taskfile.Task{}
taskWithoutSummaryOrDescription := &ast.Task{}
summary.PrintTask(&l, taskWithoutSummary)
@@ -152,18 +152,18 @@ func TestPrintDescriptionAsFallback(t *testing.T) {
func TestPrintAllWithSpaces(t *testing.T) {
buffer, l := createDummyLogger()
t1 := &taskfile.Task{Task: "t1"}
t2 := &taskfile.Task{Task: "t2"}
t3 := &taskfile.Task{Task: "t3"}
t1 := &ast.Task{Task: "t1"}
t2 := &ast.Task{Task: "t2"}
t3 := &ast.Task{Task: "t3"}
tasks := taskfile.Tasks{}
tasks := ast.Tasks{}
tasks.Set("t1", t1)
tasks.Set("t2", t2)
tasks.Set("t3", t3)
summary.PrintTasks(&l,
&taskfile.Taskfile{Tasks: tasks},
[]taskfile.Call{{Task: "t1"}, {Task: "t2"}, {Task: "t3"}})
&ast.Taskfile{Tasks: tasks},
[]ast.Call{{Task: "t1"}, {Task: "t2"}, {Task: "t3"}})
assert.True(t, strings.HasPrefix(buffer.String(), "task: t1"))
assert.Contains(t, buffer.String(), "\n(task does not have description or summary)\n\n\ntask: t2")