2018-07-22 21:05:47 +02:00
|
|
|
package read
|
|
|
|
|
|
|
|
import (
|
2018-10-13 22:52:09 +02:00
|
|
|
"errors"
|
2018-07-22 21:05:47 +02:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
|
2018-11-05 01:23:35 +02:00
|
|
|
"github.com/go-task/task/v2/internal/taskfile"
|
2018-07-22 21:05:47 +02:00
|
|
|
|
2019-05-11 16:22:13 +02:00
|
|
|
"gopkg.in/yaml.v2"
|
2018-07-22 21:05:47 +02:00
|
|
|
)
|
|
|
|
|
2019-01-21 11:56:14 +02:00
|
|
|
var (
|
|
|
|
// ErrIncludedTaskfilesCantHaveIncludes is returned when a included Taskfile contains includes
|
|
|
|
ErrIncludedTaskfilesCantHaveIncludes = errors.New("task: Included Taskfiles can't have includes. Please, move the include to the main Taskfile")
|
|
|
|
)
|
2018-10-13 22:52:09 +02:00
|
|
|
|
2018-07-22 21:05:47 +02:00
|
|
|
// Taskfile reads a Taskfile for a given directory
|
2019-07-21 15:54:09 +02:00
|
|
|
func Taskfile(dir string, entrypoint string) (*taskfile.Taskfile, error) {
|
|
|
|
path := filepath.Join(dir, entrypoint)
|
2018-09-22 22:29:18 +02:00
|
|
|
if _, err := os.Stat(path); err != nil {
|
2019-07-21 15:54:09 +02:00
|
|
|
return nil, fmt.Errorf(`task: No Taskfile found on "%s". Use "task --init" to create a new one`, path)
|
2018-09-22 22:29:18 +02:00
|
|
|
}
|
2018-07-22 21:05:47 +02:00
|
|
|
t, err := readTaskfile(path)
|
|
|
|
if err != nil {
|
2018-09-22 22:29:18 +02:00
|
|
|
return nil, err
|
2018-07-22 21:05:47 +02:00
|
|
|
}
|
|
|
|
|
2020-01-29 09:03:06 +02:00
|
|
|
for namespace, includedTask := range t.Includes {
|
|
|
|
path = filepath.Join(dir, includedTask.Taskfile)
|
2018-09-10 03:29:29 +02:00
|
|
|
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
|
|
|
|
}
|
2018-10-13 22:52:09 +02:00
|
|
|
if len(includedTaskfile.Includes) > 0 {
|
|
|
|
return nil, ErrIncludedTaskfilesCantHaveIncludes
|
|
|
|
}
|
2020-01-29 09:03:06 +02:00
|
|
|
|
|
|
|
for _, task := range includedTaskfile.Tasks {
|
|
|
|
task.Dir = filepath.Join(includedTask.Dir, task.Dir)
|
|
|
|
}
|
|
|
|
|
2018-09-10 03:29:29 +02:00
|
|
|
if err = taskfile.Merge(t, includedTaskfile, namespace); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-22 21:05:47 +02:00
|
|
|
path = filepath.Join(dir, fmt.Sprintf("Taskfile_%s.yml", runtime.GOOS))
|
|
|
|
if _, err = os.Stat(path); err == nil {
|
|
|
|
osTaskfile, err := readTaskfile(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-07-22 22:54:44 +02:00
|
|
|
if err = taskfile.Merge(t, osTaskfile); err != nil {
|
2018-07-22 21:05:47 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, task := range t.Tasks {
|
|
|
|
task.Task = name
|
|
|
|
}
|
|
|
|
|
|
|
|
return t, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func readTaskfile(file string) (*taskfile.Taskfile, error) {
|
|
|
|
f, err := os.Open(file)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var t taskfile.Taskfile
|
|
|
|
return &t, yaml.NewDecoder(f).Decode(&t)
|
|
|
|
}
|