Files
sbt/scripted/plugin/ScriptedPlugin.scala
T

91 lines
4.6 KiB
Scala
Raw Normal View History

2011-04-26 14:22:33 +03:00
/* sbt -- Simple Build Tool
* Copyright 2011 Artyom Olshevskiy
*/
2011-10-16 17:27:36 -04:00
package sbt
2011-04-25 15:27:32 +03:00
import Project.Initialize
import Keys._
import classpath.ClasspathUtilities
import java.lang.reflect.{InvocationTargetException,Method}
2011-04-25 15:27:32 +03:00
import java.util.Properties
object ScriptedPlugin extends Plugin {
def scriptedConf = config("scripted-sbt") hide
private[this] def scriptedLaunchConf = config("scripted-sbt-launch") hide
2011-04-25 15:27:32 +03:00
val scriptedSbt = SettingKey[String]("scripted-sbt")
val sbtLauncher = SettingKey[File]("sbt-launcher")
// need to get the launcher from 'update', which requires a Task, but cannot break compatibility in 0.12
private[this] val sbtLauncherTask = TaskKey[File]("sbt-launcher-task")
2011-04-25 15:27:32 +03:00
val sbtTestDirectory = SettingKey[File]("sbt-test-directory")
val scriptedBufferLog = SettingKey[Boolean]("scripted-buffer-log")
final case class ScriptedScalas(build: String, versions: String)
val scriptedScalas = SettingKey[ScriptedScalas]("scripted-scalas")
val scriptedClasspath = TaskKey[PathFinder]("scripted-classpath")
val scriptedTests = TaskKey[AnyRef]("scripted-tests")
val scriptedRun = TaskKey[Method]("scripted-run")
val scriptedLaunchOpts = SettingKey[Seq[String]]("scripted-launch-opts", "options to pass to jvm launching scripted tasks")
2011-04-25 15:27:32 +03:00
val scriptedDependencies = TaskKey[Unit]("scripted-dependencies")
val scripted = InputKey[Unit]("scripted")
def scriptedTestsTask: Initialize[Task[AnyRef]] = (scriptedClasspath, scalaInstance) map {
(classpath, scala) =>
val parent = new sbt.classpath.FilteredLoader(scala.loader, "jline." :: Nil)
val loader = ClasspathUtilities.toLoader(classpath, parent)
2011-04-25 15:27:32 +03:00
ModuleUtilities.getObject("sbt.test.ScriptedTests", loader)
}
def scriptedRunTask: Initialize[Task[Method]] = (scriptedTests) map {
(m) => try get012Method(m) catch { case e12: NoSuchMethodException =>
try get013Method(m) catch { case _: NoSuchMethodException => throw e12 }
}
2011-04-25 15:27:32 +03:00
}
private[this] def get012Method(m: AnyRef): Method =
m.getClass.getMethod("run", classOf[File], classOf[Boolean], classOf[String], classOf[String], classOf[String], classOf[Array[String]], classOf[File], classOf[Array[String]])
private[this] def get013Method(m: AnyRef): Method =
m.getClass.getMethod("run", classOf[File], classOf[Boolean], classOf[Array[String]], classOf[File], classOf[Array[String]])
2011-04-25 15:27:32 +03:00
def scriptedTask: Initialize[InputTask[Unit]] = InputTask(_ => complete.Parsers.spaceDelimited("<arg>")) { result =>
(scriptedDependencies, scriptedTests, scriptedRun, sbtTestDirectory, scriptedBufferLog, scriptedSbt, scriptedScalas, sbtLauncherTask, scriptedLaunchOpts, result) map {
(deps, m, r, testdir, bufferlog, version, scriptedScalas, launcher, launchOpts, args) =>
def invoke12 = r.invoke(m, testdir, bufferlog: java.lang.Boolean, version.toString, scriptedScalas.build, scriptedScalas.versions, args.toArray, launcher, launchOpts.toArray)
def invoke13 = r.invoke(m, testdir, bufferlog: java.lang.Boolean, args.toArray, launcher, launchOpts.toArray)
try invoke12 catch {
case e: InvocationTargetException => throw e.getCause
case i: IllegalArgumentException =>
try invoke13 catch { case e: InvocationTargetException => throw e.getCause }
}
2011-04-25 15:27:32 +03:00
}
}
val scriptedSettings = Seq(
ivyConfigurations ++= Seq(scriptedConf, scriptedLaunchConf),
scriptedSbt <<= sbtVersion,
scriptedScalas <<= (scalaVersion) { (scala) => ScriptedScalas(scala, scala) },
libraryDependencies <<= (libraryDependencies, scriptedSbt) {(deps, version) =>
deps ++ Seq(
"org.scala-sbt" % "scripted-sbt" % version % scriptedConf.toString,
"org.scala-sbt" % "sbt-launch" % version % scriptedLaunchConf.toString from launcherURL(version)
)
},
scriptedClasspath <<= getJars(scriptedConf),
2011-04-25 15:27:32 +03:00
sbtLauncher <<= (appConfiguration)(app => IO.classLocationFile(app.provider.scalaProvider.launcher.getClass)),
sbtLauncherTask <<= getJars(scriptedLaunchConf).map(_.get.head),
2011-04-25 15:27:32 +03:00
sbtTestDirectory <<= sourceDirectory / "sbt-test",
scriptedBufferLog := true,
2011-05-14 18:21:41 -04:00
scriptedClasspath <<= (classpathTypes, update) map { (ct, report) => PathFinder(Classpaths.managedJars(scriptedConf, ct, report).map(_.data)) },
2011-04-25 15:27:32 +03:00
scriptedTests <<= scriptedTestsTask,
scriptedRun <<= scriptedRunTask,
scriptedDependencies <<= (compile in Test, publishLocal) map { (analysis, pub) => Unit },
scriptedLaunchOpts := Seq(),
2011-04-25 15:27:32 +03:00
scripted <<= scriptedTask
)
private[this] def getJars(config: Configuration): Initialize[Task[PathFinder]] =
(classpathTypes, update) map { (ct, report) => PathFinder(Classpaths.managedJars(config, ct, report).map(_.data)) }
private[this] def launcherURL(v: String): String =
"http://repo.typesafe.com/typesafe/ivy-releases/org.scala-sbt/sbt-launch/" + v + "/sbt-launch.jar"
2011-04-25 15:27:32 +03:00
}