/
import.go
198 lines (173 loc) · 4.14 KB
/
import.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
package goutil
import (
"go/build"
"io/ioutil"
"path/filepath"
"strings"
"sync"
)
type ident struct {
ctx *build.Context
imp string
}
var (
pkgcache = map[ident]*Package{}
cmux = new(sync.Mutex)
defaultctx = &build.Default
)
//Context returns a *build.Context with the appropriate tags.
//
//This does not change GOARCH or GOOS, it only sets additional tags.
//
//If no tags are specified, the default context is returned.
func Context(tags ...string) *build.Context {
if len(tags) == 0 {
return defaultctx
}
c := build.Default
//BuildTags currently always nil but nothing says that can't change.
c.BuildTags = append(c.BuildTags, tags...)
return &c
}
func pkgget(ident ident) *Package {
cmux.Lock()
defer cmux.Unlock()
return pkgcache[ident]
}
func pkgset(ident ident, pkg *Package) {
cmux.Lock()
defer cmux.Unlock()
pkgcache[ident] = pkg
}
//Import imports a package.
//
//path is run through ToImport.
//
//If ctx is nil, the default context is used.
//
//N.B. we require a pointer to a build.Context for caching.
//Two build contexts with identical values that are not represented
//by the same pointer will have all packages imported by them
//cached separately.
func Import(ctx *build.Context, path string) (*Package, error) {
if ctx == nil {
ctx = defaultctx
}
root, path, err := ToImport(path)
if err != nil {
return nil, err
}
ident := ident{ctx, path}
if pkg := pkgget(ident); pkg != nil {
return pkg, nil
}
p, err := ctx.Import(path, root, 0)
if err != nil {
return nil, err
}
pkg := &Package{
Context: ctx,
Build: p,
}
pkgset(ident, pkg)
return pkg, nil
}
//ImportTree imports every package in the directory tree rooted at root.
//Root need not have a valid package.
//
//If there are any errors, the first is returned and as many packages as can be
//imported are returned.
func ImportTree(ctx *build.Context, root string) (pkgs Packages, first error) {
root, err := filepath.Abs(root)
if err != nil {
return nil, err
}
importdir(ctx, root, &pkgs, &first)
return
}
//ImportAll imports every package from the standard library
//and every $GOPATH.
//
//If there are any errors, the first is returned and as many packages as can be
//imported are returned.
func ImportAll(ctx *build.Context) (pkgs Packages, first error) {
for _, root := range gopaths {
p, err := ImportTree(ctx, root)
if first != nil {
first = err
}
pkgs = append(pkgs, p...)
}
return
}
func importdir(ctx *build.Context, root string, acc *Packages, first *error) {
hasGoFiles := false
var subdirs []string
fis, err := ioutil.ReadDir(root)
if err != nil && *first == nil {
*first = err
}
for _, fi := range fis {
nm := fi.Name()
if fi.IsDir() {
subdirs = append(subdirs, nm)
} else if strings.HasSuffix(nm, ".go") {
hasGoFiles = true
}
}
if hasGoFiles {
pkg, err := Import(ctx, root)
if err != nil && *first == nil {
*first = err
} else {
*acc = append(*acc, pkg)
}
}
for _, dir := range subdirs {
importdir(ctx, filepath.Join(root, dir), acc, first)
}
}
//ImportDeps imports all dependencies of this package recursively.
//
//It uses the same build.Context this Package was built with.
func (p *Package) ImportDeps() (pkgs Packages, err error) {
seen := map[string]bool{p.Build.ImportPath: true, "C": true}
acc := Packages{p}
defer func() {
if x := recover(); x != nil {
if e, ok := x.(error); ok {
pkgs, err = nil, e
} else {
panic(x)
}
}
}()
recimp(p.Context, p, &acc, seen)
return acc, nil
}
func recimp(ctx *build.Context, root *Package, acc *Packages, seen map[string]bool) {
for _, ch := range root.Build.Imports {
if !seen[ch] {
seen[ch] = true
p, err := Import(ctx, ch)
if err != nil {
panic(err)
}
*acc = append(*acc, p)
recimp(ctx, p, acc, seen)
}
}
}
//ImportRec calls Import on path and then ImportDeps and returns all packages
//with the package described by path as the first element.
func ImportRec(ctx *build.Context, path string) (Packages, error) {
pkg, err := Import(ctx, path)
if err != nil {
return nil, err
}
pkgs, err := pkg.ImportDeps()
if err != nil {
return nil, err
}
return append(Packages{pkg}, pkgs...), nil
}