mirror of
https://github.com/go-task/task.git
synced 2026-06-26 14:16:16 +00:00
feat: add task location data to json output (#1056)
* feat: add task location data to json output * feat: add root taskfile location to --json output
This commit is contained in:
18
taskfile/location.go
Normal file
18
taskfile/location.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package taskfile
|
||||
|
||||
type Location struct {
|
||||
Line int
|
||||
Column int
|
||||
Taskfile string
|
||||
}
|
||||
|
||||
func (l *Location) DeepCopy() *Location {
|
||||
if l == nil {
|
||||
return nil
|
||||
}
|
||||
return &Location{
|
||||
Line: l.Line,
|
||||
Column: l.Column,
|
||||
Taskfile: l.Taskfile,
|
||||
}
|
||||
}
|
||||
@@ -65,7 +65,9 @@ func Merge(t1, t2 *Taskfile, includedTaskfile *IncludedTaskfile, namespaces ...s
|
||||
}
|
||||
|
||||
// Add the task to the merged taskfile
|
||||
t1.Tasks[taskNameWithNamespace(k, namespaces...)] = task
|
||||
taskNameWithNamespace := taskNameWithNamespace(k, namespaces...)
|
||||
task.Task = taskNameWithNamespace
|
||||
t1.Tasks[taskNameWithNamespace] = task
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
@@ -176,12 +176,18 @@ func Taskfile(readerNode *ReaderNode) (*taskfile.Taskfile, string, error) {
|
||||
}
|
||||
}
|
||||
|
||||
for name, task := range t.Tasks {
|
||||
// Set the location of the Taskfile
|
||||
t.Location = path
|
||||
|
||||
for _, task := range t.Tasks {
|
||||
// If the task is not defined, create a new one
|
||||
if task == nil {
|
||||
task = &taskfile.Task{}
|
||||
t.Tasks[name] = task
|
||||
}
|
||||
task.Task = name
|
||||
// Set the location of the taskfile for each task
|
||||
if task.Location.Taskfile == "" {
|
||||
task.Location.Taskfile = path
|
||||
}
|
||||
}
|
||||
|
||||
return t, readerNode.Dir, nil
|
||||
|
||||
@@ -6,9 +6,6 @@ import (
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
// Tasks represents a group of tasks
|
||||
type Tasks map[string]*Task
|
||||
|
||||
// Task represents a task
|
||||
type Task struct {
|
||||
Task string
|
||||
@@ -39,6 +36,7 @@ type Task struct {
|
||||
IncludedTaskfileVars *Vars
|
||||
IncludedTaskfile *IncludedTaskfile
|
||||
Platforms []*Platform
|
||||
Location *Location
|
||||
}
|
||||
|
||||
func (t *Task) Name() string {
|
||||
@@ -162,6 +160,7 @@ func (t *Task) DeepCopy() *Task {
|
||||
IncludedTaskfileVars: t.IncludedTaskfileVars.DeepCopy(),
|
||||
IncludedTaskfile: t.IncludedTaskfile.DeepCopy(),
|
||||
Platforms: deepCopySlice(t.Platforms),
|
||||
Location: t.Location.DeepCopy(),
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ var (
|
||||
|
||||
// Taskfile represents a Taskfile.yml
|
||||
type Taskfile struct {
|
||||
Location string
|
||||
Version *semver.Version
|
||||
Expansions int
|
||||
Output Output
|
||||
|
||||
45
taskfile/tasks.go
Normal file
45
taskfile/tasks.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package taskfile
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
// Tasks represents a group of tasks
|
||||
type Tasks map[string]*Task
|
||||
|
||||
func (t *Tasks) UnmarshalYAML(node *yaml.Node) error {
|
||||
switch node.Kind {
|
||||
case yaml.MappingNode:
|
||||
tasks := map[string]*Task{}
|
||||
if err := node.Decode(tasks); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for name := range tasks {
|
||||
// Set the task's name
|
||||
if tasks[name] == nil {
|
||||
tasks[name] = &Task{
|
||||
Task: name,
|
||||
}
|
||||
}
|
||||
tasks[name].Task = name
|
||||
|
||||
// Set the task's location
|
||||
for _, keys := range node.Content {
|
||||
if keys.Value == name {
|
||||
tasks[name].Location = &Location{
|
||||
Line: keys.Line,
|
||||
Column: keys.Column,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
*t = Tasks(tasks)
|
||||
return nil
|
||||
}
|
||||
|
||||
return fmt.Errorf("yaml: line %d: cannot unmarshal %s into tasks", node.Line, node.ShortTag())
|
||||
}
|
||||
Reference in New Issue
Block a user