Skip to content

Commit

Permalink
Reduce InputTask to the ideal wrapper around 'State => Parser[Initial…
Browse files Browse the repository at this point in the history
…ize[Task[T]]]'

Ref #407.
  • Loading branch information
harrah committed Jan 28, 2013
1 parent 1f32688 commit 076480b
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 127 deletions.
141 changes: 72 additions & 69 deletions main/settings/src/main/scala/sbt/InputTask.scala
Expand Up @@ -7,112 +7,115 @@ package sbt
import Types._

/** Parses input and produces a task to run. Constructed using the companion object. */
sealed trait InputTask[T]
{ outer =>
private[sbt] type Result
private[sbt] def parser: State => Parser[Result]
private[sbt] def defined: AttributeKey[Result]
private[sbt] def task: Task[T]

def mapTask[S](f: Task[T] => Task[S]) = new InputTask[S] {
type Result = outer.Result
def parser = outer.parser
def task = f(outer.task)
def defined = outer.defined
}
final class InputTask[T] private(val parser: State => Parser[Task[T]])
{
def mapTask[S](f: Task[T] => Task[S]): InputTask[S] =
new InputTask[S](s => parser(s) map f)
}

object InputTask
{
@deprecated("Use `create` or the `Def.inputTask` macro.", "0.13.0")
def make[T](p: State => Parser[Task[T]]): InputTask[T] = new InputTask[T](p)

def static[T](p: Parser[Task[T]]): InputTask[T] = free(_ => p)

@deprecated("Use `create` or the `Def.inputTask` macro.", "0.13.0")
def static[I,T](p: Parser[I])(c: I => Task[T]): InputTask[T] = static(p map c)

@deprecated("Use `create` or the `Def.inputTask` macro.", "0.13.0")
def free[T](p: State => Parser[Task[T]]): InputTask[T] = {
val key = localKey[Task[T]]
new InputTask[T] {
type Result = Task[T]
def parser = p
def defined = key
def task = getResult(key)
}
}
def free[T](p: State => Parser[Task[T]]): InputTask[T] = make(p)

@deprecated("Use `create` or the `Def.inputTask` macro.", "0.13.0")
def free[I,T](p: State => Parser[I])(c: I => Task[T]): InputTask[T] = free(s => p(s) map c)

@deprecated("Use `create` or the `Def.inputTask` macro.", "0.13.0")
def separate[I,T](p: State => Parser[I])(action: Initialize[I => Task[T]]): Initialize[InputTask[T]] =
separate(Def value p)(action)

@deprecated("Use `create` or the `Def.inputTask` macro.", "0.13.0")
def separate[I,T](p: Initialize[State => Parser[I]])(action: Initialize[I => Task[T]]): Initialize[InputTask[T]] =
p.zipWith(action)((parser, act) => free(parser)(act))

@deprecated("Use `create` or the `Def.inputTask` macro.", "0.13.0")
/** Constructs an InputTask that accepts no user input. */
def createFree[T](action: Initialize[Task[T]]): Initialize[InputTask[T]] =
action { tsk => free(emptyParser)( const(tsk) ) }

/** Constructs an InputTask from:
* a) a Parser constructed using other Settings, but not Tasks
* b) an action constructed using Settings, Tasks, and the result of parsing */
def create[I,T](p: Initialize[State => Parser[I]])(action: Initialize[Task[I => T]]): Initialize[InputTask[T]] =
{
val act = action { (tf: Task[I => T]) => (i: I) => tf.map(f => f(i)) }
separate(p)(act)
}

/** Constructs an InputTask from:
* a) a Parser constructed using other Settings, but not Tasks
* b) a dynamically constructed Task that uses Settings, Tasks, and the result of parsing. */
def createDyn[I,T](p: Initialize[State => Parser[I]])(action: Initialize[Task[I => Initialize[Task[T]]]]): Initialize[InputTask[T]] =
separate(p)(std.FullInstance.flattenFun[I,T](action))

/** A dummy parser that consumes no input and produces nothing useful (unit).*/
def emptyParser: State => Parser[Unit] = Types.const(complete.DefaultParsers.success(()))

/** Implementation detail that is public because it is used by a macro.*/
def parserAsInput[T](p: Parser[T]): Initialize[State => Parser[T]] = Def.valueStrict(Types.const(p))

/** Implementation detail that is public because it is used y a macro.*/
def initParserAsInput[T](i: Initialize[Parser[T]]): Initialize[State => Parser[T]] = i(Types.const)



@deprecated("Use another InputTask constructor or the `Def.inputTask` macro.", "0.13.0")
def apply[I,T](p: Initialize[State => Parser[I]])(action: TaskKey[I] => Initialize[Task[T]]): Initialize[InputTask[T]] =
{
create(p){ it =>
val dummy = TaskKey(localKey[Task[I]])
action(dummy) mapConstant subResultForDummy(dummy)
}
val dummyKey = localKey[Task[I]]
val (marker, dummy) = dummyTask[I]
val it = action(TaskKey(dummyKey)) mapConstant subResultForDummy(dummyKey, dummy)
val act = it { tsk => (value: I) => subForDummy(marker, value, tsk) }
separate(p)(act)
}

@deprecated("Use `create` or the `Def.inputTask` macro.", "0.13.0")
@deprecated("Use another InputTask constructor or the `Def.inputTask` macro.", "0.13.0")
def apply[I,T](p: State => Parser[I])(action: TaskKey[I] => Initialize[Task[T]]): Initialize[InputTask[T]] =
apply(Def.value(p))(action)

// dummy task that will get replaced with the actual mappings from InputTasks to parse results
private[sbt] lazy val inputMap: Task[AttributeMap] = mktask { error("Internal sbt error: input map not substituted.") }
private[this] def getResult[T](key: AttributeKey[Task[T]]): Task[T] = inputMap flatMap { im =>
im get key getOrElse error("Internal sbt error: could not get parser result.")
}
/** The proper solution is to have a Manifest context bound and accept slight source incompatibility,
* The affected InputTask construction methods are all deprecated and so it is better to keep complete
* compatibility. Because the AttributeKey is local, it uses object equality and the manifest is not used. */
private[this] def localKey[T]: AttributeKey[T] = AttributeKey.local[Unit].asInstanceOf[AttributeKey[T]]

private[this] def subResultForDummy[I](dummy: TaskKey[I]) =
private[this] def subResultForDummy[I](dummyKey: AttributeKey[Task[I]], dummyTask: Task[I]) =
new (ScopedKey ~> Option) { def apply[T](sk: ScopedKey[T]) =
if(sk.key eq dummy.key) {
if(sk.key eq dummyKey) {
// sk.key: AttributeKey[T], dummy.key: AttributeKey[Task[I]]
// (sk.key eq dummy.key) ==> T == Task[I] because AttributeKey is invariant
Some(getResult(dummy.key).asInstanceOf[T])
Some(dummyTask.asInstanceOf[T])
} else
None
}

// This interface allows the Parser to be constructed using other Settings, but not Tasks (which is desired).
// The action can be constructed using Settings and Tasks and with the parse result injected into a Task.
// This requires Aggregation.applyDynamicTasks to inject an AttributeMap with the parse results.
def create[I,T](p: Initialize[State => Parser[I]])(action: Initialize[Task[I]] => Initialize[Task[T]]): Initialize[InputTask[T]] =
private[this] def dummyTask[I]: (AttributeKey[Option[I]], Task[I]) =
{
val key = localKey[I] // TODO: AttributeKey.local[I]
val result: Initialize[Task[I]] = (Def.resolvedScoped zipWith Def.valueStrict(InputTask.inputMap)){(scoped, imTask) =>
imTask map { im => im get key getOrElse error("No parsed value for " + Def.displayFull(scoped) + "\n" + im) }
}

(p zipWith action(result)) { (parserF, act) =>
new InputTask[T]
{
type Result = I
def parser = parserF
def task = act
def defined = key
}
}
val key = localKey[Option[I]]
val f: () => I = () => error(s"Internal sbt error: InputTask stub was not substituted properly.")
val t: Task[I] = Task(Info[I]().set(key, None), Pure(f, false))
(key, t)
}
private[this] def subForDummy[I, T](marker: AttributeKey[Option[I]], value: I, task: Task[T]): Task[T] =
{
val seen = new java.util.IdentityHashMap[Task[_], Task[_]]
lazy val f: Task ~> Task = new (Task ~> Task) { def apply[T](t: Task[T]): Task[T] =
{
val t0 = seen.get(t)
if(t0 == null) {
val newAction =
if(t.info.get(marker).isDefined)
Pure(() => value.asInstanceOf[T], inline = true)
else
t.work.mapTask(f)
val newTask = Task(t.info, newAction)
seen.put(t, newTask)
newTask
} else
t0.asInstanceOf[Task[T]]
}}
f(task)
}

/** A dummy parser that consumes no input and produces nothing useful (unit).*/
def emptyParser: Initialize[State => Parser[Unit]] = parserAsInput(complete.DefaultParsers.success(()))

/** Implementation detail that is public because it is used by a macro.*/
def parserAsInput[T](p: Parser[T]): Initialize[State => Parser[T]] = Def.valueStrict(Types.const(p))

/** Implementation detail that is public because it is used y a macro.*/
def initParserAsInput[T](i: Initialize[Parser[T]]): Initialize[State => Parser[T]] = i(Types.const)
}

61 changes: 37 additions & 24 deletions main/settings/src/main/scala/sbt/std/TaskMacro.scala
Expand Up @@ -17,7 +17,7 @@ object TaskInstance extends MonadInstance
import TaskExtra._

final type M[x] = Task[x]
def app[K[L[x]], Z](in: K[Task], f: K[Id] => Z)(implicit a: AList[K]): Task[Z] = new Mapped[Z,K](in, f compose allM, a)
def app[K[L[x]], Z](in: K[Task], f: K[Id] => Z)(implicit a: AList[K]): Task[Z] = Task(Info(), new Mapped[Z,K](in, f compose allM, a))
def map[S,T](in: Task[S], f: S => T): Task[T] = in map f
def flatten[T](in: Task[Task[T]]): Task[T] = in flatMap idFun[Task[T]]
def pure[T](t: () => T): Task[T] = toTask(t)
Expand All @@ -38,6 +38,15 @@ object FullInstance extends Instance.Composed[Initialize, Task](InitializeInstan
(a, data) flatMap { case (a,d) => f(a) evaluate d }
}
}
def flattenFun[S,T](in: Initialize[Task[ S => Initialize[Task[T]] ]]): Initialize[S => Task[T]] =
{
import Scoped._
(in,settingsData, Def.capturedTransformations) apply{
(a: Task[S => Initialize[Task[T]]], data: Task[SS], f) => (s: S) =>
import TaskExtra.multT2Task
(a, data) flatMap { case (af,d) => f(af(s)) evaluate d }
}
}
}
/** Converts an input `Tree` of type `Initialize[T]`, `Initialize[Task[T]]`, or `Task[T]` into a `Tree` of type `Initialize[Task[T]]`.*/
object FullConvert extends Convert
Expand Down Expand Up @@ -69,6 +78,8 @@ object TaskMacro
final val AppendNInitName = "appendN"
final val TransformInitName = "transform"
final val InputTaskCreateName = "create"
final val InputTaskCreateDynName = "createDyn"
final val InputTaskCreateFreeName = "createFree"

