From bb79fa1dc37a58ac3c48fe1a470d6347c1045c8a Mon Sep 17 00:00:00 2001 From: Pete Davison Date: Sun, 2 Oct 2022 05:07:58 +0000 Subject: [PATCH] feat: namespace aliases --- taskfile/included_taskfile.go | 3 +++ taskfile/merge.go | 31 ++++++++++++++++++++++++------- taskfile/read/taskfile.go | 5 +++-- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/taskfile/included_taskfile.go b/taskfile/included_taskfile.go index fe83bd7d..d24bb4e9 100644 --- a/taskfile/included_taskfile.go +++ b/taskfile/included_taskfile.go @@ -17,6 +17,7 @@ type IncludedTaskfile struct { Dir string Optional bool Internal bool + Aliases []string AdvancedImport bool Vars *Vars BaseDir string // The directory from which the including taskfile was loaded; used to resolve relative paths @@ -103,6 +104,7 @@ func (it *IncludedTaskfile) UnmarshalYAML(unmarshal func(interface{}) error) err Dir string Optional bool Internal bool + Aliases []string Vars *Vars } if err := unmarshal(&includedTaskfile); err != nil { @@ -112,6 +114,7 @@ func (it *IncludedTaskfile) UnmarshalYAML(unmarshal func(interface{}) error) err it.Dir = includedTaskfile.Dir it.Optional = includedTaskfile.Optional it.Internal = includedTaskfile.Internal + it.Aliases = includedTaskfile.Aliases it.AdvancedImport = true it.Vars = includedTaskfile.Vars return nil diff --git a/taskfile/merge.go b/taskfile/merge.go index ecf58e41..6454e8c7 100644 --- a/taskfile/merge.go +++ b/taskfile/merge.go @@ -9,7 +9,7 @@ import ( const NamespaceSeparator = ":" // Merge merges the second Taskfile into the first -func Merge(t1, t2 *Taskfile, internal bool, namespaces ...string) error { +func Merge(t1, t2 *Taskfile, includedTaskfile *IncludedTaskfile, namespaces ...string) error { if t1.Version != t2.Version { return fmt.Errorf(`task: Taskfiles versions should match. First is "%s" but second is "%s"`, t1.Version, t2.Version) } @@ -42,22 +42,39 @@ func Merge(t1, t2 *Taskfile, internal bool, namespaces ...string) error { // FIXME(@andreynering): Refactor this block, otherwise we can // have serious side-effects in the future, since we're editing // the original references instead of deep copying them. + task := v - v.Internal = v.Internal || internal + // Set the task to internal if EITHER the included task or the included + // taskfile are marked as internal + task.Internal = task.Internal || includedTaskfile.Internal - t1.Tasks[taskNameWithNamespace(k, namespaces...)] = v + // Deep copy the aliases so we can use them later + origAliases := make([]string, len(task.Aliases)) + copy(origAliases, task.Aliases) - for _, dep := range v.Deps { + // Add namespaces to dependencies, commands and aliases + for _, dep := range task.Deps { dep.Task = taskNameWithNamespace(dep.Task, namespaces...) } - for _, cmd := range v.Cmds { + for _, cmd := range task.Cmds { if cmd != nil && cmd.Task != "" { cmd.Task = taskNameWithNamespace(cmd.Task, namespaces...) } } - for i, alias := range v.Aliases { - v.Aliases[i] = taskNameWithNamespace(alias, namespaces...) + for i, alias := range task.Aliases { + task.Aliases[i] = taskNameWithNamespace(alias, namespaces...) } + // Add namespace aliases + if includedTaskfile != nil { + for _, namespaceAlias := range includedTaskfile.Aliases { + for _, alias := range origAliases { + task.Aliases = append(v.Aliases, taskNameWithNamespace(alias, namespaceAlias)) + } + } + } + + // Add the task to the merged taskfile + t1.Tasks[taskNameWithNamespace(k, namespaces...)] = task } return nil diff --git a/taskfile/read/taskfile.go b/taskfile/read/taskfile.go index 220b66c4..7c6f8685 100644 --- a/taskfile/read/taskfile.go +++ b/taskfile/read/taskfile.go @@ -79,6 +79,7 @@ func Taskfile(readerNode *ReaderNode) (*taskfile.Taskfile, error) { Dir: tr.Replace(includedTask.Dir), Optional: includedTask.Optional, Internal: includedTask.Internal, + Aliases: includedTask.Aliases, AdvancedImport: includedTask.AdvancedImport, Vars: includedTask.Vars, BaseDir: includedTask.BaseDir, @@ -149,7 +150,7 @@ func Taskfile(readerNode *ReaderNode) (*taskfile.Taskfile, error) { } } - if err = taskfile.Merge(t, includedTaskfile, includedTask.Internal, namespace); err != nil { + if err = taskfile.Merge(t, includedTaskfile, &includedTask, namespace); err != nil { return err } return nil @@ -165,7 +166,7 @@ func Taskfile(readerNode *ReaderNode) (*taskfile.Taskfile, error) { if err != nil { return nil, err } - if err = taskfile.Merge(t, osTaskfile, false); err != nil { + if err = taskfile.Merge(t, osTaskfile, nil); err != nil { return nil, err } }