diff --git a/WORKSPACE b/WORKSPACE index 0904d6ee0..7dc3a2f37 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -259,6 +259,22 @@ load("@maven_akka//:defs.bzl", pinned_maven_akka_install = "pinned_maven_install pinned_maven_akka_install() +# bazel run @maven_zio//:pin, but first comment out the "maven_install_json" +# (put it back once pinned again) +maven_install( + name = "maven_zio", + artifacts = [ + "com.thesamet.scalapb.zio-grpc:zio-grpc-codegen_2.12:0.6.0", + ], + fetch_sources = True, + maven_install_json = "//:maven_zio_install.json", + repositories = ["https://repo1.maven.org/maven2"], +) + +load("@maven_zio//:defs.bzl", pinned_maven_zio_install = "pinned_maven_install") + +pinned_maven_zio_install() + # ---------------------------------------------------- # Closure # ---------------------------------------------------- diff --git a/language/protobuf/BUILD.bazel b/language/protobuf/BUILD.bazel index 296d88eba..ddb19069c 100644 --- a/language/protobuf/BUILD.bazel +++ b/language/protobuf/BUILD.bazel @@ -18,6 +18,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 fe8064fa9..79fe6567a 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/bufbuild" _ "github.com/stackb/rules_proto/pkg/plugin/stephenh/ts-proto" diff --git a/maven_zio_install.json b/maven_zio_install.json new file mode 100644 index 000000000..dad9ea3fa --- /dev/null +++ b/maven_zio_install.json @@ -0,0 +1,179 @@ +{ + "__AUTOGENERATED_FILE_DO_NOT_MODIFY_THIS_FILE_MANUALLY": "THERE_IS_NO_DATA_ONLY_ZUUL", + "__INPUT_ARTIFACTS_HASH": 799888365, + "__RESOLVED_ARTIFACTS_HASH": 669377701, + "artifacts": { + "com.google.protobuf:protobuf-java": { + "shasums": { + "jar": "6a9a2dff91dcf71f85be71ae971f6164b5a631dcd34bff08f0618535ca44ad02", + "sources": "98118edf28c74f173b25d8fce03f86ff95d61009df0c66324333573ec842f5d3" + }, + "version": "3.19.6" + }, + "com.thesamet.scalapb.zio-grpc:zio-grpc-codegen_2.12": { + "shasums": { + "jar": "0a47494a502470e5a7e13a55a2a91b020ef3cef7642932ae0332a9d53fed2bd4", + "sources": "a2ea65c99beb8375f16f79a57b8f69cf396245d228f43f72d8a3b8fedc494e95" + }, + "version": "0.6.0" + }, + "com.thesamet.scalapb:compilerplugin_2.12": { + "shasums": { + "jar": "7fae6c1e7301fba88cef19551027dee077208799130c8f8bbfe3e8addf0b5aa9", + "sources": "1c0ae73755353a67f3bd5e50496243c1edabeadbdff895e56234e9e0d601b8af" + }, + "version": "0.11.13" + }, + "com.thesamet.scalapb:protoc-bridge_2.12": { + "shasums": { + "jar": "5bbecd8ede37b266740ea523d65ebec27cdfbded40850013667ea9ed23be76d1", + "sources": "c2287667773b171051065ceb76adfbe9f6dc1f4e4fff03e0f0b288d659b7a47c" + }, + "version": "0.9.6" + }, + "com.thesamet.scalapb:protoc-gen_2.12": { + "shasums": { + "jar": "9483c434afe0bdb9e68da0a4a9b76f76a350ef4f6f397bda23f8fa6061153727", + "sources": "da917be8e7ba6c8eccdb5b155c9c515fbf2ef9e295b30934c27e2cf6519ab8b5" + }, + "version": "0.9.6" + }, + "dev.dirs:directories": { + "shasums": { + "jar": "6d18fe25aa30b7e08b908cd21151d8f96e22965c640acd7751add9bbfe6137d4", + "sources": "192050e3a2a0eba7f22745765aaaf567ce6d515fe6a992688b4e262e9f14947b" + }, + "version": "26" + }, + "org.scala-lang.modules:scala-collection-compat_2.12": { + "shasums": { + "jar": "873344f3788c84b6314afd20334b18a0821109a3635ecc71dac3598325e030ac", + "sources": "70682c87d4b7c3fd21f1f7a2c3a09fce812bab6d5aad2fbc5a68f1aa3f48e0dd" + }, + "version": "2.9.0" + }, + "org.scala-lang:scala-library": { + "shasums": { + "jar": "e51e6636c003359e106bea4ad99def70e613c290190c8c84f10f9560dd5b00ae", + "sources": "a34db9ab4b38c57f998e8f671b33762e2d6c3e39b8abf6fb8660a417447fa449" + }, + "version": "2.12.18" + } + }, + "dependencies": { + "com.thesamet.scalapb.zio-grpc:zio-grpc-codegen_2.12": [ + "com.thesamet.scalapb:compilerplugin_2.12", + "org.scala-lang:scala-library" + ], + "com.thesamet.scalapb:compilerplugin_2.12": [ + "com.google.protobuf:protobuf-java", + "com.thesamet.scalapb:protoc-gen_2.12", + "org.scala-lang.modules:scala-collection-compat_2.12", + "org.scala-lang:scala-library" + ], + "com.thesamet.scalapb:protoc-bridge_2.12": [ + "dev.dirs:directories", + "org.scala-lang:scala-library" + ], + "com.thesamet.scalapb:protoc-gen_2.12": [ + "com.thesamet.scalapb:protoc-bridge_2.12", + "org.scala-lang:scala-library" + ], + "org.scala-lang.modules:scala-collection-compat_2.12": [ + "org.scala-lang:scala-library" + ] + }, + "packages": { + "com.google.protobuf:protobuf-java": [ + "com.google.protobuf", + "com.google.protobuf.compiler" + ], + "com.thesamet.scalapb.zio-grpc:zio-grpc-codegen_2.12": [ + "scalapb.zio_grpc" + ], + "com.thesamet.scalapb:compilerplugin_2.12": [ + "scalapb", + "scalapb.compiler", + "scalapb.internal", + "scalapb.options", + "scalapb.options.compiler" + ], + "com.thesamet.scalapb:protoc-bridge_2.12": [ + "protocbridge", + "protocbridge.codegen", + "protocbridge.frontend" + ], + "com.thesamet.scalapb:protoc-gen_2.12": [ + "protocgen" + ], + "dev.dirs:directories": [ + "dev.dirs" + ], + "org.scala-lang.modules:scala-collection-compat_2.12": [ + "scala.annotation", + "scala.collection.compat", + "scala.collection.compat.immutable", + "scala.jdk", + "scala.jdk.javaapi", + "scala.util", + "scala.util.control.compat" + ], + "org.scala-lang:scala-library": [ + "scala", + "scala.annotation", + "scala.annotation.meta", + "scala.annotation.unchecked", + "scala.beans", + "scala.collection", + "scala.collection.concurrent", + "scala.collection.convert", + "scala.collection.generic", + "scala.collection.immutable", + "scala.collection.mutable", + "scala.collection.parallel", + "scala.collection.parallel.immutable", + "scala.collection.parallel.mutable", + "scala.collection.script", + "scala.compat", + "scala.concurrent", + "scala.concurrent.duration", + "scala.concurrent.forkjoin", + "scala.concurrent.impl", + "scala.io", + "scala.math", + "scala.ref", + "scala.reflect", + "scala.reflect.macros.internal", + "scala.runtime", + "scala.runtime.java8", + "scala.sys", + "scala.sys.process", + "scala.text", + "scala.util", + "scala.util.control", + "scala.util.hashing", + "scala.util.matching" + ] + }, + "repositories": { + "https://repo1.maven.org/maven2/": [ + "com.google.protobuf:protobuf-java", + "com.google.protobuf:protobuf-java:jar:sources", + "com.thesamet.scalapb.zio-grpc:zio-grpc-codegen_2.12", + "com.thesamet.scalapb.zio-grpc:zio-grpc-codegen_2.12:jar:sources", + "com.thesamet.scalapb:compilerplugin_2.12", + "com.thesamet.scalapb:compilerplugin_2.12:jar:sources", + "com.thesamet.scalapb:protoc-bridge_2.12", + "com.thesamet.scalapb:protoc-bridge_2.12:jar:sources", + "com.thesamet.scalapb:protoc-gen_2.12", + "com.thesamet.scalapb:protoc-gen_2.12:jar:sources", + "dev.dirs:directories", + "dev.dirs:directories:jar:sources", + "org.scala-lang.modules:scala-collection-compat_2.12", + "org.scala-lang.modules:scala-collection-compat_2.12:jar:sources", + "org.scala-lang:scala-library", + "org.scala-lang:scala-library:jar:sources" + ] + }, + "version": "2" +} 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 4ed574ba9..e60555ccd 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)