1
0
mirror of https://github.com/go-task/task.git synced 2025-03-19 21:17:46 +02:00

Start support to including Taskfiles

This commit is contained in:
Andrey Nering 2018-09-09 22:29:29 -03:00
parent 687b4ec837
commit 9a5a1e2253
3 changed files with 33 additions and 2 deletions

View File

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

View File

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

View File

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