From 5c966ee10f2ab6d33165749b2b03a5088e3d742e Mon Sep 17 00:00:00 2001 From: Andy Scott Date: Tue, 24 Dec 2019 23:53:03 -0800 Subject: [PATCH] expand locations in scalacopts (#890) * expand locations in scalac options * allow plugins in expansion * add a happy path test * make the target names more obvious * comment --- scala/private/rule_impls.bzl | 2 +- test/plugins/BUILD | 40 +++++++++++++++++++ .../check_expand_location_plugin.scala | 25 ++++++++++++ test/plugins/trivial.scala | 5 +++ 4 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 test/plugins/BUILD create mode 100644 test/plugins/check_expand_location_plugin.scala create mode 100644 test/plugins/trivial.scala diff --git a/scala/private/rule_impls.bzl b/scala/private/rule_impls.bzl index 925179430..9f36f130a 100644 --- a/scala/private/rule_impls.bzl +++ b/scala/private/rule_impls.bzl @@ -238,7 +238,7 @@ CurrentTarget: {current_target} compiler_classpath = _join_path(compiler_classpath_jars.to_list(), separator) toolchain = ctx.toolchains["@io_bazel_rules_scala//scala:toolchain_type"] - scalacopts = toolchain.scalacopts + in_scalacopts + scalacopts = [ctx.expand_location(v, ctx.attr.plugins) for v in toolchain.scalacopts + in_scalacopts] scalac_args = """ Classpath: {cp} diff --git a/test/plugins/BUILD b/test/plugins/BUILD new file mode 100644 index 000000000..1a5cf607e --- /dev/null +++ b/test/plugins/BUILD @@ -0,0 +1,40 @@ +load("//scala:scala.bzl", "scala_library") + +scala_library( + name = "check_expand_location", + srcs = ["trivial.scala"], + plugins = [ + ":check_expand_location_plugin_deploy.jar", + ], + scalacopts = [ + "-P:diablerie:location=$(location :check_expand_location_plugin_deploy.jar)", + ], +) + +scala_library( + name = "check_expand_location_plugin", + srcs = [ + "check_expand_location_plugin.scala", + ], + resource_strip_prefix = package_name(), + resources = [ + ":gen-scalac-plugin.xml", + ], + deps = [ + "@io_bazel_rules_scala_scala_compiler", + ], +) + +_gen_plugin_xml_cmd = """ +cat > $@ << EOF + + plugin + plugin.Plugin + +""" + +genrule( + name = "gen-scalac-plugin.xml", + outs = ["scalac-plugin.xml"], + cmd = _gen_plugin_xml_cmd, +) diff --git a/test/plugins/check_expand_location_plugin.scala b/test/plugins/check_expand_location_plugin.scala new file mode 100644 index 000000000..fc5f61289 --- /dev/null +++ b/test/plugins/check_expand_location_plugin.scala @@ -0,0 +1,25 @@ +package plugin + +import scala.tools.nsc.Global +import scala.tools.nsc.Phase +import scala.tools.nsc.plugins.{ Plugin => NscPlugin} +import scala.tools.nsc.plugins.PluginComponent + +import java.io.File + +final class Plugin(override val global: Global) extends NscPlugin { + override val name: String = "diablerie" + override val description: String = "just another plugin" + override val components: List[PluginComponent] = Nil + + override def processOptions(options: List[String], error: String => Unit): Unit = { + options + .find(_.startsWith("location=")) + .map(_.stripPrefix("location=")) + .map(v => new File(v).exists) match { + case Some(true) => () + case Some(false) => error("expanded location doesn't exist") + case None => error("missing location argument") + } + } +} diff --git a/test/plugins/trivial.scala b/test/plugins/trivial.scala new file mode 100644 index 000000000..ed5e226bc --- /dev/null +++ b/test/plugins/trivial.scala @@ -0,0 +1,5 @@ +package trivial + +object Trivial { + // feel free to reuse this file for other plugin tests +}