1
0
mirror of https://github.com/go-task/task.git synced 2024-12-14 10:52:43 +02:00
task/taskfile/read/taskfile.go

178 lines
4.1 KiB
Go
Raw Normal View History

package read
import (
"errors"
"fmt"
"os"
"path/filepath"
"runtime"
2020-03-28 16:27:49 +02:00
"gopkg.in/yaml.v3"
"github.com/go-task/task/v3/internal/execext"
"github.com/go-task/task/v3/internal/templater"
"github.com/go-task/task/v3/taskfile"
)
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")
2020-08-16 00:12:39 +02:00
// ErrIncludedTaskfilesCantHaveDotenvs is returned when a included Taskfile contains dotenvs
2020-08-04 00:18:38 +02:00
ErrIncludedTaskfilesCantHaveDotenvs = errors.New("task: Included Taskfiles can't have dotenv declarations. Please, move the dotenv declaration to the main Taskfile")
defaultTaskfiles = []string{
"Taskfile.yml",
"Taskfile.yaml",
"Taskfile.dist.yml",
"Taskfile.dist.yaml",
}
)
// Taskfile reads a Taskfile for a given directory
// Uses current dir when dir is left empty. Uses Taskfile.yml
// or Taskfile.yaml when entrypoint is left empty
func Taskfile(dir string, entrypoint string) (*taskfile.Taskfile, error) {
if dir == "" {
d, err := os.Getwd()
if err != nil {
return nil, err
}
dir = d
}
path, err := exists(filepath.Join(dir, entrypoint))
if err != nil {
return nil, err
}
t, err := readTaskfile(path)
if err != nil {
return nil, err
}
v, err := t.ParsedVersion()
if err != nil {
return nil, err
}
err = t.Includes.Range(func(namespace string, includedTask taskfile.IncludedTaskfile) error {
if v >= 3.0 {
tr := templater.Templater{Vars: &taskfile.Vars{}, RemoveNoValue: true}
includedTask = taskfile.IncludedTaskfile{
Taskfile: tr.Replace(includedTask.Taskfile),
Dir: tr.Replace(includedTask.Dir),
Optional: includedTask.Optional,
AdvancedImport: includedTask.AdvancedImport,
}
if err := tr.Err(); err != nil {
return err
}
}
path, err := execext.Expand(includedTask.Taskfile)
if err != nil {
return err
}
2021-09-06 15:30:45 +02:00
if !filepath.IsAbs(path) {
path = filepath.Join(dir, path)
}
path, err = exists(path)
2018-09-10 03:29:29 +02:00
if err != nil {
2021-09-25 14:40:03 +02:00
if includedTask.Optional {
return nil
}
return err
2018-09-10 03:29:29 +02:00
}
2018-09-10 03:29:29 +02:00
includedTaskfile, err := readTaskfile(path)
if err != nil {
return err
2018-09-10 03:29:29 +02:00
}
if includedTaskfile.Includes.Len() > 0 {
return ErrIncludedTaskfilesCantHaveIncludes
}
2020-08-16 00:12:39 +02:00
if v >= 3.0 && len(includedTaskfile.Dotenv) > 0 {
return ErrIncludedTaskfilesCantHaveDotenvs
2020-08-04 00:18:38 +02:00
}
if includedTask.AdvancedImport {
for k, v := range includedTaskfile.Vars.Mapping {
o := v
o.Dir = filepath.Join(dir, includedTask.Dir)
includedTaskfile.Vars.Mapping[k] = o
}
for k, v := range includedTaskfile.Env.Mapping {
o := v
o.Dir = filepath.Join(dir, includedTask.Dir)
includedTaskfile.Env.Mapping[k] = o
}
for _, task := range includedTaskfile.Tasks {
if !filepath.IsAbs(task.Dir) {
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 err
2018-09-10 03:29:29 +02:00
}
return nil
})
if err != nil {
return nil, err
2018-09-10 03:29:29 +02:00
}
if v < 3.0 {
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
}
if err = taskfile.Merge(t, osTaskfile); err != nil {
return nil, err
}
}
}
for name, task := range t.Tasks {
if task == nil {
task = &taskfile.Task{}
t.Tasks[name] = task
}
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)
}
func exists(path string) (string, error) {
fi, err := os.Stat(path)
if err != nil {
return "", err
}
if fi.Mode().IsRegular() {
return path, nil
}
for _, n := range defaultTaskfiles {
fpath := filepath.Join(path, n)
if _, err := os.Stat(fpath); err == nil {
return fpath, nil
}
}
return "", fmt.Errorf(`task: No Taskfile found in "%s". Use "task --init" to create a new one`, path)
}