diff --git a/taskfile/ast/graph.go b/taskfile/ast/graph.go index cb30093d..b6c27438 100644 --- a/taskfile/ast/graph.go +++ b/taskfile/ast/graph.go @@ -45,6 +45,18 @@ func (tfg *TaskfileGraph) Visualize(filename string) error { return draw.DOT(tfg.Graph, f) } +// Root returns the root vertex of the graph (the entrypoint Taskfile). +func (tfg *TaskfileGraph) Root() (*TaskfileVertex, error) { + hashes, err := graph.TopologicalSort(tfg.Graph) + if err != nil { + return nil, err + } + if len(hashes) == 0 { + return nil, fmt.Errorf("task: graph has no vertices") + } + return tfg.Vertex(hashes[0]) +} + func (tfg *TaskfileGraph) Merge() (*Taskfile, error) { hashes, err := graph.TopologicalSort(tfg.Graph) if err != nil {