github.com/stackb/rules_proto@v0.0.0-20240221195024-5428336c51f1/pkg/plugin/grpc/grpcweb/protoc-gen-grpc-web.go (about) 1 package grpcnode 2 3 import ( 4 "path" 5 "strings" 6 7 "github.com/bazelbuild/bazel-gazelle/label" 8 "github.com/stackb/rules_proto/pkg/protoc" 9 ) 10 11 func init() { 12 protoc.Plugins().MustRegisterPlugin(&ProtocGenGrpcWeb{}) 13 } 14 15 // ProtocGenGrpcWeb implements Plugin for grpc_web_plugin in the 16 // grpc/grpc-web repo. 17 type ProtocGenGrpcWeb struct{} 18 19 // Name implements part of the Plugin interface. 20 func (p *ProtocGenGrpcWeb) Name() string { 21 return "grpc:grpc-web:protoc-gen-grpc-web" 22 } 23 24 // Configure implements part of the Plugin interface. 25 func (p *ProtocGenGrpcWeb) Configure(ctx *protoc.PluginContext) *protoc.PluginConfiguration { 26 if !protoc.HasServices(ctx.ProtoLibrary.Files()...) { 27 return nil 28 } 29 return &protoc.PluginConfiguration{ 30 Label: label.New("build_stack_rules_proto", "plugin/grpc/grpc-web", "protoc-gen-grpc-web"), 31 Outputs: protoc.FlatMapFiles( 32 grpcGeneratedFileName(ctx.Rel), 33 protoc.HasService, 34 ctx.ProtoLibrary.Files()..., 35 ), 36 Options: ctx.PluginConfig.GetOptions(), 37 } 38 } 39 40 // grpcGeneratedFileName is a utility function that returns a function that 41 // computes the name of a predicted generated file having the given extension(s) 42 // relative to the given dir. 43 func grpcGeneratedFileName(reldir string) func(f *protoc.File) []string { 44 return func(f *protoc.File) []string { 45 name := strings.ReplaceAll(f.Name, "-", "_") 46 if reldir != "" { 47 name = path.Join(reldir, name) 48 } 49 return []string{name + "_grpc_web_pb.js"} 50 } 51 }