-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwalkDepGraph.go
61 lines (52 loc) · 1.34 KB
/
walkDepGraph.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
package main
import (
"fmt"
)
// analogous to godepgraph's processPackage
func WalkDepGraph(dir string, pkgImportPath string, dc DepContext, pkgMap map[string]JsonObject) error {
if dc.Ignored[pkgImportPath] {
return nil
}
pkg, err := JsonImmediateDep(dir, pkgImportPath)
if err != nil {
return fmt.Errorf("failed to import %s, %s", pkgImportPath, err)
}
if dc.IsIgnored(pkg) {
return nil
}
pkgMap[pkg.GetString("ImportPath")] = pkg
if (pkg.GetBool("Goroot")) && !dc.DelveGoroot {
return nil
}
for _, imp := range getImports(dc, pkg) {
if _, ok := pkgMap[imp]; !ok {
if err := WalkDepGraph(dir, imp, dc, pkgMap); err != nil {
return err
}
}
}
return nil
}
func getImports(dc DepContext, pkg JsonObject) []string {
allImports := pkg.GetStringSlice("Imports")
if dc.IncludeTests {
allImports = append(allImports, pkg.GetStringSlice("TestImports")...)
allImports = append(allImports, pkg.GetStringSlice("XTestImports")...)
}
var imports []string
found := make(map[string]struct{})
pkgImportPath := pkg.GetString("ImportPath")
for _, imp := range allImports {
if imp == pkgImportPath {
// avoiding self-reference
continue
}
if _, ok := found[imp]; ok {
// skipping repeated packges contained in allImports
continue
}
found[imp] = struct{}{}
imports = append(imports, imp)
}
return imports
}