-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
mod.go
112 lines (103 loc) · 2.07 KB
/
mod.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
package kimono
import (
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/rwxrob/bonzai/futil"
"github.com/rwxrob/bonzai/run"
)
// Tidy runs `go get -u` and `go mod tidy` on all supported Go
// modules in the current git repository.
func TidyAll(root string) error {
return filepath.WalkDir(root, sanitizeWalkDirFn)
}
func TidyDependents() error {
deps, err := dependencyGraph()
if err != nil {
return err
}
root, err := futil.HereOrAbove(`.git`)
if err != nil {
return err
}
root = filepath.Dir(root)
modName := strings.TrimSpace(run.Out("go", "list", "-m"))
dependents := getDependents(deps, modName)
for _, dep := range dependents {
if dep.path == "" {
continue
}
rel, err := filepath.Rel(root, dep.path)
if err != nil {
return err
}
fmt.Printf("\n%s:\n", rel)
os.Chdir(dep.path)
update()
tidy()
}
return nil
}
func TidyDependencies() error {
deps, err := dependencyGraph()
if err != nil {
return err
}
root, err := futil.HereOrAbove(`.git`)
if err != nil {
return err
}
root = filepath.Dir(root)
modName := strings.TrimSpace(run.Out("go", "list", "-m"))
node := deps.nodes[modName]
for _, dep := range node.dependencies {
if dep.path == "" {
continue
}
rel, err := filepath.Rel(root, dep.path)
if err != nil {
return err
}
fmt.Printf("\n%s:\n", rel)
os.Chdir(dep.path)
update()
tidy()
}
return nil
}
func sanitizeWalkDirFn(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if !d.IsDir() {
return nil
}
if d.Name() == ".git" || d.Name() == "vendor" {
return filepath.SkipDir
}
if !futil.Exists(filepath.Join(path, "go.mod")) {
return filepath.SkipDir
}
if err := os.Chdir(path); err != nil {
return err
}
if !hasDependencies() {
return filepath.SkipDir
}
fmt.Printf("\n%s:\n", path)
_ = update()
_ = tidy()
return nil
}
func hasDependencies() bool {
out := run.Out(`go`, `list`, `-m`, `all`)
return len(strings.Split(out, "\n")) > 1
}
func update() error {
return run.Exec("go", "get", "-u")
}
func tidy() error {
return run.Exec("go", "mod", "tidy")
}