forked from github/depstubber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto-detect.go
283 lines (250 loc) · 7.44 KB
/
auto-detect.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
282
283
package main
import (
"bytes"
"fmt"
"go/token"
"go/types"
"sort"
"strings"
"github.com/golang/dep/gps/paths"
"golang.org/x/tools/go/packages"
"golang.org/x/tools/go/vcs"
)
type CombinedErrors struct {
errs []error
}
func (ce *CombinedErrors) Error() string {
buf := new(bytes.Buffer)
buf.WriteString("The following errors occurred:")
for _, err := range ce.errs {
if err != nil {
buf.WriteString("\n - " + err.Error())
}
}
return buf.String()
}
func allNil(errs ...error) bool {
for _, err := range errs {
if err != nil {
return false
}
}
return true
}
func CombineErrors(errs ...error) error {
if len(errs) == 0 || allNil(errs...) {
return nil
}
return &CombinedErrors{
errs: errs,
}
}
func loadPackage(startPkg string, dir string) (*packages.Package, error) {
config := &packages.Config{
Mode: packages.LoadSyntax | packages.NeedModule,
}
// Set the package loader Dir to the `dir`; that will force
// the package loader to use the `go.mod` file and thus
// load the wanted version of the package:
config.Dir = dir
pkgs, err := packages.Load(config, startPkg)
if err != nil {
return nil, fmt.Errorf("error while running packages.Load: %s", err)
}
var errs []error
packages.Visit(pkgs, nil, func(pkg *packages.Package) {
for _, err := range pkg.Errors {
errs = append(errs, err)
}
})
if len(errs) > 0 {
return nil, fmt.Errorf("error while packages.Load: %s", CombineErrors(errs...))
}
return pkgs[0], nil
}
// DeduplicateStrings returns a new slice with duplicate values removed.
func DeduplicateStrings(slice []string) []string {
if len(slice) <= 1 {
return slice
}
result := []string{}
seen := make(map[string]struct{})
for _, val := range slice {
if _, ok := seen[val]; !ok {
result = append(result, val)
seen[val] = struct{}{}
}
}
return result
}
// removeBlankIdentifier returns a new slice with blank identifier `_` removed.
func removeBlankIdentifier(slice []string) []string {
result := []string{}
for _, val := range slice {
if val != "_" {
result = append(result, val)
}
}
return result
}
// removeUnexported returns a new slice with all unexported identifiers removed.
func removeUnexported(slice []string) []string {
result := []string{}
for _, val := range slice {
if token.IsExported(val) {
result = append(result, val)
}
}
return result
}
func autoDetect(startPkg string, dir string) (map[string][]string, map[string][]string, map[string][]string, error) {
pk, err := loadPackage(startPkg, dir)
if err != nil {
return nil, nil, nil, fmt.Errorf("error while loading package: %s", err)
}
rootOfStartPkg, _ := vcs.RepoRootForImportPath(pk.Types.Path(), false)
pathToTypeNames := make(map[string][]string)
pathToFuncAndVarNames := make(map[string][]string)
pathToDirTmp := make(map[string][]string)
for path, v := range pk.Imports {
if v.Module != nil && v.Module.Dir != "" {
pathToDirTmp[path] = append(pathToDirTmp[path], v.Module.Dir)
}
}
for _, obj := range pk.TypesInfo.Uses {
if obj.Pkg() == nil || obj.Pkg().Path() == "" {
// Skip objects that don't belong to a package.
continue
}
if isStd := paths.IsStandardImportPath(obj.Pkg().Path()); isStd {
// Skip objects that belong to a Go standard library (supposedly).
continue
}
if packageIsSamePath := obj.Pkg().Path() == pk.Types.Path(); packageIsSamePath {
// Skip objects that belong to the initial package that was scanned.
continue
}
if notExported := !obj.Exported(); notExported {
panic(fmt.Sprintf("Encountered unexpected unexported type %v, which should not be accessible by this package (%s).", obj, obj.Pkg().Path()))
}
// Check whether obj.Pkg().Path() is a subpath of pk.Types.Path() (or the other way round), i.e. they belong to the same root package.
// Skip objects belonging to packages that have the same root as the initial package.
pathsOverlap := strings.HasPrefix(obj.Pkg().Path(), pk.Types.Path()+"/") || strings.HasPrefix(pk.Types.Path(), obj.Pkg().Path()+"/")
if rootOfStartPkg != nil {
// Check with root:
rootOfThisObjPkg, err := vcs.RepoRootForImportPath(obj.Pkg().Path(), false)
if err == nil && rootOfStartPkg.Root == rootOfThisObjPkg.Root {
continue
} else {
// Check with string prefix:
if pathsOverlap {
continue
}
}
} else {
// Check with string prefix:
if pathsOverlap {
continue
}
}
pkgPath := obj.Pkg().Path()
switch thing := obj.(type) {
case *types.TypeName:
pathToTypeNames[pkgPath] = append(pathToTypeNames[pkgPath], obj.Name())
case *types.Const:
pathToFuncAndVarNames[pkgPath] = append(pathToFuncAndVarNames[pkgPath], thing.Name())
case *types.Var:
// Ignore fields
if isNotAField := !thing.IsField(); isNotAField {
pathToFuncAndVarNames[pkgPath] = append(pathToFuncAndVarNames[pkgPath], thing.Name())
}
case *types.Func:
switch sig := thing.Type().(type) {
case *types.Signature:
if notAMethod := sig.Recv() == nil; notAMethod {
// This is a normal function.
pathToFuncAndVarNames[pkgPath] = append(pathToFuncAndVarNames[pkgPath], thing.Name())
}
default:
panic(fmt.Sprintf("non-signature type %T for function %s", thing.Type(), obj.String()))
}
default:
panic(fmt.Sprintf("unknown type %T for object %s", obj, obj.String()))
}
}
{
// Deduplicate and sort:
for pkgPath := range pathToTypeNames {
dedup := DeduplicateStrings(pathToTypeNames[pkgPath])
dedup = removeBlankIdentifier(dedup)
dedup = removeUnexported(dedup)
sort.Strings(dedup)
pathToTypeNames[pkgPath] = dedup
}
for pkgPath := range pathToFuncAndVarNames {
dedup := DeduplicateStrings(pathToFuncAndVarNames[pkgPath])
dedup = removeBlankIdentifier(dedup)
dedup = removeUnexported(dedup)
sort.Strings(dedup)
pathToFuncAndVarNames[pkgPath] = dedup
}
}
pathToDir := make(map[string][]string)
// Select only used paths:
{
for pkgPath := range pathToTypeNames {
pathToDir[pkgPath] = pathToDirTmp[pkgPath]
}
for pkgPath := range pathToFuncAndVarNames {
pathToDir[pkgPath] = pathToDirTmp[pkgPath]
}
}
return pathToTypeNames, pathToFuncAndVarNames, pathToDir, nil
}
// FormatDepstubberComment returns the `depstubber` comment that will be used to stub types.
// The returned string is prefixed with //
func FormatDepstubberComment(path string, typeNames []string, funcAndVarNames []string) string {
var first string
if len(typeNames) > 0 {
typeNames = DeduplicateStrings(typeNames)
sort.Strings(typeNames)
first = strings.Join(typeNames, ",")
} else {
first = `""`
}
var second string
if len(funcAndVarNames) > 0 {
funcAndVarNames = DeduplicateStrings(funcAndVarNames)
sort.Strings(funcAndVarNames)
second = strings.Join(funcAndVarNames, ",")
}
return strings.TrimSpace(fmt.Sprintf(
"//go:generate depstubber -vendor %s %s %s",
path,
first,
second,
))
}
// printGoGenerateComments prints the `go:generate` depstubber comments.
func printGoGenerateComments(pathToTypeNames map[string][]string, pathToFuncAndVarNames map[string][]string) {
pkgPaths := make([]string, 0)
{
// Get a list of all package paths:
for path := range pathToTypeNames {
pkgPaths = append(pkgPaths, path)
}
for path := range pathToFuncAndVarNames {
pkgPaths = append(pkgPaths, path)
}
pkgPaths = DeduplicateStrings(pkgPaths)
sort.Strings(pkgPaths)
}
for _, pkgPath := range pkgPaths {
comment := FormatDepstubberComment(
pkgPath,
pathToTypeNames[pkgPath],
pathToFuncAndVarNames[pkgPath],
)
fmt.Println(comment)
}
}