1
0
mirror of https://github.com/go-task/task.git synced 2025-01-10 04:18:53 +02:00
task/taskfile/ast/graph.go

128 lines
2.6 KiB
Go
Raw Normal View History

2024-01-02 01:12:28 +02:00
package ast
import (
2024-01-04 13:58:46 +02:00
"fmt"
2024-01-02 01:12:28 +02:00
"os"
"github.com/dominikbraun/graph"
"github.com/dominikbraun/graph/draw"
2024-01-04 13:58:46 +02:00
"golang.org/x/sync/errgroup"
2024-01-02 01:12:28 +02:00
)
type TaskfileGraph struct {
graph.Graph[string, *TaskfileVertex]
}
// A TaskfileVertex is a vertex on the Taskfile DAG.
type TaskfileVertex struct {
URI string
Taskfile *Taskfile
}
func taskfileHash(vertex *TaskfileVertex) string {
return vertex.URI
}
func NewTaskfileGraph() *TaskfileGraph {
return &TaskfileGraph{
graph.New(taskfileHash,
graph.Directed(),
graph.PreventCycles(),
graph.Rooted(),
),
}
}
2024-01-04 13:58:46 +02:00
func (tfg *TaskfileGraph) Visualize(filename string) error {
2024-01-02 01:12:28 +02:00
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()
2024-01-04 13:58:46 +02:00
return draw.DOT(tfg.Graph, f)
}
func (tfg *TaskfileGraph) Merge() (*Taskfile, error) {
hashes, err := graph.TopologicalSort(tfg.Graph)
if err != nil {
return nil, err
}
predecessorMap, err := tfg.PredecessorMap()
if err != nil {
return nil, err
}
2024-03-19 03:09:07 +02:00
// Loop over each vertex in reverse topological order except for the root vertex.
// This gives us a loop over every included Taskfile in an order which is safe to merge.
for i := len(hashes) - 1; i > 0; i-- {
2024-01-04 13:58:46 +02:00
hash := hashes[i]
2024-03-19 03:09:07 +02:00
// Get the included vertex
includedVertex, err := tfg.Vertex(hash)
2024-01-04 13:58:46 +02:00
if err != nil {
return nil, err
}
// Create an error group to wait for all the included Taskfiles to be merged with all its parents
var g errgroup.Group
2024-03-19 03:09:07 +02:00
// Loop over edge that leads to a vertex that includes the current vertex
2024-01-04 13:58:46 +02:00
for _, edge := range predecessorMap[hash] {
// TODO: Enable goroutines
// Start a goroutine to process each included Taskfile
// g.Go(
err := func() error {
2024-03-19 03:09:07 +02:00
// Get the base vertex
vertex, err := tfg.Vertex(edge.Source)
2024-01-04 13:58:46 +02:00
if err != nil {
return err
}
// Get the merge options
2024-03-19 03:09:07 +02:00
include, ok := edge.Properties.Data.(Include)
2024-01-04 13:58:46 +02:00
if !ok {
return fmt.Errorf("task: Failed to get merge options")
}
// Merge the included Taskfile into the parent Taskfile
2024-03-19 03:09:07 +02:00
if err := vertex.Taskfile.Merge(
includedVertex.Taskfile,
&include,
2024-01-04 13:58:46 +02:00
); err != nil {
return err
}
return nil
}()
if err != nil {
return nil, err
}
// )
}
// Wait for all the go routines to finish
if err := g.Wait(); err != nil {
return nil, err
}
}
// Get the root vertex
rootVertex, err := tfg.Vertex(hashes[0])
if err != nil {
return nil, err
}
rootVertex.Taskfile.Tasks.Range(func(name string, task *Task) error {
if task == nil {
task = &Task{}
rootVertex.Taskfile.Tasks.Set(name, task)
}
task.Task = name
return nil
})
return rootVertex.Taskfile, nil
2024-01-02 01:12:28 +02:00
}