mirror of
https://github.com/go-task/task.git
synced 2025-11-23 22:24:45 +02:00
feat: dag reader
This commit is contained in:
41
taskfile/ast/graph.go
Normal file
41
taskfile/ast/graph.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package ast
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/dominikbraun/graph"
|
||||
"github.com/dominikbraun/graph/draw"
|
||||
)
|
||||
|
||||
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(),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
func (r *TaskfileGraph) Visualize(filename string) error {
|
||||
f, err := os.Create(filename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
return draw.DOT(r.Graph, f)
|
||||
}
|
||||
Reference in New Issue
Block a user