github.com/stackb/rules_proto@v0.0.0-20240221195024-5428336c51f1/pkg/plugin/builtin/php_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(&PhpPlugin{}) 13 } 14 15 // PhpPlugin implements Plugin for the built-in protoc php plugin. 16 type PhpPlugin struct{} 17 18 // Name implements part of the Plugin interface. 19 func (p *PhpPlugin) Name() string { 20 return "builtin:php" 21 } 22 23 // Configure implements part of the Plugin interface. 24 func (p *PhpPlugin) Configure(ctx *protoc.PluginContext) *protoc.PluginConfiguration { 25 return &protoc.PluginConfiguration{ 26 Label: label.New("build_stack_rules_proto", "plugin/builtin", "php"), 27 Outputs: protoc.FlatMapFiles( 28 phpFileName(ctx.Rel), 29 protoc.Always, 30 ctx.ProtoLibrary.Files()..., 31 ), 32 Out: ctx.Rel, 33 Options: ctx.PluginConfig.GetOptions(), 34 } 35 } 36 37 func phpFileName(rel string) func(f *protoc.File) []string { 38 relDir := strings.Title(rel) 39 40 return func(f *protoc.File) []string { 41 outs := make([]string, 0) 42 43 // Compute the base dir where files are generated 44 dir := "" 45 pkg := f.Package() 46 if pkg.Name != "" { 47 dir = path.Join(strings.Title(strings.ReplaceAll(pkg.Name, ".", "/")), dir) 48 } 49 50 // php_namespace overrides package 51 ns, _ := protoc.GetNamedOption(f.Options(), "php_namespace") 52 if ns != "" { 53 dir = ns 54 } 55 56 // Add the metadata file 57 mns, _ := protoc.GetNamedOption(f.Options(), "php_metadata_namespace") 58 if mns == "" { 59 mns = "GPBMetadata" 60 } 61 outs = append(outs, path.Join(rel, mns, relDir, strings.Title(f.Name))+".php") 62 63 // Add enums 64 for _, e := range f.Enums() { 65 outs = append(outs, path.Join(dir, rel, strings.Title(e.Name))+".php") 66 } 67 68 // Add messages 69 for _, m := range f.Messages() { 70 outs = append(outs, path.Join(dir, rel, strings.Title(m.Name))+".php") 71 } 72 73 return outs 74 } 75 }