From e048c6d07e56157eefc607838e50f2bfed970e40 Mon Sep 17 00:00:00 2001 From: Mark Harrah Date: Mon, 23 May 2011 21:06:33 -0400 Subject: [PATCH] fully configurable run task easier to create --- main/Defaults.scala | 24 ++++++++++++++++--- main/Structure.scala | 14 +++++++++++ sbt/src/sbt-test/actions/run-task/A.scala | 7 ++++++ .../sbt-test/actions/run-task/project/B.scala | 19 +++++++++++++++ sbt/src/sbt-test/actions/run-task/test | 5 ++++ 5 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 sbt/src/sbt-test/actions/run-task/A.scala create mode 100644 sbt/src/sbt-test/actions/run-task/project/B.scala create mode 100644 sbt/src/sbt-test/actions/run-task/test diff --git a/main/Defaults.scala b/main/Defaults.scala index d9523f60f..d2a0f6e19 100644 --- a/main/Defaults.scala +++ b/main/Defaults.scala @@ -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 { diff --git a/main/Structure.scala b/main/Structure.scala index eb400c1f0..c15379f6a 100644 --- a/main/Structure.scala +++ b/main/Structure.scala @@ -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) } \ No newline at end of file diff --git a/sbt/src/sbt-test/actions/run-task/A.scala b/sbt/src/sbt-test/actions/run-task/A.scala new file mode 100644 index 000000000..f12139e7b --- /dev/null +++ b/sbt/src/sbt-test/actions/run-task/A.scala @@ -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")) + } +} diff --git a/sbt/src/sbt-test/actions/run-task/project/B.scala b/sbt/src/sbt-test/actions/run-task/project/B.scala new file mode 100644 index 000000000..6799b2d01 --- /dev/null +++ b/sbt/src/sbt-test/actions/run-task/project/B.scala @@ -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") +} diff --git a/sbt/src/sbt-test/actions/run-task/test b/sbt/src/sbt-test/actions/run-task/test new file mode 100644 index 000000000..f73891519 --- /dev/null +++ b/sbt/src/sbt-test/actions/run-task/test @@ -0,0 +1,5 @@ +> demo +-> demo 1 +> demo-in 1 +-> demo-in 2 +> demo-in 1 2