mirror of
https://github.com/go-task/task.git
synced 2025-11-29 22:48:03 +02:00
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:
@@ -4,22 +4,22 @@ import (
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/go-task/task/v3/taskfile"
|
||||
"github.com/go-task/task/v3/taskfile/ast"
|
||||
)
|
||||
|
||||
type TaskSorter interface {
|
||||
Sort([]*taskfile.Task)
|
||||
Sort([]*ast.Task)
|
||||
}
|
||||
|
||||
type Noop struct{}
|
||||
|
||||
func (s *Noop) Sort(tasks []*taskfile.Task) {}
|
||||
func (s *Noop) Sort(tasks []*ast.Task) {}
|
||||
|
||||
type AlphaNumeric struct{}
|
||||
|
||||
// Tasks that are not namespaced should be listed before tasks that are.
|
||||
// We detect this by searching for a ':' in the task name.
|
||||
func (s *AlphaNumeric) Sort(tasks []*taskfile.Task) {
|
||||
func (s *AlphaNumeric) Sort(tasks []*ast.Task) {
|
||||
sort.Slice(tasks, func(i, j int) bool {
|
||||
return tasks[i].Task < tasks[j].Task
|
||||
})
|
||||
@@ -29,7 +29,7 @@ type AlphaNumericWithRootTasksFirst struct{}
|
||||
|
||||
// Tasks that are not namespaced should be listed before tasks that are.
|
||||
// We detect this by searching for a ':' in the task name.
|
||||
func (s *AlphaNumericWithRootTasksFirst) Sort(tasks []*taskfile.Task) {
|
||||
func (s *AlphaNumericWithRootTasksFirst) Sort(tasks []*ast.Task) {
|
||||
sort.Slice(tasks, func(i, j int) bool {
|
||||
iContainsColon := strings.Contains(tasks[i].Task, ":")
|
||||
jContainsColon := strings.Contains(tasks[j].Task, ":")
|
||||
|
||||
@@ -5,36 +5,36 @@ import (
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/go-task/task/v3/taskfile"
|
||||
"github.com/go-task/task/v3/taskfile/ast"
|
||||
)
|
||||
|
||||
func TestAlphaNumericWithRootTasksFirst_Sort(t *testing.T) {
|
||||
task1 := &taskfile.Task{Task: "task1"}
|
||||
task2 := &taskfile.Task{Task: "task2"}
|
||||
task3 := &taskfile.Task{Task: "ns1:task3"}
|
||||
task4 := &taskfile.Task{Task: "ns2:task4"}
|
||||
task5 := &taskfile.Task{Task: "task5"}
|
||||
task6 := &taskfile.Task{Task: "ns3:task6"}
|
||||
task1 := &ast.Task{Task: "task1"}
|
||||
task2 := &ast.Task{Task: "task2"}
|
||||
task3 := &ast.Task{Task: "ns1:task3"}
|
||||
task4 := &ast.Task{Task: "ns2:task4"}
|
||||
task5 := &ast.Task{Task: "task5"}
|
||||
task6 := &ast.Task{Task: "ns3:task6"}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
tasks []*taskfile.Task
|
||||
want []*taskfile.Task
|
||||
tasks []*ast.Task
|
||||
want []*ast.Task
|
||||
}{
|
||||
{
|
||||
name: "no namespace tasks sorted alphabetically first",
|
||||
tasks: []*taskfile.Task{task3, task2, task1},
|
||||
want: []*taskfile.Task{task1, task2, task3},
|
||||
tasks: []*ast.Task{task3, task2, task1},
|
||||
want: []*ast.Task{task1, task2, task3},
|
||||
},
|
||||
{
|
||||
name: "namespace tasks sorted alphabetically after non-namespaced tasks",
|
||||
tasks: []*taskfile.Task{task3, task4, task5},
|
||||
want: []*taskfile.Task{task5, task3, task4},
|
||||
tasks: []*ast.Task{task3, task4, task5},
|
||||
want: []*ast.Task{task5, task3, task4},
|
||||
},
|
||||
{
|
||||
name: "all tasks sorted alphabetically with root tasks first",
|
||||
tasks: []*taskfile.Task{task6, task5, task4, task3, task2, task1},
|
||||
want: []*taskfile.Task{task1, task2, task5, task3, task4, task6},
|
||||
tasks: []*ast.Task{task6, task5, task4, task3, task2, task1},
|
||||
want: []*ast.Task{task1, task2, task5, task3, task4, task6},
|
||||
},
|
||||
}
|
||||
|
||||
@@ -48,22 +48,22 @@ func TestAlphaNumericWithRootTasksFirst_Sort(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestAlphaNumeric_Sort(t *testing.T) {
|
||||
task1 := &taskfile.Task{Task: "task1"}
|
||||
task2 := &taskfile.Task{Task: "task2"}
|
||||
task3 := &taskfile.Task{Task: "ns1:task3"}
|
||||
task4 := &taskfile.Task{Task: "ns2:task4"}
|
||||
task5 := &taskfile.Task{Task: "task5"}
|
||||
task6 := &taskfile.Task{Task: "ns3:task6"}
|
||||
task1 := &ast.Task{Task: "task1"}
|
||||
task2 := &ast.Task{Task: "task2"}
|
||||
task3 := &ast.Task{Task: "ns1:task3"}
|
||||
task4 := &ast.Task{Task: "ns2:task4"}
|
||||
task5 := &ast.Task{Task: "task5"}
|
||||
task6 := &ast.Task{Task: "ns3:task6"}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
tasks []*taskfile.Task
|
||||
want []*taskfile.Task
|
||||
tasks []*ast.Task
|
||||
want []*ast.Task
|
||||
}{
|
||||
{
|
||||
name: "all tasks sorted alphabetically",
|
||||
tasks: []*taskfile.Task{task3, task2, task5, task1, task4, task6},
|
||||
want: []*taskfile.Task{task3, task4, task6, task1, task2, task5},
|
||||
tasks: []*ast.Task{task3, task2, task5, task1, task4, task6},
|
||||
want: []*ast.Task{task3, task4, task6, task1, task2, task5},
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user