github.com/stackb/rules_proto@v0.0.0-20240221195024-5428336c51f1/pkg/rule/rules_nodejs/js_library.go (about) 1 package rules_nodejs 2 3 import ( 4 "strings" 5 6 "github.com/bazelbuild/bazel-gazelle/config" 7 "github.com/bazelbuild/bazel-gazelle/label" 8 "github.com/bazelbuild/bazel-gazelle/resolve" 9 "github.com/bazelbuild/bazel-gazelle/rule" 10 11 "github.com/stackb/rules_proto/pkg/protoc" 12 ) 13 14 var jsLibraryKindInfo = rule.KindInfo{ 15 MergeableAttrs: map[string]bool{ 16 "srcs": true, 17 }, 18 NonEmptyAttrs: map[string]bool{ 19 "srcs": true, 20 }, 21 ResolveAttrs: map[string]bool{ 22 "deps": true, 23 }, 24 } 25 26 // JsLibrary implements RuleProvider for 'js_library'-derived rules. 27 type JsLibrary struct { 28 KindName string 29 RuleNameSuffix string 30 Outputs []string 31 Config *protoc.ProtocConfiguration 32 RuleConfig *protoc.LanguageRuleConfig 33 Resolver protoc.DepsResolver 34 } 35 36 // Kind implements part of the ruleProvider interface. 37 func (s *JsLibrary) Kind() string { 38 return s.KindName 39 } 40 41 // Name implements part of the ruleProvider interface. 42 func (s *JsLibrary) Name() string { 43 return s.Config.Library.BaseName() + s.RuleNameSuffix 44 } 45 46 // Srcs computes the srcs list for the rule. 47 func (s *JsLibrary) Srcs() []string { 48 srcs := make([]string, 0) 49 for _, output := range s.Outputs { 50 if strings.HasSuffix(output, ".js") { 51 srcs = append(srcs, protoc.StripRel(s.Config.Rel, output)) 52 } 53 } 54 return srcs 55 } 56 57 // Deps computes the deps list for the rule. 58 func (s *JsLibrary) Deps() []string { 59 return s.RuleConfig.GetDeps() 60 } 61 62 // Visibility provides visibility labels. 63 func (s *JsLibrary) Visibility() []string { 64 return s.RuleConfig.GetVisibility() 65 } 66 67 // Rule implements part of the ruleProvider interface. 68 func (s *JsLibrary) Rule(otherGen ...*rule.Rule) *rule.Rule { 69 newRule := rule.NewRule(s.Kind(), s.Name()) 70 71 newRule.SetAttr("srcs", s.Srcs()) 72 73 deps := s.Deps() 74 if len(deps) > 0 { 75 newRule.SetAttr("deps", deps) 76 } 77 78 visibility := s.Visibility() 79 if len(visibility) > 0 { 80 newRule.SetAttr("visibility", visibility) 81 } 82 83 return newRule 84 } 85 86 // Imports implements part of the RuleProvider interface. 87 func (s *JsLibrary) Imports(c *config.Config, r *rule.Rule, file *rule.File) []resolve.ImportSpec { 88 return nil 89 } 90 91 // Resolve implements part of the RuleProvider interface. 92 func (s *JsLibrary) Resolve(c *config.Config, ix *resolve.RuleIndex, r *rule.Rule, imports []string, from label.Label) { 93 if s.Resolver == nil { 94 return 95 } 96 s.Resolver(c, ix, r, imports, from) 97 }