2010-07-17 18:07:41 +02:00
|
|
|
/* sbt -- Simple Build Tool
|
|
|
|
|
* Copyright 2009, 2010 Mark Harrah
|
|
|
|
|
*/
|
|
|
|
|
package sbt
|
|
|
|
|
|
2010-12-19 18:03:10 +01:00
|
|
|
import Execute.NodeView
|
|
|
|
|
import java.io.File
|
|
|
|
|
import Function.untupled
|
2011-01-19 00:24:11 +01:00
|
|
|
import parse.Parser
|
2010-07-19 18:31:22 +02:00
|
|
|
|
2011-01-19 00:24:11 +01:00
|
|
|
trait NewCommand // to replace Command
|
|
|
|
|
{
|
|
|
|
|
type T
|
|
|
|
|
def parser: State => Option[Parser[T]]
|
|
|
|
|
def run: (T, State) => State
|
|
|
|
|
}
|
2010-08-10 14:44:34 +02:00
|
|
|
trait Command
|
2010-07-17 18:07:41 +02:00
|
|
|
{
|
2010-12-19 18:03:10 +01:00
|
|
|
def help: State => Seq[Help]
|
|
|
|
|
def run: (Input, State) => Option[State]
|
2010-07-28 05:01:45 +02:00
|
|
|
}
|
|
|
|
|
trait Help
|
|
|
|
|
{
|
|
|
|
|
def detail: (Set[String], String)
|
|
|
|
|
def brief: (String, String)
|
|
|
|
|
}
|
|
|
|
|
object Help
|
|
|
|
|
{
|
2010-12-19 18:03:10 +01:00
|
|
|
def apply(briefHelp: (String, String), detailedHelp: (Set[String], String) = (Set.empty, "") ): Help =
|
|
|
|
|
new Help { def detail = detailedHelp; def brief = briefHelp }
|
2010-07-17 18:07:41 +02:00
|
|
|
}
|
|
|
|
|
object Command
|
|
|
|
|
{
|
2010-12-19 18:03:10 +01:00
|
|
|
val Logged = AttributeKey[Logger]("log")
|
2011-01-19 00:24:11 +01:00
|
|
|
val HistoryPath = SettingKey[Option[File]]("history")
|
2010-12-19 18:03:10 +01:00
|
|
|
val Analysis = AttributeKey[inc.Analysis]("analysis")
|
2011-01-19 00:24:11 +01:00
|
|
|
val Watch = SettingKey[Watched]("continuous-watch")
|
2010-11-24 20:10:48 +01:00
|
|
|
|
2010-12-19 18:03:10 +01:00
|
|
|
def direct(h: Help*)(r: (Input, State) => Option[State]): Command =
|
|
|
|
|
new Command { def help = _ => h; def run = r }
|
2010-07-28 05:01:45 +02:00
|
|
|
|
2010-12-19 18:03:10 +01:00
|
|
|
def apply(h: Help*)(r: PartialFunction[(Input, State), State]): Command =
|
|
|
|
|
direct(h : _*)(untupled(r.lift))
|
2010-07-28 05:01:45 +02:00
|
|
|
|
2010-12-19 18:03:10 +01:00
|
|
|
def simple(name: String, brief: (String, String), detail: String)(f: (Input, State) => State): Command =
|
2010-07-28 05:01:45 +02:00
|
|
|
{
|
|
|
|
|
val h = Help(brief, (Set(name), detail) )
|
2010-11-24 20:10:48 +01:00
|
|
|
simple(name, h)(f)
|
2010-07-28 05:01:45 +02:00
|
|
|
}
|
2010-12-19 18:03:10 +01:00
|
|
|
def simple(name: String, help: Help*)(f: (Input, State) => State): Command =
|
|
|
|
|
Command( help: _* ){ case (in, s) if name == in.name => f( in, s) }
|
|
|
|
|
}
|
2010-07-28 05:01:45 +02:00
|
|
|
final case class Input(line: String, cursor: Option[Int])
|
|
|
|
|
{
|
|
|
|
|
lazy val (name, arguments) = line match { case Input.NameRegex(n, a) => (n, a); case _ => (line, "") }
|
2010-09-28 00:49:13 +02:00
|
|
|
lazy val splitArgs: Seq[String] = if(arguments.isEmpty) Nil else (arguments split """\s+""").toSeq
|
2010-07-28 05:01:45 +02:00
|
|
|
}
|
|
|
|
|
object Input
|
2010-07-17 18:07:41 +02:00
|
|
|
{
|
2010-07-28 05:01:45 +02:00
|
|
|
val NameRegex = """\s*(\p{Punct}+|[\w-]+)\s*(.*)""".r
|
2010-07-17 18:07:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
object Next extends Enumeration {
|
|
|
|
|
val Reload, Fail, Done, Continue = Value
|
2010-08-10 14:44:34 +02:00
|
|
|
}
|
|
|
|
|
trait CommandDefinitions
|
|
|
|
|
{
|
|
|
|
|
def commands: Seq[Command]
|
|
|
|
|
}
|
|
|
|
|
trait ReflectedCommands extends CommandDefinitions
|
|
|
|
|
{
|
|
|
|
|
def commands = ReflectUtilities.allVals[Command](this).values.toSeq
|
2010-07-17 18:07:41 +02:00
|
|
|
}
|