-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
168 lines (153 loc) · 3.1 KB
/
main.go
File metadata and controls
168 lines (153 loc) · 3.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
package main
import (
"fmt"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"sort"
"strings"
)
func main() {
err := run()
if err != nil {
log.Fatal(err)
}
}
type Dep struct {
name string
deps []string
directSize int64
totalSize int64
resolved bool
approx bool
}
func run() error {
cmd := exec.Command("go", "mod", "graph")
if len(os.Args) == 2 {
cmd.Dir = os.Args[1]
}
rawStdout, err := cmd.Output()
if err != nil {
return fmt.Errorf("run go mod graph: %v", err)
}
stdout := string(rawStdout)
// Collect direct sizes
deps := map[string]*Dep{}
for line := range strings.SplitSeq(stdout, "\n") {
parent, child, found := strings.Cut(line, " ")
if !found {
continue
}
dep := deps[parent]
if dep != nil {
dep.deps = append(dep.deps, child)
} else {
size, _ := getSize(parent)
deps[parent] = &Dep{
name: parent,
deps: []string{child},
directSize: size,
}
}
}
// Accumulate total sizes
for !allResolved(deps) {
dep := pickUnresolvedDep(deps, 0)
if dep == nil {
for i := 1; dep == nil; i++ {
dep = pickUnresolvedDep(deps, i)
}
dep.approx = true
}
dep.resolved = true
dep.totalSize = dep.directSize
for _, childName := range dep.deps {
child := deps[childName]
if child != nil {
dep.totalSize += child.totalSize
}
}
}
// Sort.
sorted := make([]Dep, 0, len(deps))
for _, dep := range deps {
sorted = append(sorted, *dep)
}
sort.Slice(sorted, func(i, j int) bool {
return sorted[i].totalSize > sorted[j].totalSize
})
// Print.
for _, dep := range sorted {
direct := formatSize(dep.directSize)
total := formatSize(dep.totalSize)
if dep.approx {
total = "~" + total
}
fmt.Printf("%-80s %10s %10s\n", dep.name, direct, total)
}
return nil
}
func allResolved(deps map[string]*Dep) bool {
for _, parent := range deps {
if !parent.resolved {
return false
}
}
return true
}
func pickUnresolvedDep(deps map[string]*Dep, allowUnresolved int) *Dep {
for _, parent := range deps {
if parent.resolved {
continue
}
// If any subdependency doesn't have its total size resolved,
// don't resolve this one just yet.
depsUnresolved := 0
for _, childName := range parent.deps {
child := deps[childName]
if child != nil && !child.resolved {
depsUnresolved++
}
}
if depsUnresolved <= allowUnresolved {
return parent
}
}
return nil
}
func getSize(name string) (int64, error) {
gopath, found := os.LookupEnv("GOPATH")
if !found {
home, err := os.UserHomeDir()
if err != nil {
return 0, err
}
gopath = path.Join(home, "go")
}
depPath := path.Join(gopath, "pkg", "mod", name)
var size int64
err := filepath.Walk(depPath, func(_ string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
size += info.Size()
}
return err
})
return size, err
}
func formatSize(b int64) string {
if b < 1024 {
return fmt.Sprintf("%dB", b)
}
div, exp := int64(1024), 0
for n := b / 1024; n >= 1024; n /= 1024 {
div *= 1024
exp++
}
unit := "KMGTPE"[exp]
return fmt.Sprintf("%.1f%c", float64(b)/float64(div), unit)
}