Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move scala_repl rule to its own file #820

Merged
merged 1 commit into from
Aug 19, 2019
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
65 changes: 0 additions & 65 deletions scala/private/rule_impls.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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" %
Expand Down
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"],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should double check this. I think this may be a deprecated or even removed Java sandwich API. Cc @iirina

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean requesting the java fragment? It's required when using something from the java configuration, like java_toolchain.

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
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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