-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce bufbuild/connect-es (#338)
* feat: add bufbuild es-connect * show working example again * feat: add gazelle extension plugin for bufbuild * fix: gazelle extension creates both plugins * chore: set options on generated bufbuild rules * chore: restore original example which conflicts with bufbuild/connect usage * chore: minify delta
- Loading branch information
Showing
15 changed files
with
476 additions
and
276 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
docs/ | ||
node_modules | ||
plugin/bufbuild/node_modules | ||
plugin/stephenh/ts-proto/node_modules | ||
rules/ts/node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") | ||
|
||
go_library( | ||
name = "bufbuild", | ||
srcs = [ | ||
"connect_es_plugin.go", | ||
"es_plugin.go", | ||
], | ||
importpath = "github.com/stackb/rules_proto/pkg/plugin/bufbuild", | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"//pkg/protoc", | ||
"@bazel_gazelle//label:go_default_library", | ||
], | ||
) | ||
|
||
go_test( | ||
name = "bufbuild_test", | ||
srcs = ["es_plugin_test.go"], | ||
deps = [ | ||
":bufbuild", | ||
"//pkg/plugintest", | ||
], | ||
) | ||
|
||
filegroup( | ||
name = "all_files", | ||
testonly = True, | ||
srcs = [ | ||
"BUILD.bazel", | ||
] + glob(["*.go"]), | ||
visibility = ["//pkg:__pkg__"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package bufbuild | ||
|
||
import ( | ||
"flag" | ||
"log" | ||
"path" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/bazelbuild/bazel-gazelle/label" | ||
"github.com/stackb/rules_proto/pkg/protoc" | ||
) | ||
|
||
func init() { | ||
protoc.Plugins().MustRegisterPlugin(&ConnectEsProto{}) | ||
} | ||
|
||
// ConnectEsProto implements Plugin for the bufbuild/connect-es plugin. | ||
type ConnectEsProto struct{} | ||
|
||
// Name implements part of the Plugin interface. | ||
func (p *ConnectEsProto) Name() string { | ||
return "bufbuild:connect-es" | ||
} | ||
|
||
// Configure implements part of the Plugin interface. | ||
func (p *ConnectEsProto) Configure(ctx *protoc.PluginContext) *protoc.PluginConfiguration { | ||
flags := parseConnectEsProtoOptions(p.Name(), ctx.PluginConfig.GetFlags()) | ||
imports := make(map[string]bool) | ||
for _, file := range ctx.ProtoLibrary.Files() { | ||
for _, imp := range file.Imports() { | ||
imports[imp.Filename] = true | ||
} | ||
} | ||
// TODO: get target option from directive | ||
var options = []string{"keep_empty_files=true", "target=ts"} | ||
tsFiles := make([]string, 0) | ||
for _, file := range ctx.ProtoLibrary.Files() { | ||
// TODO: outputs should be conditional on which target= value is used | ||
tsFile := file.Name + "_connect.ts" | ||
if flags.excludeOutput[filepath.Base(tsFile)] { | ||
continue | ||
} | ||
if ctx.Rel != "" { | ||
tsFile = path.Join(ctx.Rel, tsFile) | ||
} | ||
tsFiles = append(tsFiles, tsFile) | ||
} | ||
|
||
pc := &protoc.PluginConfiguration{ | ||
Label: label.New("build_stack_rules_proto", "plugin/bufbuild", "connect-es"), | ||
Outputs: protoc.DeduplicateAndSort(tsFiles), | ||
Options: protoc.DeduplicateAndSort(options), | ||
} | ||
if len(pc.Outputs) == 0 { | ||
pc.Outputs = nil | ||
} | ||
return pc | ||
} | ||
|
||
// ConnectEsProtoOptions represents the parsed flag configuration for the | ||
// ConnectEsProto implementation. | ||
type ConnectEsProtoOptions struct { | ||
excludeOutput map[string]bool | ||
} | ||
|
||
func parseConnectEsProtoOptions(kindName string, args []string) *ConnectEsProtoOptions { | ||
flags := flag.NewFlagSet(kindName, flag.ExitOnError) | ||
|
||
var excludeOutput string | ||
flags.StringVar(&excludeOutput, "exclude_output", "", "--exclude_output=foo.ts suppresses the file 'foo.ts' from the output list") | ||
|
||
if err := flags.Parse(args); err != nil { | ||
log.Fatalf("failed to parse flags for %q: %v", kindName, err) | ||
} | ||
config := &ConnectEsProtoOptions{ | ||
excludeOutput: make(map[string]bool), | ||
} | ||
for _, value := range strings.Split(excludeOutput, ",") { | ||
config.excludeOutput[value] = true | ||
} | ||
|
||
return config | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package bufbuild | ||
|
||
import ( | ||
"flag" | ||
"log" | ||
"path" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/bazelbuild/bazel-gazelle/label" | ||
"github.com/stackb/rules_proto/pkg/protoc" | ||
) | ||
|
||
func init() { | ||
protoc.Plugins().MustRegisterPlugin(&EsProto{}) | ||
} | ||
|
||
// EsProto implements Plugin for the bufbuild/protoc-gen-es plugin. | ||
type EsProto struct{} | ||
|
||
// Name implements part of the Plugin interface. | ||
func (p *EsProto) Name() string { | ||
return "bufbuild:es" | ||
} | ||
|
||
// Configure implements part of the Plugin interface. | ||
func (p *EsProto) Configure(ctx *protoc.PluginContext) *protoc.PluginConfiguration { | ||
flags := parseEsProtoOptions(p.Name(), ctx.PluginConfig.GetFlags()) | ||
imports := make(map[string]bool) | ||
for _, file := range ctx.ProtoLibrary.Files() { | ||
for _, imp := range file.Imports() { | ||
imports[imp.Filename] = true | ||
} | ||
} | ||
// TODO: get target option from directive | ||
var options = []string{"keep_empty_files=true", "target=ts"} | ||
tsFiles := make([]string, 0) | ||
for _, file := range ctx.ProtoLibrary.Files() { | ||
// TODO: outputs should be conditional on which target= value is used | ||
tsFile := file.Name + "_pb.ts" | ||
if flags.excludeOutput[filepath.Base(tsFile)] { | ||
continue | ||
} | ||
if ctx.Rel != "" { | ||
tsFile = path.Join(ctx.Rel, tsFile) | ||
} | ||
tsFiles = append(tsFiles, tsFile) | ||
} | ||
|
||
pc := &protoc.PluginConfiguration{ | ||
Label: label.New("build_stack_rules_proto", "plugin/bufbuild", "es"), | ||
Outputs: protoc.DeduplicateAndSort(tsFiles), | ||
Options: protoc.DeduplicateAndSort(options), | ||
} | ||
if len(pc.Outputs) == 0 { | ||
pc.Outputs = nil | ||
} | ||
return pc | ||
} | ||
|
||
// EsProtoOptions represents the parsed flag configuration for the | ||
// EsProto implementation. | ||
type EsProtoOptions struct { | ||
excludeOutput map[string]bool | ||
} | ||
|
||
func parseEsProtoOptions(kindName string, args []string) *EsProtoOptions { | ||
flags := flag.NewFlagSet(kindName, flag.ExitOnError) | ||
|
||
var excludeOutput string | ||
flags.StringVar(&excludeOutput, "exclude_output", "", "--exclude_output=foo.ts suppresses the file 'foo.ts' from the output list") | ||
|
||
if err := flags.Parse(args); err != nil { | ||
log.Fatalf("failed to parse flags for %q: %v", kindName, err) | ||
} | ||
config := &EsProtoOptions{ | ||
excludeOutput: make(map[string]bool), | ||
} | ||
for _, value := range strings.Split(excludeOutput, ",") { | ||
config.excludeOutput[value] = true | ||
} | ||
|
||
return config | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package bufbuild_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stackb/rules_proto/pkg/plugin/bufbuild" | ||
"github.com/stackb/rules_proto/pkg/plugintest" | ||
) | ||
|
||
func TestProtocGenTsProtoPlugin(t *testing.T) { | ||
plugintest.Cases(t, &bufbuild.EsProto{}, map[string]plugintest.Case{ | ||
"simple": { | ||
Input: "message M{}", | ||
Directives: plugintest.WithDirectives( | ||
"proto_plugin", "es implementation bufbuild:connect-es", | ||
), | ||
PluginName: "es", | ||
Configuration: plugintest.WithConfiguration( | ||
plugintest.WithLabel(t, "@build_stack_rules_proto//plugin/bufbuild:es"), | ||
plugintest.WithOptions("keep_empty_files=true", "target=ts"), | ||
plugintest.WithOutputs("test_pb.ts"), | ||
), | ||
SkipIntegration: true, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
load("@build_stack_rules_proto//rules:proto_plugin.bzl", "proto_plugin") | ||
load("@npm_ts_proto//plugin/bufbuild:@bufbuild/protoc-gen-es/package_json.bzl", gen_bin = "bin") | ||
load("@npm_ts_proto//plugin/bufbuild:@bufbuild/protoc-gen-connect-es/package_json.bzl", connect_bin = "bin") | ||
|
||
gen_bin.protoc_gen_es_binary( | ||
name = "protoc-gen-es", | ||
) | ||
|
||
connect_bin.protoc_gen_connect_es_binary( | ||
name = "protoc-gen-connect", | ||
) | ||
|
||
proto_plugin( | ||
name = "connect-es", | ||
tool = ":protoc-gen-connect", | ||
visibility = ["//visibility:public"], | ||
) | ||
|
||
proto_plugin( | ||
name = "es", | ||
tool = ":protoc-gen-es", | ||
visibility = ["//visibility:public"], | ||
) | ||
|
||
filegroup( | ||
name = "all_files", | ||
testonly = True, | ||
srcs = ["BUILD.bazel"], | ||
visibility = ["//plugin:__pkg__"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"dependencies": { | ||
"@bufbuild/protobuf": "1.3.0", | ||
"@bufbuild/protoc-gen-es": "1.3.0", | ||
"@bufbuild/protoc-gen-connect-es": "0.12.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"dependencies": { | ||
"ts-proto": "1.156.0" | ||
} | ||
} |
Oops, something went wrong.