config.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /* Copyright 2018 The Bazel Authors. All rights reserved.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package walk
  13. import (
  14. "flag"
  15. "path"
  16. "github.com/bazelbuild/bazel-gazelle/internal/config"
  17. "github.com/bazelbuild/bazel-gazelle/internal/rule"
  18. )
  19. type walkConfig struct {
  20. excludes []string
  21. ignore bool
  22. follow []string
  23. }
  24. const walkName = "_walk"
  25. func getWalkConfig(c *config.Config) *walkConfig {
  26. return c.Exts[walkName].(*walkConfig)
  27. }
  28. func (wc *walkConfig) isExcluded(rel, base string) bool {
  29. f := path.Join(rel, base)
  30. for _, x := range wc.excludes {
  31. if f == x {
  32. return true
  33. }
  34. }
  35. return false
  36. }
  37. type Configurer struct{}
  38. func (_ *Configurer) RegisterFlags(fs *flag.FlagSet, cmd string, c *config.Config) {
  39. c.Exts[walkName] = &walkConfig{}
  40. }
  41. func (_ *Configurer) CheckFlags(fs *flag.FlagSet, c *config.Config) error { return nil }
  42. func (_ *Configurer) KnownDirectives() []string {
  43. return []string{"exclude", "follow", "ignore"}
  44. }
  45. func (_ *Configurer) Configure(c *config.Config, rel string, f *rule.File) {
  46. wc := getWalkConfig(c)
  47. wcCopy := &walkConfig{}
  48. *wcCopy = *wc
  49. wcCopy.ignore = false
  50. if f != nil {
  51. for _, d := range f.Directives {
  52. switch d.Key {
  53. case "exclude":
  54. wcCopy.excludes = append(wcCopy.excludes, path.Join(rel, d.Value))
  55. case "follow":
  56. wcCopy.follow = append(wcCopy.follow, path.Join(rel, d.Value))
  57. case "ignore":
  58. wcCopy.ignore = true
  59. }
  60. }
  61. }
  62. c.Exts[walkName] = wcCopy
  63. }