forked from jmank88/modgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
225 lines (204 loc) · 5.1 KB
/
Copy pathmain.go
File metadata and controls
225 lines (204 loc) · 5.1 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package main
import (
"bufio"
"flag"
"fmt"
"io"
"iter"
"log/slog"
"maps"
"os"
"slices"
"strings"
)
var (
prefix = flag.String("prefix", "", "prefix to filter")
verbose = flag.Bool("v", false, "verbose mode")
)
func main() {
os.Exit(Main())
}
func Main() int {
flag.Parse()
if *prefix == "" {
slog.Error("Must provide -prefix")
return 1
}
deps := newState()
for mod, dep := range scanDeps(os.Stdin) {
if !strings.HasPrefix(mod, *prefix) || !strings.HasPrefix(dep, *prefix) {
if *verbose {
slog.Info("Prefix mismatch", "mod", mod, "dep", dep, "prefix", *prefix)
}
continue
}
modPath, depPath := strings.TrimPrefix(mod, *prefix), strings.TrimPrefix(dep, *prefix)
deps.add(modPath, depPath)
}
deps.transitiveReduction()
for m, ds := range deps.depsSorted() {
if len(ds) == 0 {
fmt.Printf("\t%s\n", m)
} else {
slices.Sort(ds)
for _, d := range ds {
fmt.Printf("\t%s --> %s\n", m, d)
}
}
repo, _, _ := strings.Cut(m, "/")
fmt.Printf("\tclick %s href \"https://%s%s\"\n", m, *prefix, repo)
}
var subgraphs []string
for repo, mods := range deps.reposSorted() {
if len(mods) <= 1 {
if *verbose {
slog.Info("Skipping repo with single module", "repo", repo, "mods", mods)
}
continue
}
subgraphs = append(subgraphs, repo+"-repo")
slices.Sort(mods)
fmt.Printf("\n\tsubgraph %[1]s-repo[%[1]s]\n", repo)
for _, mod := range mods {
fmt.Printf("\t\t %s\n", mod)
}
fmt.Println("\tend")
fmt.Printf("\tclick %[1]s-repo href \"https://%s%[1]s\"\n", repo, *prefix)
}
if len(subgraphs) > 0 {
fmt.Println("\n\tclassDef outline stroke-dasharray:6,fill:none;")
fmt.Printf("\tclass %s outline\n", strings.Join(subgraphs, ","))
}
return 0
}
// scanDeps returns parsed modules from go mod graph output
// Example: module@version dep-module@version
func scanDeps(r io.Reader) iter.Seq2[string, string] {
return func(yield func(string, string) bool) {
scanner := bufio.NewScanner(r)
var total, invalid int
for scanner.Scan() {
total++
line := strings.TrimSpace(scanner.Text())
if line == "" {
continue
}
parts := strings.Split(line, " ")
if len(parts) != 2 {
invalid++
slog.Warn("Invalid line: expected one space", "spaces", len(parts)-1, "line", line)
continue
}
mod, _, _ := strings.Cut(parts[0], "@")
dep, _, _ := strings.Cut(parts[1], "@")
if !yield(mod, dep) {
return
}
}
if *verbose {
slog.Info("Scanned lines", "total", total, "invalid", invalid)
}
if err := scanner.Err(); err != nil {
slog.Error("Failed to read input", "err", err)
os.Exit(1)
}
}
}
type state struct {
deps map[string][]string // [path][]path
seen map[string]struct{} // [path]
repos map[string][]string // [repo][]path
}
func newState() *state {
return &state{
deps: make(map[string][]string),
seen: make(map[string]struct{}),
repos: make(map[string][]string),
}
}
func (s *state) add(mod, dep string) {
s.sawMod(mod)
s.sawMod(dep)
if d, ok := s.deps[mod]; ok {
if !slices.Contains(d, dep) {
s.deps[mod] = append(d, dep)
}
} else {
s.deps[mod] = []string{dep}
}
}
func (s *state) sawMod(path string) {
if _, ok := s.seen[path]; !ok {
s.seen[path] = struct{}{}
repo, _, _ := strings.Cut(path, "/")
if _, ok = s.repos[repo]; !ok {
s.repos[repo] = []string{path}
} else {
s.repos[repo] = append(s.repos[repo], path)
}
}
}
func (s *state) transitiveReduction() {
noPath := make(map[string]map[string]struct{}) // [path][path]
for m, deps := range s.deps {
s.deps[m] = slices.DeleteFunc(deps, func(d string) bool {
// BFS for indirect paths to d, tracking nodes we touch along the way
var touched []string
children := slices.DeleteFunc(slices.Clone(deps), func(s string) bool { return s == d }) // exclude direct
for len(children) > 0 {
touched = append(touched, children...)
var next []string
for _, child := range children {
if child == d {
if *verbose {
slog.Info("Excluding transitive edge", "mod", m, "dep", d)
}
return true // found an indirect path, so remove this edge
}
if none, ok := noPath[child]; ok {
if _, ok2 := none[d]; ok2 {
if *verbose {
slog.Info("Skipping known disconnected nodes", "mod", child, "dep", d)
}
continue // known not to lead to d
}
}
next = append(next, s.deps[child]...)
}
children = next
}
// none of the touched nodes have direct paths
for _, t := range touched {
none, ok := noPath[t]
if !ok {
none = make(map[string]struct{})
noPath[t] = none
}
none[d] = struct{}{}
}
return false
})
}
}
func (s *state) depsSorted() iter.Seq2[string, []string] {
return func(yield func(string, []string) bool) {
keys := slices.Collect(maps.Keys(s.seen))
slices.Sort(keys)
for _, m := range keys {
if !yield(m, s.deps[m]) {
return
}
}
}
}
func (s *state) reposSorted() iter.Seq2[string, []string] {
return func(yield func(string, []string) bool) {
keys := slices.Collect(maps.Keys(s.repos))
slices.Sort(keys)
for _, repo := range keys {
if !yield(repo, s.repos[repo]) {
return
}
}
}
}