2023-12-29 20:32:03 +00:00
|
|
|
package ast
|
2020-01-29 10:02:22 +03:00
|
|
|
|
2021-01-01 18:27:50 -03:00
|
|
|
import (
|
2022-07-26 10:10:16 +12:00
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
2023-09-12 16:42:54 -05:00
|
|
|
"strings"
|
2022-07-26 10:10:16 +12:00
|
|
|
|
|
|
|
"github.com/go-task/task/v3/internal/execext"
|
2022-08-06 18:19:07 -03:00
|
|
|
"github.com/go-task/task/v3/internal/filepathext"
|
2024-01-04 00:31:24 +00:00
|
|
|
omap "github.com/go-task/task/v3/internal/omap"
|
2021-01-01 18:27:50 -03:00
|
|
|
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
)
|
2020-01-29 10:02:22 +03:00
|
|
|
|
2024-01-04 00:04:53 +00:00
|
|
|
// Include represents information about included taskfiles
|
|
|
|
type Include struct {
|
2024-01-04 00:17:30 +00:00
|
|
|
Namespace string
|
2020-02-15 17:24:06 +03:00
|
|
|
Taskfile string
|
|
|
|
Dir string
|
2021-08-11 17:28:44 +01:00
|
|
|
Optional bool
|
2022-07-22 02:16:14 +00:00
|
|
|
Internal bool
|
2022-10-02 05:07:58 +00:00
|
|
|
Aliases []string
|
2020-02-15 17:24:06 +03:00
|
|
|
AdvancedImport bool
|
2022-02-23 16:53:46 -06:00
|
|
|
Vars *Vars
|
2022-07-26 10:10:16 +12:00
|
|
|
BaseDir string // The directory from which the including taskfile was loaded; used to resolve relative paths
|
2020-01-29 10:02:22 +03:00
|
|
|
}
|
|
|
|
|
2024-01-04 00:04:53 +00:00
|
|
|
// Includes represents information about included tasksfiles
|
|
|
|
type Includes struct {
|
2024-01-04 00:31:24 +00:00
|
|
|
omap.OrderedMap[string, Include]
|
2021-01-01 18:27:50 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalYAML implements the yaml.Unmarshaler interface.
|
2024-01-04 00:04:53 +00:00
|
|
|
func (includes *Includes) UnmarshalYAML(node *yaml.Node) error {
|
2022-12-19 01:11:31 +00:00
|
|
|
switch node.Kind {
|
|
|
|
case yaml.MappingNode:
|
|
|
|
// NOTE(@andreynering): on this style of custom unmarshalling,
|
|
|
|
// even number contains the keys, while odd numbers contains
|
|
|
|
// the values.
|
|
|
|
for i := 0; i < len(node.Content); i += 2 {
|
|
|
|
keyNode := node.Content[i]
|
|
|
|
valueNode := node.Content[i+1]
|
2021-01-01 18:27:50 -03:00
|
|
|
|
2024-01-04 00:04:53 +00:00
|
|
|
var v Include
|
2022-12-19 01:11:31 +00:00
|
|
|
if err := valueNode.Decode(&v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-01-04 00:17:30 +00:00
|
|
|
v.Namespace = keyNode.Value
|
2024-01-04 00:04:53 +00:00
|
|
|
includes.Set(keyNode.Value, v)
|
2021-01-01 18:27:50 -03:00
|
|
|
}
|
2022-12-19 01:11:31 +00:00
|
|
|
return nil
|
2021-01-01 18:27:50 -03:00
|
|
|
}
|
2022-12-19 01:11:31 +00:00
|
|
|
|
|
|
|
return fmt.Errorf("yaml: line %d: cannot unmarshal %s into included taskfiles", node.Line, node.ShortTag())
|
2021-01-01 18:27:50 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Len returns the length of the map
|
2024-01-04 00:04:53 +00:00
|
|
|
func (includes *Includes) Len() int {
|
|
|
|
if includes == nil {
|
2021-01-01 18:27:50 -03:00
|
|
|
return 0
|
|
|
|
}
|
2024-01-04 00:27:46 +00:00
|
|
|
return includes.OrderedMap.Len()
|
2021-01-01 18:27:50 -03:00
|
|
|
}
|
|
|
|
|
2024-01-04 00:27:46 +00:00
|
|
|
// Wrapper around OrderedMap.Set to ensure we don't get nil pointer errors
|
|
|
|
func (includes *Includes) Range(f func(k string, v Include) error) error {
|
2024-01-04 00:04:53 +00:00
|
|
|
if includes == nil {
|
2021-01-01 18:27:50 -03:00
|
|
|
return nil
|
|
|
|
}
|
2024-01-04 00:27:46 +00:00
|
|
|
return includes.OrderedMap.Range(f)
|
2021-01-01 18:27:50 -03:00
|
|
|
}
|
2020-01-29 10:02:22 +03:00
|
|
|
|
2024-01-04 00:04:53 +00:00
|
|
|
func (include *Include) UnmarshalYAML(node *yaml.Node) error {
|
2022-12-19 01:11:31 +00:00
|
|
|
switch node.Kind {
|
|
|
|
|
|
|
|
case yaml.ScalarNode:
|
|
|
|
var str string
|
|
|
|
if err := node.Decode(&str); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-01-04 00:04:53 +00:00
|
|
|
include.Taskfile = str
|
2020-01-29 10:02:22 +03:00
|
|
|
return nil
|
|
|
|
|
2022-12-19 01:11:31 +00:00
|
|
|
case yaml.MappingNode:
|
|
|
|
var includedTaskfile struct {
|
|
|
|
Taskfile string
|
|
|
|
Dir string
|
|
|
|
Optional bool
|
|
|
|
Internal bool
|
|
|
|
Aliases []string
|
|
|
|
Vars *Vars
|
|
|
|
}
|
|
|
|
if err := node.Decode(&includedTaskfile); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-01-04 00:04:53 +00:00
|
|
|
include.Taskfile = includedTaskfile.Taskfile
|
|
|
|
include.Dir = includedTaskfile.Dir
|
|
|
|
include.Optional = includedTaskfile.Optional
|
|
|
|
include.Internal = includedTaskfile.Internal
|
|
|
|
include.Aliases = includedTaskfile.Aliases
|
|
|
|
include.AdvancedImport = true
|
|
|
|
include.Vars = includedTaskfile.Vars
|
2022-12-19 01:11:31 +00:00
|
|
|
return nil
|
2020-01-29 10:02:22 +03:00
|
|
|
}
|
2022-12-19 01:11:31 +00:00
|
|
|
|
|
|
|
return fmt.Errorf("yaml: line %d: cannot unmarshal %s into included taskfile", node.Line, node.ShortTag())
|
2020-01-29 10:02:22 +03:00
|
|
|
}
|
2022-07-26 10:10:16 +12:00
|
|
|
|
2022-10-02 05:45:27 +00:00
|
|
|
// DeepCopy creates a new instance of IncludedTaskfile and copies
|
|
|
|
// data by value from the source struct.
|
2024-01-04 00:04:53 +00:00
|
|
|
func (include *Include) DeepCopy() *Include {
|
|
|
|
if include == nil {
|
2022-10-02 05:45:27 +00:00
|
|
|
return nil
|
|
|
|
}
|
2024-01-04 00:04:53 +00:00
|
|
|
return &Include{
|
2024-01-04 00:17:30 +00:00
|
|
|
Namespace: include.Namespace,
|
2024-01-04 00:04:53 +00:00
|
|
|
Taskfile: include.Taskfile,
|
|
|
|
Dir: include.Dir,
|
|
|
|
Optional: include.Optional,
|
|
|
|
Internal: include.Internal,
|
|
|
|
AdvancedImport: include.AdvancedImport,
|
|
|
|
Vars: include.Vars.DeepCopy(),
|
|
|
|
BaseDir: include.BaseDir,
|
2022-10-02 05:45:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-26 10:10:16 +12:00
|
|
|
// FullTaskfilePath returns the fully qualified path to the included taskfile
|
2024-01-04 00:04:53 +00:00
|
|
|
func (include *Include) FullTaskfilePath() (string, error) {
|
|
|
|
return include.resolvePath(include.Taskfile)
|
2022-07-26 10:10:16 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
// FullDirPath returns the fully qualified path to the included taskfile's working directory
|
2024-01-04 00:04:53 +00:00
|
|
|
func (include *Include) FullDirPath() (string, error) {
|
|
|
|
return include.resolvePath(include.Dir)
|
2022-07-26 10:10:16 +12:00
|
|
|
}
|
|
|
|
|
2024-01-04 00:04:53 +00:00
|
|
|
func (include *Include) resolvePath(path string) (string, error) {
|
2023-09-12 16:42:54 -05:00
|
|
|
// If the file is remote, we don't need to resolve the path
|
2024-01-04 00:04:53 +00:00
|
|
|
if strings.Contains(include.Taskfile, "://") {
|
2023-09-12 16:42:54 -05:00
|
|
|
return path, nil
|
|
|
|
}
|
|
|
|
|
2022-07-26 10:10:16 +12:00
|
|
|
path, err := execext.Expand(path)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2023-08-26 18:06:50 -03:00
|
|
|
if filepathext.IsAbs(path) {
|
2022-07-26 10:10:16 +12:00
|
|
|
return path, nil
|
|
|
|
}
|
|
|
|
|
2024-01-04 00:04:53 +00:00
|
|
|
result, err := filepath.Abs(filepathext.SmartJoin(include.BaseDir, path))
|
2022-07-26 10:10:16 +12:00
|
|
|
if err != nil {
|
2024-01-04 00:04:53 +00:00
|
|
|
return "", fmt.Errorf("task: error resolving path %s relative to %s: %w", path, include.BaseDir, err)
|
2022-07-26 10:10:16 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|