Skip to content
This repository has been archived by the owner on Jun 4, 2024. It is now read-only.

Improve proto rules #292

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions rules/scala_proto.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ scala_proto_library = rule(
{
"deps": attr.label_list(
doc = "The proto_library targets you wish to generate Scala from",
providers = [ProtoInfo],
),
"_zipper": attr.label(cfg = "host", default = "@bazel_tools//tools/zip:zipper", executable = True),
},
Expand Down
14 changes: 12 additions & 2 deletions rules/scala_proto/private/ScalaProtoWorker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package annex.scala.proto
import higherkindness.rules_scala.common.args.implicits._
import higherkindness.rules_scala.common.worker.WorkerMain
import java.io.File
import java.nio.file.{Files, Paths}
import java.nio.file.Files
import java.util.Collections
import net.sourceforge.argparse4j.ArgumentParsers
import net.sourceforge.argparse4j.impl.Arguments
Expand All @@ -21,6 +21,13 @@ object ScalaProtoWorker extends WorkerMain[Unit] {
.help("Output dir")
.metavar("output_dir")
.`type`(Arguments.fileType.verifyCanCreate)
parser
.addArgument("--proto_paths")
.help("Paths to be passed to protoc as --proto_path arguments")
.metavar("proto_paths")
.nargs("*")
.`type`(Arguments.fileType.verifyCanRead.verifyIsDirectory)
.setDefault_(Collections.emptyList)
parser
.addArgument("sources")
.help("Source files")
Expand All @@ -36,11 +43,14 @@ object ScalaProtoWorker extends WorkerMain[Unit] {
protected[this] def work(ctx: Unit, args: Array[String]): Unit = {
val namespace = argParser.parseArgs(args)
val sources = namespace.getList[File]("sources").asScala.toList
val protoPaths = namespace.getList[File]("proto_paths").asScala.toList

val scalaOut = namespace.get[File]("output_dir").toPath
Files.createDirectories(scalaOut)

val params = s"--scala_out=$scalaOut" :: sources.map(_.getPath)
val params = s"--scala_out=$scalaOut" ::
sources.map(_.getPath) :::
protoPaths.map(dir => s"--proto_path=${dir.getPath}")

ProtocBridge.runWithGenerators(
protoc = a => com.github.os72.protocjar.Protoc.runProtoc(a.toArray),
Expand Down
13 changes: 5 additions & 8 deletions rules/scala_proto/private/core.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,9 @@ load(
scala_proto_library_private_attributes = {}

def scala_proto_library_implementation(ctx):
proto_deps = [dep for dep in ctx.attr.deps if ProtoInfo in dep]
if proto_deps != ctx.attr.deps:
fail("disallowed non proto deps in %s" % ctx.attr.deps)
protos = [dep[ProtoInfo] for dep in ctx.attr.deps]

protos = [dep[ProtoInfo] for dep in proto_deps]

transitive_sources = depset(transitive = [proto.transitive_sources for proto in protos])
sources = depset(direct = [source for proto in protos for source in proto.direct_sources])
transitive_proto_path = depset(transitive = [proto.transitive_proto_path for proto in protos])

compiler = ctx.toolchains["@rules_scala_annex//rules/scala_proto:compiler_toolchain_type"]
Expand All @@ -32,7 +28,8 @@ def scala_proto_library_implementation(ctx):

args = ctx.actions.args()
args.add("--output_dir", gendir.path)
args.add_all("--", transitive_sources)
args.add_all("--proto_paths", transitive_proto_path)
args.add_all("--", sources)
args.set_param_file_format("multiline")
args.use_param_file("@%s", use_always = True)

Expand All @@ -43,7 +40,7 @@ def scala_proto_library_implementation(ctx):

ctx.actions.run(
mnemonic = "ScalaProtoCompile",
inputs = depset(direct = [], transitive = [transitive_sources]),
inputs = depset(transitive = [proto.transitive_sources for proto in protos]),
outputs = [gendir],
executable = compiler.compiler.files_to_run.executable,
tools = compiler_inputs,
Expand Down
27 changes: 27 additions & 0 deletions tests/proto/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,30 @@ scala_library(
"@annex_test//:com_thesamet_scalapb_scalapb_runtime_2_12",
],
)

proto_library(
name = "one_proto",
srcs = ["one.proto"],
visibility = ["//visibility:public"],
deps = [
":zero_proto",
],
)

scala_proto_library(
name = "one_scala_proto",
deps = [
":one_proto",
],
)

scala_library(
name = "one_scala",
srcs = [":one_scala_proto"],
deps = [
":zero_scala",
"@annex_test//:com_google_protobuf_protobuf_java",
"@annex_test//:com_thesamet_scalapb_lenses_2_12",
"@annex_test//:com_thesamet_scalapb_scalapb_runtime_2_12",
],
)
10 changes: 10 additions & 0 deletions tests/proto/expected_one
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-rw---- 2.0 fat 0 bx 0% stor 20100101.000000 META-INF/
-rw---- 2.0 fat 48 b- 0% stor 20100101.000000 META-INF/MANIFEST.MF
-rw---- 1.0 fat 0 b- 0% stor 20100101.000000 anx/
-rw---- 1.0 fat 0 b- 0% stor 20100101.000000 anx/proto/
-rw---- 1.0 fat 0 b- 0% stor 20100101.000000 anx/proto/one/
-rw---- 2.0 fat 14827 b- 0% stor 20100101.000002 anx/proto/one/One$.class
-rw---- 2.0 fat 3097 b- 0% stor 20100101.000002 anx/proto/one/One$OneLens.class
-rw---- 2.0 fat 18959 b- 0% stor 20100101.000002 anx/proto/one/One.class
-rw---- 2.0 fat 6376 b- 0% stor 20100101.000002 anx/proto/one/OneProto$.class
-rw---- 2.0 fat 1937 b- 0% stor 20100101.000002 anx/proto/one/OneProto.class
File renamed without changes.
9 changes: 9 additions & 0 deletions tests/proto/one.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
syntax = "proto2";

option java_package = "anx.proto";

import "proto/zero.proto";

message One {
optional Zero zero = 1;
}
7 changes: 6 additions & 1 deletion tests/proto/test
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
[[ "$(bazel version | head -n1 | cut -d: -f2 | tr -d ' ')" =~ 0.15.*|0.16.* ]] || bazel build :zero_scala_proto

bazel build :zero_scala
diff expected <(
diff expected_zero <(
zipinfo -m -T --h-t "$(bazel info bazel-bin)/proto/zero_scala.jar"
)

bazel build :one_scala
diff expected_one <(
zipinfo -m -T --h-t "$(bazel info bazel-bin)/proto/one_scala.jar"
)