github.com/stackb/rules_proto@v0.0.0-20240221195024-5428336c51f1/pkg/rule/rules_scala/scala_proto_library.go (about)

     1  package rules_scala
     2  
     3  import (
     4  	"github.com/bazelbuild/bazel-gazelle/config"
     5  	"github.com/bazelbuild/bazel-gazelle/label"
     6  	"github.com/bazelbuild/bazel-gazelle/resolve"
     7  	"github.com/bazelbuild/bazel-gazelle/rule"
     8  
     9  	"github.com/stackb/rules_proto/pkg/protoc"
    10  )
    11  
    12  func init() {
    13  	protoc.Rules().MustRegisterRule("bazelbuild:rules_scala:scala_proto_library",
    14  		&scalaProtoLibrary{})
    15  }
    16  
    17  // scalaProtoLibrary implements LanguageRule for the 'scala_proto_library' rule
    18  // from @rules_scala.
    19  type scalaProtoLibrary struct{}
    20  
    21  // Name implements part of the LanguageRule interface.
    22  func (s *scalaProtoLibrary) Name() string {
    23  	return "scala_proto_library"
    24  }
    25  
    26  // KindInfo implements part of the LanguageRule interface.
    27  func (s *scalaProtoLibrary) KindInfo() rule.KindInfo {
    28  	return rule.KindInfo{
    29  		MergeableAttrs: map[string]bool{
    30  			"srcs":       true,
    31  			"deps":       true,
    32  			"visibility": true,
    33  		},
    34  		NonEmptyAttrs: map[string]bool{
    35  			"srcs": true,
    36  		},
    37  	}
    38  }
    39  
    40  // LoadInfo implements part of the LanguageRule interface.
    41  func (s *scalaProtoLibrary) LoadInfo() rule.LoadInfo {
    42  	return rule.LoadInfo{
    43  		Name:    "@build_stack_rules_proto//rules/scala:scala_proto_library.bzl",
    44  		Symbols: []string{s.Name()},
    45  	}
    46  }
    47  
    48  // ProvideRule implements part of the LanguageRule interface.
    49  func (s *scalaProtoLibrary) ProvideRule(cfg *protoc.LanguageRuleConfig, pc *protoc.ProtocConfiguration) protoc.RuleProvider {
    50  	return &scalaProtoLibraryRule{
    51  		kindName:   s.Name(),
    52  		ruleConfig: cfg,
    53  		config:     pc,
    54  	}
    55  }
    56  
    57  // scalaProtoLibraryRule implements RuleProvider for 'scala_library'-derived rules.
    58  type scalaProtoLibraryRule struct {
    59  	kindName   string
    60  	config     *protoc.ProtocConfiguration
    61  	ruleConfig *protoc.LanguageRuleConfig
    62  }
    63  
    64  // Kind implements part of the ruleProvider interface.
    65  func (s *scalaProtoLibraryRule) Kind() string {
    66  	return s.kindName
    67  }
    68  
    69  // Name implements part of the ruleProvider interface.
    70  func (s *scalaProtoLibraryRule) Name() string {
    71  	return s.config.Library.BaseName() + "_scala_proto"
    72  }
    73  
    74  // Visibility provides visibility labels.
    75  func (s *scalaProtoLibraryRule) Visibility() []string {
    76  	return s.ruleConfig.GetVisibility()
    77  }
    78  
    79  // Rule implements part of the ruleProvider interface.
    80  func (s *scalaProtoLibraryRule) Rule(otherGen ...*rule.Rule) *rule.Rule {
    81  	newRule := rule.NewRule(s.Kind(), s.Name())
    82  
    83  	visibility := s.Visibility()
    84  	if len(visibility) > 0 {
    85  		newRule.SetAttr("visibility", visibility)
    86  	}
    87  
    88  	return newRule
    89  }
    90  
    91  // Imports implements part of the RuleProvider interface.
    92  func (s *scalaProtoLibraryRule) Imports(c *config.Config, r *rule.Rule, file *rule.File) []resolve.ImportSpec {
    93  	return nil
    94  }
    95  
    96  // Resolve implements part of the RuleProvider interface.
    97  func (s *scalaProtoLibraryRule) Resolve(c *config.Config, ix *resolve.RuleIndex, r *rule.Rule, imports []string, from label.Label) {
    98  	r.SetAttr("deps", []string{":" + s.config.Library.Name()})
    99  }