forked from rwxrob/bonzai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.go
185 lines (170 loc) · 4.03 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
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
package kimono
import (
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/rwxrob/bonzai/futil"
"github.com/rwxrob/bonzai/run"
"github.com/rwxrob/bonzai/to"
)
type node struct {
name string
path string
dependencies []*node
dependents []*node
}
type graph struct {
nodes map[string]*node
}
func newGraph() *graph {
return &graph{nodes: make(map[string]*node)}
}
func (g *graph) addNode(name, path string) {
if _, ok := g.nodes[name]; !ok {
if path == "" {
g.nodes[name] = &node{name: name}
} else {
g.nodes[name] = &node{name: name, path: path}
}
} else {
if g.nodes[name].path == "" {
g.nodes[name].path = path
}
}
}
func (g *graph) addDependency(from, to string) {
fromNode := g.nodes[from]
toNode := g.nodes[to]
fromNode.dependencies = append(fromNode.dependencies, toNode)
toNode.dependents = append(toNode.dependents, fromNode)
}
func (g *graph) getDependencies(name string) []*node {
return g.nodes[name].dependencies
}
func (g *graph) getDependents(name string) []*node {
return g.nodes[name].dependents
}
func getDependents(g *graph, name string) []*node {
// Strip version information and search for dependents
strippedName := stripVersion(name)
var dependents []*node
for _, node := range g.nodes {
if stripVersion(node.name) == strippedName {
dependents = append(
dependents,
node.dependents...)
}
}
return dependents
}
func stripVersion(name string) string {
parts := strings.Split(name, "@")
return parts[0]
}
func (g *graph) hasDependencies(name string) bool {
return len(g.nodes[name].dependencies) > 0
}
// ListDependents returns a list of all Go modules that depend on the
// current module.
func ListDependents() ([]string, error) {
modName := strings.TrimSpace(run.Out("go", "list", "-m"))
graph, err := dependencyGraph()
if err != nil {
return nil, err
}
name := fmt.Sprintf("%s@%s", modName, latestTag())
depNodes := getDependents(graph, name)
dependents := make([]string, 0)
for _, dep := range depNodes {
if dep.name == modName {
continue
}
dependents = append(dependents, dep.name)
}
return dependents, nil
}
// ListDependencies returns a list of all Go modules that the current
// module depends on.
func ListDependencies() ([]string, error) {
modName := strings.TrimSpace(run.Out("go", "list", "-m"))
graph, err := dependencyGraph()
if err != nil {
return nil, err
}
name := fmt.Sprintf("%s@%s", modName, latestTag())
depNodes := graph.getDependencies(name)
dependencies := make([]string, 0)
for _, dep := range depNodes {
dependencies = append(dependencies, dep.name)
}
return dependencies, nil
}
// dependencyGraph walks the current git repository and builds a graph of all
// Go modules and their dependencies.
func dependencyGraph() (*graph, error) {
graph := newGraph()
root, err := futil.HereOrAbove(".git")
if err != nil {
return nil, err
}
ppwd, err := os.Getwd()
if err != nil {
return nil, err
}
filepath.WalkDir(
filepath.Dir(root),
func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if !d.IsDir() {
return nil
}
if d.Name() == ".git" {
return filepath.SkipDir
}
if !futil.Exists(
filepath.Join(path, "go.mod"),
) {
return nil
}
if err := os.Chdir(path); err != nil {
return err
}
err = os.Chdir(path)
if err != nil {
return nil
}
modName := strings.TrimSpace(
run.Out("go", "list", "-m"),
)
modName = fmt.Sprint(modName, "@", latestTag())
graph.addNode(modName, path)
dependencies := to.Lines(run.Out("go", "list", "-m", "all"))
if len(dependencies) < 2 {
return nil
}
dependencies = dependencies[1:]
for _, dep := range dependencies {
dep = strings.TrimSpace(dep)
parts := strings.Split(dep, " ")
if len(parts) < 2 {
continue
}
name := parts[0]
ver := parts[1]
dep = fmt.Sprint(name, "@", ver)
graph.addNode(dep, "")
graph.addDependency(modName, dep)
}
return nil
},
)
err = os.Chdir(ppwd)
if err != nil {
return nil, err
}
return graph, nil
}