forked from src-d/go-parse-utils
/
importer.go
235 lines (196 loc) · 6.09 KB
/
importer.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
package parseutil
import (
"fmt"
"go/ast"
"go/build"
"go/importer"
"go/parser"
"go/token"
"go/types"
"os"
"path/filepath"
"strings"
"sync"
)
// ErrNotInGoPath is an error returned when a package is not in any of the
// possible go paths.
var ErrNotInGoPath = fmt.Errorf("parseutil: package is not in any of the go paths")
// GoPath is the collection of all go paths.
type GoPath []string
// Abs returns the absolute path to a package. The go path in the absolute path
// that will be returned is the first that contains the given package.
func (gp GoPath) Abs(pkg string) (string, error) {
path, err := gp.PathOf(pkg)
if err != nil {
return "", err
}
return filepath.Join(path, "src", pkg), nil
}
// PathOf returns the first go path that contains the given package.
func (gp GoPath) PathOf(pkg string) (string, error) {
for _, p := range gp {
if _, err := os.Stat(filepath.Join(p, "src", pkg)); err == nil {
return p, nil
} else if !os.IsNotExist(err) {
return "", err
}
}
return "", ErrNotInGoPath
}
// DefaultGoPath contains the default list of go paths provided either via
// GOPATH environment variable or the default value.
var DefaultGoPath = GoPath(filepath.SplitList(build.Default.GOPATH))
// FileFilter returns true if the given file needs to be kept.
type FileFilter func(pkgPath, file string, typ FileType) bool
// FileFilters represents a colection of FileFilter
type FileFilters []FileFilter
// KeepFile returns true if and only if the file passes all FileFilters.
func (fs FileFilters) KeepFile(pkgPath, file string, typ FileType) bool {
for _, f := range fs {
if !f(pkgPath, file, typ) {
return false
}
}
return true
}
// Filter returns the files passed in files that satisfy all FileFilters.
func (fs FileFilters) Filter(pkgPath string, files []string, typ FileType) (filtered []string) {
for _, f := range files {
if fs.KeepFile(pkgPath, f, typ) {
filtered = append(filtered, f)
}
}
return
}
// FileType represents the type of go source file type.
type FileType string
const (
GoFile FileType = "go"
CgoFile FileType = "cgo"
)
// Importer is an implementation of `types.Importer` and `types.ImporterFrom`
// that builds actual source files and not the compiled objects in the pkg
// directory.
// It is safe to use it concurrently.
// A package is cached after building it the first time.
type Importer struct {
mut sync.RWMutex
cache map[string]*types.Package
defaultImporter types.Importer
}
// NewImporter creates a new Importer instance with the default importer of
// the runtime assigned as the underlying importer.
func NewImporter() *Importer {
return &Importer{
cache: make(map[string]*types.Package),
defaultImporter: importer.Default(),
}
}
// Import returns the imported package for the given import
// path, or an error if the package couldn't be imported.
// Two calls to Import with the same path return the same
// package.
func (i *Importer) Import(path string) (*types.Package, error) {
return i.ImportWithFilters(path, nil)
}
// ImportWithFilters works like Import but filtering the source files to parse using
// the passed FileFilters.
func (i *Importer) ImportWithFilters(path string, filters FileFilters) (*types.Package, error) {
return i.ImportFromWithFilters(path, "", 0, filters)
}
// ImportFrom returns the imported package for the given import
// path when imported by the package in srcDir, or an error
// if the package couldn't be imported. The mode value must
// be 0; it is reserved for future use.
// Two calls to ImportFrom with the same path and srcDir return
// the same package.
func (i *Importer) ImportFrom(path, srcDir string, mode types.ImportMode) (*types.Package, error) {
return i.ImportFromWithFilters(path, srcDir, mode, nil)
}
// ImportFromWithFilters works like ImportFrom but filters the source files using
// the passed FileFilters.
func (i *Importer) ImportFromWithFilters(path, srcDir string, mode types.ImportMode, filters FileFilters) (*types.Package, error) {
i.mut.Lock()
if pkg, ok := i.cache[path]; ok {
i.mut.Unlock()
return pkg, nil
}
i.mut.Unlock()
root, files, err := i.GetSourceFiles(path, srcDir, filters)
if err != nil {
return nil, err
}
// If it's not on the GOPATH use the default importer instead
useDefaultImporter := true
for _, p := range DefaultGoPath {
if strings.HasPrefix(root, p) {
useDefaultImporter = false
break
}
}
if useDefaultImporter {
i.mut.Lock()
defer i.mut.Unlock()
var pkg *types.Package
var err error
imp, ok := i.defaultImporter.(types.ImporterFrom)
if ok {
pkg, err = imp.ImportFrom(path, srcDir, mode)
} else {
pkg, err = imp.Import(path)
}
if err != nil {
return nil, err
}
i.cache[path] = pkg
return pkg, nil
}
pkg, err := i.ParseSourceFiles(root, files)
if err != nil {
return nil, err
}
i.mut.Lock()
i.cache[path] = pkg
i.mut.Unlock()
return pkg, nil
}
// GetSourceFiles return the go files available in src under path after applying the filters.
func (i *Importer) GetSourceFiles(path, srcDir string, filters FileFilters) (string, []string, error) {
srcDir, err := filepath.Abs(srcDir)
if err != nil {
return "", nil, err
}
pkg, err := build.Import(path, srcDir, 0)
if err != nil {
return "", nil, err
}
var filenames []string
filenames = append(filenames, filters.Filter(path, pkg.GoFiles, GoFile)...)
filenames = append(filenames, filters.Filter(path, pkg.CgoFiles, CgoFile)...)
if len(filenames) == 0 {
return "", nil, fmt.Errorf("no go source files in path: %s", path)
}
var paths []string
for _, f := range filenames {
paths = append(paths, filepath.Join(pkg.Dir, f))
}
return pkg.Dir, paths, nil
}
// ParseSourceFiles parses the files in paths under root returning a types.Package
// and an optional error
func (i *Importer) ParseSourceFiles(root string, paths []string) (*types.Package, error) {
var files []*ast.File
fs := token.NewFileSet()
for _, p := range paths {
f, err := parser.ParseFile(fs, p, nil, 0)
if err != nil {
return nil, err
}
files = append(files, f)
}
config := types.Config{
FakeImportC: true,
Importer: i,
}
return config.Check(root, fs, files, nil)
}