sbt/main/State.scala

74 lines
2.2 KiB
Scala
Raw Normal View History

/* sbt -- Simple Build Tool
* Copyright 2008, 2009, 2010 Mark Harrah
*/
package sbt
2010-09-05 17:16:53 +02:00
import java.io.File
import CommandSupport.FailureWall
2010-07-28 05:01:45 +02:00
final case class State(
configuration: xsbti.AppConfiguration,
processors: Seq[Command],
exitHooks: Set[ExitHook],
onFailure: Option[String],
commands: Seq[String],
attributes: AttributeMap,
next: Next.Value
) extends Identity
trait Identity {
override final def hashCode = super.hashCode
override final def equals(a: Any) = super.equals(a)
override final def toString = super.toString
}
trait StateOps {
def process(f: (String, State) => State): State
def ::: (commands: Seq[String]): State
def :: (command: String): State
def continue: State
def reload: State
def exit(ok: Boolean): State
def fail: State
2010-07-28 05:01:45 +02:00
def ++ (newCommands: Seq[Command]): State
def + (newCommand: Command): State
2010-09-05 16:56:24 +02:00
def get[T](key: AttributeKey[T]): Option[T]
def put[T](key: AttributeKey[T], value: T): State
2010-07-28 05:01:45 +02:00
def baseDir: File
}
object State
{
implicit def stateOps(s: State): StateOps = new StateOps {
def process(f: (String, State) => State): State =
s.commands match {
case Seq(x, xs @ _*) => f(x, s.copy(commands = xs))
2010-07-28 05:01:45 +02:00
case Seq() => exit(true)
}
s.copy(commands = s.commands.drop(1))
def ::: (newCommands: Seq[String]): State = s.copy(commands = newCommands ++ s.commands)
2010-07-28 05:01:45 +02:00
def :: (command: String): State = (command :: Nil) ::: this
def ++ (newCommands: Seq[Command]): State = s.copy(processors = s.processors ++ newCommands)
2010-07-28 05:01:45 +02:00
def + (newCommand: Command): State = this ++ (newCommand :: Nil)
def baseDir: File = s.configuration.baseDirectory
def setNext(n: Next.Value) = s.copy(next = n)
def continue = setNext(Next.Continue)
def reload = setNext(Next.Reload)
def exit(ok: Boolean) = setNext(if(ok) Next.Fail else Next.Done)
2010-09-05 16:56:24 +02:00
def get[T](key: AttributeKey[T]) = s.attributes.get(key)
def put[T](key: AttributeKey[T], value: T) = s.copy(attributes = s.attributes.put(key, value))
def fail =
2010-09-05 17:16:53 +02:00
{
val remaining = s.commands.dropWhile(_ != FailureWall)
if(remaining.isEmpty)
{
2010-09-05 17:16:53 +02:00
s.onFailure match
{
case Some(c) => s.copy(commands = c :: Nil, onFailure = None)
2010-09-05 17:16:53 +02:00
case None => exit(ok = false)
}
}
2010-09-05 17:16:53 +02:00
else
s.copy(commands = remaining)
2010-09-05 17:16:53 +02:00
}
}
}