github.com/stackb/rules_proto@v0.0.0-20240221195024-5428336c51f1/pkg/plugin/builtin/pyi_plugin.go (about) 1 package builtin 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(&PyiPlugin{}) 13 } 14 15 // PyiPlugin implements Plugin for the built-in protoc --pyi_out. 16 type PyiPlugin struct{} 17 18 // Name implements part of the Plugin interface. 19 func (p *PyiPlugin) Name() string { 20 return "builtin:pyi" 21 } 22 23 // Configure implements part of the Plugin interface. 24 func (p *PyiPlugin) Configure(ctx *protoc.PluginContext) *protoc.PluginConfiguration { 25 return &protoc.PluginConfiguration{ 26 Label: label.New("build_stack_rules_proto", "plugin/builtin", "pyi"), 27 Outputs: protoc.FlatMapFiles( 28 pyiGeneratedFileName(ctx.Rel), 29 protoc.Always, 30 ctx.ProtoLibrary.Files()..., 31 ), 32 Options: ctx.PluginConfig.GetOptions(), 33 } 34 } 35 36 // pyiGeneratedFileName is a utility function that returns a function that 37 // computes the name of a predicted generated file having the given 38 // extension(s) relative to the given dir. 39 func pyiGeneratedFileName(reldir string) func(f *protoc.File) []string { 40 return func(f *protoc.File) []string { 41 name := strings.ReplaceAll(f.Name, "-", "_") 42 if reldir != "" { 43 name = path.Join(reldir, name) 44 } 45 return []string{name + "_pb2.pyi"} 46 } 47 }