-
Notifications
You must be signed in to change notification settings - Fork 0
/
read.go
192 lines (181 loc) · 4.36 KB
/
read.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
package srcdom
import (
"fmt"
"go/ast"
"go/build"
"go/build/constraint"
"go/parser"
"go/token"
"log"
"os"
"sort"
)
// readFile reads a file as a Package.
func readFile(name string) (*Package, error) {
f, err := os.Open(name)
if err != nil {
return nil, err
}
defer f.Close()
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, name, f, 0)
if err != nil {
return nil, err
}
p := &Parser{}
err = p.ScanFile(file)
if err != nil {
return nil, err
}
return p.Package, nil
}
func sortFileNames(src map[string]*ast.File) []string {
names := make([]string, 0, len(src))
for n := range src {
names = append(names, n)
}
sort.Slice(names, func(i, j int) bool {
return names[i] < names[j]
})
return names
}
func toPackages(pkgMap map[string]*ast.Package) []*ast.Package {
pkgs := make([]*ast.Package, 0, len(pkgMap))
for _, p := range pkgMap {
pkgs = append(pkgs, p)
}
return pkgs
}
func joinExprListWithOrExpr(list []constraint.Expr) constraint.Expr {
if len(list) == 1 {
return list[0]
}
return &constraint.OrExpr{X: list[0], Y: joinExprListWithOrExpr(list[1:])}
}
func extractBuildDirectives(file *ast.File) (constraint.Expr, error) {
var plusBuilds []constraint.Expr
for _, group := range file.Comments {
if group.Pos() >= file.Package {
break
}
for _, c := range group.List {
if constraint.IsGoBuild(c.Text) {
return constraint.Parse(c.Text)
}
if !constraint.IsPlusBuild(c.Text) {
continue
}
expr, err := constraint.Parse(c.Text)
if err != nil {
return nil, err
}
plusBuilds = append(plusBuilds, expr)
}
}
if len(plusBuilds) == 0 {
return nil, nil
}
return joinExprListWithOrExpr(plusBuilds), nil
}
var debugFilterdPackage bool = true
// readDir reads all files in a directory as a Package.
func readDir(path string, testPackage bool, tags map[string]bool) (*Package, error) {
fset := token.NewFileSet()
pkgMap, err := parser.ParseDir(fset, path, nil, parser.ParseComments)
if err != nil {
return nil, err
}
var filtered bool
// remove packages which have no files to be built.
for pname, pkg := range pkgMap {
// filter pkg.Files by build tags
for fname, file := range pkg.Files {
expr, err := extractBuildDirectives(file)
if err != nil {
return nil, err
}
if expr == nil {
continue
}
if !expr.Eval(func(tag string) bool { return tags[tag] }) {
delete(pkg.Files, fname)
}
}
if len(pkg.Files) == 0 {
delete(pkgMap, pname)
filtered = true
}
}
if len(pkgMap) == 0 {
if debugFilterdPackage && filtered {
log.Printf("package:%s is empty because filtered\n", path)
}
return &Package{}, nil
}
if len(pkgMap) > 2 {
return nil, fmt.Errorf("multiple packages in directory %s", path)
}
pkgs := toPackages(pkgMap)
// check pkgs includes only target and test packages.
pkg := pkgs[0]
if len(pkgs) == 2 {
testPkg := pkgs[1]
if len(pkg.Name) > len(testPkg.Name) {
pkg, testPkg = testPkg, pkg
}
if pkg.Name+"_test" != testPkg.Name {
return nil, fmt.Errorf("multiple non-test packages in directory %s: %s, %s", path, pkg.Name, testPkg.Name)
}
// use test package.
if testPackage {
pkg = testPkg
}
}
p := &Parser{}
for _, n := range sortFileNames(pkg.Files) {
file := pkg.Files[n]
err := p.ScanFile(file)
if err != nil {
return nil, err
}
}
return p.Package, nil
}
func getTags() map[string]bool {
tagMap := map[string]bool{}
tagMap[build.Default.GOARCH] = true
tagMap[build.Default.GOOS] = true
for _, tags := range [][]string{build.Default.BuildTags, build.Default.ToolTags, build.Default.ReleaseTags} {
for _, tag := range tags {
tagMap[tag] = true
}
}
return tagMap
}
// Read reads a file or directory as a Package.
// If you are going to read a directory, see also ReadDir.
func Read(path string) (*Package, error) {
fi, err := os.Stat(path)
if err != nil {
return nil, err
}
if fi.IsDir() {
tags := getTags()
return readDir(path, false, tags)
}
return readFile(path)
}
// ReadDir reads a directory as a Package. It reads "test" package when
// `testPackage` is set. It will fail if the directory contains non-test
// multiple packages.
func ReadDir(path string, testPackage bool) (*Package, error) {
fi, err := os.Stat(path)
if err != nil {
return nil, err
}
if !fi.IsDir() {
return nil, fmt.Errorf("path is not a directory: %q", path)
}
tags := getTags()
return readDir(path, testPackage, tags)
}