|
| 1 | +package bufbuild |
| 2 | + |
| 3 | +import ( |
| 4 | + "flag" |
| 5 | + "log" |
| 6 | + "path" |
| 7 | + "path/filepath" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/bazelbuild/bazel-gazelle/label" |
| 11 | + "github.com/stackb/rules_proto/pkg/protoc" |
| 12 | +) |
| 13 | + |
| 14 | +func init() { |
| 15 | + protoc.Plugins().MustRegisterPlugin(&ConnectEsProto{}) |
| 16 | +} |
| 17 | + |
| 18 | +// ConnectEsProto implements Plugin for the bufbuild/connect-es plugin. |
| 19 | +type ConnectEsProto struct{} |
| 20 | + |
| 21 | +// Name implements part of the Plugin interface. |
| 22 | +func (p *ConnectEsProto) Name() string { |
| 23 | + return "bufbuild:connect-es" |
| 24 | +} |
| 25 | + |
| 26 | +// Configure implements part of the Plugin interface. |
| 27 | +func (p *ConnectEsProto) Configure(ctx *protoc.PluginContext) *protoc.PluginConfiguration { |
| 28 | + flags := parseConnectEsProtoOptions(p.Name(), ctx.PluginConfig.GetFlags()) |
| 29 | + imports := make(map[string]bool) |
| 30 | + for _, file := range ctx.ProtoLibrary.Files() { |
| 31 | + for _, imp := range file.Imports() { |
| 32 | + imports[imp.Filename] = true |
| 33 | + } |
| 34 | + } |
| 35 | + // TODO: get target option from directive |
| 36 | + var options = []string{"keep_empty_files=true", "target=ts"} |
| 37 | + tsFiles := make([]string, 0) |
| 38 | + for _, file := range ctx.ProtoLibrary.Files() { |
| 39 | + // TODO: outputs should be conditional on which target= value is used |
| 40 | + tsFile := file.Name + "_connect.ts" |
| 41 | + if flags.excludeOutput[filepath.Base(tsFile)] { |
| 42 | + continue |
| 43 | + } |
| 44 | + if ctx.Rel != "" { |
| 45 | + tsFile = path.Join(ctx.Rel, tsFile) |
| 46 | + } |
| 47 | + tsFiles = append(tsFiles, tsFile) |
| 48 | + } |
| 49 | + |
| 50 | + pc := &protoc.PluginConfiguration{ |
| 51 | + Label: label.New("build_stack_rules_proto", "plugin/bufbuild", "connect-es"), |
| 52 | + Outputs: protoc.DeduplicateAndSort(tsFiles), |
| 53 | + Options: protoc.DeduplicateAndSort(options), |
| 54 | + } |
| 55 | + if len(pc.Outputs) == 0 { |
| 56 | + pc.Outputs = nil |
| 57 | + } |
| 58 | + return pc |
| 59 | +} |
| 60 | + |
| 61 | +// ConnectEsProtoOptions represents the parsed flag configuration for the |
| 62 | +// ConnectEsProto implementation. |
| 63 | +type ConnectEsProtoOptions struct { |
| 64 | + excludeOutput map[string]bool |
| 65 | +} |
| 66 | + |
| 67 | +func parseConnectEsProtoOptions(kindName string, args []string) *ConnectEsProtoOptions { |
| 68 | + flags := flag.NewFlagSet(kindName, flag.ExitOnError) |
| 69 | + |
| 70 | + var excludeOutput string |
| 71 | + flags.StringVar(&excludeOutput, "exclude_output", "", "--exclude_output=foo.ts suppresses the file 'foo.ts' from the output list") |
| 72 | + |
| 73 | + if err := flags.Parse(args); err != nil { |
| 74 | + log.Fatalf("failed to parse flags for %q: %v", kindName, err) |
| 75 | + } |
| 76 | + config := &ConnectEsProtoOptions{ |
| 77 | + excludeOutput: make(map[string]bool), |
| 78 | + } |
| 79 | + for _, value := range strings.Split(excludeOutput, ",") { |
| 80 | + config.excludeOutput[value] = true |
| 81 | + } |
| 82 | + |
| 83 | + return config |
| 84 | +} |
0 commit comments