forked from davecheney/graphpkg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
150 lines (135 loc) · 2.55 KB
/
main.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
// Graphpkg produces an svg graph of the dependency tree of a package
//
// Requires
// - dot (graphviz)
//
// Usage
//
// graphpkg path/to/your/package
package main
import (
"flag"
"fmt"
"go/build"
"io"
"log"
"os"
"os/exec"
"path"
"regexp"
"strings"
"github.com/pkg/browser"
)
var (
pkgs = make(map[string][]string)
matchvar = flag.String("match", ".*", "filter packages")
stdout = flag.Bool("stdout", false, "print to standard output instead of browser")
pkgmatch *regexp.Regexp
)
func findImport(p string, rootp string) {
if !pkgmatch.MatchString(p) {
// doesn't match the filter, skip it
return
}
if p == "C" {
// C isn't really a package
pkgs["C"] = nil
}
if _, ok := pkgs[p]; ok {
// seen this package before, skip it
return
}
if strings.HasPrefix(p, "golang_org") {
p = path.Join("vendor", p)
}
pkg, err := build.Import(p, rootp, 0)
if err != nil {
log.Fatal(err)
}
pkgs[p] = filter(pkg.Imports)
for _, pkg := range pkgs[p] {
findImport(pkg, rootp)
}
}
func filter(s []string) []string {
var r []string
for _, v := range s {
if pkgmatch.MatchString(v) {
r = append(r, v)
}
}
return r
}
func allKeys() []string {
keys := make(map[string]bool)
for k, v := range pkgs {
keys[k] = true
for _, v := range v {
keys[v] = true
}
}
v := make([]string, 0, len(keys))
for k, _ := range keys {
v = append(v, k)
}
return v
}
func keys() map[string]int {
m := make(map[string]int)
for i, k := range allKeys() {
m[k] = i
}
return m
}
func init() {
flag.Parse()
pkgmatch = regexp.MustCompile(*matchvar)
}
func check(err error) {
if err != nil {
log.Fatal(err)
}
}
func main() {
var rootPkg * build.Package
var e error
for _, pkg := range flag.Args() {
rootPkg, e = build.Import(pkg, "", 0)
if e != nil {
log.Fatal(e)
}
findImport(pkg, rootPkg.Dir)
}
cmd := exec.Command("dot", "-Tsvg")
in, err := cmd.StdinPipe()
check(err)
out, err := cmd.StdoutPipe()
cmd.Stderr = os.Stderr
check(cmd.Start())
fmt.Fprintf(in, "digraph {\n")
keys := keys()
for p, i := range keys {
fmt.Fprintf(in, "\tN%d [label=%q,shape=box];\n", i, p)
}
for k, v := range pkgs {
for _, p := range v {
fmt.Fprintf(in, "\tN%d -> N%d [weight=1];\n", keys[k], keys[p])
}
}
fmt.Fprintf(in, "}\n")
in.Close()
if *stdout {
// print to standard output
io.Copy(os.Stdout, out)
} else {
// pipe output to browser
ch := make(chan error)
go func() {
ch <- browser.OpenReader(out)
}()
check(cmd.Wait())
if err := <-ch; err != nil {
log.Fatalf("unable to open browser: %s", err)
}
}
}