fully configurable run task easier to create

This commit is contained in:
Mark Harrah 2011-05-23 21:06:33 -04:00
parent 43600cd3f2
commit e048c6d07e
5 changed files with 66 additions and 3 deletions

View File

@ -4,7 +4,7 @@
package sbt
import Build.data
import Scope.{GlobalScope, ThisScope}
import Scope.{fillTaskAxis, GlobalScope, ThisScope}
import compiler.Discovery
import Project.{inConfig, Initialize, inScope, inTask, ScopedKey, Setting, SettingsDefinition}
import Configurations.{Compile, CompilerPlugin, IntegrationTest, Runtime, Test}
@ -362,8 +362,8 @@ object Defaults extends BuildCommon
}
}
def runnerSetting =
runner <<= (scalaInstance, baseDirectory, javaOptions, outputStrategy, fork, javaHome, trapExit) { (si, base, options, strategy, forkRun, javaHomeDir, trap) =>
def runnerSetting = runner <<= runnerInit
def runnerInit: Initialize[ScalaRun] = (scalaInstance, baseDirectory, javaOptions, outputStrategy, fork, javaHome, trapExit) { (si, base, options, strategy, forkRun, javaHomeDir, trap) =>
if(forkRun) {
new ForkRun( ForkOptions(scalaJars = si.jars, javaHome = javaHomeDir, outputStrategy = strategy,
runJVMOptions = options, workingDirectory = Some(base)) )
@ -884,6 +884,24 @@ trait BuildExtra extends BuildCommon
(fullClasspath in config, runner in (config, run), streams) map { (cp, r, s) =>
toError(r.run(mainClass, data(cp), arguments, s.log))
}
def fullRunInputTask(scoped: ScopedInput[Unit], config: Configuration, mainClass: String, baseArguments: String*): Setting[InputTask[Unit]] =
scoped <<= inputTask { result =>
( inScoped(scoped.scoped, runnerInit) zipWith (fullClasspath in config, streams, result).identityMap) { (r, t) =>
t map { case (cp, s, args) =>
toError(r.run(mainClass, data(cp), baseArguments ++ args, s.log))
}
}
}
def fullRunTask(scoped: ScopedTask[Unit], config: Configuration, mainClass: String, arguments: String*): Setting[Task[Unit]] =
scoped <<= ( inScoped(scoped.scoped, runnerInit) zipWith (fullClasspath in config, streams).identityMap ) { case (r, t) =>
t map { case (cp, s) =>
toError(r.run(mainClass, data(cp), arguments, s.log))
}
}
private[this] def inScoped[T](sk: ScopedKey[_], i: Initialize[T]): Initialize[T] = inScope(fillTaskAxis(sk.scope, sk.key), i)
private[this] def inScope[T](s: Scope, i: Initialize[T]): Initialize[T] = i mapReferenced Project.mapScope(Scope.replaceThis(s))
}
trait BuildCommon
{

View File

@ -522,12 +522,20 @@ object Scoped
def k7[M[_], A, B, C, D, E, F, G](t7: (M[A], M[B], M[C], M[D], M[E], M[F], M[G])) = t7._1 :^: t7._2 :^: t7._3 :^: t7._4 :^: t7._5 :^: t7._6 :^: t7._7 :^: KNil
def k8[M[_], A, B, C, D, E, F, G, H](t8: (M[A], M[B], M[C], M[D], M[E], M[F], M[G], M[H])) = t8._1 :^: t8._2 :^: t8._3 :^: t8._4 :^: t8._5 :^: t8._6 :^: t8._7 :^: t8._8 :^: KNil
def k9[M[_], A, B, C, D, E, F, G, H, I](t9: (M[A], M[B], M[C], M[D], M[E], M[F], M[G], M[H], M[I])) = t9._1 :^: t9._2 :^: t9._3 :^: t9._4 :^: t9._5 :^: t9._6 :^: t9._7 :^: t9._8 :^: t9._9 :^: KNil
private[sbt] def extendScoped(s1: Scoped, ss: Seq[Scoped]): Seq[AttributeKey[_]] = s1.key +: ss.map(_.key)
}
import Scoped.extendScoped
object InputKey
{
def apply[T](label: String, description: String = ""): InputKey[T] =
apply( AttributeKey[InputTask[T]](label, description) )
def apply[T](label: String, description: String, extend1: Scoped, extendN: Scoped*): InputKey[T] =
apply( AttributeKey[InputTask[T]](label, description, extendScoped(extend1, extendN)) )
def apply[T](akey: AttributeKey[InputTask[T]]): InputKey[T] =
new InputKey[T](akey)
}
@ -536,6 +544,9 @@ object TaskKey
def apply[T](label: String, description: String = ""): TaskKey[T] =
apply( AttributeKey[Task[T]](label, description) )
def apply[T](label: String, description: String, extend1: Scoped, extendN: Scoped*): TaskKey[T] =
apply( AttributeKey[Task[T]](label, description, extendScoped(extend1, extendN)) )
def apply[T](akey: AttributeKey[Task[T]]): TaskKey[T] =
new TaskKey[T](akey)
}
@ -544,6 +555,9 @@ object SettingKey
def apply[T](label: String, description: String = ""): SettingKey[T] =
apply( AttributeKey[T](label, description) )
def apply[T](label: String, description: String, extend1: Scoped, extendN: Scoped*): SettingKey[T] =
apply( AttributeKey[T](label, description, extendScoped(extend1, extendN)) )
def apply[T](akey: AttributeKey[T]): SettingKey[T] =
new SettingKey[T](akey)
}

View File

@ -0,0 +1,7 @@
object A {
def main(args: Array[String]) =
{
assert(args(0).toInt == args(1).toInt)
assert(java.lang.Boolean.getBoolean("sbt.check.forked"))
}
}

View File

@ -0,0 +1,19 @@
import sbt._
import Keys._
object B extends Build
{
lazy val projects = Seq(root)
lazy val root = Project("root", file(".")) settings(
myRun,
fork in demo := true,
javaOptions in demo := "-Dsbt.check.forked=true" :: Nil,
myIn
)
lazy val demoIn = InputKey[Unit]("demo-in", "Demo run input task", demo)
lazy val demo = TaskKey[Unit]("demo", "Demo run task")
def myRun = fullRunTask( demo, Compile, "A", "1", "1")
def myIn = fullRunInputTask( demoIn, Compile, "A", "1")
}

View File

@ -0,0 +1,5 @@
> demo
-> demo 1
> demo-in 1
-> demo-in 2
> demo-in 1 2