-
Notifications
You must be signed in to change notification settings - Fork 387
/
dot.go
125 lines (106 loc) · 3.14 KB
/
dot.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
// Copyright (C) 2024 Storj Labs, Inc.
// See LICENSE for copying information.
package mud
import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
"reflect"
"strings"
"github.com/zeebo/errs"
)
// DotAll generates graph report of the modules in dot format.
func DotAll(w io.Writer, ball *Ball) (err error) {
return Dot(w, ball.registry)
}
// Dot generates graph report of the modules in dot format, but only the selected components are included.
func Dot(w io.Writer, components []*Component) (err error) {
p := func(args ...any) {
if err != nil {
return
}
_, err = fmt.Fprint(w, args...)
}
pf := func(format string, args ...any) {
if err != nil {
return
}
_, err = fmt.Fprintf(w, format, args...)
}
p("digraph G {\n")
p("\tnode [style=filled, shape=box, fillcolor=white];\n")
defer p("}\n")
annotationStr := func(c *Component) string {
annotations := []string{}
for _, tag := range c.tags {
annotations = append(annotations, fmt.Sprintf("%s", tag))
}
annotationStr := strings.Join(annotations, "\n")
if len(annotationStr) > 0 {
annotationStr = "\n" + annotationStr
}
return annotationStr
}
covered := map[reflect.Type]struct{}{}
for _, component := range components {
componentID := typeLabel(component.target)
entries := []string{"label=\"" + component.Name() + annotationStr(component) + "\""}
if component.instance == nil {
entries = append(entries, "color=darkgray", "fontcolor=darkgray")
}
if component.run != nil && !component.run.started.IsZero() && component.run.finished.IsZero() {
entries = append(entries, "fillcolor=green")
}
entries = append(entries, "URL=\"./"+strings.ReplaceAll(componentID, "/", "_")+"\"")
pf("%q [%v];\n", componentID, strings.Join(entries, " "))
covered[component.target] = struct{}{}
}
for _, component := range components {
for _, dep := range component.requirements {
if _, found := covered[dep]; !found {
continue
}
componentID := typeLabel(component.target)
pf("%q -> %q;\n", componentID, typeLabel(dep))
}
}
return err
}
// GenerateComponentsGraph generates dot and svg file including the selected components.
func GenerateComponentsGraph(fileprefix string, components []*Component) error {
var b bytes.Buffer
if err := Dot(&b, components); err != nil {
_, err = fmt.Fprintf(os.Stderr, "fail: %v\n", err)
if err != nil {
return errs.Wrap(err)
}
} else {
err = os.WriteFile(fileprefix+".dot", b.Bytes(), 0644)
if err != nil {
return errs.Wrap(err)
}
output, err := exec.Command("dot", "-Tsvg", fileprefix+".dot", "-o", fileprefix+".svg").CombinedOutput()
if err != nil {
return errs.New("Execution of dot is failed with %s, %v", output, err)
}
}
return nil
}
// MustGenerateGraph generates dot and svg files from components selected by the selector.
func MustGenerateGraph(ball *Ball, fileprefix string, selector ComponentSelector) {
var components []*Component
for _, c := range ball.registry {
if selector(c) {
components = append(components, c)
}
}
err := GenerateComponentsGraph(fileprefix, components)
if err != nil {
panic(err)
}
}
func typeLabel(t reflect.Type) string {
return fullyQualifiedTypeName(t)
}