Skip to content

Commit

Permalink
expand locations in scalacopts (bazelbuild#890)
Browse files Browse the repository at this point in the history
* expand locations in scalac options

* allow plugins in expansion

* add a happy path test

* make the target names more obvious

* comment
  • Loading branch information
andyscott committed Dec 26, 2019
1 parent 29db4f4 commit c220f4d
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
2 changes: 1 addition & 1 deletion scala/private/rule_impls.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
40 changes: 40 additions & 0 deletions test/plugins/BUILD
Original file line number Diff line number Diff line change
@@ -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>
<name>plugin</name>
<classname>plugin.Plugin</classname>
</plugin>
"""

genrule(
name = "gen-scalac-plugin.xml",
outs = ["scalac-plugin.xml"],
cmd = _gen_plugin_xml_cmd,
)
25 changes: 25 additions & 0 deletions test/plugins/check_expand_location_plugin.scala
Original file line number Diff line number Diff line change
@@ -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")
}
}
}
5 changes: 5 additions & 0 deletions test/plugins/trivial.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package trivial

object Trivial {
// feel free to reuse this file for other plugin tests
}

0 comments on commit c220f4d

Please sign in to comment.