diff --git a/internal/taskfile/merge.go b/internal/taskfile/merge.go index 4825cb1d..19a4fec7 100644 --- a/internal/taskfile/merge.go +++ b/internal/taskfile/merge.go @@ -2,10 +2,14 @@ package taskfile import ( "fmt" + "strings" ) +// NamespaceSeparator contains the character that separates namescapes +const NamespaceSeparator = ":" + // Merge merges the second Taskfile into the first -func Merge(t1, t2 *Taskfile) error { +func Merge(t1, t2 *Taskfile, namespaces ...string) error { if t1.Version != t2.Version { return fmt.Errorf(`Taskfiles versions should match. First is "%s" but second is "%s"`, t1.Version, t2.Version) } @@ -16,12 +20,19 @@ func Merge(t1, t2 *Taskfile) error { if t2.Output != "" { t1.Output = t2.Output } + for k, v := range t2.Includes { + t1.Includes[k] = v + } for k, v := range t2.Vars { t1.Vars[k] = v } for k, v := range t2.Tasks { - t1.Tasks[k] = v + t1.Tasks[taskNameWithNamespace(k, namespaces...)] = v } return nil } + +func taskNameWithNamespace(taskName string, namespaces ...string) string { + return strings.Join(append(namespaces, taskName), NamespaceSeparator) +} diff --git a/internal/taskfile/read/taskfile.go b/internal/taskfile/read/taskfile.go index 0c36380d..e9c08960 100644 --- a/internal/taskfile/read/taskfile.go +++ b/internal/taskfile/read/taskfile.go @@ -19,6 +19,23 @@ func Taskfile(dir string) (*taskfile.Taskfile, error) { return nil, fmt.Errorf(`No Taskfile.yml found. Use "task --init" to create a new one`) } + for namespace, path := range t.Includes { + info, err := os.Stat(path) + if err != nil { + return nil, err + } + if info.IsDir() { + path = filepath.Join(path, "Taskfile.yml") + } + includedTaskfile, err := readTaskfile(path) + if err != nil { + return nil, err + } + if err = taskfile.Merge(t, includedTaskfile, namespace); err != nil { + return nil, err + } + } + path = filepath.Join(dir, fmt.Sprintf("Taskfile_%s.yml", runtime.GOOS)) if _, err = os.Stat(path); err == nil { osTaskfile, err := readTaskfile(path) diff --git a/internal/taskfile/taskfile.go b/internal/taskfile/taskfile.go index 2ac0cf50..58311aab 100644 --- a/internal/taskfile/taskfile.go +++ b/internal/taskfile/taskfile.go @@ -5,6 +5,7 @@ type Taskfile struct { Version string Expansions int Output string + Includes map[string]string Vars Vars Tasks Tasks } @@ -20,6 +21,7 @@ func (tf *Taskfile) UnmarshalYAML(unmarshal func(interface{}) error) error { Version string Expansions int Output string + Includes map[string]string Vars Vars Tasks Tasks } @@ -29,6 +31,7 @@ func (tf *Taskfile) UnmarshalYAML(unmarshal func(interface{}) error) error { tf.Version = taskfile.Version tf.Expansions = taskfile.Expansions tf.Output = taskfile.Output + tf.Includes = taskfile.Includes tf.Vars = taskfile.Vars tf.Tasks = taskfile.Tasks if tf.Expansions <= 0 {