mirror of
https://github.com/go-task/task.git
synced 2025-06-23 00:38:19 +02:00
refactor: IncludedTaskfiles -> Includes
This commit is contained in:
@ -12,8 +12,8 @@ import (
|
|||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
// IncludedTaskfile represents information about included taskfiles
|
// Include represents information about included taskfiles
|
||||||
type IncludedTaskfile struct {
|
type Include struct {
|
||||||
Taskfile string
|
Taskfile string
|
||||||
Dir string
|
Dir string
|
||||||
Optional bool
|
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
|
BaseDir string // The directory from which the including taskfile was loaded; used to resolve relative paths
|
||||||
}
|
}
|
||||||
|
|
||||||
// IncludedTaskfiles represents information about included tasksfiles
|
// Includes represents information about included tasksfiles
|
||||||
type IncludedTaskfiles struct {
|
type Includes struct {
|
||||||
Keys []string
|
Keys []string
|
||||||
Mapping map[string]IncludedTaskfile
|
Mapping map[string]Include
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnmarshalYAML implements the yaml.Unmarshaler interface.
|
// 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 {
|
switch node.Kind {
|
||||||
case yaml.MappingNode:
|
case yaml.MappingNode:
|
||||||
// NOTE(@andreynering): on this style of custom unmarshalling,
|
// NOTE(@andreynering): on this style of custom unmarshalling,
|
||||||
@ -41,11 +41,11 @@ func (tfs *IncludedTaskfiles) UnmarshalYAML(node *yaml.Node) error {
|
|||||||
keyNode := node.Content[i]
|
keyNode := node.Content[i]
|
||||||
valueNode := node.Content[i+1]
|
valueNode := node.Content[i+1]
|
||||||
|
|
||||||
var v IncludedTaskfile
|
var v Include
|
||||||
if err := valueNode.Decode(&v); err != nil {
|
if err := valueNode.Decode(&v); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
tfs.Set(keyNode.Value, v)
|
includes.Set(keyNode.Value, v)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -54,38 +54,38 @@ func (tfs *IncludedTaskfiles) UnmarshalYAML(node *yaml.Node) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Len returns the length of the map
|
// Len returns the length of the map
|
||||||
func (tfs *IncludedTaskfiles) Len() int {
|
func (includes *Includes) Len() int {
|
||||||
if tfs == nil {
|
if includes == nil {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
return len(tfs.Keys)
|
return len(includes.Keys)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set sets a value to a given key
|
// Set sets a value to a given key
|
||||||
func (tfs *IncludedTaskfiles) Set(key string, includedTaskfile IncludedTaskfile) {
|
func (includes *Includes) Set(namespace string, include Include) {
|
||||||
if tfs.Mapping == nil {
|
if includes.Mapping == nil {
|
||||||
tfs.Mapping = make(map[string]IncludedTaskfile, 1)
|
includes.Mapping = make(map[string]Include, 1)
|
||||||
}
|
}
|
||||||
if !slices.Contains(tfs.Keys, key) {
|
if !slices.Contains(includes.Keys, namespace) {
|
||||||
tfs.Keys = append(tfs.Keys, key)
|
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
|
// 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 {
|
func (includes *Includes) Range(yield func(namespace string, include Include) error) error {
|
||||||
if tfs == nil {
|
if includes == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
for _, k := range tfs.Keys {
|
for _, k := range includes.Keys {
|
||||||
if err := yield(k, tfs.Mapping[k]); err != nil {
|
if err := yield(k, includes.Mapping[k]); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (it *IncludedTaskfile) UnmarshalYAML(node *yaml.Node) error {
|
func (include *Include) UnmarshalYAML(node *yaml.Node) error {
|
||||||
switch node.Kind {
|
switch node.Kind {
|
||||||
|
|
||||||
case yaml.ScalarNode:
|
case yaml.ScalarNode:
|
||||||
@ -93,7 +93,7 @@ func (it *IncludedTaskfile) UnmarshalYAML(node *yaml.Node) error {
|
|||||||
if err := node.Decode(&str); err != nil {
|
if err := node.Decode(&str); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
it.Taskfile = str
|
include.Taskfile = str
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
case yaml.MappingNode:
|
case yaml.MappingNode:
|
||||||
@ -108,13 +108,13 @@ func (it *IncludedTaskfile) UnmarshalYAML(node *yaml.Node) error {
|
|||||||
if err := node.Decode(&includedTaskfile); err != nil {
|
if err := node.Decode(&includedTaskfile); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
it.Taskfile = includedTaskfile.Taskfile
|
include.Taskfile = includedTaskfile.Taskfile
|
||||||
it.Dir = includedTaskfile.Dir
|
include.Dir = includedTaskfile.Dir
|
||||||
it.Optional = includedTaskfile.Optional
|
include.Optional = includedTaskfile.Optional
|
||||||
it.Internal = includedTaskfile.Internal
|
include.Internal = includedTaskfile.Internal
|
||||||
it.Aliases = includedTaskfile.Aliases
|
include.Aliases = includedTaskfile.Aliases
|
||||||
it.AdvancedImport = true
|
include.AdvancedImport = true
|
||||||
it.Vars = includedTaskfile.Vars
|
include.Vars = includedTaskfile.Vars
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -123,34 +123,34 @@ func (it *IncludedTaskfile) UnmarshalYAML(node *yaml.Node) error {
|
|||||||
|
|
||||||
// DeepCopy creates a new instance of IncludedTaskfile and copies
|
// DeepCopy creates a new instance of IncludedTaskfile and copies
|
||||||
// data by value from the source struct.
|
// data by value from the source struct.
|
||||||
func (it *IncludedTaskfile) DeepCopy() *IncludedTaskfile {
|
func (include *Include) DeepCopy() *Include {
|
||||||
if it == nil {
|
if include == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return &IncludedTaskfile{
|
return &Include{
|
||||||
Taskfile: it.Taskfile,
|
Taskfile: include.Taskfile,
|
||||||
Dir: it.Dir,
|
Dir: include.Dir,
|
||||||
Optional: it.Optional,
|
Optional: include.Optional,
|
||||||
Internal: it.Internal,
|
Internal: include.Internal,
|
||||||
AdvancedImport: it.AdvancedImport,
|
AdvancedImport: include.AdvancedImport,
|
||||||
Vars: it.Vars.DeepCopy(),
|
Vars: include.Vars.DeepCopy(),
|
||||||
BaseDir: it.BaseDir,
|
BaseDir: include.BaseDir,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// FullTaskfilePath returns the fully qualified path to the included taskfile
|
// FullTaskfilePath returns the fully qualified path to the included taskfile
|
||||||
func (it *IncludedTaskfile) FullTaskfilePath() (string, error) {
|
func (include *Include) FullTaskfilePath() (string, error) {
|
||||||
return it.resolvePath(it.Taskfile)
|
return include.resolvePath(include.Taskfile)
|
||||||
}
|
}
|
||||||
|
|
||||||
// FullDirPath returns the fully qualified path to the included taskfile's working directory
|
// FullDirPath returns the fully qualified path to the included taskfile's working directory
|
||||||
func (it *IncludedTaskfile) FullDirPath() (string, error) {
|
func (include *Include) FullDirPath() (string, error) {
|
||||||
return it.resolvePath(it.Dir)
|
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 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
|
return path, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -163,9 +163,9 @@ func (it *IncludedTaskfile) resolvePath(path string) (string, error) {
|
|||||||
return path, nil
|
return path, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
result, err := filepath.Abs(filepathext.SmartJoin(it.BaseDir, path))
|
result, err := filepath.Abs(filepathext.SmartJoin(include.BaseDir, path))
|
||||||
if err != nil {
|
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
|
return result, nil
|
@ -38,7 +38,7 @@ type Task struct {
|
|||||||
Run string
|
Run string
|
||||||
IncludeVars *Vars
|
IncludeVars *Vars
|
||||||
IncludedTaskfileVars *Vars
|
IncludedTaskfileVars *Vars
|
||||||
IncludedTaskfile *IncludedTaskfile
|
IncludedTaskfile *Include
|
||||||
Platforms []*Platform
|
Platforms []*Platform
|
||||||
Location *Location
|
Location *Location
|
||||||
Watch bool
|
Watch bool
|
||||||
|
@ -18,7 +18,7 @@ type Taskfile struct {
|
|||||||
Version *semver.Version
|
Version *semver.Version
|
||||||
Output Output
|
Output Output
|
||||||
Method string
|
Method string
|
||||||
Includes *IncludedTaskfiles
|
Includes *Includes
|
||||||
Set []string
|
Set []string
|
||||||
Shopt []string
|
Shopt []string
|
||||||
Vars *Vars
|
Vars *Vars
|
||||||
@ -37,7 +37,7 @@ func (tf *Taskfile) UnmarshalYAML(node *yaml.Node) error {
|
|||||||
Version *semver.Version
|
Version *semver.Version
|
||||||
Output Output
|
Output Output
|
||||||
Method string
|
Method string
|
||||||
Includes *IncludedTaskfiles
|
Includes *Includes
|
||||||
Set []string
|
Set []string
|
||||||
Shopt []string
|
Shopt []string
|
||||||
Vars *Vars
|
Vars *Vars
|
||||||
|
@ -11,7 +11,7 @@ import (
|
|||||||
const NamespaceSeparator = ":"
|
const NamespaceSeparator = ":"
|
||||||
|
|
||||||
// Merge merges the second Taskfile into the first
|
// 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) {
|
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)
|
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
|
// Set the task to internal if EITHER the included task or the included
|
||||||
// taskfile are marked as internal
|
// 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
|
// Add namespaces to dependencies, commands and aliases
|
||||||
for _, dep := range task.Deps {
|
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...)
|
task.Aliases[i] = taskNameWithNamespace(alias, namespaces...)
|
||||||
}
|
}
|
||||||
// Add namespace aliases
|
// Add namespace aliases
|
||||||
if includedTaskfile != nil {
|
if include != nil {
|
||||||
for _, namespaceAlias := range includedTaskfile.Aliases {
|
for _, namespaceAlias := range include.Aliases {
|
||||||
task.Aliases = append(task.Aliases, taskNameWithNamespace(task.Task, namespaceAlias))
|
task.Aliases = append(task.Aliases, taskNameWithNamespace(task.Task, namespaceAlias))
|
||||||
for _, alias := range v.Aliases {
|
for _, alias := range v.Aliases {
|
||||||
task.Aliases = append(task.Aliases, taskNameWithNamespace(alias, namespaceAlias))
|
task.Aliases = append(task.Aliases, taskNameWithNamespace(alias, namespaceAlias))
|
||||||
|
@ -162,43 +162,43 @@ func Read(
|
|||||||
|
|
||||||
// Annotate any included Taskfile reference with a base directory for resolving relative paths
|
// Annotate any included Taskfile reference with a base directory for resolving relative paths
|
||||||
if node, isFileNode := node.(*FileNode); isFileNode {
|
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
|
// Set the base directory for resolving relative paths, but only if not already set
|
||||||
if includedFile.BaseDir == "" {
|
if include.BaseDir == "" {
|
||||||
includedFile.BaseDir = node.Dir
|
include.BaseDir = node.Dir
|
||||||
t.Includes.Set(key, includedFile)
|
t.Includes.Set(namespace, include)
|
||||||
}
|
}
|
||||||
return nil
|
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}
|
tr := templater.Templater{Vars: t.Vars}
|
||||||
includedTask = ast.IncludedTaskfile{
|
include = ast.Include{
|
||||||
Taskfile: tr.Replace(includedTask.Taskfile),
|
Taskfile: tr.Replace(include.Taskfile),
|
||||||
Dir: tr.Replace(includedTask.Dir),
|
Dir: tr.Replace(include.Dir),
|
||||||
Optional: includedTask.Optional,
|
Optional: include.Optional,
|
||||||
Internal: includedTask.Internal,
|
Internal: include.Internal,
|
||||||
Aliases: includedTask.Aliases,
|
Aliases: include.Aliases,
|
||||||
AdvancedImport: includedTask.AdvancedImport,
|
AdvancedImport: include.AdvancedImport,
|
||||||
Vars: includedTask.Vars,
|
Vars: include.Vars,
|
||||||
BaseDir: includedTask.BaseDir,
|
BaseDir: include.BaseDir,
|
||||||
}
|
}
|
||||||
if err := tr.Err(); err != nil {
|
if err := tr.Err(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
uri, err := includedTask.FullTaskfilePath()
|
uri, err := include.FullTaskfilePath()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
includeReaderNode, err := NewNode(uri, insecure,
|
includeReaderNode, err := NewNode(uri, insecure,
|
||||||
WithParent(node),
|
WithParent(node),
|
||||||
WithOptional(includedTask.Optional),
|
WithOptional(include.Optional),
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if includedTask.Optional {
|
if include.Optional {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
@ -210,7 +210,7 @@ func Read(
|
|||||||
|
|
||||||
includedTaskfile, err := _taskfile(includeReaderNode)
|
includedTaskfile, err := _taskfile(includeReaderNode)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if includedTask.Optional {
|
if include.Optional {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
@ -220,8 +220,8 @@ func Read(
|
|||||||
return ErrIncludedTaskfilesCantHaveDotenvs
|
return ErrIncludedTaskfilesCantHaveDotenvs
|
||||||
}
|
}
|
||||||
|
|
||||||
if includedTask.AdvancedImport {
|
if include.AdvancedImport {
|
||||||
dir, err := includedTask.FullDirPath()
|
dir, err := include.FullDirPath()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -246,13 +246,13 @@ func Read(
|
|||||||
if task.IncludeVars == nil {
|
if task.IncludeVars == nil {
|
||||||
task.IncludeVars = &ast.Vars{}
|
task.IncludeVars = &ast.Vars{}
|
||||||
}
|
}
|
||||||
task.IncludeVars.Merge(includedTask.Vars)
|
task.IncludeVars.Merge(include.Vars)
|
||||||
task.IncludedTaskfileVars = includedTaskfile.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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -260,7 +260,7 @@ func Read(
|
|||||||
defaultTaskName := fmt.Sprintf("%s:default", namespace)
|
defaultTaskName := fmt.Sprintf("%s:default", namespace)
|
||||||
task := t.Tasks.Get(defaultTaskName)
|
task := t.Tasks.Get(defaultTaskName)
|
||||||
task.Aliases = append(task.Aliases, namespace)
|
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)
|
t.Tasks.Set(defaultTaskName, task)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user