enable org.scalajs.sbtplugin.ScalaJSPlugin

This commit is contained in:
Eugene Yokota 2019-05-09 10:03:29 -04:00
parent 7e0a1249e3
commit ee82dd9243
4 changed files with 59 additions and 5 deletions

View File

@ -4,6 +4,7 @@ import java.util.Locale
import scala.collection.immutable.ListMap
import Keys._
import sbt.librarymanagement.CrossVersion.partialVersion
import scala.util.Try
/**
* A project matrix is an implementation of a composite project
@ -210,8 +211,23 @@ object ProjectMatrix {
jsPlatform(scalaVersions, Nil)
override def jsPlatform(scalaVersions: Seq[String], settings: Seq[Setting[_]]): ProjectMatrix =
custom(jsIdSuffix, jsDirectorySuffix, scalaVersions, { _.settings(settings) })
custom(jsIdSuffix, jsDirectorySuffix, scalaVersions,
{ _
.enablePlugins(scalajsPlugin(this.getClass.getClassLoader).getOrElse(
sys.error("""Scala.js plugin was not found. Add the sbt-scalajs plugin into project/plugins.sbt:
| addSbtPlugin("org.scala-js" % "sbt-scalajs" % "x.y.z")
|""".stripMargin)
))
.settings(settings)
})
def scalajsPlugin(classLoader: ClassLoader): Try[AutoPlugin] = {
import sbtprojectmatrix.ReflectionUtil._
withContextClassloader(classLoader) { loader =>
getSingletonObject[AutoPlugin](loader, "org.scalajs.sbtplugin.ScalaJSPlugin$")
}
}
override def js: ProjectFinder = new SuffixBaseProjectFinder(jsIdSuffix)
override def crossLibrary(scalaVersions: Seq[String], suffix: String, settings: Seq[Setting[_]]): ProjectMatrix =

View File

@ -0,0 +1,36 @@
package sbtprojectmatrix
import java.lang.reflect.InvocationTargetException
import scala.reflect.ClassTag
import scala.util.Try
object ReflectionUtil {
def getSingletonObject[A: ClassTag](classLoader: ClassLoader, className: String): Try[A] =
Try {
val clazz = classLoader.loadClass(className)
val t = implicitly[ClassTag[A]].runtimeClass
Option(clazz.getField("MODULE$").get(null)) match {
case None => throw new ClassNotFoundException(s"Unable to find $className using classloader: $classLoader")
case Some(c) if !t.isInstance(c) => throw new ClassCastException(s"${clazz.getName} is not a subtype of $t")
case Some(c: A) => c
}
}
.recover {
case i: InvocationTargetException if i.getTargetException != null => throw i.getTargetException
}
def objectExists(classLoader: ClassLoader, className: String): Boolean =
try {
classLoader.loadClass(className).getField("MODULE$").get(null) != null
} catch {
case _: Throwable => false
}
def withContextClassloader[A](loader: ClassLoader)(body: ClassLoader => A): A = {
val current = Thread.currentThread().getContextClassLoader
try {
Thread.currentThread().setContextClassLoader(loader)
body(loader)
} finally Thread.currentThread().setContextClassLoader(current)
}
}

View File

@ -3,3 +3,4 @@ sys.props.get("plugin.version") match {
case _ => sys.error("""|The system property 'plugin.version' is not defined.
|Specify this property using the scriptedLaunchOpts -D.""".stripMargin)
}
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "0.6.27")

View File

@ -1,4 +1,5 @@
> compile
> fastOptJS
$ exists core/target/js-2.12/classes/a/Core.class
$ exists core/target/js-2.11/classes/a/Core.class
$ exists app/target/js-2.12/app-fastopt.js
$ exists core/target/js-2.12/core-fastopt.js
$ exists core/target/js-2.11/core-fastopt.js