This repository has been archived by the owner on Nov 6, 2019. It is now read-only.
/
dep.go
194 lines (158 loc) · 3.52 KB
/
dep.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
package main
import (
"go/build"
"os"
"path/filepath"
"github.com/pkg/errors"
)
type DepType uint8
const (
DepTypeMain DepType = 1 << iota
DepTypeTest
)
type Link struct {
DepType DepType
Node *Node
}
type Node struct {
Name string
Dir string
Imports map[string]Link
}
type Graph struct {
Entries map[string]*Node
}
func BuildDepGraph(pkgs []string, basedir string) (*Graph, error) {
n := &nursery{
cwd: basedir,
ctx: build.Default,
seen: make(map[[2]string]*Node),
byPath: make(map[string]*Node),
basedirs: make(map[string]string),
}
var err error
f := &Graph{Entries: make(map[string]*Node)}
for _, path := range pkgs {
f.Entries[path], err = n.visit(basedir, path)
if err != nil {
return nil, errors.Wrap(err, "could not build dependency tree for "+path)
}
}
return f, nil
}
type nursery struct {
cwd string
ctx build.Context
seen map[[2]string]*Node
byPath map[string]*Node
basedirs map[string]string
}
// TODO: Implement proper CGO support. For now, users will have to manually
// specify their CGO deps.
var cgo = &Node{Name: "C", Imports: make(map[string]Link)}
// visit visits a node and its children in depth-first order, so we know that as
// we process the node itself, all of its deps are already seen.
func (n *nursery) visit(basedir, path string) (*Node, error) {
if path == "C" {
return cgo, nil
}
var bds []string
td := basedir
Loop:
for {
if bd, ok := n.basedirs[td]; ok {
td = bd
break
}
hv, err := hasVendor(td)
switch {
case err != nil:
return nil, errors.Wrap(err, "could not check vendor dir")
case !hv:
next := filepath.Dir(td)
if next == n.ctx.GOPATH || next == filepath.Join(n.ctx.GOROOT, "src") {
break Loop
}
bds = append(bds, td)
td = next
default:
break Loop
}
}
for _, bd := range bds {
n.basedirs[bd] = td
}
basedir = td
if node, ok := n.seen[[2]string{basedir, path}]; ok {
return node, nil
}
pkg, err := n.ctx.Import(path, basedir, 0)
if err != nil {
return nil, errors.Wrap(err, "could not visit node "+path)
}
if cNode, ok := n.byPath[pkg.Dir]; ok {
n.seen[[2]string{basedir, path}] = cNode
return cNode, nil
}
node := &Node{
Name: path,
Dir: pkg.Dir,
Imports: make(map[string]Link),
}
n.seen[[2]string{basedir, path}] = node
n.byPath[pkg.Dir] = node
for _, i := range pkg.Imports {
child, err := n.visit(pkg.Dir, i)
if err != nil {
return nil, errors.Wrap(err, "could not visit child node of "+path)
}
node.Imports[i] = Link{
DepType: DepTypeMain,
Node: child,
}
}
for _, i := range pkg.TestImports {
child, err := n.visit(pkg.Dir, i)
if err != nil {
return nil, errors.Wrap(err, "could not visit child node of "+path)
}
link, ok := node.Imports[i]
if !ok {
link = Link{
Node: child,
}
}
link.DepType |= DepTypeTest
node.Imports[i] = link
}
return node, nil
}
func hasVendor(path string) (bool, error) {
_, err := os.Stat(filepath.Join(path, "vendor"))
switch {
case err == nil:
return true, nil
case os.IsNotExist(err):
return false, nil
default:
return false, err
}
}
// TopoWalk visits every node in each tree in the graph in reverse topological order.
func (f *Graph) TopoWalk(visit func(*Node)) {
visited := make(map[*Node]struct{})
var topovisit func(n *Node)
topovisit = func(n *Node) {
if _, ok := visited[n]; ok {
return
}
visited[n] = struct{}{}
for _, c := range n.Imports {
topovisit(c.Node)
}
visit(n)
}
for _, n := range f.Entries {
topovisit(n)
}
}