-
Notifications
You must be signed in to change notification settings - Fork 0
/
mermaid.go
137 lines (116 loc) · 2.66 KB
/
mermaid.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
package main
import (
"encoding/json"
"fmt"
"slices"
"strings"
)
func generateMermaidDiagram(
ctxCancel func(),
rootModule ModuleName,
isModuleMatching ModuleMatchingFunc,
direction Direction,
chains <-chan DependencyChain,
maxTraces uint,
) string {
var (
b strings.Builder
)
write := func(format string, a ...interface{}) {
_, _ = fmt.Fprintf(&b, format+"\n", a...)
}
mermaidConfig := map[string]any{
"flowchart": map[string]any{
"useMaxWidth": 0,
"curve": "linear",
"htmlLabels": false,
},
"theme": "base",
"themeVariables": map[string]any{
"primaryColor": "#3e4042",
"primaryBorderColor": "#6e7072",
"primaryTextColor": "#f0f1f2",
"lineColor": "#50b8e0",
"secondaryColor": "#6e7072",
"tertiaryColor": "#6e7072",
},
}
links, maxDepth := generateMermaidLinks(ctxCancel, chains, maxTraces)
ids := generateMermaidIDs(links)
data, _ := json.MarshalIndent(mermaidConfig, "", " ")
write("%%%%{\ninit")
write(string(data))
write("}%%%%")
if direction == "" {
direction = computeBestDirection(maxDepth)
}
write("graph %s", direction)
for module, id := range ids {
write("%d(%q)", id, module)
}
for module, deps := range links {
for _, dep := range deps {
moduleID, depID := ids[module], ids[dep]
write("%d --> %d", moduleID, depID)
}
}
write("style %d stroke:#50b8e0,color:#50b8e0;", ids[rootModule])
for module, id := range ids {
if isModuleMatching(module) {
write("style %d fill:#fceea5,stroke:#807956,color:#202224;", id)
}
}
return b.String()
}
func generateMermaidLinks(
ctxCancel func(),
chains <-chan DependencyChain,
maxTraces uint,
) (map[ModuleName][]ModuleName, int) {
var (
links = make(map[ModuleName][]ModuleName)
maxDepth = 0
nbTraces uint = 0
)
for chain := range chains {
modules := chain.Deps()
if len(modules) > maxDepth {
maxDepth = len(modules)
}
for i := 0; i < len(modules)-1; i++ {
module, dep := modules[i], modules[i+1]
deps := links[module]
if !slices.Contains(deps, dep) {
links[module] = append(deps, dep)
}
}
nbTraces++
if nbTraces == maxTraces {
ctxCancel()
break
}
}
return links, maxDepth
}
func generateMermaidIDs(links map[ModuleName][]ModuleName) map[ModuleName]int {
uniqueModules := make(map[ModuleName]bool)
for module, deps := range links {
uniqueModules[module] = true
for _, dep := range deps {
uniqueModules[dep] = true
}
}
labels := make(map[ModuleName]int)
id := 0
for module := range uniqueModules {
labels[module] = id
id++
}
return labels
}
func computeBestDirection(maxDepth int) Direction {
if maxDepth > 5 {
return "LR"
}
return "TB"
}