forked from Masterminds/glide
/
tree.go
251 lines (217 loc) · 5.01 KB
/
tree.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
package cmd
import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"github.com/Masterminds/cookoo"
)
// Tree prints a tree representing dependencies.
func Tree(c cookoo.Context, p *cookoo.Params) (interface{}, cookoo.Interrupt) {
buildContext, err := GetBuildContext()
if err != nil {
return nil, err
}
showcore := p.Get("showcore", false).(bool)
basedir := p.Get("dir", ".").(string)
myName := guessPackageName(buildContext, basedir)
if basedir == "." {
var err error
basedir, err = os.Getwd()
if err != nil {
Error("Could not get working directory")
return nil, err
}
}
fmt.Println(myName)
displayTree(buildContext, basedir, myName, 1, showcore)
return nil, nil
}
// ListDeps lists all of the dependencies of the current project.
//
// Params:
//
// Returns:
//
func ListDeps(c cookoo.Context, p *cookoo.Params) (interface{}, cookoo.Interrupt) {
buildContext, err := GetBuildContext()
if err != nil {
return nil, err
}
basedir := p.Get("dir", ".").(string)
myName := guessPackageName(buildContext, basedir)
basedir, err = filepath.Abs(basedir)
if err != nil {
return nil, err
}
direct := map[string]*pinfo{}
d := walkDeps(buildContext, basedir, myName)
for _, i := range d {
listDeps(buildContext, direct, i, basedir)
}
sortable := make([]string, len(direct))
i := 0
for k := range direct {
sortable[i] = k
i++
}
sort.Strings(sortable)
for _, k := range sortable {
t := direct[k].PType
fmt.Printf("%s (Location: %s)\n", k, ptypeString(t))
}
return nil, nil
}
func listDeps(b *BuildCtxt, info map[string]*pinfo, name, path string) {
found := findPkg(b, name, path)
switch found.PType {
case ptypeUnknown:
info[name] = found
break
case ptypeGoroot, ptypeCgo:
break
default:
info[name] = found
for _, i := range walkDeps(b, found.Path, found.Name) {
listDeps(b, info, i, found.Path)
}
}
}
func displayTree(b *BuildCtxt, basedir, myName string, level int, core bool) {
deps := walkDeps(b, basedir, myName)
for _, name := range deps {
found := findPkg(b, name, basedir)
if found.PType == ptypeUnknown {
msg := "glide get " + found.Name
fmt.Printf("\t%s\t(%s)\n", found.Name, msg)
continue
}
if !core && found.PType == ptypeGoroot || found.PType == ptypeCgo {
continue
}
fmt.Print(strings.Repeat("\t", level))
fmt.Printf("%s (%s)\n", found.Name, found.Path)
displayTree(b, found.Path, found.Name, level+1, core)
}
}
type ptype int8
const (
ptypeUnknown ptype = iota
ptypeLocal
ptypeVendor
ptypeGopath
ptypeGoroot
ptypeCgo
)
func ptypeString(t ptype) string {
switch t {
case ptypeLocal:
return "local"
case ptypeVendor:
return "vendored"
case ptypeGopath:
return "gopath"
case ptypeGoroot:
return "core"
case ptypeCgo:
return "cgo"
default:
return "missing"
}
}
type pinfo struct {
Name, Path string
PType ptype
Vendored bool
}
func findPkg(b *BuildCtxt, name, cwd string) *pinfo {
var fi os.FileInfo
var err error
var p string
info := &pinfo{
Name: name,
}
// Recurse backward to scan other vendor/ directories
for wd := cwd; wd != "/"; wd = filepath.Dir(wd) {
p = filepath.Join(wd, "vendor", name)
if fi, err = os.Stat(p); err == nil && (fi.IsDir() || isLink(fi)) {
info.Path = p
info.PType = ptypeVendor
info.Vendored = true
return info
}
}
// Check $GOPATH
for _, r := range strings.Split(b.GOPATH, ":") {
p = filepath.Join(r, "src", name)
if fi, err = os.Stat(p); err == nil && (fi.IsDir() || isLink(fi)) {
info.Path = p
info.PType = ptypeGopath
return info
}
}
// Check $GOROOT
for _, r := range strings.Split(b.GOROOT, ":") {
p = filepath.Join(r, "src", name)
if fi, err = os.Stat(p); err == nil && (fi.IsDir() || isLink(fi)) {
info.Path = p
info.PType = ptypeGoroot
return info
}
}
// Finally, if this is "C", we're dealing with cgo
if name == "C" {
info.PType = ptypeCgo
}
return info
}
func isLink(fi os.FileInfo) bool {
return fi.Mode()&os.ModeSymlink == os.ModeSymlink
}
func walkDeps(b *BuildCtxt, base, myName string) []string {
externalDeps := []string{}
filepath.Walk(base, func(path string, fi os.FileInfo, err error) error {
if excludeSubtree(path, fi) {
if fi.IsDir() {
return filepath.SkipDir
}
return nil
}
pkg, err := b.ImportDir(path, 0)
if err != nil {
return err
}
if pkg.Goroot {
return nil
}
for _, imp := range pkg.Imports {
//if strings.HasPrefix(imp, myName) {
////Info("Skipping %s because it is a subpackage of %s", imp, myName)
//continue
//}
if imp == myName {
continue
}
externalDeps = append(externalDeps, imp)
}
return nil
})
return externalDeps
}
func excludeSubtree(path string, fi os.FileInfo) bool {
top := filepath.Base(path)
if !fi.IsDir() && !isLink(fi) {
return true
}
// Provisionally, we'll skip vendor. We definitely
// should skip testdata.
if top == "vendor" || top == "testdata" {
return true
}
// Skip anything that starts with _
if strings.HasPrefix(top, "_") || (strings.HasPrefix(top, ".") && top != ".") {
return true
}
return false
}