Skip to content

Commit

Permalink
Add support for the -with-compiler runner option
Browse files Browse the repository at this point in the history
  • Loading branch information
Gedochao committed Jan 16, 2023
1 parent 62ed433 commit 6b51d62
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@ final case class SharedOptions(
@Tag(tags.implementation)
@Hidden
scalaLibrary: Option[Boolean] = None,
@Group("Scala")
@HelpMessage("Allows to include the Scala compiler artifacts on the classpath.")
@Tag(tags.must)
@Name("withScalaCompiler")
@Name("-with-compiler")
withCompiler: Option[Boolean] = None,
@Group("Java")
@HelpMessage("Do not add dependency to Scala Standard library. This is useful, when Scala CLI works with pure Java projects.")
@Tag(tags.implementation)
Expand Down Expand Up @@ -276,6 +282,7 @@ final case class SharedOptions(
.map(bo.MaybeScalaVersion(_)),
scalaBinaryVersion = scalaBinaryVersion.map(_.trim).filter(_.nonEmpty),
addScalaLibrary = scalaLibrary.orElse(java.map(!_)),
addScalaCompiler = withCompiler,
generateSemanticDbs = semanticDb,
scalacOptions = scalac
.scalacOption
Expand Down
1 change: 1 addition & 0 deletions modules/cli/src/main/scala/scala/cli/exportCmd/Mill.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ final case class Mill(
private def scalaVersionSettings(options: BuildOptions, sources: Sources): MillProject = {

val pureJava = !options.scalaOptions.addScalaLibrary.contains(true) &&
!options.scalaOptions.addScalaCompiler.contains(true) &&
sources.paths.forall(_._1.last.endsWith(".java")) &&
sources.inMemory.forall(_.generatedRelPath.last.endsWith(".java")) &&
options.classPathOptions.extraDependencies.toSeq
Expand Down
1 change: 1 addition & 0 deletions modules/cli/src/main/scala/scala/cli/exportCmd/Sbt.scala
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ final case class Sbt(
private def pureJavaSettings(options: BuildOptions, sources: Sources): SbtProject = {

val pureJava = !options.scalaOptions.addScalaLibrary.contains(true) &&
!options.scalaOptions.addScalaCompiler.contains(true) &&
sources.paths.forall(_._1.last.endsWith(".java")) &&
sources.inMemory.forall(_.generatedRelPath.last.endsWith(".java")) &&
options.classPathOptions.extraDependencies.toSeq
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -540,4 +540,32 @@ abstract class CompileTestDefinitions(val scalaVersionOpt: Option[String])
expect(classFiles.contains(os.rel / "Hello.class"))
}
}

private def compilerArtifactName: String =
if (actualScalaVersion.startsWith("3")) "scala3-compiler" else "scala-compiler"

test(s"ensure the -with-compiler option adds $compilerArtifactName to the classpath") {
TestInputs(os.rel / "s.sc" -> """println("Hello")""")
.fromRoot { root =>
val compileRes = os.proc(
TestUtil.cli,
"compile",
"s.sc",
"--print-classpath",
extraOptions
)
.call(cwd = root)
expect(!compileRes.out.trim().contains(compilerArtifactName))
val compileWithCompilerRes = os.proc(
TestUtil.cli,
"compile",
"s.sc",
"-with-compiler",
"--print-classpath",
extraOptions
)
.call(cwd = root)
expect(compileWithCompilerRes.out.trim().contains(compilerArtifactName))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -466,4 +466,29 @@ trait RunScalacCompatTestDefinitions { _: RunTestDefinitions =>
}
}
}

if (actualScalaVersion.startsWith("3"))
test("ensure -with-compiler is supported for Scala 3") {
TestInputs(
os.rel / "s.sc" ->
"println(dotty.tools.dotc.config.Properties.simpleVersionString)"
)
.fromRoot { root =>
val res =
os.proc(TestUtil.cli, "-with-compiler", "s.sc", extraOptions)
.call(cwd = root)
expect(res.out.trim() == actualScalaVersion)
}
}

if (actualScalaVersion.startsWith("2.13"))
test("ensure -with-compiler is supported for Scala 2.13") {
TestInputs(os.rel / "s.sc" -> "println(scala.tools.nsc.Properties.versionString)")
.fromRoot { root =>
val res =
os.proc(TestUtil.cli, "-with-compiler", "s.sc", extraOptions)
.call(cwd = root)
expect(res.out.trim() == s"version $actualScalaVersion")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,19 @@ final case class BuildOptions(
}
}

private def scalaCompilerDependencies: Either[BuildException, Seq[AnyDependency]] = either {
value(scalaParams).toSeq.flatMap { sp =>
if (scalaOptions.addScalaCompiler.getOrElse(false))
Seq(
if (sp.scalaVersion.startsWith("3."))
dep"org.scala-lang::scala3-compiler::${sp.scalaVersion}"
else
dep"org.scala-lang:scala-compiler:${sp.scalaVersion}"
)
else Nil
}
}

private def maybeJsDependencies: Either[BuildException, Seq[AnyDependency]] = either {
if (platform.value == Platform.JS)
value(scalaParams).toSeq.flatMap { scalaParams0 =>
Expand All @@ -103,6 +116,7 @@ final case class BuildOptions(
value(maybeJsDependencies).map(Positioned.none(_)) ++
value(maybeNativeDependencies).map(Positioned.none(_)) ++
value(scalaLibraryDependencies).map(Positioned.none(_)) ++
value(scalaCompilerDependencies).map(Positioned.none(_)) ++
classPathOptions.extraDependencies.toSeq
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ final case class ScalaOptions(
scalaVersion: Option[MaybeScalaVersion] = None,
scalaBinaryVersion: Option[String] = None,
addScalaLibrary: Option[Boolean] = None,
addScalaCompiler: Option[Boolean] = None,
generateSemanticDbs: Option[Boolean] = None,
scalacOptions: ShadowingSeq[Positioned[ScalacOpt]] = ShadowingSeq.empty,
extraScalaVersions: Set[String] = Set.empty,
Expand Down
6 changes: 6 additions & 0 deletions website/docs/reference/cli-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -1379,6 +1379,12 @@ Specify platform
### `--scala-library`

[Internal]
### `--with-compiler`

Aliases: `-with-compiler`, `--with-scala-compiler`

Allows to include the Scala compiler artifacts on the classpath.

### `--java`

[Internal]
Expand Down
8 changes: 8 additions & 0 deletions website/docs/reference/scala-command/cli-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,14 @@ Specify platform

`IMPLEMENTATION specific` per Scala Runner specification

### `--with-compiler`

Aliases: `-with-compiler`, `--with-scala-compiler`

`MUST have` per Scala Runner specification

Allows to include the Scala compiler artifacts on the classpath.

### `--java`

`IMPLEMENTATION specific` per Scala Runner specification
Expand Down
54 changes: 54 additions & 0 deletions website/docs/reference/scala-command/runner-specification.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ Add a resource directory

Aliases: `--resource-dir`

**--with-compiler**

Allows to include the Scala compiler artifacts on the classpath.

Aliases: `-with-compiler` ,`--with-scala-compiler`

**--compilation-output**

Copy compilation results to output directory using either relative or absolute path
Expand Down Expand Up @@ -591,6 +597,12 @@ Add a resource directory

Aliases: `--resource-dir`

**--with-compiler**

Allows to include the Scala compiler artifacts on the classpath.

Aliases: `-with-compiler` ,`--with-scala-compiler`

**--compilation-output**

Copy compilation results to output directory using either relative or absolute path
Expand Down Expand Up @@ -1100,6 +1112,12 @@ Add a resource directory

Aliases: `--resource-dir`

**--with-compiler**

Allows to include the Scala compiler artifacts on the classpath.

Aliases: `-with-compiler` ,`--with-scala-compiler`

**--compilation-output**

Copy compilation results to output directory using either relative or absolute path
Expand Down Expand Up @@ -1623,6 +1641,12 @@ Add a resource directory

Aliases: `--resource-dir`

**--with-compiler**

Allows to include the Scala compiler artifacts on the classpath.

Aliases: `-with-compiler` ,`--with-scala-compiler`

**--compilation-output**

Copy compilation results to output directory using either relative or absolute path
Expand Down Expand Up @@ -2184,6 +2208,12 @@ Add a resource directory
Aliases: `--resource-dir`
**--with-compiler**
Allows to include the Scala compiler artifacts on the classpath.
Aliases: `-with-compiler` ,`--with-scala-compiler`
**--compilation-output**
Copy compilation results to output directory using either relative or absolute path
Expand Down Expand Up @@ -2725,6 +2755,12 @@ Add a resource directory
Aliases: `--resource-dir`
**--with-compiler**
Allows to include the Scala compiler artifacts on the classpath.
Aliases: `-with-compiler` ,`--with-scala-compiler`
**--compilation-output**
Copy compilation results to output directory using either relative or absolute path
Expand Down Expand Up @@ -3276,6 +3312,12 @@ Add a resource directory
Aliases: `--resource-dir`
**--with-compiler**
Allows to include the Scala compiler artifacts on the classpath.
Aliases: `-with-compiler` ,`--with-scala-compiler`
**--compilation-output**
Copy compilation results to output directory using either relative or absolute path
Expand Down Expand Up @@ -3880,6 +3922,12 @@ Add a resource directory
Aliases: `--resource-dir`
**--with-compiler**
Allows to include the Scala compiler artifacts on the classpath.
Aliases: `-with-compiler` ,`--with-scala-compiler`
**--compilation-output**
Copy compilation results to output directory using either relative or absolute path
Expand Down Expand Up @@ -4681,6 +4729,12 @@ Add a resource directory
Aliases: `--resource-dir`
**--with-compiler**
Allows to include the Scala compiler artifacts on the classpath.
Aliases: `-with-compiler` ,`--with-scala-compiler`
**--compilation-output**
Copy compilation results to output directory using either relative or absolute path
Expand Down

0 comments on commit 6b51d62

Please sign in to comment.