1
0
mirror of https://github.com/go-task/task.git synced 2025-08-10 22:42:19 +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:
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

@@ -1,4 +1,4 @@
package taskfile
package ast
// Call is the parameters to a task call
type Call struct {

View File

@@ -1,4 +1,4 @@
package taskfile
package ast
import (
"fmt"

View File

@@ -1,4 +1,4 @@
package taskfile
package ast
import (
"fmt"

View File

@@ -1,4 +1,4 @@
package taskfile
package ast
import (
"fmt"

View File

@@ -1,4 +1,4 @@
package taskfile
package ast
import (
"fmt"

View File

@@ -1,4 +1,4 @@
package taskfile
package ast
import (
"fmt"

View File

@@ -1,4 +1,4 @@
package taskfile
package ast
type Location struct {
Line int

View File

@@ -1,4 +1,4 @@
package taskfile
package ast
import (
"fmt"

View File

@@ -1,4 +1,4 @@
package taskfile
package ast
import (
"fmt"

View File

@@ -1,4 +1,4 @@
package taskfile
package ast
import (
"testing"

View File

@@ -1,4 +1,4 @@
package taskfile
package ast
import (
"errors"

View File

@@ -1,4 +1,4 @@
package taskfile_test
package ast_test
import (
"testing"
@@ -7,7 +7,7 @@ import (
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
"github.com/go-task/task/v3/taskfile"
"github.com/go-task/task/v3/taskfile/ast"
)
func TestPreconditionParse(t *testing.T) {
@@ -18,29 +18,29 @@ func TestPreconditionParse(t *testing.T) {
}{
{
"test -f foo.txt",
&taskfile.Precondition{},
&taskfile.Precondition{Sh: `test -f foo.txt`, Msg: "`test -f foo.txt` failed"},
&ast.Precondition{},
&ast.Precondition{Sh: `test -f foo.txt`, Msg: "`test -f foo.txt` failed"},
},
{
"sh: '[ 1 = 0 ]'",
&taskfile.Precondition{},
&taskfile.Precondition{Sh: "[ 1 = 0 ]", Msg: "[ 1 = 0 ] failed"},
&ast.Precondition{},
&ast.Precondition{Sh: "[ 1 = 0 ]", Msg: "[ 1 = 0 ] failed"},
},
{
`
sh: "[ 1 = 2 ]"
msg: "1 is not 2"
`,
&taskfile.Precondition{},
&taskfile.Precondition{Sh: "[ 1 = 2 ]", Msg: "1 is not 2"},
&ast.Precondition{},
&ast.Precondition{Sh: "[ 1 = 2 ]", Msg: "1 is not 2"},
},
{
`
sh: "[ 1 = 2 ]"
msg: "1 is not 2"
`,
&taskfile.Precondition{},
&taskfile.Precondition{Sh: "[ 1 = 2 ]", Msg: "1 is not 2"},
&ast.Precondition{},
&ast.Precondition{Sh: "[ 1 = 2 ]", Msg: "1 is not 2"},
},
}
for _, test := range tests {

View File

@@ -1,4 +1,4 @@
package taskfile
package ast
import "github.com/go-task/task/v3/internal/deepcopy"

View File

@@ -1,4 +1,4 @@
package taskfile
package ast
import (
"fmt"

View File

@@ -1,4 +1,4 @@
package taskfile
package ast
import (
"fmt"
@@ -12,7 +12,7 @@ import (
var V3 = semver.MustParse("3")
// Taskfile represents a Taskfile.yml
// Taskfile is the abstract syntax tree for a Taskfile
type Taskfile struct {
Location string
Version *semver.Version

View File

@@ -1,4 +1,4 @@
package taskfile_test
package ast_test
import (
"testing"
@@ -8,7 +8,7 @@ import (
"gopkg.in/yaml.v3"
"github.com/go-task/task/v3/internal/orderedmap"
"github.com/go-task/task/v3/taskfile"
"github.com/go-task/task/v3/taskfile/ast"
)
func TestCmdParse(t *testing.T) {
@@ -31,16 +31,16 @@ vars:
}{
{
yamlCmd,
&taskfile.Cmd{},
&taskfile.Cmd{Cmd: `echo "a string command"`},
&ast.Cmd{},
&ast.Cmd{Cmd: `echo "a string command"`},
},
{
yamlTaskCall,
&taskfile.Cmd{},
&taskfile.Cmd{
Task: "another-task", Vars: &taskfile.Vars{
&ast.Cmd{},
&ast.Cmd{
Task: "another-task", Vars: &ast.Vars{
OrderedMap: orderedmap.FromMapWithOrder(
map[string]taskfile.Var{
map[string]ast.Var{
"PARAM1": {Value: "VALUE1"},
"PARAM2": {Value: "VALUE2"},
},
@@ -51,16 +51,16 @@ vars:
},
{
yamlDeferredCmd,
&taskfile.Cmd{},
&taskfile.Cmd{Cmd: "echo 'test'", Defer: true},
&ast.Cmd{},
&ast.Cmd{Cmd: "echo 'test'", Defer: true},
},
{
yamlDeferredCall,
&taskfile.Cmd{},
&taskfile.Cmd{
Task: "some_task", Vars: &taskfile.Vars{
&ast.Cmd{},
&ast.Cmd{
Task: "some_task", Vars: &ast.Vars{
OrderedMap: orderedmap.FromMapWithOrder(
map[string]taskfile.Var{
map[string]ast.Var{
"PARAM1": {Value: "var"},
},
[]string{"PARAM1"},
@@ -71,16 +71,16 @@ vars:
},
{
yamlDep,
&taskfile.Dep{},
&taskfile.Dep{Task: "task-name"},
&ast.Dep{},
&ast.Dep{Task: "task-name"},
},
{
yamlTaskCall,
&taskfile.Dep{},
&taskfile.Dep{
Task: "another-task", Vars: &taskfile.Vars{
&ast.Dep{},
&ast.Dep{
Task: "another-task", Vars: &ast.Vars{
OrderedMap: orderedmap.FromMapWithOrder(
map[string]taskfile.Var{
map[string]ast.Var{
"PARAM1": {Value: "VALUE1"},
"PARAM2": {Value: "VALUE2"},
},

View File

@@ -1,4 +1,4 @@
package taskfile
package ast
import (
"fmt"

View File

@@ -1,4 +1,4 @@
package taskfile
package ast
import (
"fmt"

View File

@@ -1,4 +1,4 @@
package read
package taskfile
import (
"crypto/sha256"

View File

@@ -1,4 +1,4 @@
package read
package taskfile
import (
"os"
@@ -8,10 +8,10 @@ import (
"github.com/go-task/task/v3/internal/compiler"
"github.com/go-task/task/v3/internal/filepathext"
"github.com/go-task/task/v3/internal/templater"
"github.com/go-task/task/v3/taskfile"
"github.com/go-task/task/v3/taskfile/ast"
)
func Dotenv(c *compiler.Compiler, tf *taskfile.Taskfile, dir string) (*taskfile.Vars, error) {
func Dotenv(c *compiler.Compiler, tf *ast.Taskfile, dir string) (*ast.Vars, error) {
if len(tf.Dotenv) == 0 {
return nil, nil
}
@@ -21,7 +21,7 @@ func Dotenv(c *compiler.Compiler, tf *taskfile.Taskfile, dir string) (*taskfile.
return nil, err
}
env := &taskfile.Vars{}
env := &ast.Vars{}
tr := templater.Templater{Vars: vars}
@@ -42,7 +42,7 @@ func Dotenv(c *compiler.Compiler, tf *taskfile.Taskfile, dir string) (*taskfile.
}
for key, value := range envs {
if ok := env.Exists(key); !ok {
env.Set(key, taskfile.Var{Value: value})
env.Set(key, ast.Var{Value: value})
}
}
}

View File

@@ -3,13 +3,15 @@ package taskfile
import (
"fmt"
"strings"
"github.com/go-task/task/v3/taskfile/ast"
)
// NamespaceSeparator contains the character that separates namespaces
const NamespaceSeparator = ":"
// Merge merges the second Taskfile into the first
func Merge(t1, t2 *Taskfile, includedTaskfile *IncludedTaskfile, namespaces ...string) error {
func Merge(t1, t2 *ast.Taskfile, includedTaskfile *ast.IncludedTaskfile, namespaces ...string) error {
if !t1.Version.Equal(t2.Version) {
return fmt.Errorf(`task: Taskfiles versions should match. First is "%s" but second is "%s"`, t1.Version, t2.Version)
}
@@ -18,15 +20,15 @@ func Merge(t1, t2 *Taskfile, includedTaskfile *IncludedTaskfile, namespaces ...s
}
if t1.Vars == nil {
t1.Vars = &Vars{}
t1.Vars = &ast.Vars{}
}
if t1.Env == nil {
t1.Env = &Vars{}
t1.Env = &ast.Vars{}
}
t1.Vars.Merge(t2.Vars)
t1.Env.Merge(t2.Env)
return t2.Tasks.Range(func(k string, v *Task) error {
return t2.Tasks.Range(func(k string, v *ast.Task) error {
// We do a deep copy of the task struct here to ensure that no data can
// be changed elsewhere once the taskfile is merged.
task := v.DeepCopy()

View File

@@ -1,4 +1,4 @@
package read
package taskfile
import (
"context"
@@ -31,7 +31,7 @@ func NewNode(
node, err = NewFileNode(uri, opts...)
}
if node.Remote() && !experiments.RemoteTaskfiles {
return nil, errors.New("task: Remote taskfiles are not enabled. You can read more about this experiment and how to enable it at https://taskfile.dev/experiments/remote-taskfiles")
return nil, errors.New("task: Remote taskfiles are not enabled. You can read more about this experiment and how to enable it at https://ast.dev/experiments/remote-taskfiles")
}
return node, err
}

View File

@@ -1,4 +1,4 @@
package read
package taskfile
type (
NodeOption func(*BaseNode)

View File

@@ -1,4 +1,4 @@
package read
package taskfile
import (
"context"

View File

@@ -1,4 +1,4 @@
package read
package taskfile
import (
"context"

View File

@@ -1,4 +1,4 @@
package read
package taskfile
import (
"context"
@@ -14,7 +14,7 @@ import (
"github.com/go-task/task/v3/internal/logger"
"github.com/go-task/task/v3/internal/sysinfo"
"github.com/go-task/task/v3/internal/templater"
"github.com/go-task/task/v3/taskfile"
"github.com/go-task/task/v3/taskfile/ast"
)
var (
@@ -40,7 +40,7 @@ func readTaskfile(
timeout time.Duration,
tempDir string,
l *logger.Logger,
) (*taskfile.Taskfile, error) {
) (*ast.Taskfile, error) {
var b []byte
var err error
var cache *Cache
@@ -127,7 +127,7 @@ func readTaskfile(
}
}
var t taskfile.Taskfile
var t ast.Taskfile
if err := yaml.Unmarshal(b, &t); err != nil {
return nil, &errors.TaskfileInvalidError{URI: filepathext.TryAbsToRel(node.Location()), Err: err}
}
@@ -136,10 +136,10 @@ func readTaskfile(
return &t, nil
}
// Taskfile reads a Taskfile for a given directory
// Uses current dir when dir is left empty. Uses Taskfile.yml
// or Taskfile.yaml when entrypoint is left empty
func Taskfile(
// Read reads a Read for a given directory
// Uses current dir when dir is left empty. Uses Read.yml
// or Read.yaml when entrypoint is left empty
func Read(
node Node,
insecure bool,
download bool,
@@ -147,9 +147,9 @@ func Taskfile(
timeout time.Duration,
tempDir string,
l *logger.Logger,
) (*taskfile.Taskfile, error) {
var _taskfile func(Node) (*taskfile.Taskfile, error)
_taskfile = func(node Node) (*taskfile.Taskfile, error) {
) (*ast.Taskfile, error) {
var _taskfile func(Node) (*ast.Taskfile, error)
_taskfile = func(node Node) (*ast.Taskfile, error) {
t, err := readTaskfile(node, download, offline, timeout, tempDir, l)
if err != nil {
return nil, err
@@ -162,7 +162,7 @@ func Taskfile(
// Annotate any included Taskfile reference with a base directory for resolving relative paths
if node, isFileNode := node.(*FileNode); isFileNode {
_ = t.Includes.Range(func(key string, includedFile taskfile.IncludedTaskfile) error {
_ = t.Includes.Range(func(key string, includedFile ast.IncludedTaskfile) error {
// Set the base directory for resolving relative paths, but only if not already set
if includedFile.BaseDir == "" {
includedFile.BaseDir = node.Dir
@@ -172,9 +172,9 @@ func Taskfile(
})
}
err = t.Includes.Range(func(namespace string, includedTask taskfile.IncludedTaskfile) error {
err = t.Includes.Range(func(namespace string, includedTask ast.IncludedTaskfile) error {
tr := templater.Templater{Vars: t.Vars}
includedTask = taskfile.IncludedTaskfile{
includedTask = ast.IncludedTaskfile{
Taskfile: tr.Replace(includedTask.Taskfile),
Dir: tr.Replace(includedTask.Dir),
Optional: includedTask.Optional,
@@ -227,14 +227,14 @@ func Taskfile(
}
// nolint: errcheck
includedTaskfile.Vars.Range(func(k string, v taskfile.Var) error {
includedTaskfile.Vars.Range(func(k string, v ast.Var) error {
o := v
o.Dir = dir
includedTaskfile.Vars.Set(k, o)
return nil
})
// nolint: errcheck
includedTaskfile.Env.Range(func(k string, v taskfile.Var) error {
includedTaskfile.Env.Range(func(k string, v ast.Var) error {
o := v
o.Dir = dir
includedTaskfile.Env.Set(k, o)
@@ -244,7 +244,7 @@ func Taskfile(
for _, task := range includedTaskfile.Tasks.Values() {
task.Dir = filepathext.SmartJoin(dir, task.Dir)
if task.IncludeVars == nil {
task.IncludeVars = &taskfile.Vars{}
task.IncludeVars = &ast.Vars{}
}
task.IncludeVars.Merge(includedTask.Vars)
task.IncludedTaskfileVars = includedTaskfile.Vars
@@ -252,7 +252,7 @@ func Taskfile(
}
}
if err = taskfile.Merge(t, includedTaskfile, &includedTask, namespace); err != nil {
if err = Merge(t, includedTaskfile, &includedTask, namespace); err != nil {
return err
}
@@ -273,7 +273,7 @@ func Taskfile(
for _, task := range t.Tasks.Values() {
// If the task is not defined, create a new one
if task == nil {
task = &taskfile.Task{}
task = &ast.Task{}
}
// Set the location of the taskfile for each task
if task.Location.Taskfile == "" {