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 (
|
2023-08-29 21:04:01 +00:00
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
|
2024-05-16 02:24:02 +01:00
|
|
|
"github.com/go-task/task/v3/errors"
|
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
|
|
|
)
|
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
|
2024-08-26 23:17:39 +02:00
|
|
|
Flatten bool
|
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-03-29 04:29:03 +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 {
|
2024-05-16 02:24:02 +01:00
|
|
|
return errors.NewTaskfileDecodeError(err, node)
|
2022-12-19 01:11:31 +00:00
|
|
|
}
|
2024-01-04 00:17:30 +00:00
|
|
|
v.Namespace = keyNode.Value
|
2024-03-29 04:29:03 +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
|
|
|
|
2024-05-16 02:24:02 +01:00
|
|
|
return errors.NewTaskfileDecodeError(nil, node).WithTypeMessage("includes")
|
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
|
2024-03-29 04:29:03 +00:00
|
|
|
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 {
|
2024-05-16 02:24:02 +01:00
|
|
|
return errors.NewTaskfileDecodeError(err, node)
|
2022-12-19 01:11:31 +00:00
|
|
|
}
|
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
|
2024-08-26 23:17:39 +02:00
|
|
|
Flatten bool
|
2022-12-19 01:11:31 +00:00
|
|
|
Aliases []string
|
|
|
|
Vars *Vars
|
|
|
|
}
|
|
|
|
if err := node.Decode(&includedTaskfile); err != nil {
|
2024-05-16 02:24:02 +01:00
|
|
|
return errors.NewTaskfileDecodeError(err, node)
|
2022-12-19 01:11:31 +00:00
|
|
|
}
|
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
|
2024-08-26 23:17:39 +02:00
|
|
|
include.Flatten = includedTaskfile.Flatten
|
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
|
|
|
|
2024-05-16 02:24:02 +01:00
|
|
|
return errors.NewTaskfileDecodeError(nil, node).WithTypeMessage("include")
|
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(),
|
2024-08-26 23:17:39 +02:00
|
|
|
Flatten: include.Flatten,
|
2022-10-02 05:45:27 +00:00
|
|
|
}
|
|
|
|
}
|