-
-
Notifications
You must be signed in to change notification settings - Fork 302
/
importpaths.go
281 lines (248 loc) · 5.69 KB
/
importpaths.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
package importpaths
import (
"go/ast"
"go/build"
"go/parser"
"go/token"
"gosublime/margo"
"os"
"path/filepath"
"strings"
"sync"
"time"
)
var (
qidCache = struct {
sync.Mutex
m map[string]*qidNode
}{m: map[string]*qidNode{}}
)
type qidNode struct {
sync.Mutex
Pkg *build.Package
EntName string
DirMod time.Time
EntMod time.Time
}
func PathFilter(path string) bool {
return margo.FilterPath(path) &&
margo.FilterPathExt(path) &&
!strings.Contains(filepath.Base(path), "node_modules")
}
func MakeImportPathsFunc(pathFilter margo.PathFilterFunc) margo.ImportPathsFunc {
return func(srcDir string, bctx *build.Context) map[string]string {
return ImportPaths(srcDir, bctx, pathFilter)
}
}
func ImportPaths(srcDir string, bctx *build.Context, pathFilter margo.PathFilterFunc) map[string]string {
rootDirs := bctx.SrcDirs()
importDir := func(dir string) *build.Package {
p := quickImportDir(bctx, rootDirs, dir)
if p != nil && p.Name != "" && p.ImportPath != "" {
return p
}
return nil
}
srcImportPath := quickImportPath(srcDir)
var pkgs []*build.Package
for _, dir := range rootDirs {
pkgs = append(pkgs, importablePackages(dir, importDir, pathFilter)...)
}
res := make(map[string]string, len(pkgs))
res["unsafe"] = "" // this package doesn't exist on-disk
const vdir = "/vendor/"
var vendored []*build.Package
for _, p := range pkgs {
switch {
case p.Name == "main":
// it's rarely useful to import `package main`
case p.ImportPath == "builtin":
// this package exists for documentation only
case strings.HasPrefix(p.ImportPath, vdir[1:]) || strings.Contains(p.ImportPath, vdir):
// fill these in after everything else so we can tag them
vendored = append(vendored, p)
default:
res[p.ImportPath] = importsName(p)
}
}
if srcImportPath != "" {
sfx := srcImportPath + "/"
for _, p := range vendored {
name := importsName(p) + " [vendored]"
ipath := p.ImportPath
vpos := strings.LastIndex(ipath, vdir)
switch {
case vpos > 0:
pfx := ipath[:vpos+1]
if strings.HasPrefix(sfx, pfx) {
ipath := ipath[vpos+len(vdir):]
res[ipath] = name
}
case strings.HasPrefix(ipath, vdir[1:]):
ipath := ipath[len(vdir)-1:]
res[ipath] = name
}
}
}
return res
}
func quickImportPath(srcDir string) string {
dir := filepath.ToSlash(filepath.Clean(srcDir))
if i := strings.LastIndex(dir, "/src/"); i >= 0 {
return dir[i+5:]
}
return ""
}
func quickImportDir(bctx *build.Context, rootDirs []string, srcDir string) *build.Package {
srcDir = filepath.Clean(srcDir)
qidCache.Lock()
qn := qidCache.m[srcDir]
if qn == nil {
qn = &qidNode{
Pkg: &build.Package{
Dir: srcDir,
ImportPath: quickImportPath(srcDir),
},
}
qidCache.m[srcDir] = qn
}
qidCache.Unlock()
qn.Lock()
defer qn.Unlock()
if qn.Pkg.ImportPath == "" {
return nil
}
dirMod := fileModTime(srcDir)
if dirMod.IsZero() {
return nil
}
if qn.DirMod.Equal(dirMod) {
if qn.Pkg.Name == "" {
// not a Go pkg
return nil
}
if qn.EntName != "" && qn.EntMod.Equal(fileModTime(filepath.Join(srcDir, qn.EntName))) {
return qn.Pkg
}
}
// reset cache
qn.DirMod = dirMod
qn.EntName = ""
qn.Pkg.Name = ""
f, err := os.Open(srcDir)
if err != nil {
return nil
}
defer f.Close()
fset := token.NewFileSet()
for {
names, _ := f.Readdirnames(100)
if len(names) == 0 {
break
}
for _, nm := range names {
ignore := !strings.HasSuffix(nm, ".go") ||
strings.HasSuffix(nm, "_test.go") ||
strings.HasPrefix(nm, ".") ||
strings.HasPrefix(nm, "_")
if ignore {
continue
}
path := filepath.Join(srcDir, nm)
entMod := fileModTime(path)
if entMod.IsZero() {
continue
}
mode := parser.PackageClauseOnly | parser.ParseComments
af, _ := parser.ParseFile(fset, path, nil, mode)
if af == nil {
continue
}
if ok, _ := bctx.MatchFile(srcDir, nm); !ok {
continue
}
qn.Pkg.Name = astFileName(af)
if qn.Pkg.Name != "" {
qn.EntName = nm
qn.EntMod = entMod
return qn.Pkg
}
}
}
return nil
}
func fileModTime(fn string) time.Time {
if fi := fileInfo(fn); fi != nil {
return fi.ModTime()
}
return time.Time{}
}
func fileInfo(fn string) os.FileInfo {
fi, _ := os.Lstat(fn)
return fi
}
func astFileName(af *ast.File) string {
if af == nil || af.Name == nil {
return ""
}
name := af.Name.String()
if name == "" || name == "documentation" {
return ""
}
for _, g := range af.Comments {
for _, c := range g.List {
for _, ln := range strings.Split(c.Text, "\n") {
ln = strings.TrimSpace(ln)
if strings.HasPrefix(ln, "+build ") && strings.Index(ln+" ", " ignore ") > 0 {
return ""
}
}
}
}
return name
}
func importsName(p *build.Package) string {
return p.Name
}
func importablePackages(root string, importDir func(path string) *build.Package, pathFilter margo.PathFilterFunc) []*build.Package {
dirs := allDirNames(root, pathFilter)
var ents []*build.Package
for _, dir := range dirs {
if p := importDir(dir); p != nil {
ents = append(ents, p)
}
}
return ents
}
func allDirNames(dir string, pathFilter margo.PathFilterFunc) []string {
dirs := dirNames(dir, pathFilter)
for _, dir := range dirs {
dirs = append(dirs, allDirNames(dir, pathFilter)...)
}
return dirs
}
func dirNames(dir string, pathFilter func(basename string) bool) []string {
f, err := os.Open(dir)
if err != nil {
return nil
}
defer f.Close()
var dirs []string
for {
names, _ := f.Readdirnames(100)
if len(names) == 0 {
break
}
for _, nm := range names {
path := filepath.Join(dir, nm)
if !pathFilter(path) {
continue
}
fi := fileInfo(path)
if fi != nil && fi.IsDir() {
dirs = append(dirs, path)
}
}
}
return dirs
}