diff --git a/taskfile/ast/included_taskfile.go b/taskfile/ast/include.go similarity index 54% rename from taskfile/ast/included_taskfile.go rename to taskfile/ast/include.go index aea7d6ef..97d3ffef 100644 --- a/taskfile/ast/included_taskfile.go +++ b/taskfile/ast/include.go @@ -12,8 +12,8 @@ import ( "gopkg.in/yaml.v3" ) -// IncludedTaskfile represents information about included taskfiles -type IncludedTaskfile struct { +// Include represents information about included taskfiles +type Include struct { Taskfile string Dir string Optional bool @@ -24,14 +24,14 @@ type IncludedTaskfile struct { BaseDir string // The directory from which the including taskfile was loaded; used to resolve relative paths } -// IncludedTaskfiles represents information about included tasksfiles -type IncludedTaskfiles struct { +// Includes represents information about included tasksfiles +type Includes struct { Keys []string - Mapping map[string]IncludedTaskfile + Mapping map[string]Include } // UnmarshalYAML implements the yaml.Unmarshaler interface. -func (tfs *IncludedTaskfiles) UnmarshalYAML(node *yaml.Node) error { +func (includes *Includes) UnmarshalYAML(node *yaml.Node) error { switch node.Kind { case yaml.MappingNode: // NOTE(@andreynering): on this style of custom unmarshalling, @@ -41,11 +41,11 @@ func (tfs *IncludedTaskfiles) UnmarshalYAML(node *yaml.Node) error { keyNode := node.Content[i] valueNode := node.Content[i+1] - var v IncludedTaskfile + var v Include if err := valueNode.Decode(&v); err != nil { return err } - tfs.Set(keyNode.Value, v) + includes.Set(keyNode.Value, v) } return nil } @@ -54,38 +54,38 @@ func (tfs *IncludedTaskfiles) UnmarshalYAML(node *yaml.Node) error { } // Len returns the length of the map -func (tfs *IncludedTaskfiles) Len() int { - if tfs == nil { +func (includes *Includes) Len() int { + if includes == nil { return 0 } - return len(tfs.Keys) + return len(includes.Keys) } // Set sets a value to a given key -func (tfs *IncludedTaskfiles) Set(key string, includedTaskfile IncludedTaskfile) { - if tfs.Mapping == nil { - tfs.Mapping = make(map[string]IncludedTaskfile, 1) +func (includes *Includes) Set(namespace string, include Include) { + if includes.Mapping == nil { + includes.Mapping = make(map[string]Include, 1) } - if !slices.Contains(tfs.Keys, key) { - tfs.Keys = append(tfs.Keys, key) + if !slices.Contains(includes.Keys, namespace) { + includes.Keys = append(includes.Keys, namespace) } - tfs.Mapping[key] = includedTaskfile + includes.Mapping[namespace] = include } // Range allows you to loop into the included taskfiles in its right order -func (tfs *IncludedTaskfiles) Range(yield func(key string, includedTaskfile IncludedTaskfile) error) error { - if tfs == nil { +func (includes *Includes) Range(yield func(namespace string, include Include) error) error { + if includes == nil { return nil } - for _, k := range tfs.Keys { - if err := yield(k, tfs.Mapping[k]); err != nil { + for _, k := range includes.Keys { + if err := yield(k, includes.Mapping[k]); err != nil { return err } } return nil } -func (it *IncludedTaskfile) UnmarshalYAML(node *yaml.Node) error { +func (include *Include) UnmarshalYAML(node *yaml.Node) error { switch node.Kind { case yaml.ScalarNode: @@ -93,7 +93,7 @@ func (it *IncludedTaskfile) UnmarshalYAML(node *yaml.Node) error { if err := node.Decode(&str); err != nil { return err } - it.Taskfile = str + include.Taskfile = str return nil case yaml.MappingNode: @@ -108,13 +108,13 @@ func (it *IncludedTaskfile) UnmarshalYAML(node *yaml.Node) error { if err := node.Decode(&includedTaskfile); err != nil { return err } - it.Taskfile = includedTaskfile.Taskfile - it.Dir = includedTaskfile.Dir - it.Optional = includedTaskfile.Optional - it.Internal = includedTaskfile.Internal - it.Aliases = includedTaskfile.Aliases - it.AdvancedImport = true - it.Vars = includedTaskfile.Vars + include.Taskfile = includedTaskfile.Taskfile + include.Dir = includedTaskfile.Dir + include.Optional = includedTaskfile.Optional + include.Internal = includedTaskfile.Internal + include.Aliases = includedTaskfile.Aliases + include.AdvancedImport = true + include.Vars = includedTaskfile.Vars return nil } @@ -123,34 +123,34 @@ func (it *IncludedTaskfile) UnmarshalYAML(node *yaml.Node) error { // DeepCopy creates a new instance of IncludedTaskfile and copies // data by value from the source struct. -func (it *IncludedTaskfile) DeepCopy() *IncludedTaskfile { - if it == nil { +func (include *Include) DeepCopy() *Include { + if include == nil { return nil } - return &IncludedTaskfile{ - Taskfile: it.Taskfile, - Dir: it.Dir, - Optional: it.Optional, - Internal: it.Internal, - AdvancedImport: it.AdvancedImport, - Vars: it.Vars.DeepCopy(), - BaseDir: it.BaseDir, + return &Include{ + Taskfile: include.Taskfile, + Dir: include.Dir, + Optional: include.Optional, + Internal: include.Internal, + AdvancedImport: include.AdvancedImport, + Vars: include.Vars.DeepCopy(), + BaseDir: include.BaseDir, } } // FullTaskfilePath returns the fully qualified path to the included taskfile -func (it *IncludedTaskfile) FullTaskfilePath() (string, error) { - return it.resolvePath(it.Taskfile) +func (include *Include) FullTaskfilePath() (string, error) { + return include.resolvePath(include.Taskfile) } // FullDirPath returns the fully qualified path to the included taskfile's working directory -func (it *IncludedTaskfile) FullDirPath() (string, error) { - return it.resolvePath(it.Dir) +func (include *Include) FullDirPath() (string, error) { + return include.resolvePath(include.Dir) } -func (it *IncludedTaskfile) resolvePath(path string) (string, error) { +func (include *Include) resolvePath(path string) (string, error) { // If the file is remote, we don't need to resolve the path - if strings.Contains(it.Taskfile, "://") { + if strings.Contains(include.Taskfile, "://") { return path, nil } @@ -163,9 +163,9 @@ func (it *IncludedTaskfile) resolvePath(path string) (string, error) { return path, nil } - result, err := filepath.Abs(filepathext.SmartJoin(it.BaseDir, path)) + result, err := filepath.Abs(filepathext.SmartJoin(include.BaseDir, path)) if err != nil { - return "", fmt.Errorf("task: error resolving path %s relative to %s: %w", path, it.BaseDir, err) + return "", fmt.Errorf("task: error resolving path %s relative to %s: %w", path, include.BaseDir, err) } return result, nil diff --git a/taskfile/ast/task.go b/taskfile/ast/task.go index 1a289337..4e28625f 100644 --- a/taskfile/ast/task.go +++ b/taskfile/ast/task.go @@ -38,7 +38,7 @@ type Task struct { Run string IncludeVars *Vars IncludedTaskfileVars *Vars - IncludedTaskfile *IncludedTaskfile + IncludedTaskfile *Include Platforms []*Platform Location *Location Watch bool diff --git a/taskfile/ast/taskfile.go b/taskfile/ast/taskfile.go index 7595b2c8..1e62c7ad 100644 --- a/taskfile/ast/taskfile.go +++ b/taskfile/ast/taskfile.go @@ -18,7 +18,7 @@ type Taskfile struct { Version *semver.Version Output Output Method string - Includes *IncludedTaskfiles + Includes *Includes Set []string Shopt []string Vars *Vars @@ -37,7 +37,7 @@ func (tf *Taskfile) UnmarshalYAML(node *yaml.Node) error { Version *semver.Version Output Output Method string - Includes *IncludedTaskfiles + Includes *Includes Set []string Shopt []string Vars *Vars diff --git a/taskfile/merge.go b/taskfile/merge.go index 3836fc22..658a0e7f 100644 --- a/taskfile/merge.go +++ b/taskfile/merge.go @@ -11,7 +11,7 @@ import ( const NamespaceSeparator = ":" // Merge merges the second Taskfile into the first -func Merge(t1, t2 *ast.Taskfile, includedTaskfile *ast.IncludedTaskfile, namespaces ...string) error { +func Merge(t1, t2 *ast.Taskfile, include *ast.Include, 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) } @@ -35,7 +35,7 @@ func Merge(t1, t2 *ast.Taskfile, includedTaskfile *ast.IncludedTaskfile, namespa // Set the task to internal if EITHER the included task or the included // taskfile are marked as internal - task.Internal = task.Internal || (includedTaskfile != nil && includedTaskfile.Internal) + task.Internal = task.Internal || (include != nil && include.Internal) // Add namespaces to dependencies, commands and aliases for _, dep := range task.Deps { @@ -52,8 +52,8 @@ func Merge(t1, t2 *ast.Taskfile, includedTaskfile *ast.IncludedTaskfile, namespa task.Aliases[i] = taskNameWithNamespace(alias, namespaces...) } // Add namespace aliases - if includedTaskfile != nil { - for _, namespaceAlias := range includedTaskfile.Aliases { + if include != nil { + for _, namespaceAlias := range include.Aliases { task.Aliases = append(task.Aliases, taskNameWithNamespace(task.Task, namespaceAlias)) for _, alias := range v.Aliases { task.Aliases = append(task.Aliases, taskNameWithNamespace(alias, namespaceAlias)) diff --git a/taskfile/read.go b/taskfile/read.go index 44260c95..d010131b 100644 --- a/taskfile/read.go +++ b/taskfile/read.go @@ -162,43 +162,43 @@ func Read( // 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 ast.IncludedTaskfile) error { + _ = t.Includes.Range(func(namespace string, include ast.Include) error { // Set the base directory for resolving relative paths, but only if not already set - if includedFile.BaseDir == "" { - includedFile.BaseDir = node.Dir - t.Includes.Set(key, includedFile) + if include.BaseDir == "" { + include.BaseDir = node.Dir + t.Includes.Set(namespace, include) } return nil }) } - err = t.Includes.Range(func(namespace string, includedTask ast.IncludedTaskfile) error { + err = t.Includes.Range(func(namespace string, include ast.Include) error { tr := templater.Templater{Vars: t.Vars} - includedTask = ast.IncludedTaskfile{ - Taskfile: tr.Replace(includedTask.Taskfile), - Dir: tr.Replace(includedTask.Dir), - Optional: includedTask.Optional, - Internal: includedTask.Internal, - Aliases: includedTask.Aliases, - AdvancedImport: includedTask.AdvancedImport, - Vars: includedTask.Vars, - BaseDir: includedTask.BaseDir, + include = ast.Include{ + Taskfile: tr.Replace(include.Taskfile), + Dir: tr.Replace(include.Dir), + Optional: include.Optional, + Internal: include.Internal, + Aliases: include.Aliases, + AdvancedImport: include.AdvancedImport, + Vars: include.Vars, + BaseDir: include.BaseDir, } if err := tr.Err(); err != nil { return err } - uri, err := includedTask.FullTaskfilePath() + uri, err := include.FullTaskfilePath() if err != nil { return err } includeReaderNode, err := NewNode(uri, insecure, WithParent(node), - WithOptional(includedTask.Optional), + WithOptional(include.Optional), ) if err != nil { - if includedTask.Optional { + if include.Optional { return nil } return err @@ -210,7 +210,7 @@ func Read( includedTaskfile, err := _taskfile(includeReaderNode) if err != nil { - if includedTask.Optional { + if include.Optional { return nil } return err @@ -220,8 +220,8 @@ func Read( return ErrIncludedTaskfilesCantHaveDotenvs } - if includedTask.AdvancedImport { - dir, err := includedTask.FullDirPath() + if include.AdvancedImport { + dir, err := include.FullDirPath() if err != nil { return err } @@ -246,13 +246,13 @@ func Read( if task.IncludeVars == nil { task.IncludeVars = &ast.Vars{} } - task.IncludeVars.Merge(includedTask.Vars) + task.IncludeVars.Merge(include.Vars) task.IncludedTaskfileVars = includedTaskfile.Vars - task.IncludedTaskfile = &includedTask + task.IncludedTaskfile = &include } } - if err = Merge(t, includedTaskfile, &includedTask, namespace); err != nil { + if err = Merge(t, includedTaskfile, &include, namespace); err != nil { return err } @@ -260,7 +260,7 @@ func Read( defaultTaskName := fmt.Sprintf("%s:default", namespace) task := t.Tasks.Get(defaultTaskName) task.Aliases = append(task.Aliases, namespace) - task.Aliases = append(task.Aliases, includedTask.Aliases...) + task.Aliases = append(task.Aliases, include.Aliases...) t.Tasks.Set(defaultTaskName, task) }