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