44 _scala_proto_deps_providers = "scala_proto_deps_providers" ,
55)
66
7- def _generators (ctx ):
8- return dict (
9- ctx .attr .named_generators ,
10- scala = ctx .attr .main_generator ,
11- )
12-
137def _generators_jars (ctx ):
148 generator_deps = ctx .attr .extra_generator_dependencies + [
159 ctx .attr ._main_generator_dep ,
@@ -19,22 +13,6 @@ def _generators_jars(ctx):
1913 for dep in generator_deps
2014 ])
2115
22- def _generators_opts (ctx ):
23- opts = []
24- if ctx .attr .with_grpc :
25- opts .append ("grpc" )
26- if ctx .attr .with_flat_package :
27- opts .append ("flat_package" )
28- if ctx .attr .with_single_line_to_string :
29- opts .append ("single_line_to_proto_string" )
30- return "," .join (opts )
31-
32- def _compile_dep_ids (ctx ):
33- deps = ["scalapb_compile_deps" ]
34- if ctx .attr .with_grpc :
35- deps .append ("scalapb_grpc_deps" )
36- return deps
37-
3816def _ignored_proto_targets_by_label (ctx ):
3917 return {p .label : p for p in ctx .attr .blacklisted_protos }
4018
@@ -49,13 +27,14 @@ def _worker_flags(ctx, generators, jars):
4927 return "--jvm_flags=" + " " .join (["-D%s=%s" % i for i in env .items ()])
5028
5129def _scala_proto_toolchain_impl (ctx ):
52- generators = _generators ( ctx )
30+ generators = ctx . attr . generators
5331 generators_jars = _generators_jars (ctx )
32+ compile_dep_ids = ["scalapb_compile_deps" ]
5433 toolchain = platform_common .ToolchainInfo (
5534 generators = generators ,
5635 generators_jars = generators_jars ,
57- generators_opts = _generators_opts ( ctx ) ,
58- compile_dep_ids = _compile_dep_ids ( ctx ) ,
36+ generators_opts = ctx . attr . generators_opts ,
37+ compile_dep_ids = compile_dep_ids ,
5938 blacklisted_protos = _ignored_proto_targets_by_label (ctx ),
6039 protoc = ctx .executable .protoc ,
6140 scalac = ctx .attr .scalac .files_to_run ,
@@ -66,35 +45,20 @@ def _scala_proto_toolchain_impl(ctx):
6645 return [toolchain ]
6746
6847# Args:
69- # with_grpc: Enables generation of grpc service bindings for services
70- # with_flat_package: When true, ScalaPB will not append the protofile base name to the package name
71- # with_single_line_to_string: Enables generation of toString() methods that use the single line format
7248# blacklisted_protos: list of protobuf targets to exclude from recursive building
7349# code_generator: what code generator to use, usually you'll want the default
7450scala_proto_toolchain = rule (
7551 implementation = _scala_proto_toolchain_impl ,
7652 attrs = {
77- "with_grpc" : attr .bool (),
78- "with_flat_package" : attr .bool (),
79- "with_single_line_to_string" : attr .bool (),
8053 "blacklisted_protos" : attr .label_list (default = []),
8154 "code_generator" : attr .label (
8255 executable = True ,
8356 cfg = "exec" ,
8457 default = Label ("//src/scala/scripts:scalapb_worker" ),
8558 allow_files = True ,
8659 ),
87- # `scripts.ScalaPbCodeGenerator` and `_main_generator_dep` are currently
88- # necessary to support protoc-bridge < 0.9.8, specifically 0.7.14
89- # required by Scala 2.11. See #1647 and scalapb/ScalaPB#1771.
90- #
91- # If we drop 2.11 support, restore `scalapb.ScalaPbCodeGenerator` here,
92- # remove `_main_generator_dep`, and delete
93- # `//src/scala/scripts:scalapb_codegenerator_wrapper` and its files.
94- "main_generator" : attr .string (
95- default = "scripts.ScalaPbCodeGenerator" ,
96- ),
97- "named_generators" : attr .string_dict (),
60+ "generators" : attr .string_dict (),
61+ "generators_opts" : attr .string_list_dict (),
9862 "extra_generator_dependencies" : attr .label_list (
9963 providers = [JavaInfo ],
10064 ),
@@ -121,6 +85,13 @@ scala_proto_toolchain = rule(
12185 [proto rules documentation](https://docs.bazel.build/versions/master/be/protocol-buffer.html#proto_library)
12286 """ ,
12387 ),
88+ # `scripts.ScalaPbCodeGenerator` and `_main_generator_dep` are currently
89+ # necessary to support protoc-bridge < 0.9.8, specifically 0.7.14
90+ # required by Scala 2.11. See #1647 and scalapb/ScalaPB#1771.
91+ #
92+ # If we drop 2.11 support, restore `scalapb.ScalaPbCodeGenerator` here,
93+ # remove `_main_generator_dep`, and delete
94+ # `//src/scala/scripts:scalapb_codegenerator_wrapper` and its files.
12495 "_main_generator_dep" : attr .label (
12596 default = Label (
12697 "//src/scala/scripts:scalapb_codegenerator_wrapper" ,
@@ -132,6 +103,25 @@ scala_proto_toolchain = rule(
132103 },
133104)
134105
106+ def scalapb_toolchain (name , opts = [], ** kwargs ):
107+ """Sets up a scala_proto_toolchain using ScalaPB.
108+
109+ Args:
110+ name: A unique name for this target
111+ opts: scalapb generator options like 'grpc' or 'flat_package'
112+ kwargs: remaining arguments to `scala_proto_toolchain`
113+ """
114+ scala_proto_toolchain (
115+ name = name ,
116+ generators = {
117+ "scala" : "scripts.ScalaPbCodeGenerator" ,
118+ },
119+ generators_opts = {
120+ "scala" : opts ,
121+ },
122+ ** kwargs
123+ )
124+
135125def _scala_proto_deps_toolchain (ctx ):
136126 toolchain = platform_common .ToolchainInfo (
137127 dep_providers = ctx .attr .dep_providers ,
0 commit comments