forked from stackb/rules_proto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
protoc-gen-go-grpc.go
58 lines (50 loc) · 1.71 KB
/
protoc-gen-go-grpc.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
package grpcgo
import (
"github.com/bazelbuild/bazel-gazelle/label"
"github.com/bazelbuild/bazel-gazelle/rule"
"github.com/stackb/rules_proto/pkg/plugin/golang/protobuf"
"github.com/stackb/rules_proto/pkg/protoc"
)
func init() {
protoc.Plugins().MustRegisterPlugin(&ProtocGenGoGrpcPlugin{})
}
// ProtocGenGoGrpcPlugin implements Plugin for the the gogo_* family of plugins.
type ProtocGenGoGrpcPlugin struct{}
// Name implements part of the Plugin interface.
func (p *ProtocGenGoGrpcPlugin) Name() string {
return "grpc:grpc-go:protoc-gen-go-grpc"
}
// Configure implements part of the Plugin interface.
func (p *ProtocGenGoGrpcPlugin) Configure(ctx *protoc.PluginContext) *protoc.PluginConfiguration {
if !p.shouldApply(ctx.ProtoLibrary) {
return nil
}
options := ctx.PluginConfig.GetOptions()
mappings, _ := protobuf.GetImportMappings(options)
return &protoc.PluginConfiguration{
Label: label.New("build_stack_rules_proto", "plugin/grpc/grpc-go", "protoc-gen-go-grpc"),
Outputs: p.outputs(ctx.ProtoLibrary, mappings),
Options: options,
}
}
func (p *ProtocGenGoGrpcPlugin) shouldApply(lib protoc.ProtoLibrary) bool {
for _, f := range lib.Files() {
if f.HasServices() {
return true
}
}
return false
}
func (p *ProtocGenGoGrpcPlugin) outputs(lib protoc.ProtoLibrary, importMappings map[string]string) []string {
srcs := make([]string, 0)
for _, f := range lib.Files() {
if !f.HasServices() {
continue
}
srcs = append(srcs, protobuf.GetGoOutputBaseName(f, importMappings)+"_grpc.pb.go")
}
return srcs
}
func (p *ProtocGenGoGrpcPlugin) ResolvePluginOptions(cfg *protoc.PluginConfiguration, r *rule.Rule, from label.Label) []string {
return protobuf.ResolvePluginOptionsTransitive(cfg, r, from)
}