@@ -25,6 +25,7 @@ import (
2525 "flag"
2626 "log"
2727 "maps"
28+ "strings"
2829
2930 "github.com/bazelbuild/bazel-gazelle/config"
3031 "github.com/bazelbuild/bazel-gazelle/label"
@@ -35,13 +36,15 @@ import (
3536)
3637
3738const (
38- languageName = "starlark_bundle"
39- starlarkBundleRootDirectiveName = "starlark_bundle_root"
39+ languageName = "starlark_bundle"
40+ starlarkBundleRootDirectiveName = "starlark_bundle_root"
41+ starlarkBundleExcludeDirectiveName = "starlark_bundle_exclude"
4042)
4143
4244type starlarkBundleLang struct {
43- starlarkBundleRoot * string
44- starlarkLibraries map [label.Label ]* rule.Rule
45+ starlarkBundleRoot * string
46+ starlarkBundleExcludeDirs []string
47+ starlarkLibraries map [label.Label ]* rule.Rule
4548}
4649
4750// NewLanguage is called by Gazelle to install this language extension in a
@@ -62,25 +65,31 @@ func (*starlarkBundleLang) Name() string {
6265// The following methods are implemented to satisfy the
6366// https://pkg.go.dev/github.com/bazelbuild/bazel-gazelle/resolve?tab=doc#Resolver
6467// interface, but are otherwise unused.
65- func (l * starlarkBundleLang ) RegisterFlags (fs * flag.FlagSet , cmd string , c * config.Config ) {
68+ func (ext * starlarkBundleLang ) RegisterFlags (fs * flag.FlagSet , cmd string , c * config.Config ) {
6669}
6770
6871func (* starlarkBundleLang ) CheckFlags (fs * flag.FlagSet , c * config.Config ) error {
6972 return nil
7073}
7174
7275func (* starlarkBundleLang ) KnownDirectives () []string {
73- return []string {starlarkBundleRootDirectiveName }
76+ return []string {
77+ starlarkBundleRootDirectiveName ,
78+ starlarkBundleExcludeDirectiveName ,
79+ }
7480}
7581
76- func (l * starlarkBundleLang ) Configure (c * config.Config , rel string , f * rule.File ) {
82+ func (ext * starlarkBundleLang ) Configure (c * config.Config , rel string , f * rule.File ) {
7783 if f != nil {
7884 for _ , d := range f .Directives {
79- if d .Key == starlarkBundleRootDirectiveName {
80- if l .starlarkBundleRoot != nil {
81- log .Fatalf ("gazelle:%s should only be set once (refusing to override %q with %q)" , starlarkBundleRootDirectiveName , * l .starlarkBundleRoot , d .Value )
85+ switch d .Key {
86+ case starlarkBundleRootDirectiveName :
87+ if ext .starlarkBundleRoot != nil {
88+ log .Fatalf ("gazelle:%s should only be set once (refusing to override %q with %q)" , starlarkBundleRootDirectiveName , * ext .starlarkBundleRoot , d .Value )
8289 }
83- l .starlarkBundleRoot = & rel
90+ ext .starlarkBundleRoot = & rel
91+ case starlarkBundleExcludeDirectiveName :
92+ ext .starlarkBundleExcludeDirs = append (ext .starlarkBundleExcludeDirs , d .Value )
8493 }
8594 }
8695 }
@@ -92,6 +101,7 @@ func (l *starlarkBundleLang) Configure(c *config.Config, rel string, f *rule.Fil
92101func (* starlarkBundleLang ) Kinds () map [string ]rule.KindInfo {
93102 kinds := map [string ]rule.KindInfo {}
94103 maps .Copy (kinds , starlarkLibraryKindInfo )
104+ maps .Copy (kinds , starlarkBundleKindInfo )
95105 return kinds
96106}
97107
@@ -101,6 +111,7 @@ func (*starlarkBundleLang) Kinds() map[string]rule.KindInfo {
101111func (* starlarkBundleLang ) Loads () []rule.LoadInfo {
102112 return []rule.LoadInfo {
103113 starlarkLibraryLoadInfo ,
114+ starlarkBundleLoadInfo ,
104115 }
105116}
106117
@@ -115,10 +126,12 @@ func (*starlarkBundleLang) Fix(c *config.Config, f *rule.File) {
115126//
116127// If nil is returned, the rule will not be indexed. If any non-nil slice is
117128// returned, including an empty slice, the rule will be indexed.
118- func (b * starlarkBundleLang ) Imports (c * config.Config , r * rule.Rule , f * rule.File ) []resolve.ImportSpec {
129+ func (ext * starlarkBundleLang ) Imports (c * config.Config , r * rule.Rule , f * rule.File ) []resolve.ImportSpec {
119130 switch r .Kind () {
120131 case starlarkLibraryKind :
121132 return starlarkLibraryImports (c , r , f )
133+ case starlarkBundleKind :
134+ return starlarkBundleImports (c , r , f )
122135 }
123136 return nil
124137}
@@ -138,10 +151,12 @@ func (*starlarkBundleLang) Embeds(r *rule.Rule, from label.Label) []label.Label
138151// Resolve generates a "deps" attribute (or the appropriate language-specific
139152// equivalent) for each import according to language-specific rules and
140153// heuristics.
141- func (* starlarkBundleLang ) Resolve (c * config.Config , ix * resolve.RuleIndex , rc * repo.RemoteCache , r * rule.Rule , importsRaw interface {}, from label.Label ) {
154+ func (ext * starlarkBundleLang ) Resolve (c * config.Config , ix * resolve.RuleIndex , rc * repo.RemoteCache , r * rule.Rule , importsRaw interface {}, from label.Label ) {
142155 switch r .Kind () {
143156 case starlarkLibraryKind :
144- starlarkLibraryResolve (c , ix , rc , r , importsRaw , from )
157+ starlarkLibraryResolve (c , ix , r , importsRaw , from )
158+ case starlarkBundleKind :
159+ starlarkBundleResolve (r , ext .starlarkLibraries )
145160 }
146161}
147162
@@ -157,13 +172,19 @@ func (*starlarkBundleLang) Resolve(c *config.Config, ix *resolve.RuleIndex, rc *
157172//
158173// Any non-fatal errors this function encounters should be logged using
159174// log.Print.
160- func (l * starlarkBundleLang ) GenerateRules (args language.GenerateArgs ) (result language.GenerateResult ) {
161- if l .starlarkBundleRoot == nil {
175+ func (ext * starlarkBundleLang ) GenerateRules (args language.GenerateArgs ) (result language.GenerateResult ) {
176+ if ext .starlarkBundleRoot == nil {
162177 return
163178 }
179+ for _ , root := range ext .starlarkBundleExcludeDirs {
180+ if strings .HasPrefix (args .Rel , root ) {
181+ return
182+ }
183+ }
164184
165185 for _ , r := range []language.GenerateResult {
166- starlarkLibararyGenerate (args ),
186+ starlarkLibraryGenerate (args , ext .starlarkLibraries ),
187+ starlarkBundleGenerate (args , ext .starlarkBundleRoot ),
167188 } {
168189 result .Gen = append (result .Gen , r .Gen ... )
169190 result .Imports = append (result .Imports , r .Imports ... )
0 commit comments