def taskMacroImpl[T: c.WeakTypeTag](c: Context)(t: c.Expr[T]): c.Expr[Initialize[Task[T]]] =
Instance.contImpl[T](c, FullInstance, FullConvert, MixedBuilder)(Left(t))
Expand Down Expand Up @@ -272,9 +283,6 @@ object TaskMacro
val ttree = t match { case Left(l) => l.tree; case Right(r) => r.tree }
val defs = util.collectDefs(ttree, isAnyWrapper)
val checkQual = util.checkReferences(defs, isAnyWrapper)
val unitInitTask = c.typeOf[Initialize[Task[Unit]]]
val initKeyC = c.typeOf[Initialize[Unit]].typeConstructor
val taskKeyC = c.typeOf[Task[Unit]].typeConstructor

var result: Option[(Tree, Type, ValDef)] = None

Expand All @@ -287,42 +295,47 @@ object TaskMacro
else
{
qual.foreach(checkQual)
val itType = appliedType(initKeyC, appliedType(taskKeyC, tpe :: Nil) :: Nil) // Initialize[Task[<tpe>]]
val vd = util.freshValDef(itType, qual.symbol) // val $x: Initialize[Task[<tpe>]]
val vd = util.freshValDef(tpe, qual.symbol) // val $x: <tpe>
result = Some( (qual, tpe, vd) )
val tree = util.refVal(vd) // $x
tree.setPos(qual.pos) // position needs to be set so that wrapKey passes the position onto the wrapper
assert(tree.tpe != null, "Null type: " + tree)
val wrapped = InputWrapper.wrapKey(c)( c.Expr[Any](tree) )( c.WeakTypeTag(tpe) )
wrapped.tree.setType(tpe)
tree.setType(tpe)
tree
}
// Tree for InputTask.create[<tpeA>, <tpeB>](arg1)(arg2)
def inputTaskCreate(tpeA: Type, tpeB: Type, arg1: Tree, arg2: Tree) =
// Tree for InputTask.<name>[<tpeA>, <tpeB>](arg1)(arg2)
def inputTaskCreate(name: String, tpeA: Type, tpeB: Type, arg1: Tree, arg2: Tree) =
{
val typedApp = TypeApply(Select(it, InputTaskCreateName), TypeTree(tpeA) :: TypeTree(tpeB) :: Nil)
val typedApp = TypeApply(Select(it, name), TypeTree(tpeA) :: TypeTree(tpeB) :: Nil)
val app = ApplyTree( ApplyTree(typedApp, arg1 :: Nil), arg2 :: Nil)
c.Expr[Initialize[InputTask[T]]](app)
}
def expandTask(dyn: Boolean, tx: Tree): c.Expr[Initialize[Task[T]]] =
// Tree for InputTask.createFree[<tpe>](arg1)
def inputTaskCreateFree(tpe: Type, arg: Tree) =
{
val typedApp = TypeApply(Select(it, InputTaskCreateFreeName), TypeTree(tpe) :: Nil)
val app = ApplyTree(typedApp, arg :: Nil)
c.Expr[Initialize[InputTask[T]]](app)
}
def expandTask[I: WeakTypeTag](dyn: Boolean, tx: Tree): c.Expr[Initialize[Task[I]]] =
if(dyn)
taskDynMacroImpl[T](c)( c.Expr[Initialize[Task[T]]](tx) )
taskDynMacroImpl[I](c)( c.Expr[Initialize[Task[I]]](tx) )
else
taskMacroImpl[T](c)( c.Expr[T](tx) )
taskMacroImpl[I](c)( c.Expr[I](tx) )
def wrapTag[I: WeakTypeTag]: WeakTypeTag[Initialize[Task[I]]] = weakTypeTag

