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

Allow passing ScalaPB generator options to the toolchain #209

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
12 changes: 11 additions & 1 deletion docs/stardoc/scala_proto.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ See example use in [/tests/proto/BUILD](/tests/proto/BUILD)
## scala_proto_toolchain

<pre>
scala_proto_toolchain(<a href="#scala_proto_toolchain-name">name</a>, <a href="#scala_proto_toolchain-compiler">compiler</a>, <a href="#scala_proto_toolchain-compiler_supports_workers">compiler_supports_workers</a>)
scala_proto_toolchain(<a href="#scala_proto_toolchain-name">name</a>, <a href="#scala_proto_toolchain-compiler">compiler</a>, <a href="#scala_proto_toolchain-compiler_supports_workers">compiler_supports_workers</a>, <a href="#scala_proto_toolchain-generator_params">generator_params</a>)
</pre>


Expand All @@ -65,6 +65,7 @@ scala_proto_toolchain(
name = "scalapb_toolchain_example",
compiler = ":worker",
compiler_supports_workers = True,
generator_params = "grpc,java_conversions",
visibility = ["//visibility:public"],
)

Expand Down Expand Up @@ -117,6 +118,15 @@ toolchain(
Boolean; optional
</td>
</tr>
<tr id="scala_proto_toolchain-generator_params">
<td><code>generator_params</code></td>
<td>
String; optional
<p>
Generator params to pass to the scala PB compiler (ie java_conversions, flat_package, grpc)
</p>
</td>
</tr>
</tbody>
</table>

Expand Down
6 changes: 6 additions & 0 deletions rules/scala_proto.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def _scala_proto_toolchain_implementation(ctx):
return [platform_common.ToolchainInfo(
compiler = ctx.attr.compiler,
compiler_supports_workers = ctx.attr.compiler_supports_workers,
generator_params = ctx.attr.generator_params,
)]

scala_proto_toolchain = rule(
Expand All @@ -44,6 +45,10 @@ scala_proto_toolchain = rule(
cfg = "host",
),
"compiler_supports_workers": attr.bool(default = False),
"generator_params": attr.string(
doc = "Generator params to pass to the scala PB compiler (ie java_conversions, flat_package, grpc)",
default = "",
),
},
doc = """
Specifies a toolchain of the `@rules_scala_annex//rules/scala_proto:compiler_toolchain_type` toolchain type.
Expand All @@ -58,6 +63,7 @@ scala_proto_toolchain(
name = "scalapb_toolchain_example",
compiler = ":worker",
compiler_supports_workers = True,
generator_params = "grpc,java_conversions",
visibility = ["//visibility:public"],
)

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 @@ -21,6 +21,10 @@ object ScalaProtoWorker extends WorkerMain[Unit] {
.help("Output dir")
.metavar("output_dir")
.`type`(Arguments.fileType.verifyCanCreate)
parser
.addArgument("--generator_params")
.help("Generator params to pass to scala_out")
.metavar("generator_params")
parser
.addArgument("sources")
.help("Source files")
Expand All @@ -40,12 +44,18 @@ object ScalaProtoWorker extends WorkerMain[Unit] {
val scalaOut = namespace.get[File]("output_dir").toPath
Files.createDirectories(scalaOut)

val params = s"--scala_out=$scalaOut" :: sources.map(_.getPath)
val generatorParams = namespace.get[String]("generator_params")
val params = if (generatorParams == null) {
s"--scala_out=$scalaOut" :: sources.map(_.getPath)
} else {
s"--scala_out=$generatorParams:$scalaOut" :: sources.map(_.getPath)
}

ProtocBridge.runWithGenerators(
protoc = a => com.github.os72.protocjar.Protoc.runProtoc(a.toArray),
namedGenerators = List("scala" -> ScalaPbCodeGenerator),
params = params)
params = params
)
}

}
2 changes: 2 additions & 0 deletions rules/scala_proto/private/core.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ def scala_proto_library_implementation(ctx):

args = ctx.actions.args()
args.add("--output_dir", gendir.path)
if compiler.generator_params:
args.add("--generator_params", compiler.generator_params)
args.add_all("--", transitive_sources)
args.set_param_file_format("multiline")
args.use_param_file("@%s", use_always = True)
Expand Down