forked from tools/godep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pkg.go
81 lines (72 loc) · 1.91 KB
/
pkg.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
package main
import (
"regexp"
"strings"
)
// Package represents a Go package.
type Package struct {
Dir string
Root string
ImportPath string
Deps []string
Standard bool
Processed bool
GoFiles []string
CgoFiles []string
IgnoredGoFiles []string
TestGoFiles []string
TestImports []string
XTestGoFiles []string
XTestImports []string
Error struct {
Err string
}
// --- New stuff for now
Imports []string
}
// LoadPackages loads the named packages using go list -json.
// Unlike the go tool, an empty argument list is treated as an empty list; "."
// must be given explicitly if desired.
// IgnoredGoFiles will be processed and their dependencies resolved recursively
// Files with a build tag of `ignore` are skipped. Files with other build tags
// are however processed.
func LoadPackages(names ...string) (a []*Package, err error) {
debugln("LoadPackages", names)
if len(names) == 0 {
return nil, nil
}
for _, i := range importPaths(names) {
p, err := listPackage(i)
if err != nil {
return nil, err
}
a = append(a, p)
}
return a, nil
}
func (p *Package) allGoFiles() []string {
var a []string
a = append(a, p.GoFiles...)
a = append(a, p.CgoFiles...)
a = append(a, p.TestGoFiles...)
a = append(a, p.XTestGoFiles...)
a = append(a, p.IgnoredGoFiles...)
return a
}
// matchPattern(pattern)(name) reports whether
// name matches pattern. Pattern is a limited glob
// pattern in which '...' means 'any string' and there
// is no other special syntax.
// Taken from $GOROOT/src/cmd/go/main.go.
func matchPattern(pattern string) func(name string) bool {
re := regexp.QuoteMeta(pattern)
re = strings.Replace(re, `\.\.\.`, `.*`, -1)
// Special case: foo/... matches foo too.
if strings.HasSuffix(re, `/.*`) {
re = re[:len(re)-len(`/.*`)] + `(/.*)?`
}
reg := regexp.MustCompile(`^` + re + `$`)
return func(name string) bool {
return reg.MatchString(name)
}
}