Skip to content

Commit 8fe4ed5

Browse files
Allow to add resource dirs via using directives
1 parent 22111a8 commit 8fe4ed5

File tree

4 files changed

+49
-4
lines changed

4 files changed

+49
-4
lines changed

modules/build/src/main/scala/scala/build/preprocessing/ScalaPreprocessor.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ case object ScalaPreprocessor extends Preprocessor {
2929
UsingJavaOptionsDirectiveHandler,
3030
UsingJavaHomeDirectiveHandler,
3131
UsingTestFrameworkDirectiveHandler,
32-
UsingCustomJarDirectiveHandler
32+
UsingCustomJarDirectiveHandler,
33+
UsingResourcesDirectiveHandler
3334
)
3435

3536
val requireDirectiveHandlers = Seq[RequireDirectiveHandler](
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package scala.build.preprocessing.directives
2+
3+
import scala.build.Os
4+
import scala.build.options.{BuildOptions, ClassPathOptions}
5+
6+
case object UsingResourcesDirectiveHandler extends UsingDirectiveHandler {
7+
def name = "Resources"
8+
def description = "Manually adds a resource directory to the class path"
9+
def usage = "using resource _path_ | using resources _path1_ _path2_ …"
10+
override def usageMd =
11+
"`using resource `_path_ | `using resources `_path1_ _path2_ …"
12+
override def examples = Seq(
13+
"using resource \"./resources\""
14+
)
15+
16+
def handle(directive: Directive): Option[Either[String, BuildOptions]] =
17+
directive.values match {
18+
case Seq("resource" | "resources", paths @ _*) =>
19+
val paths0 = paths.map(os.Path(_, Os.pwd))
20+
val options = BuildOptions(
21+
classPathOptions = ClassPathOptions(
22+
extraClassPath = paths0
23+
)
24+
)
25+
Some(Right(options))
26+
case _ =>
27+
None
28+
}
29+
}

modules/integration/src/test/scala/scala/cli/integration/RunTestDefinitions.scala

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -980,13 +980,14 @@ abstract class RunTestDefinitions(val scalaVersionOpt: Option[String])
980980
forbiddenDirTest()
981981
}
982982

983-
private val resourcesInputs = {
983+
private def resourcesInputs(directive: String = "") = {
984984
val resourceContent = "Hello from resources"
985985
TestInputs(
986986
Seq(
987987
os.rel / "resources" / "test" / "data" -> resourceContent,
988988
os.rel / "Test.scala" ->
989-
s"""object Test {
989+
s"""$directive
990+
|object Test {
990991
| def main(args: Array[String]): Unit = {
991992
| val cl = Thread.currentThread().getContextClassLoader
992993
| val is = cl.getResourceAsStream("test/data")
@@ -999,9 +1000,14 @@ abstract class RunTestDefinitions(val scalaVersionOpt: Option[String])
9991000
)
10001001
}
10011002
test("resources") {
1002-
resourcesInputs.fromRoot { root =>
1003+
resourcesInputs().fromRoot { root =>
10031004
os.proc(TestUtil.cli, "run", ".", "--resources", "./resources").call(cwd = root)
10041005
}
10051006
}
1007+
test("resources via directive") {
1008+
resourcesInputs("using resources ./resources").fromRoot { root =>
1009+
os.proc(TestUtil.cli, "run", ".").call(cwd = root)
1010+
}
1011+
}
10061012

10071013
}

website/docs/reference/directives.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,15 @@ Adds a repository for dependency resolution
8080

8181
`using repository https://maven-central.storage-download.googleapis.com/maven2`
8282

83+
### Resources
84+
85+
Manually adds a resource directory to the class path
86+
87+
`using resource `_path_ | `using resources `_path1_ _path2_
88+
89+
#### Examples
90+
`using resource "./resources"`
91+
8392
### Scala version
8493

8594
Sets the default Scala version

0 commit comments

Comments
 (0)