mirror of
https://github.com/go-task/task.git
synced 2025-01-30 05:39:17 +02:00
feat: store namespace directly in include
This commit is contained in:
parent
5fc66293b0
commit
d684e59b6a
@ -14,6 +14,7 @@ import (
|
|||||||
|
|
||||||
// Include represents information about included taskfiles
|
// Include represents information about included taskfiles
|
||||||
type Include struct {
|
type Include struct {
|
||||||
|
Namespace string
|
||||||
Taskfile string
|
Taskfile string
|
||||||
Dir string
|
Dir string
|
||||||
Optional bool
|
Optional bool
|
||||||
@ -45,6 +46,7 @@ func (includes *Includes) UnmarshalYAML(node *yaml.Node) error {
|
|||||||
if err := valueNode.Decode(&v); err != nil {
|
if err := valueNode.Decode(&v); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
v.Namespace = keyNode.Value
|
||||||
includes.Set(keyNode.Value, v)
|
includes.Set(keyNode.Value, v)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
@ -128,6 +130,7 @@ func (include *Include) DeepCopy() *Include {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return &Include{
|
return &Include{
|
||||||
|
Namespace: include.Namespace,
|
||||||
Taskfile: include.Taskfile,
|
Taskfile: include.Taskfile,
|
||||||
Dir: include.Dir,
|
Dir: include.Dir,
|
||||||
Optional: include.Optional,
|
Optional: include.Optional,
|
||||||
|
@ -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, include *ast.Include, namespaces ...string) error {
|
func Merge(t1, t2 *ast.Taskfile, include *ast.Include) 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)
|
||||||
}
|
}
|
||||||
@ -28,7 +28,7 @@ func Merge(t1, t2 *ast.Taskfile, include *ast.Include, namespaces ...string) err
|
|||||||
t1.Vars.Merge(t2.Vars)
|
t1.Vars.Merge(t2.Vars)
|
||||||
t1.Env.Merge(t2.Env)
|
t1.Env.Merge(t2.Env)
|
||||||
|
|
||||||
return t2.Tasks.Range(func(k string, v *ast.Task) error {
|
if err := 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
|
// We do a deep copy of the task struct here to ensure that no data can
|
||||||
// be changed elsewhere once the taskfile is merged.
|
// be changed elsewhere once the taskfile is merged.
|
||||||
task := v.DeepCopy()
|
task := v.DeepCopy()
|
||||||
@ -40,16 +40,16 @@ func Merge(t1, t2 *ast.Taskfile, include *ast.Include, namespaces ...string) err
|
|||||||
// Add namespaces to dependencies, commands and aliases
|
// Add namespaces to dependencies, commands and aliases
|
||||||
for _, dep := range task.Deps {
|
for _, dep := range task.Deps {
|
||||||
if dep != nil && dep.Task != "" {
|
if dep != nil && dep.Task != "" {
|
||||||
dep.Task = taskNameWithNamespace(dep.Task, namespaces...)
|
dep.Task = taskNameWithNamespace(dep.Task, include.Namespace)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, cmd := range task.Cmds {
|
for _, cmd := range task.Cmds {
|
||||||
if cmd != nil && cmd.Task != "" {
|
if cmd != nil && cmd.Task != "" {
|
||||||
cmd.Task = taskNameWithNamespace(cmd.Task, namespaces...)
|
cmd.Task = taskNameWithNamespace(cmd.Task, include.Namespace)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for i, alias := range task.Aliases {
|
for i, alias := range task.Aliases {
|
||||||
task.Aliases[i] = taskNameWithNamespace(alias, namespaces...)
|
task.Aliases[i] = taskNameWithNamespace(alias, include.Namespace)
|
||||||
}
|
}
|
||||||
// Add namespace aliases
|
// Add namespace aliases
|
||||||
if include != nil {
|
if include != nil {
|
||||||
@ -62,17 +62,32 @@ func Merge(t1, t2 *ast.Taskfile, include *ast.Include, namespaces ...string) err
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Add the task to the merged taskfile
|
// Add the task to the merged taskfile
|
||||||
taskNameWithNamespace := taskNameWithNamespace(k, namespaces...)
|
taskNameWithNamespace := taskNameWithNamespace(k, include.Namespace)
|
||||||
task.Task = taskNameWithNamespace
|
task.Task = taskNameWithNamespace
|
||||||
t1.Tasks.Set(taskNameWithNamespace, task)
|
t1.Tasks.Set(taskNameWithNamespace, task)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
})
|
}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the included Taskfile has a default task and the parent namespace has
|
||||||
|
// no task with a matching name, we can add an alias so that the user can
|
||||||
|
// run the included Taskfile's default task without specifying its full
|
||||||
|
// name. If the parent namespace has aliases, we add another alias for each
|
||||||
|
// of them.
|
||||||
|
if t2.Tasks.Get("default") != nil && t1.Tasks.Get(include.Namespace) == nil {
|
||||||
|
defaultTaskName := fmt.Sprintf("%s:default", include.Namespace)
|
||||||
|
t1.Tasks.Get(defaultTaskName).Aliases = append(t1.Tasks.Get(defaultTaskName).Aliases, include.Namespace)
|
||||||
|
t1.Tasks.Get(defaultTaskName).Aliases = append(t1.Tasks.Get(defaultTaskName).Aliases, include.Aliases...)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func taskNameWithNamespace(taskName string, namespaces ...string) string {
|
func taskNameWithNamespace(taskName string, namespace string) string {
|
||||||
if strings.HasPrefix(taskName, NamespaceSeparator) {
|
if strings.HasPrefix(taskName, NamespaceSeparator) {
|
||||||
return strings.TrimPrefix(taskName, NamespaceSeparator)
|
return strings.TrimPrefix(taskName, NamespaceSeparator)
|
||||||
}
|
}
|
||||||
return strings.Join(append(namespaces, taskName), NamespaceSeparator)
|
return fmt.Sprintf("%s%s%s", namespace, NamespaceSeparator, taskName)
|
||||||
}
|
}
|
||||||
|
@ -175,6 +175,7 @@ func Read(
|
|||||||
err = t.Includes.Range(func(namespace string, include ast.Include) error {
|
err = t.Includes.Range(func(namespace string, include ast.Include) error {
|
||||||
tr := templater.Templater{Vars: t.Vars}
|
tr := templater.Templater{Vars: t.Vars}
|
||||||
include = ast.Include{
|
include = ast.Include{
|
||||||
|
Namespace: include.Namespace,
|
||||||
Taskfile: tr.Replace(include.Taskfile),
|
Taskfile: tr.Replace(include.Taskfile),
|
||||||
Dir: tr.Replace(include.Dir),
|
Dir: tr.Replace(include.Dir),
|
||||||
Optional: include.Optional,
|
Optional: include.Optional,
|
||||||
@ -252,18 +253,10 @@ func Read(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = Merge(t, includedTaskfile, &include, namespace); err != nil {
|
if err = Merge(t, includedTaskfile, &include); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if includedTaskfile.Tasks.Get("default") != nil && t.Tasks.Get(namespace) == nil {
|
|
||||||
defaultTaskName := fmt.Sprintf("%s:default", namespace)
|
|
||||||
task := t.Tasks.Get(defaultTaskName)
|
|
||||||
task.Aliases = append(task.Aliases, namespace)
|
|
||||||
task.Aliases = append(task.Aliases, include.Aliases...)
|
|
||||||
t.Tasks.Set(defaultTaskName, task)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user