Skip to content

Commit

Permalink
Support declaring test javaProps from the main scope
Browse files Browse the repository at this point in the history
  • Loading branch information
Gedochao committed Apr 20, 2023
1 parent 2c42b26 commit 43a0ab8
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ case object ScalaPreprocessor extends Preprocessor {
val usingDirectiveHandlers: Seq[DirectiveHandler[BuildOptions]] =
Seq[DirectiveHandler[_ <: HasBuildOptions]](
directives.CustomJar.handler,
directives.JavaProps.handler,
directives.JavaHome.handler,
directives.Jvm.handler,
directives.MainClass.handler,
Expand All @@ -94,7 +93,8 @@ case object ScalaPreprocessor extends Preprocessor {
Seq[DirectiveHandler[_ <: HasBuildOptionsWithRequirements]](
directives.Dependency.handler,
directives.JavaOptions.handler,
directives.JavacOptions.handler
directives.JavacOptions.handler,
directives.JavaProps.handler
).map(_.mapE(_.buildOptionsWithRequirements))

val requireDirectiveHandlers: Seq[DirectiveHandler[BuildRequirements]] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,19 @@ class DirectiveTests extends munit.FunSuite {
expect(if isTestScope then hasTestJavaOpts else !hasTestJavaOpts)
}
}
test(s"resolve test scope javaProps correctly when building for ${scope.name} scope") {
withProjectFile(projectFileContent =
"""//> using javaProp "foo=1"
|//> using test.javaProp "bar=2"
|//> using test.dep "org.scalameta::munit::0.7.29"
|""".stripMargin
) { (build, isTestScope) =>
val javaProps = build.options.javaOptions.javaOpts.toSeq.map(_.value.value)
expect(javaProps.contains("-Dfoo=1"))
val hasTestJavaProps = javaProps.contains("-Dbar=2")
expect(if isTestScope then hasTestJavaProps else !hasTestJavaProps)
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,51 @@ package scala.build.preprocessing.directives

import scala.build.directives.*
import scala.build.errors.BuildException
import scala.build.options.{BuildOptions, JavaOpt, ShadowingSeq}
import scala.build.options.{BuildOptions, JavaOpt, Scope, ShadowingSeq, WithBuildRequirements}
import scala.build.preprocessing.directives.JavaProps.buildOptions
import scala.build.{Logger, Positioned, options}
import scala.cli.commands.SpecificationLevel

@DirectiveGroupName("Java properties")
@DirectiveExamples("//> using javaProp \"foo1=bar\", \"foo2\"")
@DirectiveExamples("//> using test.javaProp \"foo3=bar\", \"foo4\"")
@DirectiveUsage(
"//> using javaProp _key=val_",
"""`//> using javaProp `_key=value_
|`//> using javaProp `_key_""".stripMargin
)
@DirectiveDescription("Add Java properties")
@DirectiveLevel(SpecificationLevel.MUST)
// format: off
final case class JavaProps(
@DirectiveName("javaProp")
javaProperty: List[Positioned[String]] = Nil
) extends HasBuildOptions {
// format: on
def buildOptions: Either[BuildException, BuildOptions] = {
val javaOpts = javaProperty.map { positioned =>
javaProperty: List[Positioned[String]] = Nil,
@DirectiveName("test.javaProp")
testJavaProperty: List[Positioned[String]] = Nil
) extends HasBuildOptionsWithRequirements {
def buildOptionsWithRequirements
: Either[BuildException, List[WithBuildRequirements[BuildOptions]]] =
Right(List(
buildOptions(javaProperty).withEmptyRequirements,
buildOptions(testJavaProperty).withScopeRequirement(Scope.Test)
))
}

object JavaProps {
val handler: DirectiveHandler[JavaProps] = DirectiveHandler.derive

def buildOptions(javaProperties: List[Positioned[String]]): BuildOptions = {
val javaOpts = javaProperties.map { positioned =>
positioned.map { v =>
v.split("=") match {
case Array(k) => JavaOpt(s"-D$k")
case Array(k, v) => JavaOpt(s"-D$k=$v")
}
}
}
val buildOpt = BuildOptions(
BuildOptions(
javaOptions = options.JavaOptions(
javaOpts = ShadowingSeq.from(javaOpts)
)
)
Right(buildOpt)
}
}

object JavaProps {
val handler: DirectiveHandler[JavaProps] = DirectiveHandler.derive
}
2 changes: 2 additions & 0 deletions website/docs/reference/directives.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ Add Java properties
#### Examples
`//> using javaProp "foo1=bar", "foo2"`

`//> using test.javaProp "foo3=bar", "foo4"`

### Javac options

Add Javac options which will be passed when compiling sources.
Expand Down
2 changes: 2 additions & 0 deletions website/docs/reference/scala-command/directives.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ Add Java properties
#### Examples
`//> using javaProp "foo1=bar", "foo2"`

`//> using test.javaProp "foo3=bar", "foo4"`

### Main class

Specify default main class
Expand Down

0 comments on commit 43a0ab8

Please sign in to comment.