Skip to content

Commit

Permalink
Add support for scalapb:zio-grpc
Browse files Browse the repository at this point in the history
  • Loading branch information
pcj committed Nov 23, 2023
1 parent 52fcb49 commit ea16d93
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 0 deletions.
1 change: 1 addition & 0 deletions language/protobuf/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
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 ea16d93

Please sign in to comment.