Skip to content

Commit

Permalink
Add stackb:rules_proto:grpc_zio_scala_library (#350)
Browse files Browse the repository at this point in the history
* Add support for scalapb:zio-grpc
* Add zio maven deps
  • Loading branch information
pcj authored Jul 16, 2024
1 parent 5428336 commit 3b147a8
Show file tree
Hide file tree
Showing 11 changed files with 349 additions and 0 deletions.
16 changes: 16 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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
# ----------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions language/protobuf/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions language/protobuf/protobuf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
179 changes: 179 additions & 0 deletions maven_zio_install.json
Original file line number Diff line number Diff line change
@@ -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"
}
30 changes: 30 additions & 0 deletions pkg/plugin/scalapb/zio_grpc/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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__"],
)
40 changes: 40 additions & 0 deletions pkg/plugin/scalapb/zio_grpc/protoc_gen_zio_grpc.go
Original file line number Diff line number Diff line change
@@ -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(),
}
}
41 changes: 41 additions & 0 deletions pkg/plugin/scalapb/zio_grpc/protoc_gen_zio_grpc_test.go
Original file line number Diff line number Diff line change
@@ -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,
},
})
}
8 changes: 8 additions & 0 deletions pkg/rule/rules_scala/scala_library.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
Expand All @@ -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
Expand Down
26 changes: 26 additions & 0 deletions plugin/scalapb/zio-grpc/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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__"],
)
1 change: 1 addition & 0 deletions rules/scala/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
Expand Down
6 changes: 6 additions & 0 deletions rules/scala/grpc_zio_scala_library.bzl
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 3b147a8

Please sign in to comment.