Skip to content
This repository was archived by the owner on Aug 5, 2022. It is now read-only.

Commit 5c966ee

Browse files
andyscottittaiz
authored andcommitted
expand locations in scalacopts (bazelbuild#890)
* expand locations in scalac options * allow plugins in expansion * add a happy path test * make the target names more obvious * comment
1 parent 937d469 commit 5c966ee

File tree

4 files changed

+71
-1
lines changed

4 files changed

+71
-1
lines changed

scala/private/rule_impls.bzl

+1-1
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ CurrentTarget: {current_target}
238238
compiler_classpath = _join_path(compiler_classpath_jars.to_list(), separator)
239239

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

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

test/plugins/BUILD

+40
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+
)
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

+5
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)