-
Notifications
You must be signed in to change notification settings - Fork 1
/
graph.go
159 lines (133 loc) · 3.32 KB
/
graph.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"context"
"fmt"
"io"
"sync"
"github.com/ejholmes/walk/internal/dag"
)
// WalkError is returned when a target fails while walking the graph.
type WalkError struct {
mu sync.Mutex
Errors map[string]error
}
func newWalkError() *WalkError {
return &WalkError{Errors: make(map[string]error)}
}
func (e *WalkError) Error() string {
return fmt.Sprintf("%d %s failed", len(e.Errors), pluralize(len(e.Errors), "target", "targets"))
}
func (e *WalkError) Add(t Target, err error) {
if err == nil {
return
}
e.mu.Lock()
defer e.mu.Unlock()
e.Errors[t.Name()] = err
}
// Graph wraps a graph of targets.
type Graph struct {
mu sync.Mutex
m map[string]Target
dag *dag.AcyclicGraph
}
func newGraph() *Graph {
return &Graph{
m: make(map[string]Target),
dag: new(dag.AcyclicGraph),
}
}
// Add adds the target to the graph unless it already exists in the graph. If
// a target with the given name already exists in the graph, that existing
// target is returned, otherwise nil
func (g *Graph) Add(target Target) Target {
g.mu.Lock()
defer g.mu.Unlock()
if t := g.target(target.Name()); t != nil {
return t
}
g.m[target.Name()] = target
g.dag.Add(target.Name())
return nil
}
// Connect connects the two targets together.
func (g *Graph) Connect(target, dependency Target) {
g.dag.Connect(dag.BasicEdge(target.Name(), dependency.Name()))
}
// Target returns the Target with the given name.
func (g *Graph) Target(name string) Target {
g.mu.Lock()
target := g.target(name)
g.mu.Unlock()
return target
}
// Walk wraps the underlying Walk function to coerce it to a Target first.
func (g *Graph) Walk(fn func(Target) error) error {
errors := newWalkError()
err := g.dag.Walk(func(v dag.Vertex) error {
target := g.Target(v.(string))
// We don't actually need to walk the root, since it's a pseudo
// target.
if _, ok := target.(*rootTarget); ok {
return nil
}
err := fn(target)
errors.Add(target, err)
return err
})
if err == dag.ErrWalk {
return errors
}
return err
}
// TransitiveReduction performs a Transitive reduction of the underlying graph.
func (g *Graph) TransitiveReduction() {
g.dag.TransitiveReduction()
}
// Validate validates the underlying graph.
func (g *Graph) Validate() error {
return g.dag.Validate()
}
func (g *Graph) String() string {
return g.dag.String()
}
func (g *Graph) target(name string) Target {
return g.m[name]
}
// rootTarget is a pseudo target for the root of the graph.
type rootTarget struct {
deps []string
}
func (t *rootTarget) Name() string {
return "(root)"
}
func (t *rootTarget) Exec(_ context.Context) error {
panic("unreachable")
}
func (t *rootTarget) Dependencies(_ context.Context) ([]string, error) {
return t.deps, nil
}
func dot(w io.Writer, g *Graph) error {
if _, err := io.WriteString(w, "digraph {\n"); err != nil {
return err
}
for _, v := range g.dag.Vertices() {
for _, dep := range dag.AsVertexList(g.dag.DownEdges(v)) {
if _, err := fmt.Fprintf(w, " \"%s\" -> \"%s\"\n", dag.VertexName(v), dag.VertexName(dep)); err != nil {
return err
}
}
}
if _, err := io.WriteString(w, "}\n"); err != nil {
return err
}
return nil
}
func plain(w io.Writer, g *Graph) error {
for _, v := range g.dag.Vertices() {
if _, err := fmt.Fprintf(w, "%s\n", dag.VertexName(v)); err != nil {
return err
}
}
return nil
}