-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
169 lines (142 loc) · 3.21 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
package main
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"golang.org/x/tools/go/vcs"
"os"
"os/exec"
"regexp"
"strings"
"sync"
)
type Package struct {
GoPackagePath string
URL string
Rev string
Sha256 string
}
const packageFmt = `{
goPackagePath = "%s";
fetch = {
type = "git";
url = "%s";
rev = "%s";
sha256 = "%s";
};
}`
func (pkg Package) String() string {
return fmt.Sprintf(packageFmt, pkg.GoPackagePath, pkg.URL, pkg.Rev, pkg.Sha256)
}
type Prefetch struct {
URL string
Rev string
Sha256 string
}
type Dependency struct {
Path string
Version string
}
func (dep Dependency) String() string {
return fmt.Sprintf("%s %s", dep.Path, dep.Version)
}
func DepsForPath(path string) map[Dependency]struct{} {
const depFmt = "{{ .ImportPath }} {{ .Standard }} {{ .DepOnly }} {{ if .Module }}{{ .Module.Path }} {{ .Module.Version }}{{ end }}"
cmd := exec.Command("go", "list", "-deps", "-f", depFmt, path)
stdout, err := cmd.StdoutPipe()
if err != nil {
panic(err)
}
if err := cmd.Start(); err != nil {
panic(err)
}
scanner := bufio.NewScanner(stdout)
deps := make(map[Dependency]struct{})
for scanner.Scan() {
components := strings.Split(scanner.Text(), " ")
importPath, standard, depOnly := components[0], components[1] == "true", components[2] == "true"
if standard || !depOnly {
continue
}
if len(components) < 5 {
panic(fmt.Sprintf("%s is neither a module nor in the standard library", importPath))
}
packagePath, version := components[3], components[4]
if version == "" {
// A package in the current directory
continue
}
deps[Dependency{
Path: packagePath,
Version: version,
}] = struct{}{}
}
if err := cmd.Wait(); err != nil {
switch err := err.(type) {
case *exec.ExitError:
panic(err.Stderr)
}
}
return deps
}
var pseudoVersionRegex = regexp.MustCompile("v[0-9.]+.-[0-9]+-([0-9a-f]+)")
func PrefetchDependency(dep Dependency) Package {
repoRoot, err := vcs.RepoRootForImportPath(dep.Path, false)
if err != nil {
panic(err)
}
rev := dep.Version
match := pseudoVersionRegex.FindStringSubmatch(rev)
if len(match) > 0 {
rev = match[1]
}
prefetchOut, err := exec.Command("nix-prefetch-git", "--quiet", repoRoot.Repo, "--rev", rev).Output()
if err != nil {
panic(err)
}
var prefetch Prefetch
json.Unmarshal([]byte(prefetchOut), &prefetch)
return Package{
GoPackagePath: dep.Path,
URL: prefetch.URL,
Sha256: prefetch.Sha256,
Rev: prefetch.Rev,
}
}
func main() {
// No options at the moment
arguments := flag.NewFlagSet("", flag.ExitOnError)
arguments.Usage = func() {
fmt.Fprintln(arguments.Output(), "Usage:", os.Args[0], "[path]")
}
arguments.Parse(os.Args[1:])
path := "./."
switch len(arguments.Args()) {
case 0:
case 1:
path = arguments.Arg(0)
default:
arguments.Usage()
os.Exit(1)
}
deps := DepsForPath(path)
queue := make(chan Package)
wg := sync.WaitGroup{}
wg.Add(len(deps))
go func() {
wg.Wait()
close(queue)
}()
for dep, _ := range deps {
go func(dep Dependency) {
defer wg.Done()
queue <- PrefetchDependency(dep)
}(dep)
}
fmt.Print("[")
for dep := range queue {
fmt.Print(dep)
}
fmt.Println("]")
}