github.com/stackb/rules_proto@v0.0.0-20240221195024-5428336c51f1/pkg/protoc/proto_descriptor_set.go (about)

     1  package protoc
     2  
     3  import (
     4  	"fmt"
     5  	"path"
     6  
     7  	"github.com/bazelbuild/bazel-gazelle/config"
     8  	"github.com/bazelbuild/bazel-gazelle/label"
     9  	"github.com/bazelbuild/bazel-gazelle/resolve"
    10  	"github.com/bazelbuild/bazel-gazelle/rule"
    11  )
    12  
    13  func init() {
    14  	Rules().MustRegisterRule("stackb:rules_proto:proto_descriptor_set", &protoDescriptorSetRule{})
    15  	Plugins().MustRegisterPlugin(&protoDescriptorSetPlugin{})
    16  }
    17  
    18  // protoDescriptorSetRule implements LanguageRule for the 'proto_descriptor_set'
    19  // rule from @rules_proto.
    20  type protoDescriptorSetRule struct{}
    21  
    22  // Name implements part of the LanguageRule interface.
    23  func (s *protoDescriptorSetRule) Name() string {
    24  	return "rules_proto_descriptor_set"
    25  }
    26  
    27  // KindInfo implements part of the LanguageRule interface.
    28  func (s *protoDescriptorSetRule) KindInfo() rule.KindInfo {
    29  	return rule.KindInfo{
    30  		MergeableAttrs: map[string]bool{
    31  			"deps": true,
    32  		},
    33  		NonEmptyAttrs: map[string]bool{
    34  			"srcs": true,
    35  		},
    36  	}
    37  }
    38  
    39  // LoadInfo implements part of the LanguageRule interface.
    40  func (s *protoDescriptorSetRule) LoadInfo() rule.LoadInfo {
    41  	return rule.LoadInfo{
    42  		Name:    "@build_stack_rules_proto//rules:proto_descriptor_set.bzl",
    43  		Symbols: []string{"rules_proto_descriptor_set"},
    44  	}
    45  }
    46  
    47  // ProvideRule implements part of the LanguageRule interface.
    48  func (s *protoDescriptorSetRule) ProvideRule(cfg *LanguageRuleConfig, config *ProtocConfiguration) RuleProvider {
    49  	return &protoDescriptorSetRuleRule{ruleConfig: cfg, config: config}
    50  }
    51  
    52  // protoDescriptorSetRule implements RuleProvider for the 'proto_compile' rule.
    53  type protoDescriptorSetRuleRule struct {
    54  	config     *ProtocConfiguration
    55  	ruleConfig *LanguageRuleConfig
    56  }
    57  
    58  // Kind implements part of the ruleProvider interface.
    59  func (s *protoDescriptorSetRuleRule) Kind() string {
    60  	return "rules_proto_descriptor_set"
    61  }
    62  
    63  // Name implements part of the ruleProvider interface.
    64  func (s *protoDescriptorSetRuleRule) Name() string {
    65  	return fmt.Sprintf("%s_descriptor", s.config.Library.BaseName())
    66  }
    67  
    68  // Visibility provides visibility labels.
    69  func (s *protoDescriptorSetRuleRule) Visibility() []string {
    70  	return s.ruleConfig.GetVisibility()
    71  }
    72  
    73  // Rule implements part of the ruleProvider interface.
    74  func (s *protoDescriptorSetRuleRule) Rule(otherGen ...*rule.Rule) *rule.Rule {
    75  	newRule := rule.NewRule(s.Kind(), s.Name())
    76  
    77  	newRule.SetAttr("deps", []string{s.config.Library.Name()})
    78  
    79  	visibility := s.Visibility()
    80  	if len(visibility) > 0 {
    81  		newRule.SetAttr("visibility", visibility)
    82  	}
    83  
    84  	return newRule
    85  }
    86  
    87  // Imports implements part of the RuleProvider interface.
    88  func (s *protoDescriptorSetRuleRule) Imports(c *config.Config, r *rule.Rule, file *rule.File) []resolve.ImportSpec {
    89  	return nil
    90  }
    91  
    92  // Resolve implements part of the RuleProvider interface.
    93  func (s *protoDescriptorSetRuleRule) Resolve(c *config.Config, ix *resolve.RuleIndex, r *rule.Rule, imports []string, from label.Label) {
    94  }
    95  
    96  type protoDescriptorSetPlugin struct{}
    97  
    98  // Name implements part of the Plugin interface.
    99  func (p *protoDescriptorSetPlugin) Name() string {
   100  	return "bazelbuild:rules_proto:proto_descriptor_set"
   101  }
   102  
   103  // Configure implements part of the Plugin interface.
   104  func (p *protoDescriptorSetPlugin) Configure(ctx *PluginContext) *PluginConfiguration {
   105  	descriptorSetOut := path.Join(ctx.Rel, fmt.Sprintf("%s_descriptor.pb", ctx.ProtoLibrary.BaseName()))
   106  
   107  	return &PluginConfiguration{
   108  		Label:   label.New("build_stack_rules_proto", "bazelbuild/rules_proto", "proto_descriptor_set"),
   109  		Outputs: []string{descriptorSetOut},
   110  		Out:     ctx.Rel,
   111  		Options: ctx.PluginConfig.GetOptions(),
   112  	}
   113  }