github.com/stackb/rules_proto@v0.0.0-20240221195024-5428336c51f1/pkg/plugin/grpc/grpcgo/protoc-gen-go-grpc.go (about) 1 package grpcgo 2 3 import ( 4 "github.com/bazelbuild/bazel-gazelle/label" 5 "github.com/bazelbuild/bazel-gazelle/rule" 6 "github.com/stackb/rules_proto/pkg/plugin/golang/protobuf" 7 "github.com/stackb/rules_proto/pkg/protoc" 8 ) 9 10 func init() { 11 protoc.Plugins().MustRegisterPlugin(&ProtocGenGoGrpcPlugin{}) 12 } 13 14 // ProtocGenGoGrpcPlugin implements Plugin for the the gogo_* family of plugins. 15 type ProtocGenGoGrpcPlugin struct{} 16 17 // Name implements part of the Plugin interface. 18 func (p *ProtocGenGoGrpcPlugin) Name() string { 19 return "grpc:grpc-go:protoc-gen-go-grpc" 20 } 21 22 // Configure implements part of the Plugin interface. 23 func (p *ProtocGenGoGrpcPlugin) Configure(ctx *protoc.PluginContext) *protoc.PluginConfiguration { 24 if !p.shouldApply(ctx.ProtoLibrary) { 25 return nil 26 } 27 options := ctx.PluginConfig.GetOptions() 28 mappings, _ := protobuf.GetImportMappings(options) 29 return &protoc.PluginConfiguration{ 30 Label: label.New("build_stack_rules_proto", "plugin/grpc/grpc-go", "protoc-gen-go-grpc"), 31 Outputs: p.outputs(ctx.ProtoLibrary, mappings), 32 Options: options, 33 } 34 } 35 36 func (p *ProtocGenGoGrpcPlugin) shouldApply(lib protoc.ProtoLibrary) bool { 37 for _, f := range lib.Files() { 38 if f.HasServices() { 39 return true 40 } 41 } 42 return false 43 } 44 45 func (p *ProtocGenGoGrpcPlugin) outputs(lib protoc.ProtoLibrary, importMappings map[string]string) []string { 46 srcs := make([]string, 0) 47 for _, f := range lib.Files() { 48 if !f.HasServices() { 49 continue 50 } 51 srcs = append(srcs, protobuf.GetGoOutputBaseName(f, importMappings)+"_grpc.pb.go") 52 } 53 return srcs 54 } 55 56 func (p *ProtocGenGoGrpcPlugin) ResolvePluginOptions(cfg *protoc.PluginConfiguration, r *rule.Rule, from label.Label) []string { 57 return protobuf.ResolvePluginOptionsTransitive(cfg, r, from) 58 }