forked from qax-os/goreporter
/
depend.go
306 lines (267 loc) · 6.19 KB
/
depend.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
package depend
import (
"bytes"
"fmt"
"go/build"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"github.com/golang/glog"
)
var (
pkgs map[string]*build.Package
ids map[string]int
nextId int
ignored = map[string]bool{
"C": true,
}
ignoredPrefixes []string
// ignore packages in the Go standard library
ignoreStdlib = true
delveGoroot = false
ignorePrefixes = ""
// a comma-separated list of packages to ignore
ignorePackages = ""
// a comma-separated list of build tags to consider satisified during the build
tagList = ""
horizontal = true
// include test packages
includeTests = false
buildTags []string
buildContext = build.Default
vendors []string
)
func Depend(path, expect string) string {
ignorePackages = expect
vendors = getVendorlist(path)
// add root vendor
vendors = append(vendors, "vendor")
pkgs = make(map[string]*build.Package)
ids = make(map[string]int)
args := []string{path}
if len(args) != 1 {
glog.Errorln("need one package name to process")
return ""
}
if ignorePrefixes != "" {
if runtime.GOOS == `windows` {
ignorePrefixes = strings.Replace(ignorePrefixes, "/", `\`, -1)
}
ignoredPrefixes = strings.Split(ignorePrefixes, ",")
}
if ignorePackages != "" {
for _, p := range strings.Split(ignorePackages, ",") {
ignored[p] = true
}
}
if tagList != "" {
buildTags = strings.Split(tagList, ",")
}
buildContext.BuildTags = buildTags
cwd, err := os.Getwd()
if err != nil {
glog.Errorf("failed to get cwd: %s", err)
return ""
}
if err := processPackage(cwd, strings.Replace(args[0], `\`, "/", -1), path); err != nil {
glog.Errorln(err)
return ""
}
graph := "digraph godep {"
// fmt.Println("digraph godep {")
if horizontal {
// fmt.Println(`rankdir="LR"`)
graph += `rankdir="LR"`
}
for pkgName, pkg := range pkgs {
pkgId := getId(pkgName)
if isIgnored(pkg) {
continue
}
var color string
if pkg.Goroot {
color = "palegreen"
} else if len(pkg.CgoFiles) > 0 {
color = "darkgoldenrod1"
} else {
color = "paleturquoise"
}
graph += fmt.Sprintf("%d [label=\"%s\" style=\"filled\" color=\"%s\"];\n", pkgId, pkgName, color)
// fmt.Printf("%d [label=\"%s\" style=\"filled\" color=\"%s\"];\n", pkgId, pkgName, color)
// Don't render imports from packages in Goroot
if pkg.Goroot && !delveGoroot {
continue
}
for _, imp := range getImports(pkg) {
impPkg := pkgs[imp]
if impPkg == nil || isIgnored(impPkg) {
continue
}
impId := getId(imp)
graph += fmt.Sprintf("%d -> %d;\n", pkgId, impId)
// fmt.Printf("%d -> %d;\n", pkgId, impId)
}
}
graph += `}`
err = ioutil.WriteFile("graph.gv", []byte(graph), 0666)
if err != nil {
glog.Errorln(err)
}
// convert file formate
cmdsvg := exec.Command("dot", "-Tsvg", "-o", "pkgdep.svg", "graph.gv")
var outsvg bytes.Buffer
cmdsvg.Stdout = &outsvg
cmdsvg.Stderr = os.Stderr
err = cmdsvg.Run()
if err != nil {
glog.Errorln(err)
}
svg, err := ioutil.ReadFile("pkgdep.svg")
if err != nil {
glog.Errorln(err)
}
err = os.Remove("pkgdep.svg")
if err != nil {
glog.Errorln(err)
}
err = os.Remove("graph.gv")
if err != nil {
glog.Errorln(err)
}
return string(svg)
}
func processPackage(root string, pkgName, path string) error {
if ignored[pkgName] {
return nil
}
var err error
if !build.IsLocalImport(pkgName) {
root, err = filepath.Abs(path)
if err != nil {
return fmt.Errorf("failed to convert path to absolute path %s ", err)
}
}
pkg, err := buildContext.Import(pkgName, root, 0)
if err != nil {
flag := false
for i := 0; i < len(vendors); i++ {
pkg, err = buildContext.Import(vendors[i]+string(filepath.Separator)+pkgName, root, 0)
if err == nil {
flag = true
break
}
}
if !flag {
return fmt.Errorf("failed to import %s: %s", pkgName, err)
}
}
if isIgnored(pkg) {
return nil
}
pkgs[pkg.ImportPath] = pkg
// Don't worry about dependencies for stdlib packages
if pkg.Goroot && !delveGoroot {
return nil
}
for _, imp := range getImports(pkg) {
if _, ok := pkgs[imp]; !ok {
if err := processPackage(root, imp, path); err != nil {
return err
}
}
}
return nil
}
func getImports(pkg *build.Package) []string {
allImports := pkg.Imports
if includeTests {
allImports = append(allImports, pkg.TestImports...)
allImports = append(allImports, pkg.XTestImports...)
}
var imports []string
found := make(map[string]struct{})
for _, imp := range allImports {
if imp == pkg.ImportPath {
// Don't draw a self-reference when foo_test depends on foo.
continue
}
if _, ok := found[imp]; ok {
continue
}
found[imp] = struct{}{}
imports = append(imports, imp)
}
return imports
}
func getId(name string) int {
id, ok := ids[name]
if !ok {
id = nextId
nextId++
ids[name] = id
}
return id
}
func hasPrefixes(s string, prefixes []string) bool {
for _, p := range prefixes {
if strings.HasPrefix(s, p) {
return true
}
}
return false
}
func hasPackage(pkgpath string) bool {
for k, _ := range ignored {
if strings.Contains(pkgpath, k) {
return true
}
}
return false
}
func isIgnored(pkg *build.Package) bool {
return ignored[pkg.ImportPath] || (pkg.Goroot && ignoreStdlib) || hasPrefixes(pkg.ImportPath, ignoredPrefixes) || hasPackage(pkg.ImportPath)
}
func debug(args ...interface{}) {
fmt.Fprintln(os.Stderr, args...)
}
func debugf(s string, args ...interface{}) {
fmt.Fprintf(os.Stderr, s, args...)
}
func getVendorlist(path string) []string {
vendors := make([]string, 0)
err := filepath.Walk(path, func(path string, f os.FileInfo, err error) error {
if f == nil {
return err
}
if !f.IsDir() {
return nil
}
if strings.HasSuffix(path, "vendor") && path != "" {
vendors = append(vendors, PackageAbsPath(path))
}
return nil
})
if err != nil {
glog.Errorf("filepath.Walk() returned %v\n", err)
}
return vendors
}
func PackageAbsPath(path string) (packagePath string) {
_, err := os.Stat(path)
if err != nil {
glog.Errorln("package path is invalid")
return ""
}
absPath, err := filepath.Abs(path)
if err != nil {
glog.Errorln(err)
}
packagePathIndex := strings.Index(absPath, "src")
if -1 != packagePathIndex {
packagePath = absPath[(packagePathIndex + 4):]
}
return packagePath
}