github.com/stackb/rules_proto@v0.0.0-20240221195024-5428336c51f1/pkg/protoc/plugin_configuration.go (about) 1 package protoc 2 3 import ( 4 "sort" 5 6 "github.com/bazelbuild/bazel-gazelle/label" 7 "github.com/bazelbuild/bazel-gazelle/rule" 8 ) 9 10 // PluginConfiguration represents the configuration of a protoc plugin and the 11 // sources & source mappings that are expected to be produced. 12 type PluginConfiguration struct { 13 // Config is the associated plugin configuration. 14 Config *LanguagePluginConfig 15 // Label is the bazel label for the corresponding proto_plugin rule. 16 Label label.Label 17 // Mappings is a dictionary that maps filenames listed in Outputs to 18 // 'Out'-relative filepaths. This is used when the plugin writes to a 19 // location outside the bazel package and needs to be relocated (copied) to 20 // the Output location. 21 Mappings map[string]string 22 // Options is the list of options that the plugin expects 23 Options []string 24 // Out is the output directory the plugin is predicted to write to 25 Out string 26 // Outputs is the list of output files the plugin generates 27 Outputs []string 28 // Plugin the Plugin implementation that created the configuration. 29 Plugin Plugin 30 } 31 32 // GetPluginLabels returns the list of labels strings for a list of plugins. 33 func GetPluginLabels(plugins []*PluginConfiguration) []string { 34 labels := make([]string, len(plugins)) 35 for i, plugin := range plugins { 36 labels[i] = plugin.Label.String() 37 } 38 sort.Strings(labels) 39 return labels 40 } 41 42 // GetPluginOptions returns the list of options by plugin. 43 func GetPluginOptions(plugins []*PluginConfiguration, r *rule.Rule, from label.Label) map[string][]string { 44 options := make(map[string][]string) 45 for _, cfg := range plugins { 46 opts := cfg.Options 47 if resolver, ok := cfg.Plugin.(PluginOptionsResolver); ok { 48 opts = resolver.ResolvePluginOptions(cfg, r, from) 49 } 50 if len(opts) == 0 { 51 continue 52 } 53 sort.Strings(opts) 54 options[cfg.Label.String()] = opts 55 } 56 return options 57 } 58 59 // GetPluginOuts returns the output location by plugin. 60 func GetPluginOuts(plugins []*PluginConfiguration) map[string]string { 61 outs := make(map[string]string) 62 for _, plugin := range plugins { 63 if plugin.Out == "" { 64 continue 65 } 66 outs[plugin.Label.String()] = plugin.Out 67 } 68 return outs 69 }