val tx = util.transformWrappers(ttree, isParserWrapper, (tpe,tree) => subWrapper(tpe,tree))
val body = c.resetLocalAttrs( expandTask(t.isRight, tx).tree )
result match {
case Some((p, tpe, param)) =>
val f = Function(param :: Nil, body)
inputTaskCreate(tpe, tag.tpe, p, f)
val fCore = Function(param :: Nil, tx)
val bodyTpe = if(t.isRight) wrapTag(tag).tpe else tag.tpe
val fTpe = util.functionType(tpe :: Nil, bodyTpe)
val fTag = c.WeakTypeTag[Any](fTpe) // don't know the actual type yet, so use Any
val fInit = c.resetLocalAttrs( expandTask(false, fCore)(fTag).tree )
inputTaskCreate(if(t.isRight) InputTaskCreateDynName else InputTaskCreateName, tpe, tag.tpe, p, fInit)
case None =>
// SI-6591 prevents the more direct version using reify:
// reify { InputTask[Unit,T].create(TaskMacro.emptyParser)(Types.const(body.splice)) }
val initType = c.weakTypeOf[Initialize[Task[T]]]
val tt = Ident(util.singleton(Types))
val f = ApplyTree(TypeApply(Select(tt, "const"), TypeTree(unitInitTask) :: TypeTree(initType) :: Nil), body :: Nil)
val p = reify { InputTask.emptyParser }
inputTaskCreate(c.typeOf[Unit], tag.tpe, p.tree, f)
val init = c.resetLocalAttrs( expandTask[T](t.isRight, tx).tree )
inputTaskCreateFree(tag.tpe, init)
}
}
}
Expand Down
14 changes: 3 additions & 11 deletions main/src/main/scala/sbt/Aggregation.scala
Expand Up @@ -95,18 +95,10 @@ final object Aggregation

