-
Notifications
You must be signed in to change notification settings - Fork 389
/
Copy pathconfig.go
253 lines (207 loc) · 6.89 KB
/
config.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
/* Copyright 2018 The Bazel Authors. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package walk
import (
"bufio"
"errors"
"flag"
"fmt"
"io/fs"
"log"
"os"
"path"
"strings"
"github.com/bazelbuild/bazel-gazelle/config"
"github.com/bazelbuild/bazel-gazelle/rule"
bzl "github.com/bazelbuild/buildtools/build"
"github.com/bmatcuk/doublestar/v4"
gzflag "github.com/bazelbuild/bazel-gazelle/flag"
)
// generationModeType represents one of the generation modes.
type generationModeType string
// Generation modes
const (
// Update: update and maintain existing BUILD files
generationModeUpdate generationModeType = "update_only"
// Create: create new and update existing BUILD files
generationModeCreate generationModeType = "create_and_update"
)
// TODO(#472): store location information to validate each exclude. They
// may be set in one directory and used in another. Excludes work on
// declared generated files, so we can't just stat.
type walkConfig struct {
updateOnly bool
excludes []string
ignore bool
follow []string
}
const walkName = "_walk"
func getWalkConfig(c *config.Config) *walkConfig {
return c.Exts[walkName].(*walkConfig)
}
func (wc *walkConfig) isExcluded(p string) bool {
return matchAnyGlob(wc.excludes, p)
}
func (wc *walkConfig) shouldFollow(p string) bool {
return matchAnyGlob(wc.follow, p)
}
var _ config.Configurer = (*Configurer)(nil)
type Configurer struct{}
func (*Configurer) RegisterFlags(fs *flag.FlagSet, cmd string, c *config.Config) {
wc := &walkConfig{}
c.Exts[walkName] = wc
fs.Var(&gzflag.MultiFlag{Values: &wc.excludes}, "exclude", "pattern that should be ignored (may be repeated)")
}
func (*Configurer) CheckFlags(fs *flag.FlagSet, c *config.Config) error { return nil }
func (*Configurer) KnownDirectives() []string {
return []string{"generation_mode", "exclude", "follow", "ignore"}
}
func (cr *Configurer) Configure(c *config.Config, rel string, f *rule.File) {
wc := getWalkConfig(c)
wcCopy := &walkConfig{}
*wcCopy = *wc
wcCopy.ignore = false
if f != nil {
for _, d := range f.Directives {
switch d.Key {
case "generation_mode":
switch generationModeType(strings.TrimSpace(d.Value)) {
case generationModeUpdate:
wcCopy.updateOnly = true
case generationModeCreate:
wcCopy.updateOnly = false
default:
log.Fatalf("unknown generation_mode %q in //%s", d.Value, f.Pkg)
continue
}
case "exclude":
if err := checkPathMatchPattern(path.Join(rel, d.Value)); err != nil {
log.Printf("the exclusion pattern is not valid %q: %s", path.Join(rel, d.Value), err)
continue
}
wcCopy.excludes = append(wcCopy.excludes, path.Join(rel, d.Value))
case "follow":
if err := checkPathMatchPattern(path.Join(rel, d.Value)); err != nil {
log.Printf("the follow pattern is not valid %q: %s", path.Join(rel, d.Value), err)
continue
}
wcCopy.follow = append(wcCopy.follow, path.Join(rel, d.Value))
case "ignore":
if d.Value != "" {
log.Printf("the ignore directive does not take any arguments. Did you mean to use gazelle:exclude instead? in //%s '# gazelle:ignore %s'", f.Pkg, d.Value)
}
wcCopy.ignore = true
}
}
}
c.Exts[walkName] = wcCopy
}
type isIgnoredFunc = func(string) bool
var nothingIgnored isIgnoredFunc = func(string) bool { return false }
func loadBazelIgnore(repoRoot string) (isIgnoredFunc, error) {
ignorePath := path.Join(repoRoot, ".bazelignore")
file, err := os.Open(ignorePath)
if errors.Is(err, fs.ErrNotExist) {
return nothingIgnored, nil
}
if err != nil {
return nothingIgnored, fmt.Errorf(".bazelignore exists but couldn't be read: %v", err)
}
defer file.Close()
excludes := make(map[string]struct{})
scanner := bufio.NewScanner(file)
for scanner.Scan() {
ignore := strings.TrimSpace(scanner.Text())
if ignore == "" || string(ignore[0]) == "#" {
continue
}
// Bazel ignore paths are always relative to repo root.
// Glob patterns are not supported.
if strings.ContainsAny(ignore, "*?[") {
log.Printf("the .bazelignore exclusion pattern must not be a glob %s", ignore)
continue
}
// Clean the path to remove any extra '.', './' etc otherwise
// the exclude matching won't work correctly.
ignore = path.Clean(ignore)
excludes[ignore] = struct{}{}
}
isBazelIgnored := func(p string) bool {
_, ok := excludes[p]
return ok
}
return isBazelIgnored, nil
}
func loadRepoDirectoryIgnore(repoRoot string) (isIgnoredFunc, error) {
repoFilePath := path.Join(repoRoot, "REPO.bazel")
repoFileContent, err := os.ReadFile(repoFilePath)
if errors.Is(err, fs.ErrNotExist) {
return nothingIgnored, nil
}
if err != nil {
return nothingIgnored, fmt.Errorf("REPO.bazel exists but couldn't be read: %v", err)
}
ast, err := bzl.Parse(repoRoot, repoFileContent)
if err != nil {
return nothingIgnored, fmt.Errorf("failed to parse REPO.bazel: %v", err)
}
var ignoreDirectories []string
// Search for ignore_directories([...ignore strings...])
for _, expr := range ast.Stmt {
if call, isCall := expr.(*bzl.CallExpr); isCall {
if inv, isIdentCall := call.X.(*bzl.Ident); isIdentCall && inv.Name == "ignore_directories" {
if len(call.List) != 1 {
return nothingIgnored, fmt.Errorf("REPO.bazel ignore_directories() expects one argument")
}
list, isList := call.List[0].(*bzl.ListExpr)
if !isList {
return nothingIgnored, fmt.Errorf("REPO.bazel ignore_directories() unexpected argument type: %T", call.List[0])
}
for _, item := range list.List {
if strExpr, isStr := item.(*bzl.StringExpr); isStr {
if err := checkPathMatchPattern(strExpr.Value); err != nil {
log.Printf("the ignore_directories() pattern %q is not valid: %s", strExpr.Value, err)
continue
}
ignoreDirectories = append(ignoreDirectories, strExpr.Value)
}
}
// Only a single ignore_directories() is supported in REPO.bazel and searching can stop.
break
}
}
}
if len(ignoreDirectories) == 0 {
return nothingIgnored, nil
}
isRepoIgnored := func(p string) bool {
for _, ignore := range ignoreDirectories {
if doublestar.MatchUnvalidated(ignore, p) {
return true
}
}
return false
}
return isRepoIgnored, nil
}
func checkPathMatchPattern(pattern string) error {
_, err := doublestar.Match(pattern, "x")
return err
}
func matchAnyGlob(patterns []string, path string) bool {
for _, x := range patterns {
if doublestar.MatchUnvalidated(x, path) {
return true
}
}
return false
}