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

277 lines
6.9 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/filepathext"
"github.com/go-task/task/v3/internal/sysinfo"
"github.com/go-task/task/v3/internal/templater"
"github.com/go-task/task/v3/taskfile"
)
var (
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",
}
)
type ReaderNode struct {
Dir string
Entrypoint string
Optional bool
Parent *ReaderNode
}
// 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(readerNode *ReaderNode) (*taskfile.Taskfile, string, error) {
if readerNode.Dir == "" {
d, err := os.Getwd()
if err != nil {
return nil, "", err
}
readerNode.Dir = d
}
path, err := existsWalk(filepathext.SmartJoin(readerNode.Dir, readerNode.Entrypoint))
if err != nil {
return nil, "", err
}
readerNode.Dir = filepath.Dir(path)
readerNode.Entrypoint = filepath.Base(path)
t, err := readTaskfile(path)
if err != nil {
return nil, "", err
}
v, err := t.ParsedVersion()
if err != nil {
return nil, "", err
}
// Annotate any included Taskfile reference with a base directory for resolving relative paths
_ = t.Includes.Range(func(key string, includedFile taskfile.IncludedTaskfile) error {
// Set the base directory for resolving relative paths, but only if not already set
if includedFile.BaseDir == "" {
includedFile.BaseDir = readerNode.Dir
t.Includes.Set(key, includedFile)
}
return nil
})
err = t.Includes.Range(func(namespace string, includedTask taskfile.IncludedTaskfile) error {
if v >= 3.0 {
tr := templater.Templater{Vars: t.Vars, RemoveNoValue: true}
includedTask = taskfile.IncludedTaskfile{
Taskfile: tr.Replace(includedTask.Taskfile),
Dir: tr.Replace(includedTask.Dir),
Optional: includedTask.Optional,
2022-07-22 04:16:14 +02:00
Internal: includedTask.Internal,
2022-10-02 07:07:58 +02:00
Aliases: includedTask.Aliases,
AdvancedImport: includedTask.AdvancedImport,
2022-02-24 00:53:46 +02:00
Vars: includedTask.Vars,
BaseDir: includedTask.BaseDir,
}
if err := tr.Err(); err != nil {
return err
}
}
path, err := includedTask.FullTaskfilePath()
if err != nil {
return err
}
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
}
includeReaderNode := &ReaderNode{
Dir: filepath.Dir(path),
Entrypoint: filepath.Base(path),
Parent: readerNode,
Optional: includedTask.Optional,
}
if err := checkCircularIncludes(includeReaderNode); err != nil {
return err
2018-09-10 03:29:29 +02:00
}
includedTaskfile, _, err := Taskfile(includeReaderNode)
2018-09-10 03:29:29 +02:00
if err != nil {
if includedTask.Optional {
return nil
}
return err
}
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 {
dir, err := includedTask.FullDirPath()
if err != nil {
return err
}
for k, v := range includedTaskfile.Vars.Mapping {
o := v
o.Dir = dir
includedTaskfile.Vars.Mapping[k] = o
}
for k, v := range includedTaskfile.Env.Mapping {
o := v
o.Dir = dir
includedTaskfile.Env.Mapping[k] = o
}
for _, task := range includedTaskfile.Tasks {
task.Dir = filepathext.SmartJoin(dir, task.Dir)
2022-03-19 23:41:03 +02:00
task.IncludeVars = includedTask.Vars
task.IncludedTaskfileVars = includedTaskfile.Vars
task.IncludedTaskfile = &includedTask
}
}
2022-10-02 07:07:58 +02:00
if err = taskfile.Merge(t, includedTaskfile, &includedTask, namespace); err != nil {
return err
2018-09-10 03:29:29 +02:00
}
if includedTaskfile.Tasks["default"] != nil && t.Tasks[namespace] == nil {
defaultTaskName := fmt.Sprintf("%s:default", namespace)
t.Tasks[defaultTaskName].Aliases = append(t.Tasks[defaultTaskName].Aliases, namespace)
t.Tasks[defaultTaskName].Aliases = append(t.Tasks[defaultTaskName].Aliases, includedTask.Aliases...)
}
return nil
})
if err != nil {
return nil, "", err
2018-09-10 03:29:29 +02:00
}
if v < 3.0 {
path = filepathext.SmartJoin(readerNode.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
}
2022-10-02 07:07:58 +02:00
if err = taskfile.Merge(t, osTaskfile, nil); 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, readerNode.Dir, nil
}
func readTaskfile(file string) (*taskfile.Taskfile, error) {
f, err := os.Open(file)
if err != nil {
return nil, err
}
var t taskfile.Taskfile
2022-10-07 12:18:53 +02:00
if err := yaml.NewDecoder(f).Decode(&t); err != nil {
2022-10-15 00:48:45 +02:00
return nil, fmt.Errorf("task: Failed to parse %s:\n%w", filepathext.TryAbsToRel(file), err)
2022-10-07 12:18:53 +02:00
}
return &t, nil
}
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 := filepathext.SmartJoin(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)
}
func existsWalk(path string) (string, error) {
origPath := path
owner, err := sysinfo.Owner(path)
if err != nil {
return "", err
}
for {
fpath, err := exists(path)
if err == nil {
return fpath, nil
}
// Get the parent path/user id
parentPath := filepath.Dir(path)
parentOwner, err := sysinfo.Owner(parentPath)
if err != nil {
return "", err
}
// Error if we reached the root directory and still haven't found a file
// OR if the user id of the directory changes
if path == parentPath || (parentOwner != owner) {
return "", fmt.Errorf(`task: No Taskfile found in "%s" (or any of the parent directories). Use "task --init" to create a new one`, origPath)
}
owner = parentOwner
path = parentPath
}
}
func checkCircularIncludes(node *ReaderNode) error {
if node == nil {
2022-02-04 05:12:58 +02:00
return errors.New("task: failed to check for include cycle: node was nil")
}
if node.Parent == nil {
2022-02-04 05:12:58 +02:00
return errors.New("task: failed to check for include cycle: node.Parent was nil")
}
var curNode = node
var basePath = filepathext.SmartJoin(node.Dir, node.Entrypoint)
for curNode.Parent != nil {
curNode = curNode.Parent
curPath := filepathext.SmartJoin(curNode.Dir, curNode.Entrypoint)
if curPath == basePath {
2022-02-04 05:12:58 +02:00
return fmt.Errorf("task: include cycle detected between %s <--> %s",
curPath,
filepathext.SmartJoin(node.Parent.Dir, node.Parent.Entrypoint),
)
}
}
return nil
}