From 9aee6c84a980aec4caace2291e6f696324511e5d Mon Sep 17 00:00:00 2001 From: Paul Johnston Date: Wed, 22 Nov 2023 21:01:25 -0700 Subject: [PATCH] Add support for scalapb:zio-grpc --- language/protobuf/BUILD.bazel | 1 + language/protobuf/protobuf.go | 1 + pkg/plugin/scalapb/zio_grpc/BUILD.bazel | 30 ++++++++++++++ .../scalapb/zio_grpc/protoc_gen_zio_grpc.go | 40 ++++++++++++++++++ .../zio_grpc/protoc_gen_zio_grpc_test.go | 41 +++++++++++++++++++ pkg/rule/rules_scala/scala_library.go | 8 ++++ plugin/scalapb/zio-grpc/BUILD.bazel | 26 ++++++++++++ rules/scala/BUILD.bazel | 1 + rules/scala/grpc_zio_scala_library.bzl | 6 +++ 9 files changed, 154 insertions(+) create mode 100644 pkg/plugin/scalapb/zio_grpc/BUILD.bazel create mode 100644 pkg/plugin/scalapb/zio_grpc/protoc_gen_zio_grpc.go create mode 100644 pkg/plugin/scalapb/zio_grpc/protoc_gen_zio_grpc_test.go create mode 100644 plugin/scalapb/zio-grpc/BUILD.bazel create mode 100644 rules/scala/grpc_zio_scala_library.bzl diff --git a/language/protobuf/BUILD.bazel b/language/protobuf/BUILD.bazel index 20c221848..61f1c6af1 100644 --- a/language/protobuf/BUILD.bazel +++ b/language/protobuf/BUILD.bazel @@ -17,6 +17,7 @@ go_library( "//pkg/plugin/grpc/grpcweb", "//pkg/plugin/grpcecosystem/grpcgateway", "//pkg/plugin/scalapb/scalapb", + "//pkg/plugin/scalapb/zio_grpc", "//pkg/plugin/stackb/grpc_js", "//pkg/plugin/stephenh/ts-proto", "//pkg/rule/rules_cc", diff --git a/language/protobuf/protobuf.go b/language/protobuf/protobuf.go index 89816b654..c91ecbd3f 100644 --- a/language/protobuf/protobuf.go +++ b/language/protobuf/protobuf.go @@ -15,6 +15,7 @@ import ( _ "github.com/stackb/rules_proto/pkg/plugin/grpc/grpcweb" _ "github.com/stackb/rules_proto/pkg/plugin/grpcecosystem/grpcgateway" _ "github.com/stackb/rules_proto/pkg/plugin/scalapb/scalapb" + _ "github.com/stackb/rules_proto/pkg/plugin/scalapb/zio_grpc" _ "github.com/stackb/rules_proto/pkg/plugin/stackb/grpc_js" _ "github.com/stackb/rules_proto/pkg/plugin/stephenh/ts-proto" _ "github.com/stackb/rules_proto/pkg/rule/rules_cc" diff --git a/pkg/plugin/scalapb/zio_grpc/BUILD.bazel b/pkg/plugin/scalapb/zio_grpc/BUILD.bazel new file mode 100644 index 000000000..3b1c71b9d --- /dev/null +++ b/pkg/plugin/scalapb/zio_grpc/BUILD.bazel @@ -0,0 +1,30 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") + +go_library( + name = "zio_grpc", + srcs = ["protoc_gen_zio_grpc.go"], + importpath = "github.com/stackb/rules_proto/pkg/plugin/scalapb/zio_grpc", + visibility = ["//visibility:public"], + deps = [ + "//pkg/protoc", + "@bazel_gazelle//label:go_default_library", + ], +) + +go_test( + name = "zio_grpc_test", + srcs = ["protoc_gen_zio_grpc_test.go"], + deps = [ + ":zio_grpc", + "//pkg/plugintest", + ], +) + +filegroup( + name = "all_files", + testonly = True, + srcs = [ + "BUILD.bazel", + ] + glob(["*.go"]), + visibility = ["//pkg:__pkg__"], +) diff --git a/pkg/plugin/scalapb/zio_grpc/protoc_gen_zio_grpc.go b/pkg/plugin/scalapb/zio_grpc/protoc_gen_zio_grpc.go new file mode 100644 index 000000000..99919fe35 --- /dev/null +++ b/pkg/plugin/scalapb/zio_grpc/protoc_gen_zio_grpc.go @@ -0,0 +1,40 @@ +package zio_grpc + +import ( + "path" + + "github.com/bazelbuild/bazel-gazelle/label" + "github.com/stackb/rules_proto/pkg/protoc" +) + +const ZioGrpcPluginName = "scalapb:zio-grpc:protoc-gen-zio-grpc" + +func init() { + protoc.Plugins().MustRegisterPlugin(&ProtocGenZioGrpcPlugin{}) +} + +// ProtocGenZioGrpcPlugin implements Plugin for the zio-grpc plugin. +type ProtocGenZioGrpcPlugin struct{} + +// Name implements part of the Plugin interface. +func (p *ProtocGenZioGrpcPlugin) Name() string { + return ZioGrpcPluginName +} + +// Configure implements part of the Plugin interface. +func (p *ProtocGenZioGrpcPlugin) Configure(ctx *protoc.PluginContext) *protoc.PluginConfiguration { + if !protoc.HasServices(ctx.ProtoLibrary.Files()...) { + return nil + } + + srcjar := ctx.ProtoLibrary.BaseName() + "_zio_grpc.srcjar" + if ctx.Rel != "" { + srcjar = path.Join(ctx.Rel, srcjar) + } + + return &protoc.PluginConfiguration{ + Label: label.New("build_stack_rules_proto", "plugin/scalapb/zio-grpc", "protoc-gen-zio-grpc"), + Outputs: []string{srcjar}, + Options: ctx.PluginConfig.GetOptions(), + } +} diff --git a/pkg/plugin/scalapb/zio_grpc/protoc_gen_zio_grpc_test.go b/pkg/plugin/scalapb/zio_grpc/protoc_gen_zio_grpc_test.go new file mode 100644 index 000000000..90f89e1e4 --- /dev/null +++ b/pkg/plugin/scalapb/zio_grpc/protoc_gen_zio_grpc_test.go @@ -0,0 +1,41 @@ +package zio_grpc_test + +import ( + "testing" + + "github.com/stackb/rules_proto/pkg/plugin/scalapb/zio_grpc" + "github.com/stackb/rules_proto/pkg/plugintest" +) + +func TestProtoGenZioGrpcPlugin(t *testing.T) { + plugintest.Cases(t, &zio_grpc.ProtocGenZioGrpcPlugin{}, map[string]plugintest.Case{ + "empty file": { + Input: "", + Directives: plugintest.WithDirectives( + "proto_plugin", "zio implementation scalapb:zio-grpc:protoc-gen-zio-grpc", + ), + PluginName: "zio", + SkipIntegration: true, + }, + "only messages, no srcjar produced": { + Input: "package pkg;\n\nmessage M{}", + Directives: plugintest.WithDirectives( + "proto_plugin", "zio implementation scalapb:zio-grpc:protoc-gen-zio-grpc", + ), + PluginName: "zio", + SkipIntegration: true, + }, + "with service": { + Input: "package pkg;\n\nservice S{}", + Directives: plugintest.WithDirectives( + "proto_plugin", "zio implementation scalapb:zio-grpc:protoc-gen-zio-grpc", + ), + PluginName: "zio", + Configuration: plugintest.WithConfiguration( + plugintest.WithLabel(t, "@build_stack_rules_proto//plugin/scalapb/zio-grpc:protoc-gen-zio-grpc"), + plugintest.WithOutputs("test_zio_grpc.srcjar"), + ), + SkipIntegration: true, + }, + }) +} diff --git a/pkg/rule/rules_scala/scala_library.go b/pkg/rule/rules_scala/scala_library.go index 72b45b168..75e6c5510 100644 --- a/pkg/rule/rules_scala/scala_library.go +++ b/pkg/rule/rules_scala/scala_library.go @@ -20,9 +20,11 @@ import ( const ( GrpcscalaLibraryRuleName = "grpc_scala_library" + GrpcZioscalaLibraryRuleName = "grpc_zio_scala_library" ProtoscalaLibraryRuleName = "proto_scala_library" protoScalaLibraryRuleSuffix = "_proto_scala_library" grpcScalaLibraryRuleSuffix = "_grpc_scala_library" + grpcZioScalaLibraryRuleSuffix = "_grpc_zio_scala_library" scalaPbPluginOptionsPrivateKey = "_scalapb_plugin" akkaGrpcPluginOptionsPrivateKey = "_akka_grpc_plugin" scalapbOptionsName = "(scalapb.options)" @@ -43,6 +45,12 @@ func init() { ruleSuffix: grpcScalaLibraryRuleSuffix, protoFileFilter: serviceFiles, }) + protoc.Rules().MustRegisterRule("stackb:rules_proto:"+GrpcZioscalaLibraryRuleName, + &scalaLibrary{ + kindName: GrpcZioscalaLibraryRuleName, + ruleSuffix: grpcZioScalaLibraryRuleSuffix, + protoFileFilter: serviceFiles, + }) } // scalaLibrary implements LanguageRule for the 'proto_scala_library' rule from diff --git a/plugin/scalapb/zio-grpc/BUILD.bazel b/plugin/scalapb/zio-grpc/BUILD.bazel new file mode 100644 index 000000000..e6c60e62a --- /dev/null +++ b/plugin/scalapb/zio-grpc/BUILD.bazel @@ -0,0 +1,26 @@ +load("@build_stack_rules_proto//rules:proto_plugin.bzl", "proto_plugin") + +proto_plugin( + name = "protoc-gen-zio-grpc", + out = "{BIN_DIR}/{PACKAGE}/{PROTO_LIBRARY_BASENAME}_zio_grpc.srcjar", + options = ["flat_package"], + tool = ":zio_grpc_codegen", + use_built_in_shell_environment = True, + visibility = ["//visibility:public"], +) + +java_binary( + name = "zio_grpc_codegen", + main_class = "scalapb.zio_grpc.ZioCodeGenerator", + visibility = ["//visibility:public"], + runtime_deps = [ + "@maven_zio//:com_thesamet_scalapb_zio_grpc_zio_grpc_codegen_2_12", + ], +) + +filegroup( + name = "all_files", + testonly = True, + srcs = ["BUILD.bazel"], + visibility = ["//plugin:__pkg__"], +) diff --git a/rules/scala/BUILD.bazel b/rules/scala/BUILD.bazel index ac85f1f4f..7fdd151dd 100644 --- a/rules/scala/BUILD.bazel +++ b/rules/scala/BUILD.bazel @@ -4,6 +4,7 @@ filegroup( srcs = [ "BUILD.bazel", "grpc_scala_library.bzl", + "grpc_zio_scala_library.bzl", "proto_scala_library.bzl", "scala_proto_library.bzl", ], diff --git a/rules/scala/grpc_zio_scala_library.bzl b/rules/scala/grpc_zio_scala_library.bzl new file mode 100644 index 000000000..0b28c94c7 --- /dev/null +++ b/rules/scala/grpc_zio_scala_library.bzl @@ -0,0 +1,6 @@ +"grpc_zio_scala_library.bzl provides a scala_library for grpc files." + +load("@io_bazel_rules_scala//scala:scala.bzl", "scala_library") + +def grpc_zio_scala_library(**kwargs): + scala_library(**kwargs)