-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgopath_libs.go
94 lines (86 loc) · 1.73 KB
/
gopath_libs.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
package dragon
import (
"bufio"
"go/ast"
"go/parser"
"go/token"
"os"
"path/filepath"
"strings"
)
func fetchGoImportsIgnore(src string) (map[string]bool, error) {
dirs := make(map[string]bool)
f, err := os.Open(filepath.Join(src, ".goimportsignore"))
if err != nil {
return dirs, nil
}
scr := bufio.NewScanner(f)
for scr.Scan() {
dir := scr.Text()
if !strings.HasPrefix(dir, "#") {
dirs[filepath.Join(src, dir)] = true
}
}
return dirs, scr.Err()
}
func isSkipDir(fi fileInfo, ignoreDirs map[string]bool) bool {
name := fi.Name()
switch name {
case "", "internal", "testdata", "vendor", "cache":
return true
}
switch name[0] {
case '.', '_':
return true
}
return ignoreDirs[filepath.Join(fi.path, fi.Name())]
}
func gopathLibs(libChan chan lib) error {
for _, srcDir := range srcDirs() {
ignoreDirs, err := fetchGoImportsIgnore(srcDir)
if err != nil {
return err
}
err = concurrentWalk(srcDir, func(info fileInfo) error {
if info.isDir(false) {
if isSkipDir(info, ignoreDirs) {
return filepath.SkipDir
}
return nil
}
if strings.HasSuffix(info.Name(), "_test.go") {
return nil
}
if !strings.HasSuffix(info.Name(), ".go") {
return nil
}
path := info.path
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, path, nil, 0)
if err != nil {
return nil
}
pkg := f.Name.Name
if pkg == "main" {
return nil
}
importPath, err := filepath.Rel(srcDir, filepath.Dir(path))
if err != nil {
return nil
}
for _, v := range f.Scope.Objects {
if ast.IsExported(v.Name) {
libChan <- lib{
object: v.Name,
path: importPath,
}
}
}
return nil
})
if err != nil {
return err
}
}
return nil
}