This repository has been archived by the owner on Aug 24, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
main.go
321 lines (277 loc) · 7.78 KB
/
main.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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package main // import "github.com/nix-community/vgo2nix"
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io"
"math"
"os"
"os/exec"
"regexp"
"sort"
"strings"
"golang.org/x/mod/module"
"golang.org/x/mod/semver"
"golang.org/x/tools/go/vcs"
)
type Package struct {
GoPackagePath string
URL string
Rev string
Sha256 string
ModuleDir string
}
type PackageResult struct {
Package *Package
Error error
}
type modEntry struct {
importPath string
repo string
rev string
moduleDir string
}
const depNixFormat = ` {
goPackagePath = "%s";
fetch = {
type = "%s";
url = "%s";
rev = "%s";
sha256 = "%s";
moduleDir = "%s";
};
}`
var versionNumber = regexp.MustCompile(`^v\d+`)
func getModules() ([]*modEntry, error) {
var entries []*modEntry
var stderr bytes.Buffer
cmd := exec.Command("go", "list", "-mod", "mod", "-json", "-m", "all")
cmd.Stderr = &stderr
cmd.Env = append(os.Environ(),
"GO111MODULE=on",
)
stdout, err := cmd.StdoutPipe()
if err != nil {
return nil, err
}
if err := cmd.Start(); err != nil {
return nil, err
}
type goModReplacement struct {
Path string
Version string
}
type goMod struct {
Path string
Main bool
Version string
Replace *goModReplacement
}
var mods []goMod
dec := json.NewDecoder(stdout)
for {
var mod goMod
if err := dec.Decode(&mod); err == io.EOF {
break
} else if err != nil {
return nil, err
}
if !mod.Main {
mods = append(mods, mod)
}
}
if err := cmd.Wait(); err != nil {
return nil, fmt.Errorf("'go list -m all' failed with %s:\n%s", err, stderr.String())
}
for _, mod := range mods {
replacedPath := mod.Path
version := mod.Version
if mod.Replace != nil {
replacedPath = mod.Replace.Path
version = mod.Replace.Version
}
// find repo, and codeRoot
repo, err := vcs.RepoRootForImportPath(replacedPath, false)
if err != nil {
return nil, err
}
// https://github.com/golang/go/blob/7bb6fed9b53494e9846689520b41b8e679bd121d/src/cmd/go/internal/modfetch/coderepo.go#L65
pathPrefix := replacedPath
if repo.Root != replacedPath {
var ok bool
pathPrefix, _, ok = module.SplitPathVersion(pathPrefix)
if !ok {
return nil, fmt.Errorf("invalid mod path: %s", replacedPath)
}
}
// find submodule relative directory
// https://github.com/golang/go/blob/7bb6fed9b53494e9846689520b41b8e679bd121d/src/cmd/go/internal/modfetch/coderepo.go#L74
moduleDir := ""
if pathPrefix != repo.Root {
moduleDir = strings.TrimPrefix(pathPrefix, repo.Root+"/")
}
// convert version to git ref
// https://github.com/golang/go/blob/7bb6fed9b53494e9846689520b41b8e679bd121d/src/cmd/go/internal/modfetch/coderepo.go#L656
build := semver.Build(version) // +incompatible
gitRef := strings.TrimSuffix(version, build)
if strings.Count(gitRef, "-") >= 2 {
// pseudo-version, use the commit hash
gitRef = gitRef[strings.LastIndex(gitRef, "-")+1:]
} else {
if len(moduleDir) > 0 {
// fix tag for submodule
gitRef = moduleDir + "/" + gitRef
}
}
fmt.Println(fmt.Sprintf("goPackagePath %s has rev: %s, module: %s", mod.Path, gitRef, moduleDir))
entries = append(entries, &modEntry{
importPath: mod.Path,
repo: repo.Repo,
rev: gitRef,
moduleDir: moduleDir,
})
}
return entries, nil
}
func getPackages(keepGoing bool, numJobs int, prevDeps map[string]*Package) ([]*Package, error) {
entries, err := getModules()
if err != nil {
return nil, err
}
processEntry := func(entry *modEntry) (*Package, error) {
wrapError := func(err error) error {
return fmt.Errorf("Error processing import path \"%s\": %v", entry.importPath, err)
}
if prevPkg, ok := prevDeps[entry.importPath]; ok {
if prevPkg.URL == entry.repo && prevPkg.Rev == entry.rev {
prevPkg.ModuleDir = entry.moduleDir
return prevPkg, nil
}
}
fmt.Println(fmt.Sprintf("Fetching %s %s", entry.importPath, entry.repo))
// The options for nix-prefetch-git need to match how buildGoPackage
// calls fetchgit:
// https://github.com/NixOS/nixpkgs/blob/8d8e56824de52a0c7a64d2ad2c4ed75ed85f446a/pkgs/development/go-modules/generic/default.nix#L54-L56
// and fetchgit's defaults:
// https://github.com/NixOS/nixpkgs/blob/8d8e56824de52a0c7a64d2ad2c4ed75ed85f446a/pkgs/build-support/fetchgit/default.nix#L15-L23
jsonOut, err := exec.Command(
"nix-prefetch-git",
"--quiet",
"--fetch-submodules",
"--no-deepClone",
"--url", entry.repo,
"--rev", entry.rev).Output()
if err != nil {
exitError, ok := err.(*exec.ExitError)
if ok {
return nil, wrapError(fmt.Errorf("nix-prefetch-git --fetch-submodules --no-deepClone --url %s --rev %s failed:\n%s",
entry.repo,
entry.rev,
exitError.Stderr))
}
return nil, wrapError(fmt.Errorf("failed to execute nix-prefetch-git: %v", err))
}
fmt.Println(fmt.Sprintf("Finished fetching %s", entry.importPath))
var resp map[string]interface{}
if err := json.Unmarshal(jsonOut, &resp); err != nil {
return nil, wrapError(err)
}
sha256 := resp["sha256"].(string)
if sha256 == "0sjjj9z1dhilhpc8pq4154czrb79z9cm044jvn75kxcjv6v5l2m5" {
return nil, wrapError(fmt.Errorf("Bad SHA256 for repo %s with rev: %s", entry.repo, entry.rev))
}
return &Package{
GoPackagePath: entry.importPath,
URL: entry.repo,
Rev: entry.rev,
Sha256: sha256,
ModuleDir: entry.moduleDir,
}, nil
}
worker := func(entries <-chan *modEntry, results chan<- *PackageResult) {
for entry := range entries {
pkg, err := processEntry(entry)
result := &PackageResult{
Package: pkg,
Error: err,
}
results <- result
}
}
jobs := make(chan *modEntry, len(entries))
results := make(chan *PackageResult, len(entries))
for w := 1; w <= int(math.Min(float64(len(entries)), float64(numJobs))); w++ {
go worker(jobs, results)
}
for _, entry := range entries {
jobs <- entry
}
close(jobs)
pkgsMap := make(map[string]*Package)
for j := 1; j <= len(entries); j++ {
result := <-results
if result.Error != nil {
if !keepGoing {
return nil, result.Error
}
msg := fmt.Sprintf("Encountered error: %v", result.Error)
fmt.Println(msg)
continue
}
pkgsMap[result.Package.GoPackagePath] = result.Package
}
// Make output order stable
var packages []*Package
keys := make([]string, 0, len(pkgsMap))
for k := range pkgsMap {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
packages = append(packages, pkgsMap[k])
}
return packages, nil
}
func main() {
var keepGoing = flag.Bool("keep-going", false, "Whether to panic or not if a rev cannot be resolved (default \"false\")")
var goDir = flag.String("dir", "./", "Go project directory")
var out = flag.String("outfile", "deps.nix", "deps.nix output file (relative to project directory)")
var in = flag.String("infile", "deps.nix", "deps.nix input file (relative to project directory)")
var jobs = flag.Int("jobs", 20, "Number of parallel jobs")
flag.Parse()
err := os.Chdir(*goDir)
if err != nil {
panic(err)
}
// Load previous deps from deps.nix so we can reuse hashes for known revs
prevDeps := loadDepsNix(*in)
packages, err := getPackages(*keepGoing, *jobs, prevDeps)
if err != nil {
panic(err)
}
outfile, err := os.Create(*out)
if err != nil {
panic(err)
}
defer func() {
if err := outfile.Close(); err != nil {
panic(err)
}
}()
write := func(line string) {
bytes := []byte(line + "\n")
if _, err := outfile.Write(bytes); err != nil {
panic(err)
}
}
write("# file generated from go.mod using vgo2nix (https://github.com/nix-community/vgo2nix)")
write("[")
for _, pkg := range packages {
write(fmt.Sprintf(depNixFormat,
pkg.GoPackagePath, "git", pkg.URL,
pkg.Rev, pkg.Sha256, pkg.ModuleDir))
}
write("]")
fmt.Println(fmt.Sprintf("Wrote %s", *out))
}