def applyDynamicTasks[I](s: State, structure: BuildStructure, inputs: Values[InputTask[I]], show: Boolean)(implicit display: Show[ScopedKey[_]]): Parser[() => State] =
{
final class Parsed[T](val input: InputTask[_] { type Result = T }, val value: T) {
def addTo(im: AttributeMap): AttributeMap = im.put(input.defined, value)
}
val parsers: Seq[Parser[Parsed[_]]] = for( KeyValue(k,t) <- inputs ) yield
t.parser(s).map( v => new Parsed[t.Result](t, v) )

Command.applyEffect(seq(parsers)) { parseds =>
val parsers = for(KeyValue(k,it) <- inputs) yield it.parser(s).map(v => KeyValue(k,v))
Command.applyEffect(seq(parsers)) { roots =>
import EvaluateTask._
val inputMap = (AttributeMap.empty /: parseds) { (im, p) => p.addTo(im) }
val dummies = DummyTaskMap(new TaskAndValue(InputTask.inputMap, inputMap) :: Nil)
val roots = maps(inputs)(_.task)
runTasks(s, structure, roots, dummies, show)
runTasks(s, structure, roots, DummyTaskMap(Nil), show)
}
}

Expand Down
2 changes: 1 addition & 1 deletion scripted/sbt/src/main/scala/sbt/test/ScriptedTests.scala
Expand Up @@ -119,7 +119,7 @@ object ScriptedTests
}
def runAll(tests: Seq[() => Option[String]])
{
val errors = for(test <- tests; err <- test()) yield err
val errors = for(test <- tests.par; err <- test()) yield err
if(errors.nonEmpty)
error(errors.mkString("Failed tests:\n\t", "\n\t", "\n"))
}
Expand Down
29 changes: 22 additions & 7 deletions tasks/standard/src/main/scala/sbt/Action.scala
Expand Up @@ -11,32 +11,47 @@ package sbt
// Various natural transformations used, such as PMap, require invariant type constructors for correctness

