forked from kisielk/godepgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
198 lines (167 loc) · 4.11 KB
/
main.go
File metadata and controls
198 lines (167 loc) · 4.11 KB
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
package main
import (
"flag"
"fmt"
"go/build"
"log"
"os"
"sort"
"strings"
)
var (
pkgs map[string]*build.Package
ids map[string]int
nextId int
ignored = map[string]bool{
"C": true,
}
ignoredPrefixes []string
ignoreStdlib = flag.Bool("s", false, "ignore packages in the Go standard library")
delveGoroot = flag.Bool("d", false, "show dependencies of packages in the Go standard library")
ignorePrefixes = flag.String("p", "", "a comma-separated list of prefixes to ignore")
ignorePackages = flag.String("i", "", "a comma-separated list of packages to ignore")
tagList = flag.String("tags", "", "a comma-separated list of build tags to consider satisified during the build")
horizontal = flag.Bool("horizontal", false, "lay out the dependency graph horizontally instead of vertically")
includeTests = flag.Bool("t", false, "include test packages")
buildTags []string
buildContext = build.Default
)
func main() {
pkgs = make(map[string]*build.Package)
ids = make(map[string]int)
flag.Parse()
args := flag.Args()
if len(args) != 1 {
log.Fatal("need one package name to process")
}
if *ignorePrefixes != "" {
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 {
log.Fatalf("failed to get cwd: %s", err)
}
if err := processPackage(cwd, args[0]); err != nil {
log.Fatal(err)
}
fmt.Println("digraph godep {")
if *horizontal {
fmt.Println(`rankdir="LR"`)
}
// sort packages
pkgKeys := []string{}
for k := range pkgs {
pkgKeys = append(pkgKeys, k)
}
sort.Strings(pkgKeys)
for _, pkgName := range pkgKeys {
pkg := pkgs[pkgName]
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"
}
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)
fmt.Printf("_%d -> _%d;\n", pkgId, impId)
}
}
fmt.Println("}")
}
func processPackage(root string, pkgName string) error {
if ignored[pkgName] {
return nil
}
pkg, err := buildContext.Import(pkgName, root, 0)
if err != nil {
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); 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 isIgnored(pkg *build.Package) bool {
return ignored[pkg.ImportPath] || (pkg.Goroot && *ignoreStdlib) || hasPrefixes(pkg.ImportPath, ignoredPrefixes)
}
func debug(args ...interface{}) {
fmt.Fprintln(os.Stderr, args...)
}
func debugf(s string, args ...interface{}) {
fmt.Fprintf(os.Stderr, s, args...)
}