2023-12-29 22:32:03 +02:00
|
|
|
package ast
|
2023-03-17 14:34:06 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-01-22 00:06:54 +02:00
|
|
|
"strings"
|
2023-03-17 14:34:06 +02:00
|
|
|
|
|
|
|
"gopkg.in/yaml.v3"
|
2023-04-06 13:07:57 +02:00
|
|
|
|
2024-01-04 02:31:24 +02:00
|
|
|
"github.com/go-task/task/v3/internal/omap"
|
2023-03-17 14:34:06 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Tasks represents a group of tasks
|
2023-04-06 13:07:57 +02:00
|
|
|
type Tasks struct {
|
2024-01-04 02:31:24 +02:00
|
|
|
omap.OrderedMap[string, *Task]
|
2023-04-06 13:07:57 +02:00
|
|
|
}
|
2023-03-17 14:34:06 +02:00
|
|
|
|
2024-02-22 22:52:05 +02:00
|
|
|
type MatchingTask struct {
|
|
|
|
Task *Task
|
|
|
|
Wildcards []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Tasks) FindMatchingTasks(call *Call) []*MatchingTask {
|
2024-01-27 17:51:43 +02:00
|
|
|
if call == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var task *Task
|
2024-02-22 22:52:05 +02:00
|
|
|
var matchingTasks []*MatchingTask
|
2024-01-27 17:51:43 +02:00
|
|
|
// If there is a direct match, return it
|
|
|
|
if task = t.OrderedMap.Get(call.Task); task != nil {
|
2024-02-22 22:52:05 +02:00
|
|
|
matchingTasks = append(matchingTasks, &MatchingTask{Task: task, Wildcards: nil})
|
|
|
|
return matchingTasks
|
2024-01-27 17:51:43 +02:00
|
|
|
}
|
|
|
|
// Attempt a wildcard match
|
|
|
|
// For now, we can just nil check the task before each loop
|
|
|
|
_ = t.Range(func(key string, value *Task) error {
|
2024-02-22 22:52:05 +02:00
|
|
|
if match, wildcards := value.WildcardMatch(call.Task); match {
|
|
|
|
matchingTasks = append(matchingTasks, &MatchingTask{
|
|
|
|
Task: value,
|
|
|
|
Wildcards: wildcards,
|
|
|
|
})
|
2024-01-27 17:51:43 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2024-02-22 22:52:05 +02:00
|
|
|
return matchingTasks
|
2024-01-27 17:51:43 +02:00
|
|
|
}
|
|
|
|
|
2024-01-22 00:06:54 +02:00
|
|
|
func (t1 *Tasks) Merge(t2 Tasks, include *Include) {
|
|
|
|
_ = t2.Range(func(k string, v *Task) error {
|
|
|
|
// We do a deep copy of the task struct here to ensure that no data can
|
|
|
|
// be changed elsewhere once the taskfile is merged.
|
|
|
|
task := v.DeepCopy()
|
|
|
|
|
|
|
|
// Set the task to internal if EITHER the included task or the included
|
|
|
|
// taskfile are marked as internal
|
|
|
|
task.Internal = task.Internal || (include != nil && include.Internal)
|
|
|
|
|
|
|
|
// Add namespaces to dependencies, commands and aliases
|
|
|
|
for _, dep := range task.Deps {
|
|
|
|
if dep != nil && dep.Task != "" {
|
|
|
|
dep.Task = taskNameWithNamespace(dep.Task, include.Namespace)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, cmd := range task.Cmds {
|
|
|
|
if cmd != nil && cmd.Task != "" {
|
|
|
|
cmd.Task = taskNameWithNamespace(cmd.Task, include.Namespace)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for i, alias := range task.Aliases {
|
|
|
|
task.Aliases[i] = taskNameWithNamespace(alias, include.Namespace)
|
|
|
|
}
|
|
|
|
// Add namespace aliases
|
|
|
|
if include != nil {
|
|
|
|
for _, namespaceAlias := range include.Aliases {
|
|
|
|
task.Aliases = append(task.Aliases, taskNameWithNamespace(task.Task, namespaceAlias))
|
|
|
|
for _, alias := range v.Aliases {
|
|
|
|
task.Aliases = append(task.Aliases, taskNameWithNamespace(alias, namespaceAlias))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the task to the merged taskfile
|
|
|
|
taskNameWithNamespace := taskNameWithNamespace(k, include.Namespace)
|
|
|
|
task.Task = taskNameWithNamespace
|
|
|
|
t1.Set(taskNameWithNamespace, task)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
// If the included Taskfile has a default task and the parent namespace has
|
|
|
|
// no task with a matching name, we can add an alias so that the user can
|
|
|
|
// run the included Taskfile's default task without specifying its full
|
|
|
|
// name. If the parent namespace has aliases, we add another alias for each
|
|
|
|
// of them.
|
2024-02-22 22:52:05 +02:00
|
|
|
if t2.Get("default") != nil && t1.Get(include.Namespace) == nil {
|
2024-01-22 00:06:54 +02:00
|
|
|
defaultTaskName := fmt.Sprintf("%s:default", include.Namespace)
|
2024-02-22 22:52:05 +02:00
|
|
|
t1.Get(defaultTaskName).Aliases = append(t1.Get(defaultTaskName).Aliases, include.Namespace)
|
|
|
|
t1.Get(defaultTaskName).Aliases = append(t1.Get(defaultTaskName).Aliases, include.Aliases...)
|
2024-01-22 00:06:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-17 14:34:06 +02:00
|
|
|
func (t *Tasks) UnmarshalYAML(node *yaml.Node) error {
|
|
|
|
switch node.Kind {
|
|
|
|
case yaml.MappingNode:
|
2024-01-04 02:31:24 +02:00
|
|
|
tasks := omap.New[string, *Task]()
|
2023-04-06 13:07:57 +02:00
|
|
|
if err := node.Decode(&tasks); err != nil {
|
2023-03-17 14:34:06 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-06 13:07:57 +02:00
|
|
|
// nolint: errcheck
|
|
|
|
tasks.Range(func(name string, task *Task) error {
|
2023-03-17 14:34:06 +02:00
|
|
|
// Set the task's name
|
2023-04-06 13:07:57 +02:00
|
|
|
if task == nil {
|
|
|
|
task = &Task{
|
2023-03-17 14:34:06 +02:00
|
|
|
Task: name,
|
|
|
|
}
|
|
|
|
}
|
2023-04-06 13:07:57 +02:00
|
|
|
task.Task = name
|
2023-03-17 14:34:06 +02:00
|
|
|
|
|
|
|
// Set the task's location
|
|
|
|
for _, keys := range node.Content {
|
|
|
|
if keys.Value == name {
|
2023-04-06 13:07:57 +02:00
|
|
|
task.Location = &Location{
|
2023-03-17 14:34:06 +02:00
|
|
|
Line: keys.Line,
|
|
|
|
Column: keys.Column,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-06 13:07:57 +02:00
|
|
|
tasks.Set(name, task)
|
|
|
|
return nil
|
|
|
|
})
|
2023-03-17 14:34:06 +02:00
|
|
|
|
2023-04-06 13:07:57 +02:00
|
|
|
*t = Tasks{
|
|
|
|
OrderedMap: tasks,
|
|
|
|
}
|
2023-03-17 14:34:06 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("yaml: line %d: cannot unmarshal %s into tasks", node.Line, node.ShortTag())
|
|
|
|
}
|
2024-01-22 00:06:54 +02:00
|
|
|
|
|
|
|
func taskNameWithNamespace(taskName string, namespace string) string {
|
|
|
|
if strings.HasPrefix(taskName, NamespaceSeparator) {
|
|
|
|
return strings.TrimPrefix(taskName, NamespaceSeparator)
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s%s%s", namespace, NamespaceSeparator, taskName)
|
|
|
|
}
|