/** Defines a task compuation*/
sealed trait Action[T]
sealed trait Action[T] {
// TODO: remove after deprecated InputTask constructors are removed
private[sbt] def mapTask(f: Task ~> Task): Action[T]
}


/** A direct computation of a value.
* If `inline` is true, `f` will be evaluated on the scheduler thread without the overhead of normal scheduling when possible.
* This is intended as an optimization for already evaluated values or very short computations. */
final case class Pure[T](f: () => T, inline: Boolean) extends Action[T]
* This is intended as an optimization for already evaluated values or very short pure computations. */
final case class Pure[T](f: () => T, inline: Boolean) extends Action[T] {
private[sbt] def mapTask(f: Task ~> Task) = this
}

/** Applies a function to the result of evaluating a heterogeneous list of other tasks.*/
final case class Mapped[T, K[L[x]]](in: K[Task], f: K[Result] => T, alist: AList[K]) extends Action[T]
final case class Mapped[T, K[L[x]]](in: K[Task], f: K[Result] => T, alist: AList[K]) extends Action[T] {
private[sbt] def mapTask(g: Task ~> Task) = Mapped[T,K](alist.transform(in, g), f, alist)
}

/** Computes another task to evaluate based on results from evaluating other tasks.*/
final case class FlatMapped[T, K[L[x]]](in: K[Task], f: K[Result] => Task[T], alist: AList[K]) extends Action[T]
final case class FlatMapped[T, K[L[x]]](in: K[Task], f: K[Result] => Task[T], alist: AList[K]) extends Action[T] {
private[sbt] def mapTask(g: Task ~> Task) = FlatMapped[T,K](alist.transform(in, g), g.fn[T] compose f, alist)
}

/** A computation `in` that requires other tasks `deps` to be evaluated first.*/
final case class DependsOn[T](in: Task[T], deps: Seq[Task[_]]) extends Action[T]
final case class DependsOn[T](in: Task[T], deps: Seq[Task[_]]) extends Action[T] {
private[sbt] def mapTask(g: Task ~> Task) = DependsOn[T](g(in), deps.map(t => g(t)))
}

/** A computation that operates on the results of a homogeneous list of other tasks.
* It can either return another task to be evaluated or the final value.*/
final case class Join[T, U](in: Seq[Task[U]], f: Seq[Result[U]] => Either[Task[T], T]) extends Action[T]
final case class Join[T, U](in: Seq[Task[U]], f: Seq[Result[U]] => Either[Task[T], T]) extends Action[T] {
private[sbt] def mapTask(g: Task ~> Task) = Join[T,U](in.map(g.fn[U]), sr => f(sr).left.map(g.fn[T]))
}

/** Combines metadata `info` and a computation `work` to define a task. */
final case class Task[T](info: Info[T], work: Action[T])
{
override def toString = info.name getOrElse ("Task(" + info + ")")
override def hashCode = info.hashCode

private[sbt] def mapTask(g: Task ~> Task): Task[T] = g(Task(info, work.mapTask(g)))
def tag(tags: Tag*): Task[T] = tagw(tags.map(t => (t, 1)) : _*)
def tagw(tags: (Tag, Int)*): Task[T] = copy(info = info.set(tagsKey, info.get(tagsKey).getOrElse(Map.empty) ++ tags ))
def tags: TagMap = info get tagsKey getOrElse Map.empty
Expand Down

0 comments on commit 076480b

Please sign in to comment.