Repository navigation
Expand file tree
/
Copy pathgo_selfdep.go
More file actions
238 lines (218 loc) · 6.83 KB
/
Copy pathgo_selfdep.go
File metadata and controls
238 lines (218 loc) · 6.83 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
226
227
228
229
230
231
232
233
234
235
236
237
238
package devflow
import (
"fmt"
"os"
"path/filepath"
"strings"
"webtyp.com/command"
)
type publishedSubmodule struct {
rel string // slash-separated relative path from repo root, e.g. "stories"
modulePath string // full Go module path, e.g. "github.com/veltylabs/service_catalog/stories"
}
var privateSubmoduleDirs = map[string]bool{
"tests": true,
"testdata": true,
"testproject": true,
"bench": true,
"compare": true,
"examples": true,
"vendor": true,
"node_modules": true,
}
func isPrivateSubmoduleDir(relSlash string) bool {
for _, seg := range strings.Split(relSlash, "/") {
if seg == "" || strings.HasPrefix(seg, ".") || strings.HasPrefix(seg, "_") || privateSubmoduleDirs[seg] {
return true
}
}
return false
}
// publishableSubmodules returns nested Go modules inside g.rootDir whose module path is
// parentModulePath + "/" + rel and that do not live under private/test directories.
func (g *Go) publishableSubmodules(parentModulePath string) []publishedSubmodule {
if g.rootDir == "" || parentModulePath == "" {
return nil
}
absRoot, err := filepath.Abs(g.rootDir)
if err != nil {
return nil
}
var subs []publishedSubmodule
_ = filepath.Walk(absRoot, func(path string, info os.FileInfo, err error) error {
if err != nil {
return nil
}
if info.IsDir() {
name := info.Name()
if path != absRoot && (strings.HasPrefix(name, ".") || strings.HasPrefix(name, "_") || privateSubmoduleDirs[name]) {
return filepath.SkipDir
}
return nil
}
if info.Name() != "go.mod" {
return nil
}
absDir, err := filepath.Abs(filepath.Dir(path))
if err != nil || absDir == absRoot {
return nil
}
rel, err := filepath.Rel(absRoot, absDir)
if err != nil {
return nil
}
relSlash := filepath.ToSlash(rel)
if isPrivateSubmoduleDir(relSlash) {
return nil
}
subMod, err := modulePathOf(absDir)
if err != nil || subMod != parentModulePath+"/"+relSlash {
return nil
}
subs = append(subs, publishedSubmodule{rel: relSlash, modulePath: subMod})
return nil
})
return subs
}
func modulePathOf(dir string) (string, error) {
data, err := os.ReadFile(filepath.Join(dir, "go.mod"))
if err != nil {
return "", err
}
for _, line := range strings.Split(string(data), "\n") {
line = strings.TrimSpace(line)
if strings.HasPrefix(line, "module ") {
return strings.TrimSpace(strings.TrimPrefix(line, "module ")), nil
}
}
return "", fmt.Errorf("module directive not found in %s", dir)
}
func (g *Go) headTags() []string {
if g.rootDir == "" {
return nil
}
out, err := command.Exec("git", "-C", g.rootDir, "tag", "--points-at", "HEAD").Output()
if err != nil {
return nil
}
var tags []string
for _, line := range strings.Split(strings.TrimSpace(string(out)), "\n") {
if t := strings.TrimSpace(line); t != "" {
tags = append(tags, t)
}
}
return tags
}
func (g *Go) hasHeadTag(tag string) bool {
for _, t := range g.headTags() {
if t == tag {
return true
}
}
return false
}
func (g *Go) missingSubmoduleTags(parentModulePath, version string) []publishedSubmodule {
if version == "" {
return nil
}
var missing []publishedSubmodule
for _, sub := range g.publishableSubmodules(parentModulePath) {
if !g.hasHeadTag(sub.rel + "/" + version) {
missing = append(missing, sub)
}
}
return missing
}
// publishSubmoduleTags creates and pushes <rel>/<version> tags for every publishable submodule
// that does not already carry that tag on HEAD.
func (g *Go) publishSubmoduleTags(parentModulePath, version string) ([]string, error) {
if g.git == nil || version == "" {
return nil, nil
}
var created []string
for _, sub := range g.missingSubmoduleTags(parentModulePath, version) {
subTag := sub.rel + "/" + version
if _, err := g.git.CreateTag(subTag); err != nil {
return created, fmt.Errorf("create submodule tag %s: %w", subTag, err)
}
if _, err := g.git.PushWithTags(subTag); err != nil {
return created, fmt.Errorf("push submodule tag %s: %w", subTag, err)
}
created = append(created, subTag)
}
return created, nil
}
// cascadeSubmodules updates dependents of every publishable submodule whose <rel>/<version>
// tag points at HEAD.
func (g *Go) cascadeSubmodules(parentModulePath, version, searchPath string) error {
for _, sub := range g.publishableSubmodules(parentModulePath) {
if !g.hasHeadTag(sub.rel + "/" + version) {
continue
}
if err := g.UpdateDependents(sub.modulePath, version, searchPath); err != nil {
return err
}
}
return nil
}
// syncInternalSubmodules finds all submodules inside the current repo that depend
// on the parent module, and prepares them for the upcoming release:
// 1. Ensures they have a relative replace pointing to the parent.
// 2. Bumps their requirement of the parent module to the nextTag.
// 3. Runs 'go mod tidy'.
// These changes are made pre-commit so they are included in the release tag.
func (g *Go) syncInternalSubmodules(parentModulePath, nextTag string) error {
absRoot, _ := filepath.Abs(g.rootDir)
// 1. Find all go.mod files inside the repo (excluding root)
var submods []string
err := filepath.Walk(absRoot, func(path string, info os.FileInfo, err error) error {
if err != nil {
return nil
}
if info.Name() == "go.mod" {
absDir, _ := filepath.Abs(filepath.Dir(path))
if absDir != absRoot {
submods = append(submods, absDir)
}
}
return nil
})
if err != nil {
return err
}
for _, subDir := range submods {
if !g.HasDependency(filepath.Join(subDir, "go.mod"), parentModulePath) {
continue
}
g.log(fmt.Sprintf("Syncing internal submodule: %s", filepath.Base(subDir)))
// 1. Ensure relative replace
rel, err := filepath.Rel(subDir, absRoot)
if err != nil {
continue
}
gomod := NewGoModHandler()
gomod.SetRootDir(subDir)
gomod.EnsureReplace(parentModulePath, rel)
if err := gomod.Save(); err != nil {
return fmt.Errorf("failed to save submodule go.mod: %w", err)
}
// 2. Bump requirement to nextTag
// Note: We use go get parent@tag. Since we have a replace, this just updates go.mod
// without needing the tag to be published yet.
target := fmt.Sprintf("%s@%s", parentModulePath, nextTag)
if _, err := command.RunInDir(subDir, "go", "get", target); err != nil {
// If it fails (e.g. tag doesn't exist yet and proxy is used),
// we might need to use a different approach or just ignore if it's not strictly needed
// for the build (because of the replace).
// But gopush's goal is to have the correct version in go.mod.
// Try with GOPROXY=off or similar?
// Actually, 'go get' should work if the replace is there and we are just updating the version string.
g.log(fmt.Sprintf("Warning: go get %s failed in %s: %v", target, subDir, err))
}
// 3. Tidy
if _, err := command.RunInDir(subDir, "go", "mod", "tidy"); err != nil {
return fmt.Errorf("go mod tidy failed in %s: %w", subDir, err)
}
}
return nil
}