1
0
mirror of https://github.com/go-task/task.git synced 2025-01-26 05:27:15 +02:00

feat: namespace aliases

This commit is contained in:
Pete Davison 2022-10-02 05:07:58 +00:00
parent 376a6182eb
commit bb79fa1dc3
3 changed files with 30 additions and 9 deletions

View File

@ -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

View File

@ -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

View File

@ -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
}
}