Skip to content
Prev Previous commit
Next Next commit
ScoverageCompiler refactoring to support SJS
  • Loading branch information
armanbilge committed May 31, 2022
commit 61be139ab8cf31a065a1593da1c5cd851e17c1ab
11 changes: 10 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ lazy val plugin =
.settings(
Test / unmanagedSourceDirectories += (Test / sourceDirectory).value / "scala-2.12+"
)
.dependsOn(domain, reporter % "test->compile", serializer)
.dependsOn(domain, reporter % "test->compile", serializer, buildInfo % Test)

lazy val reporter =
project
Expand All @@ -182,6 +182,15 @@ lazy val reporter =
)
.dependsOn(domain, serializer)

lazy val buildInfo =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How come we have a separate build target for buildInfo instead of just including the buildInfo stuff inside of the plugin module and enabling the BuildInfoPlugin there?

Copy link
Contributor Author

@armanbilge armanbilge Jun 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha, yes I suppose we could. I did this because the BuildInfo was only needed in the Test scope and there's currently no way to configure that (see sbt/sbt-buildinfo#186). But I guess here if we "leak" the BuildInfo as part of the shipped plugin, it's probably not a big deal?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was only needed in the Test scope

Yea, that's sort of what I expected. No worries. Let's leave it as is.

project
.settings(
buildInfoKeys += BuildInfoKey("scalaJSVersion", scalaJSVersion),
publishArtifact := false,
publishLocal := {}
)
.enablePlugins(BuildInfoPlugin)

lazy val domain =
project
.settings(
Expand Down
60 changes: 50 additions & 10 deletions plugin/src/test/scala/scoverage/ScoverageCompiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import scala.tools.nsc.plugins.PluginComponent
import scala.tools.nsc.transform.Transform
import scala.tools.nsc.transform.TypingTransformers

import buildinfo.BuildInfo
import scoverage.reporter.IOUtils

private[scoverage] object ScoverageCompiler {
Expand All @@ -24,9 +25,17 @@ private[scoverage] object ScoverageCompiler {
def classPath: Seq[String] =
getScalaJars.map(
_.getAbsolutePath
) :+ sbtCompileDir.getAbsolutePath :+ runtimeClasses.getAbsolutePath
) :+ sbtCompileDir.getAbsolutePath :+ runtimeClasses("jvm").getAbsolutePath

def settings: Settings = {
def jsClassPath: Seq[String] =
getScalaJsJars.map(
_.getAbsolutePath
) :+ sbtCompileDir.getAbsolutePath :+ runtimeClasses("js").getAbsolutePath

def settings: Settings = settings(classPath)
def jsSettings: Settings = settings(jsClassPath)

def settings(classPath: Seq[String]): Settings = {
val s = new scala.tools.nsc.Settings
s.Xprint.value = List("all", "_")
s.deprecation.value = true
Expand All @@ -46,6 +55,11 @@ private[scoverage] object ScoverageCompiler {
new ScoverageCompiler(settings, reporter)
}

def defaultScalaJs: ScoverageCompiler = {
val reporter = new scala.tools.nsc.reporters.ConsoleReporter(jsSettings)
new ScoverageCompiler(jsSettings, reporter)
}

def locationCompiler: LocationCompiler = {
val reporter = new scala.tools.nsc.reporters.ConsoleReporter(settings)
new LocationCompiler(settings, reporter)
Expand All @@ -56,6 +70,23 @@ private[scoverage] object ScoverageCompiler {
scalaJars.map(findScalaJar)
}

private def getScalaJsJars: List[File] = {
List(
findJar("org.scala-lang", "scala-compiler", ScalaVersion),
findJar("org.scala-lang", "scala-reflect", ScalaVersion),
findJar(
"org.scala-js",
s"scalajs-compiler_$ScalaVersion",
BuildInfo.scalaJSVersion
),
findJar(
"org.scala-js",
s"scalajs-library_$ScalaVersion",
BuildInfo.scalaJSVersion
)
)
}

private def sbtCompileDir: File = {
val dir = new File(
s"./plugin/target/scala-$ScalaVersion/classes"
Expand All @@ -67,20 +98,28 @@ private[scoverage] object ScoverageCompiler {
dir
}

private def runtimeClasses: File = new File(
s"./runtime/jvm/target/scala-$ScalaVersion/classes"
private def runtimeClasses(platform: String): File = new File(
s"./runtime/$platform/target/scala-$ScalaVersion/classes"
)

private def findScalaJar(artifactId: String): File =
findIvyJar("org.scala-lang", artifactId, ScalaVersion)
.orElse(findCoursierJar(artifactId, ScalaVersion))
findJar("org.scala-lang", artifactId, ScalaVersion)

private def findJar(
groupId: String,
artifactId: String,
version: String
): File =
findIvyJar(groupId, artifactId, version)
.orElse(findCoursierJar(groupId, artifactId, version))
.getOrElse {
throw new FileNotFoundException(
s"Could not locate $artifactId/$ScalaVersion"
s"Could not locate $groupId:$artifactId:$version"
)
}

private def findCoursierJar(
groupId: String,
artifactId: String,
version: String
): Option[File] = {
Expand All @@ -89,9 +128,10 @@ private[scoverage] object ScoverageCompiler {
".cache/coursier", // Linux
"Library/Caches/Coursier", // MacOSX
"AppData/Local/Coursier/cache" // Windows
).map(loc =>
s"$userHome/$loc/v1/https/repo1.maven.org/maven2/org/scala-lang/$artifactId/$version/$artifactId-$version.jar"
)
).map { loc =>
val gid = groupId.replace('.', '/')
s"$userHome/$loc/v1/https/repo1.maven.org/maven2/$gid/$artifactId/$version/$artifactId-$version.jar"
}
jarPaths.map(new File(_)).find(_.exists())
}

Expand Down
2 changes: 2 additions & 0 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.4.6")

addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.9.33")

addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.11.0")

libraryDependencies += "org.scala-js" %% "scalajs-env-jsdom-nodejs" % "1.1.0"