Skip to content

Commit f5ec991

Browse files
andyscottAndre Rocha
authored and
Andre Rocha
committed
expand locations in scalacopts (bazel-contrib#890)
* expand locations in scalac options * allow plugins in expansion * add a happy path test * make the target names more obvious * comment
1 parent 48b37a5 commit f5ec991

File tree

4 files changed

+71
-1
lines changed

4 files changed

+71
-1
lines changed

scala/private/rule_impls.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ CurrentTarget: {current_target}
239239
compiler_classpath = _join_path(compiler_classpath_jars.to_list(), separator)
240240

241241
toolchain = ctx.toolchains["@io_bazel_rules_scala//scala:toolchain_type"]
242-
scalacopts = toolchain.scalacopts + in_scalacopts
242+
scalacopts = [ctx.expand_location(v, ctx.attr.plugins) for v in toolchain.scalacopts + in_scalacopts]
243243

244244
scalac_args = """
245245
Classpath: {cp}

test/plugins/BUILD

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
load("//scala:scala.bzl", "scala_library")
2+
3+
scala_library(
4+
name = "check_expand_location",
5+
srcs = ["trivial.scala"],
6+
plugins = [
7+
":check_expand_location_plugin_deploy.jar",
8+
],
9+
scalacopts = [
10+
"-P:diablerie:location=$(location :check_expand_location_plugin_deploy.jar)",
11+
],
12+
)
13+
14+
scala_library(
15+
name = "check_expand_location_plugin",
16+
srcs = [
17+
"check_expand_location_plugin.scala",
18+
],
19+
resource_strip_prefix = package_name(),
20+
resources = [
21+
":gen-scalac-plugin.xml",
22+
],
23+
deps = [
24+
"@io_bazel_rules_scala_scala_compiler",
25+
],
26+
)
27+
28+
_gen_plugin_xml_cmd = """
29+
cat > $@ << EOF
30+
<plugin>
31+
<name>plugin</name>
32+
<classname>plugin.Plugin</classname>
33+
</plugin>
34+
"""
35+
36+
genrule(
37+
name = "gen-scalac-plugin.xml",
38+
outs = ["scalac-plugin.xml"],
39+
cmd = _gen_plugin_xml_cmd,
40+
)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package plugin
2+
3+
import scala.tools.nsc.Global
4+
import scala.tools.nsc.Phase
5+
import scala.tools.nsc.plugins.{ Plugin => NscPlugin}
6+
import scala.tools.nsc.plugins.PluginComponent
7+
8+
import java.io.File
9+
10+
final class Plugin(override val global: Global) extends NscPlugin {
11+
override val name: String = "diablerie"
12+
override val description: String = "just another plugin"
13+
override val components: List[PluginComponent] = Nil
14+
15+
override def processOptions(options: List[String], error: String => Unit): Unit = {
16+
options
17+
.find(_.startsWith("location="))
18+
.map(_.stripPrefix("location="))
19+
.map(v => new File(v).exists) match {
20+
case Some(true) => ()
21+
case Some(false) => error("expanded location doesn't exist")
22+
case None => error("missing location argument")
23+
}
24+
}
25+
}

test/plugins/trivial.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package trivial
2+
3+
object Trivial {
4+
// feel free to reuse this file for other plugin tests
5+
}

0 commit comments

Comments
 (0)