1
0
mirror of https://github.com/go-task/task.git synced 2025-03-23 21:29:29 +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
internal/taskfile

@ -2,10 +2,14 @@ package taskfile
import ( import (
"fmt" "fmt"
"strings"
) )
// NamespaceSeparator contains the character that separates namescapes
const NamespaceSeparator = ":"
// Merge merges the second Taskfile into the first // 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 { if t1.Version != t2.Version {
return fmt.Errorf(`Taskfiles versions should match. First is "%s" but second is "%s"`, 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 != "" { if t2.Output != "" {
t1.Output = t2.Output t1.Output = t2.Output
} }
for k, v := range t2.Includes {
t1.Includes[k] = v
}
for k, v := range t2.Vars { for k, v := range t2.Vars {
t1.Vars[k] = v t1.Vars[k] = v
} }
for k, v := range t2.Tasks { for k, v := range t2.Tasks {
t1.Tasks[k] = v t1.Tasks[taskNameWithNamespace(k, namespaces...)] = v
} }
return nil return nil
} }
func taskNameWithNamespace(taskName string, namespaces ...string) string {
return strings.Join(append(namespaces, taskName), NamespaceSeparator)
}

@ -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`) 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)) path = filepath.Join(dir, fmt.Sprintf("Taskfile_%s.yml", runtime.GOOS))
if _, err = os.Stat(path); err == nil { if _, err = os.Stat(path); err == nil {
osTaskfile, err := readTaskfile(path) osTaskfile, err := readTaskfile(path)

@ -5,6 +5,7 @@ type Taskfile struct {
Version string Version string
Expansions int Expansions int
Output string Output string
Includes map[string]string
Vars Vars Vars Vars
Tasks Tasks Tasks Tasks
} }
@ -20,6 +21,7 @@ func (tf *Taskfile) UnmarshalYAML(unmarshal func(interface{}) error) error {
Version string Version string
Expansions int Expansions int
Output string Output string
Includes map[string]string
Vars Vars Vars Vars
Tasks Tasks Tasks Tasks
} }
@ -29,6 +31,7 @@ func (tf *Taskfile) UnmarshalYAML(unmarshal func(interface{}) error) error {
tf.Version = taskfile.Version tf.Version = taskfile.Version
tf.Expansions = taskfile.Expansions tf.Expansions = taskfile.Expansions
tf.Output = taskfile.Output tf.Output = taskfile.Output
tf.Includes = taskfile.Includes
tf.Vars = taskfile.Vars tf.Vars = taskfile.Vars
tf.Tasks = taskfile.Tasks tf.Tasks = taskfile.Tasks
if tf.Expansions <= 0 { if tf.Expansions <= 0 {