/
scanner.go
261 lines (219 loc) · 5.35 KB
/
scanner.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
// Copyright (C) 2023 Shanhu Tech Inc.
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU Affero General Public License as published by the
// Free Software Foundation, either version 3 of the License, or (at your
// option) any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
// for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package goload
import (
"fmt"
"go/build"
"os"
"path"
"path/filepath"
"sort"
"strings"
"shanhu.io/g/gomod"
"shanhu.io/g/pathutil"
)
// ScanOptions provides the options for scanning a Go language repository.
type ScanOptions struct {
Context *build.Context
// TestdataWhiteList provides a whitelist of "testdata" packages that are
// valid ones and being imported.
TestdataWhiteList map[string]bool
// PkgBlackList is a list of packages that will be skipped. It will also
// skip its sub packages.
PkgBlackList map[string]bool
}
type scanner struct {
path string
ctx *build.Context
opts *ScanOptions
res *ScanResult
gomod bool
modRoot string // import path for the mod root in non-mod mode
modVerRoot string // import path for the mod root in mod enabled mode
vendorStack *vendorStack
vendorLayers map[string]*vendorLayer
vendorScanning bool
}
func newScanner(p string, opts *ScanOptions) *scanner {
if opts == nil {
opts = new(ScanOptions)
}
ret := &scanner{
path: p,
opts: opts,
vendorStack: new(vendorStack),
vendorLayers: make(map[string]*vendorLayer),
}
if opts.Context != nil {
ret.ctx = opts.Context
} else {
ret.ctx = &build.Default
}
return ret
}
func (s *scanner) srcRoot() string {
return filepath.Join(s.ctx.GOPATH, "src")
}
func (s *scanner) warning(dir string, err error) {
s.res.Warnings = append(s.res.Warnings, &scanError{
dir: dir,
err: err,
})
}
func (s *scanner) skipDir(dir *scanDir) bool {
if inSet(s.opts.PkgBlackList, dir.path) {
return true
}
base := dir.base
if strings.HasPrefix(base, "_") || strings.HasPrefix(base, ".") {
return true
}
if base == "testdata" && !inSet(s.opts.TestdataWhiteList, dir.path) {
return true
}
return false
}
func (s *scanner) enterMod(p, vp string) {
s.modRoot, s.modVerRoot = p, vp
}
func (s *scanner) exitMod() { s.modRoot, s.modVerRoot = "", "" }
func (s *scanner) handleDir(dir *scanDir) error {
switch dir.base {
case "vendor":
s.res.HasVendor = true
case "internal":
s.res.HasInternal = true
}
mode := build.ImportComment
if s.gomod {
pkg, err := s.ctx.ImportDir(dir.dir, mode)
if err != nil {
if isNoGoError(err) {
return nil
}
s.warning(dir.path, err)
return nil
}
if len(pkg.GoFiles) == 0 && len(pkg.CgoFiles) == 0 {
return nil
}
s.res.Pkgs[dir.path] = &Package{Build: pkg}
} else if s.vendorScanning {
if inSet(s.opts.PkgBlackList, "!"+dir.path) {
return nil
}
// check if it is a package
pkg, err := s.ctx.Import(dir.path, "", mode)
if err != nil {
if isNoGoError(err) {
return nil
}
s.warning(dir.path, err)
return nil
}
if len(pkg.GoFiles) == 0 && len(pkg.CgoFiles) == 0 {
return nil
}
if dir.vendor != nil {
dir.vendor.addPkg(dir.path)
}
s.res.Pkgs[dir.path] = &Package{Build: pkg}
} else {
pkg, found := s.res.Pkgs[dir.path]
if !found {
return nil
}
if s.modRoot != "" {
pkg.ModRoot = s.modRoot
pkg.ModVerRoot = s.modVerRoot
modRel := pathutil.Relative(s.modRoot, dir.path)
pkg.ModVerPath = path.Join(s.modVerRoot, modRel)
}
importMap := make(map[string]string)
for _, imp := range pkg.Build.Imports {
mapped, hit := s.vendorStack.mapImport(imp)
if !hit {
continue
}
importMap[imp] = mapped
}
if len(importMap) > 0 {
pkg.ImportMap = importMap
}
}
return nil
}
func (s *scanner) walk(dir *scanDir) error {
info, err := os.Lstat(dir.dir)
if err != nil {
return err
}
if !info.IsDir() {
return nil
}
if s.skipDir(dir) {
return nil
}
f, err := os.Open(dir.dir)
if err != nil {
return err
}
defer f.Close()
names, err := f.Readdirnames(-1)
if err != nil {
return err
}
if err := f.Close(); err != nil {
return err
}
sort.Strings(names)
if !s.vendorScanning && findInSorted(names, "vendor") {
ly := s.vendorLayers[dir.path]
if ly == nil {
panic(fmt.Sprintf("vendor layer missing: %s", dir.path))
}
if len(ly.pkgs) > 0 {
s.vendorStack.push(ly)
defer s.vendorStack.pop()
}
}
if !s.vendorScanning && !s.gomod {
if s.modRoot == "" && findInSorted(names, "go.mod") {
p := filepath.Join(dir.dir, "go.mod")
modFile, err := gomod.Parse(p)
if err != nil {
s.warning(dir.path, fmt.Errorf("parse go.mod: %s", err))
} else if isValidModPath(dir.path, modFile.Name) {
s.enterMod(dir.path, modFile.Name)
defer s.exitMod()
}
}
}
if err := s.handleDir(dir); err != nil {
return err
}
for _, name := range names {
sub := dir.sub(name)
if s.vendorScanning && name == "vendor" {
ly := newVendorLayer(dir.path)
s.vendorLayers[dir.path] = ly
sub.vendor = ly
}
if err := s.walk(sub); err != nil {
return err
}
}
return nil
}