Skip to content

Commit

Permalink
move scala_repl rule to its own file (#820)
Browse files Browse the repository at this point in the history
long-stripe authored and johnynek committed Aug 19, 2019
1 parent 082ebe8 commit 516a2bf
Showing 3 changed files with 109 additions and 85 deletions.
65 changes: 0 additions & 65 deletions scala/private/rule_impls.bzl
Original file line number Diff line number Diff line change
@@ -1014,71 +1014,6 @@ def _pack_source_jars(ctx):
#_pack_source_jar may return None if java_common.pack_sources returned None (and it can)
return [source_jar] if source_jar else []

def scala_repl_impl(ctx):
scalac_provider = get_scalac_provider(ctx)

unused_dependency_checker_mode = get_unused_dependency_checker_mode(ctx)
unused_dependency_checker_is_off = unused_dependency_checker_mode == "off"

# need scala-compiler for MainGenericRunner below
jars = collect_jars_from_common_ctx(
ctx,
scalac_provider.default_repl_classpath,
unused_dependency_checker_is_off = unused_dependency_checker_is_off,
)
(cjars, transitive_rjars) = (jars.compile_jars, jars.transitive_runtime_jars)

args = " ".join(ctx.attr.scalacopts)

executable = declare_executable(ctx)

wrapper = write_java_wrapper(
ctx,
args,
wrapper_preamble = """
# save stty like in bin/scala
saved_stty=$(stty -g 2>/dev/null)
if [[ ! $? ]]; then
saved_stty=""
fi
function finish() {
if [[ "$saved_stty" != "" ]]; then
stty $saved_stty
saved_stty=""
fi
}
trap finish EXIT
""",
)

out = scala_binary_common(
ctx,
executable,
cjars,
transitive_rjars,
jars.transitive_compile_jars,
jars.jars2labels,
wrapper,
unused_dependency_checker_ignored_targets = [
target.label
for target in scalac_provider.default_repl_classpath +
ctx.attr.unused_dependency_checker_ignored_targets
],
unused_dependency_checker_mode = unused_dependency_checker_mode,
deps_providers = jars.deps_providers,
)
write_executable(
ctx = ctx,
executable = executable,
jvm_flags = ["-Dscala.usejavacp=true"] + ctx.attr.jvm_flags,
main_class = "scala.tools.nsc.MainGenericRunner",
rjars = out.transitive_rjars,
use_jacoco = False,
wrapper = wrapper,
)

return out

def _gen_test_suite_flags_based_on_prefixes_and_suffixes(ctx, archives):
return struct(
archiveFlag = "-Dbazel.discover.classes.archives.file.paths=%s" %
104 changes: 104 additions & 0 deletions scala/private/rules/scala_repl.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
"""Rule for launching a Scala REPL with dependencies"""

load(
"@io_bazel_rules_scala//scala/private:common_attributes.bzl",
"common_attrs",
"implicit_deps",
"launcher_template",
"resolve_deps",
)
load("@io_bazel_rules_scala//scala/private:common_outputs.bzl", "common_outputs")
load(
"@io_bazel_rules_scala//scala/private:rule_impls.bzl",
"collect_jars_from_common_ctx",
"declare_executable",
"get_scalac_provider",
"get_unused_dependency_checker_mode",
"scala_binary_common",
"write_executable",
"write_java_wrapper",
)

def _scala_repl_impl(ctx):
scalac_provider = get_scalac_provider(ctx)

unused_dependency_checker_mode = get_unused_dependency_checker_mode(ctx)
unused_dependency_checker_is_off = unused_dependency_checker_mode == "off"

# need scala-compiler for MainGenericRunner below
jars = collect_jars_from_common_ctx(
ctx,
scalac_provider.default_repl_classpath,
unused_dependency_checker_is_off = unused_dependency_checker_is_off,
)
(cjars, transitive_rjars) = (jars.compile_jars, jars.transitive_runtime_jars)

args = " ".join(ctx.attr.scalacopts)

executable = declare_executable(ctx)

wrapper = write_java_wrapper(
ctx,
args,
wrapper_preamble = """
# save stty like in bin/scala
saved_stty=$(stty -g 2>/dev/null)
if [[ ! $? ]]; then
saved_stty=""
fi
function finish() {
if [[ "$saved_stty" != "" ]]; then
stty $saved_stty
saved_stty=""
fi
}
trap finish EXIT
""",
)

out = scala_binary_common(
ctx,
executable,
cjars,
transitive_rjars,
jars.transitive_compile_jars,
jars.jars2labels,
wrapper,
unused_dependency_checker_ignored_targets = [
target.label
for target in scalac_provider.default_repl_classpath +
ctx.attr.unused_dependency_checker_ignored_targets
],
unused_dependency_checker_mode = unused_dependency_checker_mode,
deps_providers = jars.deps_providers,
)
write_executable(
ctx = ctx,
executable = executable,
jvm_flags = ["-Dscala.usejavacp=true"] + ctx.attr.jvm_flags,
main_class = "scala.tools.nsc.MainGenericRunner",
rjars = out.transitive_rjars,
use_jacoco = False,
wrapper = wrapper,
)

return out

_scala_repl_attrs = {}

_scala_repl_attrs.update(launcher_template)

_scala_repl_attrs.update(implicit_deps)

_scala_repl_attrs.update(common_attrs)

_scala_repl_attrs.update(resolve_deps)

scala_repl = rule(
attrs = _scala_repl_attrs,
executable = True,
fragments = ["java"],
outputs = common_outputs,
toolchains = ["@io_bazel_rules_scala//scala:toolchain_type"],
implementation = _scala_repl_impl,
)
25 changes: 5 additions & 20 deletions scala/scala.bzl
Original file line number Diff line number Diff line change
@@ -4,7 +4,6 @@ load(
_scala_library_for_plugin_bootstrapping_impl = "scala_library_for_plugin_bootstrapping_impl",
_scala_library_impl = "scala_library_impl",
_scala_macro_library_impl = "scala_macro_library_impl",
_scala_repl_impl = "scala_repl_impl",
)
load(
"@io_bazel_rules_scala//scala/private:common_attributes.bzl",
@@ -40,6 +39,10 @@ load(
"@io_bazel_rules_scala//scala/private:rules/scala_doc.bzl",
_scala_doc = "scala_doc",
)
load(
"@io_bazel_rules_scala//scala/private:rules/scala_repl.bzl",
_scala_repl = "scala_repl",
)
load(
"@io_bazel_rules_scala//scala/private:rules/scala_test.bzl",
_scala_test = "scala_test",
@@ -143,25 +146,6 @@ scala_macro_library = rule(
implementation = _scala_macro_library_impl,
)

_scala_repl_attrs = {}

_scala_repl_attrs.update(launcher_template)

_scala_repl_attrs.update(implicit_deps)

_scala_repl_attrs.update(common_attrs)

_scala_repl_attrs.update(resolve_deps)

scala_repl = rule(
attrs = _scala_repl_attrs,
executable = True,
fragments = ["java"],
outputs = common_outputs,
toolchains = ["@io_bazel_rules_scala//scala:toolchain_type"],
implementation = _scala_repl_impl,
)

# Scala library suite generates a series of scala libraries
# then it depends on them with a meta one which exports all the sub targets
def scala_library_suite(
@@ -259,6 +243,7 @@ def scala_specs2_junit_test(name, **kwargs):
# Re-export private rules for public consumption
scala_binary = _scala_binary
scala_doc = _scala_doc
scala_repl = _scala_repl
scala_repositories = _scala_repositories
scala_test = _scala_test
scala_test_suite = _scala_test_suite

0 comments on commit 516a2bf

Please sign in to comment.