/
walker_imports.go
195 lines (159 loc) · 4.93 KB
/
walker_imports.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
package main
import (
"github.com/jackmanlabs/errors"
"go/ast"
"go/build"
"go/parser"
"go/token"
"log"
"strings"
)
type PackageInfo struct {
ImportPath string
PackageName string
Imports map[string][]string // map[importPath]aliases
}
func getPackageInfoRecursive(pkgPath string) (map[string]PackageInfo, error) {
pkgInfos := make(map[string]PackageInfo) // map[pkgInfoPath]PackageInfo
// The map key is the imported package path.
// The map value indicates if the package has already been scanned.
allImports := make(map[string]bool)
allImports[pkgPath] = false
// This is building a lot of functionality into the loop.
// Let's pray it's not too clever.
for currentImportPath := getUnscannedImport(allImports); currentImportPath != ""; currentImportPath = getUnscannedImport(allImports) {
pkgName, pkgImportPaths, err := getPackageInfo(currentImportPath)
if err != nil {
return nil, errors.Stack(err)
} else if pkgName == "" {
allImports[currentImportPath] = true
continue
} else if shouldIgnore(currentImportPath) {
log.Print("Detected ignored package: " + currentImportPath)
allImports[currentImportPath] = true
continue
}
// For each import extracted, add it to the master list as necessary.
for newImportPath := range pkgImportPaths {
if _, ok := allImports[newImportPath]; !ok {
allImports[newImportPath] = false
}
}
pkgInfo := PackageInfo{
ImportPath: currentImportPath,
PackageName: pkgName,
Imports: pkgImportPaths,
}
pkgInfos[currentImportPath] = pkgInfo
allImports[currentImportPath] = true
}
// We need to make sure that the import paths without pkgInfo have the default alias (package name).
for pkgInfoPath, pkgInfo := range pkgInfos {
for importPath, aliases := range pkgInfo.Imports {
pkgName := pkgInfos[importPath].PackageName
if !sContains(aliases, pkgName) {
// This probably doesn't need to be so verbose, but no harm done.
pkgInfos[pkgInfoPath].Imports[importPath] = append(aliases, pkgName)
}
}
}
return pkgInfos, nil
}
func getUnscannedImport(imports map[string]bool) string {
for mprt, scanned := range imports {
if !scanned {
return mprt
}
}
return ""
}
/*
Returns the package name, the list of imports (import paths), and error.
This function returns a slice to force the consumer to avoid reusing the map
within ImportVisitor.
*/
func getPackageInfo(pkgPath string) (string, map[string][]string, error) {
bpkg, err := build.Import(pkgPath, srcPath, 0)
if err != nil {
//logPackageNotFound(pkgPath)
return "", nil, nil
}
fset := token.NewFileSet()
pkgs, err := parser.ParseDir(fset, bpkg.Dir, nil, parser.AllErrors|parser.ImportsOnly)
if err != nil {
return "", nil, errors.Stack(err)
}
// Some packages irrelevant have "main" packages and "_test" packages.
// We need to prioritize packages that don't have these names.
var pkgToScan *ast.Package
for _, pkg := range pkgs {
// We absolutely don't care about test packages.
if strings.HasSuffix(pkg.Name, "_test") {
continue
}
// We'll take any package if we don't already have one.
if pkgToScan == nil {
pkgToScan = pkg
continue
}
// If we already have a package, we prefer the package that isn't a "main" package.
if pkgToScan.Name == "main" {
pkgToScan = pkg
}
}
if pkgToScan == nil {
return "", nil, errors.New("Did not find a usable package in package path: " + pkgPath)
}
importVisitor := &ImportVisitor{Fset: fset}
if pkgToScan != nil {
ast.Walk(importVisitor, pkgToScan)
}
return pkgToScan.Name, importVisitor.Imports, nil
}
/*
We could have made the ImportVisitor much more complicated by picking out the
data that we know we're going to need later on. Instead of doing that, however,
I'm deliberately choosing to make the ImportVisitor do a single job for the sake
of clarity. I realize this may result in some small measure of code duplication.
*/
type ImportVisitor struct {
Fset *token.FileSet // for debugging
Imports map[string][]string // map[pkgPath]aliases
}
func (this *ImportVisitor) Visit(node ast.Node) (w ast.Visitor) {
if this.Fset == nil {
log.Println("fset is nil.")
return nil
}
switch t := node.(type) {
case *ast.ImportSpec:
if this.Imports == nil {
this.Imports = make(map[string][]string)
}
alias := t.Name.String()
// unused imports don't interest us.
if alias == "_" {
return nil
}
importPath := t.Path.Value
importPath = strings.Trim(importPath, "\"")
_, ok := this.Imports[importPath]
if !ok {
this.Imports[importPath] = make([]string, 0)
}
if alias != "\u003cnil\u003e" {
if !sContains(this.Imports[importPath], alias) {
this.Imports[importPath] = append(this.Imports[importPath], alias)
}
}
return nil
}
return this
}
var missingPackages = make(map[string]bool)
func logPackageNotFound(pkgPath string) {
if _, ok := missingPackages[pkgPath]; !ok {
log.Print("WARNING: Could not find package: ", pkgPath)
missingPackages[pkgPath] = false
}
}