Skip to content

Commit b3fc6a2

Browse files
long-stripeittaiz
authored andcommitted
Refactor build_deployable (#832)
* build_deployable -> merge_jars with a more explicit interface * add docstring doc to merge_jars * buildifier fix * parameterize the entire progress_message
1 parent 8426026 commit b3fc6a2

File tree

2 files changed

+39
-15
lines changed

2 files changed

+39
-15
lines changed

scala/private/rule_impls.bzl

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -548,22 +548,32 @@ def _create_scala_compilation_provider(ctx, ijar, source_jar, deps_providers):
548548
runtime_deps = runtime_deps,
549549
)
550550

551-
def build_deployable(ctx, jars_list):
552-
# This calls bazels singlejar utility.
553-
# For a full list of available command line options see:
554-
# https://github.com/bazelbuild/bazel/blob/master/src/java_tools/singlejar/java/com/google/devtools/build/singlejar/SingleJar.java#L311
555-
# Use --compression to reduce size of deploy jars.
551+
def merge_jars(actions, deploy_jar, singlejar_executable, jars_list, main_class = "", progress_message = ""):
552+
"""Calls Bazel's singlejar utility.
553+
554+
For a full list of available command line options see:
555+
https://github.com/bazelbuild/bazel/blob/697d219526bffbecd29f29b402c9122ec5d9f2ee/src/java_tools/singlejar/java/com/google/devtools/build/singlejar/SingleJar.java#L337
556+
Use --compression to reduce size of deploy jars.
557+
558+
Args:
559+
actions: The actions module from ctx: https://docs.bazel.build/versions/master/skylark/lib/actions.html
560+
deploy_jar: The deploy jar, usually defined in ctx.outputs.
561+
singlejar_executable: The singlejar executable file.
562+
jars_list: The jars to pass to singlejar.
563+
main_class: The main class to run, if any. Defaults to an empty string.
564+
progress_message: A progress message to display when Bazel executes this action. Defaults to an empty string.
565+
"""
556566
args = ["--compression", "--normalize", "--sources"]
557567
args.extend([j.path for j in jars_list])
558-
if getattr(ctx.attr, "main_class", ""):
559-
args.extend(["--main_class", ctx.attr.main_class])
560-
args.extend(["--output", ctx.outputs.deploy_jar.path])
561-
ctx.actions.run(
568+
if main_class:
569+
args.extend(["--main_class", main_class])
570+
args.extend(["--output", deploy_jar.path])
571+
actions.run(
562572
inputs = jars_list,
563-
outputs = [ctx.outputs.deploy_jar],
564-
executable = ctx.executable._singlejar,
573+
outputs = [deploy_jar],
574+
executable = singlejar_executable,
565575
mnemonic = "ScalaDeployJar",
566-
progress_message = "scala deployable %s" % ctx.label,
576+
progress_message = progress_message,
567577
arguments = args,
568578
)
569579

@@ -832,7 +842,14 @@ def scala_binary_common(
832842
) # no need to build an ijar for an executable
833843
rjars = depset(outputs.full_jars, transitive = [rjars])
834844

835-
build_deployable(ctx, rjars.to_list())
845+
merge_jars(
846+
actions = ctx.actions,
847+
deploy_jar = ctx.outputs.deploy_jar,
848+
singlejar_executable = ctx.executable._singlejar,
849+
jars_list = rjars.to_list(),
850+
main_class = getattr(ctx.attr, "main_class", ""),
851+
progress_message = "Merging Scala binary jar: %s" % ctx.label,
852+
)
836853

837854
runfiles = ctx.runfiles(
838855
transitive_files = depset(

scala/private/rules/scala_library.bzl

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ load(
2020
)
2121
load(
2222
"@io_bazel_rules_scala//scala/private:rule_impls.bzl",
23-
"build_deployable",
2423
"collect_jars_from_common_ctx",
2524
"compile_or_empty",
2625
"get_scalac_provider",
2726
"get_unused_dependency_checker_mode",
27+
"merge_jars",
2828
"pack_source_jars",
2929
)
3030

@@ -82,7 +82,14 @@ def _lib(
8282

8383
transitive_rjars = depset(outputs.full_jars, transitive = [transitive_rjars])
8484

85-
build_deployable(ctx, transitive_rjars.to_list())
85+
merge_jars(
86+
actions = ctx.actions,
87+
deploy_jar = ctx.outputs.deploy_jar,
88+
singlejar_executable = ctx.executable._singlejar,
89+
jars_list = transitive_rjars.to_list(),
90+
main_class = getattr(ctx.attr, "main_class", ""),
91+
progress_message = "Merging Scala library jar: %s" % ctx.label,
92+
)
8693

8794
# Using transitive_files since transitive_rjars a depset and avoiding linearization
8895
runfiles = ctx.runfiles(

0 commit comments

Comments
 (0)