2010-07-17 18:07:41 +02:00
|
|
|
/* sbt -- Simple Build Tool
|
2011-01-22 20:01:59 +01:00
|
|
|
* Copyright 2008, 2009, 2010, 2011 Mark Harrah
|
2010-07-17 18:07:41 +02:00
|
|
|
*/
|
|
|
|
|
package sbt
|
|
|
|
|
|
2011-01-22 20:01:59 +01:00
|
|
|
import Execute.NodeView
|
2011-04-10 02:42:57 +02:00
|
|
|
import complete.{DefaultParsers, HistoryCommands, Parser}
|
2011-01-22 20:01:59 +01:00
|
|
|
import HistoryCommands.{Start => HistoryPrefix}
|
2011-03-02 12:46:28 +01:00
|
|
|
import compiler.EvalImports
|
2011-04-16 17:24:58 +02:00
|
|
|
import Types.idFun
|
2010-07-28 05:01:45 +02:00
|
|
|
|
2011-02-27 05:56:30 +01:00
|
|
|
import Command.applyEffect
|
2011-03-02 12:46:28 +01:00
|
|
|
import Keys.{analysis,historyPath,logged,shellPrompt}
|
2011-01-22 20:01:59 +01:00
|
|
|
import scala.annotation.tailrec
|
|
|
|
|
import scala.collection.JavaConversions._
|
|
|
|
|
import Function.tupled
|
2011-01-24 04:34:17 +01:00
|
|
|
import java.net.URI
|
2011-05-31 04:10:01 +02:00
|
|
|
import java.lang.reflect.InvocationTargetException
|
2011-01-22 20:01:59 +01:00
|
|
|
import Path._
|
|
|
|
|
|
|
|
|
|
import java.io.File
|
2010-07-17 18:07:41 +02:00
|
|
|
|
|
|
|
|
/** This class is the entry point for sbt.*/
|
2011-04-19 00:26:57 +02:00
|
|
|
final class xMain extends xsbti.AppMain
|
2010-07-17 18:07:41 +02:00
|
|
|
{
|
2011-04-19 00:26:57 +02:00
|
|
|
def run(configuration: xsbti.AppConfiguration): xsbti.MainResult =
|
2010-07-17 18:07:41 +02:00
|
|
|
{
|
2011-04-19 00:26:57 +02:00
|
|
|
import BuiltinCommands.{initialAttributes, initialize, defaults, DefaultBootCommands}
|
2010-07-28 05:01:45 +02:00
|
|
|
import CommandSupport.{DefaultsCommand, InitCommand}
|
|
|
|
|
val initialCommandDefs = Seq(initialize, defaults)
|
2011-02-20 04:22:09 +01:00
|
|
|
val commands = DefaultsCommand +: InitCommand +: (DefaultBootCommands ++ configuration.arguments.map(_.trim))
|
2011-07-02 05:38:03 +02:00
|
|
|
val state = State( configuration, initialCommandDefs, Set.empty, None, commands, initialAttributes, None )
|
2011-04-19 00:26:57 +02:00
|
|
|
MainLoop.run(state)
|
2010-07-17 18:07:41 +02:00
|
|
|
}
|
2011-04-19 00:26:57 +02:00
|
|
|
}
|
|
|
|
|
final class ScriptMain extends xsbti.AppMain
|
|
|
|
|
{
|
|
|
|
|
def run(configuration: xsbti.AppConfiguration): xsbti.MainResult =
|
|
|
|
|
{
|
|
|
|
|
import BuiltinCommands.{initialAttributes, ScriptCommands}
|
|
|
|
|
val commands = Script.Name +: configuration.arguments.map(_.trim)
|
2011-07-02 05:38:03 +02:00
|
|
|
val state = State( configuration, ScriptCommands, Set.empty, None, commands, initialAttributes, None )
|
2011-04-19 00:26:57 +02:00
|
|
|
MainLoop.run(state)
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-04-23 21:26:44 +02:00
|
|
|
final class ConsoleMain extends xsbti.AppMain
|
|
|
|
|
{
|
|
|
|
|
def run(configuration: xsbti.AppConfiguration): xsbti.MainResult =
|
|
|
|
|
{
|
|
|
|
|
import BuiltinCommands.{initialAttributes, ConsoleCommands}
|
|
|
|
|
val commands = IvyConsole.Name +: configuration.arguments.map(_.trim)
|
2011-07-02 05:38:03 +02:00
|
|
|
val state = State( configuration, ConsoleCommands, Set.empty, None, commands, initialAttributes, None )
|
2011-04-23 21:26:44 +02:00
|
|
|
MainLoop.run(state)
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-04-19 00:26:57 +02:00
|
|
|
object MainLoop
|
|
|
|
|
{
|
2010-07-17 18:07:41 +02:00
|
|
|
@tailrec final def run(state: State): xsbti.MainResult =
|
2011-07-02 05:38:03 +02:00
|
|
|
state.result match
|
2010-07-17 18:07:41 +02:00
|
|
|
{
|
2011-07-02 05:38:03 +02:00
|
|
|
case None => run(next(state))
|
|
|
|
|
case Some(result) => result
|
2010-07-17 18:07:41 +02:00
|
|
|
}
|
2011-07-02 05:38:03 +02:00
|
|
|
|
2010-08-05 01:41:11 +02:00
|
|
|
def next(state: State): State =
|
2011-01-22 20:01:59 +01:00
|
|
|
ErrorHandling.wideConvert { state.process(Command.process) } match
|
2010-08-05 01:41:11 +02:00
|
|
|
{
|
|
|
|
|
case Right(s) => s
|
2011-03-01 15:44:45 +01:00
|
|
|
case Left(t: xsbti.FullReload) => throw t
|
2011-02-09 02:38:18 +01:00
|
|
|
case Left(t) => BuiltinCommands.handleException(t, state)
|
2010-08-05 01:41:11 +02:00
|
|
|
}
|
2010-07-17 18:07:41 +02:00
|
|
|
}
|
|
|
|
|
|
2011-01-22 20:01:59 +01:00
|
|
|
import DefaultParsers._
|
|
|
|
|
import CommandSupport._
|
2011-02-09 02:38:18 +01:00
|
|
|
object BuiltinCommands
|
2010-07-17 18:07:41 +02:00
|
|
|
{
|
2011-04-19 00:26:57 +02:00
|
|
|
def initialAttributes = AttributeMap.empty.put(logged, ConsoleLogger())
|
|
|
|
|
|
2011-04-23 21:26:44 +02:00
|
|
|
def ConsoleCommands: Seq[Command] = Seq(ignore, exit, IvyConsole.command, act, nop)
|
2011-04-19 00:26:57 +02:00
|
|
|
def ScriptCommands: Seq[Command] = Seq(ignore, exit, Script.command, act, nop)
|
|
|
|
|
def DefaultCommands: Seq[Command] = Seq(ignore, help, reboot, read, history, continuous, exit, loadProject, loadProjectImpl, loadFailed, Cross.crossBuild, Cross.switchVersion,
|
2011-05-28 23:02:16 +02:00
|
|
|
projects, project, setOnFailure, clearOnFailure, ifLast, multi, shell, set, tasks, inspect, eval, alias, append, last, lastGrep, nop, sessionCommand, act)
|
2011-02-20 04:22:09 +01:00
|
|
|
def DefaultBootCommands: Seq[String] = LoadProject :: (IfLast + " " + Shell) :: Nil
|
2010-09-05 17:16:53 +02:00
|
|
|
|
2011-02-09 02:38:18 +01:00
|
|
|
def nop = Command.custom(s => success(() => s))
|
2011-04-16 17:24:58 +02:00
|
|
|
def ignore = Command.command(FailureWall)(idFun)
|
2010-07-28 05:01:45 +02:00
|
|
|
|
|
|
|
|
def detail(selected: Iterable[String])(h: Help): Option[String] =
|
|
|
|
|
h.detail match { case (commands, value) => if( selected exists commands ) Some(value) else None }
|
|
|
|
|
|
2011-02-09 02:38:18 +01:00
|
|
|
def help = Command.make(HelpCommand, helpBrief, helpDetailed)(helpParser)
|
2010-07-28 05:01:45 +02:00
|
|
|
|
2011-01-24 04:34:17 +01:00
|
|
|
def helpParser(s: State) =
|
|
|
|
|
{
|
2011-03-21 22:56:41 +01:00
|
|
|
val h = s.definedCommands.flatMap(_.help)
|
2011-01-24 04:34:17 +01:00
|
|
|
val helpCommands = h.flatMap(_.detail._1)
|
|
|
|
|
val args = (token(Space) ~> token( OpOrID.examples(helpCommands : _*) )).*
|
|
|
|
|
applyEffect(args)(runHelp(s, h))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def runHelp(s: State, h: Seq[Help])(args: Seq[String]): State =
|
|
|
|
|
{
|
2010-07-28 05:01:45 +02:00
|
|
|
val message =
|
2011-01-22 20:01:59 +01:00
|
|
|
if(args.isEmpty)
|
2011-05-28 23:02:16 +02:00
|
|
|
aligned(" ", " ", h.map(_.brief)).mkString("\n", "\n", "\n")
|
2010-07-28 05:01:45 +02:00
|
|
|
else
|
2011-01-22 20:01:59 +01:00
|
|
|
h flatMap detail(args) mkString("\n", "\n\n", "\n")
|
2010-07-17 18:07:41 +02:00
|
|
|
System.out.println(message)
|
|
|
|
|
s
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-28 23:02:16 +02:00
|
|
|
def tasks = Command.command(TasksCommand, tasksBrief, tasksDetailed) { s =>
|
|
|
|
|
System.out.println(tasksPreamble)
|
|
|
|
|
System.out.println(tasksHelp(s))
|
|
|
|
|
s
|
|
|
|
|
}
|
|
|
|
|
def tasksHelp(s: State): String =
|
|
|
|
|
{
|
|
|
|
|
val extracted = Project.extract(s)
|
|
|
|
|
import extracted._
|
|
|
|
|
val index = structure.index
|
|
|
|
|
val pairs = index.keyIndex.keys(Some(currentRef)).toSeq map index.keyMap sortBy(_.label) flatMap taskStrings
|
|
|
|
|
aligned(" ", " ", pairs) mkString("\n", "\n", "")
|
|
|
|
|
}
|
|
|
|
|
def taskStrings(key: AttributeKey[_]): Option[(String, String)] = key.description map { d => (key.label, d) }
|
|
|
|
|
def aligned(pre: String, sep: String, in: Seq[(String, String)]): Seq[String] =
|
|
|
|
|
{
|
|
|
|
|
val width = in.map(_._1.length).max
|
|
|
|
|
in.map { case (a, b) => (" " + fill(a, width) + sep + b) }
|
|
|
|
|
}
|
|
|
|
|
def fill(s: String, size: Int) = s + " " * math.max(size - s.length, 0)
|
|
|
|
|
|
2011-02-09 02:38:18 +01:00
|
|
|
def alias = Command.make(AliasCommand, AliasBrief, AliasDetailed) { s =>
|
2011-01-22 20:01:59 +01:00
|
|
|
val name = token(OpOrID.examples( aliasNames(s) : _*) )
|
2011-03-06 14:32:51 +01:00
|
|
|
val assign = token(OptSpace ~ '=' ~ OptSpace)
|
2011-01-24 04:34:17 +01:00
|
|
|
val sfree = removeAliases(s)
|
2011-03-06 14:21:16 +01:00
|
|
|
val to = matched(sfree.combinedParser, partial = true) | any.+.string
|
2011-01-24 04:34:17 +01:00
|
|
|
val base = (OptSpace ~> (name ~ (assign ~> to.?).?).?)
|
2011-01-22 21:01:10 +01:00
|
|
|
applyEffect(base)(t => runAlias(s, t) )
|
|
|
|
|
}
|
2011-01-24 04:34:17 +01:00
|
|
|
|
|
|
|
|
def runAlias(s: State, args: Option[(String, Option[Option[String]])]): State =
|
2011-01-22 21:01:10 +01:00
|
|
|
args match
|
|
|
|
|
{
|
2011-01-22 20:01:59 +01:00
|
|
|
case None => printAliases(s); s
|
2011-01-24 04:34:17 +01:00
|
|
|
case Some(x ~ None) if !x.isEmpty => printAlias(s, x.trim); s
|
|
|
|
|
case Some(name ~ Some(None)) => removeAlias(s, name.trim)
|
|
|
|
|
case Some(name ~ Some(Some(value))) => addAlias(s, name.trim, value.trim)
|
2010-07-28 05:01:45 +02:00
|
|
|
}
|
|
|
|
|
|
2011-01-22 20:01:59 +01:00
|
|
|
def shell = Command.command(Shell, ShellBrief, ShellDetailed) { s =>
|
2011-03-02 12:46:28 +01:00
|
|
|
val history = (s get historyPath.key) getOrElse Some((s.baseDir / ".history").asFile)
|
|
|
|
|
val prompt = (s get shellPrompt.key) match { case Some(pf) => pf(s); case None => "> " }
|
2011-03-06 14:21:16 +01:00
|
|
|
val reader = new FullReader(history, s.combinedParser)
|
2011-02-22 01:35:05 +01:00
|
|
|
val line = reader.readLine(prompt)
|
2010-08-05 01:41:46 +02:00
|
|
|
line match {
|
2011-03-21 22:56:41 +01:00
|
|
|
case Some(line) => s.copy(onFailure = Some(Shell), remainingCommands = line +: Shell +: s.remainingCommands)
|
2010-08-05 01:41:46 +02:00
|
|
|
case None => s
|
|
|
|
|
}
|
2010-07-28 05:01:45 +02:00
|
|
|
}
|
|
|
|
|
|
2011-03-11 22:52:44 +01:00
|
|
|
def multiParser(s: State): Parser[Seq[String]] =
|
2011-03-25 02:25:57 +01:00
|
|
|
( token(';' ~> OptSpace) flatMap { _ => matched(s.combinedParser) <~ token(OptSpace) } ).+
|
2011-03-11 22:52:44 +01:00
|
|
|
def multiApplied(s: State) =
|
|
|
|
|
Command.applyEffect( multiParser(s) )( _ ::: s )
|
|
|
|
|
|
|
|
|
|
def multi = Command.custom(multiApplied, Help(MultiBrief, (Set(Multi), MultiDetailed)) :: Nil )
|
2010-07-28 05:01:45 +02:00
|
|
|
|
2011-03-11 22:52:44 +01:00
|
|
|
lazy val otherCommandParser = (s: State) => token(OptSpace ~> matched(s.combinedParser) )
|
|
|
|
|
|
|
|
|
|
def ifLast = Command(IfLast, IfLastBrief, IfLastDetailed)(otherCommandParser) { (s, arg) =>
|
2011-03-21 22:56:41 +01:00
|
|
|
if(s.remainingCommands.isEmpty) arg :: s else s
|
2010-07-28 05:01:45 +02:00
|
|
|
}
|
2011-03-11 22:52:44 +01:00
|
|
|
def append = Command(Append, AppendLastBrief, AppendLastDetailed)(otherCommandParser) { (s, arg) =>
|
2011-03-21 22:56:41 +01:00
|
|
|
s.copy(remainingCommands = s.remainingCommands :+ arg)
|
2010-08-05 01:41:46 +02:00
|
|
|
}
|
2010-07-28 05:01:45 +02:00
|
|
|
|
2011-03-11 22:52:44 +01:00
|
|
|
def setOnFailure = Command(OnFailure, OnFailureBrief, OnFailureDetailed)(otherCommandParser) { (s, arg) =>
|
2011-01-22 20:01:59 +01:00
|
|
|
s.copy(onFailure = Some(arg))
|
2010-07-28 05:01:45 +02:00
|
|
|
}
|
2011-02-20 04:22:09 +01:00
|
|
|
def clearOnFailure = Command.command(ClearOnFailure)(s => s.copy(onFailure = None))
|
2010-07-28 05:01:45 +02:00
|
|
|
|
2011-03-01 15:44:45 +01:00
|
|
|
def reboot = Command(RebootCommand, RebootBrief, RebootDetailed)(rebootParser) { (s, full) =>
|
|
|
|
|
s.runExitHooks().reboot(full)
|
2010-07-28 05:01:45 +02:00
|
|
|
}
|
2011-03-01 15:44:45 +01:00
|
|
|
def rebootParser(s: State) = token(Space ~> "full" ^^^ true) ?? false
|
2010-07-28 05:01:45 +02:00
|
|
|
|
2011-01-22 20:01:59 +01:00
|
|
|
def defaults = Command.command(DefaultsCommand) { s =>
|
2010-07-28 05:01:45 +02:00
|
|
|
s ++ DefaultCommands
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-22 20:01:59 +01:00
|
|
|
def initialize = Command.command(InitCommand) { s =>
|
2010-08-05 01:41:46 +02:00
|
|
|
/*"load-commands -base ~/.sbt/commands" :: */readLines( readable( sbtRCs(s) ) ) ::: s
|
2010-07-28 05:01:45 +02:00
|
|
|
}
|
|
|
|
|
|
2011-01-22 20:01:59 +01:00
|
|
|
def readParser(s: State) =
|
|
|
|
|
{
|
|
|
|
|
val files = (token(Space) ~> fileParser(s.baseDir)).+
|
|
|
|
|
val portAndSuccess = token(OptSpace) ~> Port
|
|
|
|
|
portAndSuccess || files
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-09 02:38:18 +01:00
|
|
|
def read = Command.make(ReadCommand, ReadBrief, ReadDetailed)(s => applyEffect(readParser(s))(doRead(s)) )
|
2011-01-22 20:01:59 +01:00
|
|
|
|
|
|
|
|
def doRead(s: State)(arg: Either[Int, Seq[File]]): State =
|
|
|
|
|
arg match
|
2010-09-28 00:59:35 +02:00
|
|
|
{
|
|
|
|
|
case Left(portAndSuccess) =>
|
|
|
|
|
val port = math.abs(portAndSuccess)
|
|
|
|
|
val previousSuccess = portAndSuccess >= 0
|
|
|
|
|
readMessage(port, previousSuccess) match
|
|
|
|
|
{
|
2010-12-19 18:03:10 +01:00
|
|
|
case Some(message) => (message :: (ReadCommand + " " + port) :: s).copy(onFailure = Some(ReadCommand + " " + (-port)))
|
2010-09-28 00:59:35 +02:00
|
|
|
case None =>
|
|
|
|
|
System.err.println("Connection closed.")
|
|
|
|
|
s.fail
|
|
|
|
|
}
|
|
|
|
|
case Right(from) =>
|
|
|
|
|
val notFound = notReadable(from)
|
|
|
|
|
if(notFound.isEmpty)
|
|
|
|
|
readLines(from) ::: s // this means that all commands from all files are loaded, parsed, and inserted before any are executed
|
|
|
|
|
else {
|
|
|
|
|
logger(s).error("Command file(s) not readable: \n\t" + notFound.mkString("\n\t"))
|
|
|
|
|
s
|
|
|
|
|
}
|
2010-07-28 05:01:45 +02:00
|
|
|
}
|
2010-09-28 00:59:35 +02:00
|
|
|
private def readMessage(port: Int, previousSuccess: Boolean): Option[String] =
|
|
|
|
|
{
|
|
|
|
|
// split into two connections because this first connection ends the previous communication
|
|
|
|
|
xsbt.IPC.client(port) { _.send(previousSuccess.toString) }
|
|
|
|
|
// and this second connection starts the next communication
|
|
|
|
|
xsbt.IPC.client(port) { ipc =>
|
|
|
|
|
val message = ipc.receive
|
|
|
|
|
if(message eq null) None else Some(message)
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-01-22 20:01:59 +01:00
|
|
|
|
2010-12-19 18:03:10 +01:00
|
|
|
def continuous =
|
2011-03-11 22:52:44 +01:00
|
|
|
Command(ContinuousExecutePrefix, Help(continuousBriefHelp) )(otherCommandParser) { (s, arg) =>
|
2011-03-01 14:51:14 +01:00
|
|
|
withAttribute(s, Watched.Configuration, "Continuous execution not configured.") { w =>
|
2011-01-22 20:01:59 +01:00
|
|
|
val repeat = ContinuousExecutePrefix + (if(arg.startsWith(" ")) arg else " " + arg)
|
|
|
|
|
Watched.executeContinuously(w, s, arg, repeat)
|
2010-12-19 18:03:10 +01:00
|
|
|
}
|
2010-09-05 17:19:19 +02:00
|
|
|
}
|
|
|
|
|
|
2011-03-11 22:52:44 +01:00
|
|
|
def history = Command.custom(historyParser, historyHelp)
|
|
|
|
|
def historyParser(s: State): Parser[() => State] =
|
|
|
|
|
Command.applyEffect(HistoryCommands.actionParser) { histFun =>
|
|
|
|
|
val logError = (msg: String) => CommandSupport.logger(s).error(msg)
|
|
|
|
|
val hp = s get historyPath.key getOrElse None
|
|
|
|
|
val lines = hp.toList.flatMap( p => IO.readLines(p) ).toIndexedSeq
|
|
|
|
|
histFun( complete.History(lines, hp, logError) ) match
|
|
|
|
|
{
|
|
|
|
|
case Some(commands) =>
|
|
|
|
|
commands foreach println //printing is more appropriate than logging
|
|
|
|
|
(commands ::: s).continue
|
|
|
|
|
case None => s.fail
|
|
|
|
|
}
|
2010-07-17 18:07:41 +02:00
|
|
|
}
|
2010-07-28 05:01:45 +02:00
|
|
|
|
2011-01-25 13:24:52 +01:00
|
|
|
def eval = Command.single(EvalCommand, evalBrief, evalDetailed) { (s, arg) =>
|
|
|
|
|
val log = logger(s)
|
|
|
|
|
val extracted = Project extract s
|
|
|
|
|
import extracted._
|
2011-01-27 01:49:54 +01:00
|
|
|
val result = session.currentEval().eval(arg, srcName = "<eval>", imports = autoImports(extracted))
|
2011-07-09 22:54:41 +02:00
|
|
|
log.info("ans: " + result.tpe + " = " + result.getValue(currentLoader))
|
2011-01-25 13:24:52 +01:00
|
|
|
s
|
|
|
|
|
}
|
2011-02-09 02:38:18 +01:00
|
|
|
def sessionCommand = Command.make(SessionCommand, sessionBrief, SessionSettings.Help)(SessionSettings.command)
|
2011-02-03 04:56:11 +01:00
|
|
|
def reapply(newSession: SessionSettings, structure: Load.BuildStructure, s: State): State =
|
|
|
|
|
{
|
|
|
|
|
logger(s).info("Reapplying settings...")
|
|
|
|
|
val newStructure = Load.reapply(newSession.mergeSettings, structure)
|
2011-02-05 04:02:39 +01:00
|
|
|
Project.setProject(newSession, newStructure, s)
|
2011-02-03 04:56:11 +01:00
|
|
|
}
|
2011-01-25 13:24:52 +01:00
|
|
|
def set = Command.single(SetCommand, setBrief, setDetailed) { (s, arg) =>
|
|
|
|
|
val extracted = Project extract s
|
|
|
|
|
import extracted._
|
2011-07-09 22:54:41 +02:00
|
|
|
val settings = EvaluateConfigurations.evaluateSetting(session.currentEval(), "<set>", imports(extracted), arg, 0)(currentLoader)
|
2011-04-14 01:09:33 +02:00
|
|
|
val append = Load.transformSettings(Load.projectScope(currentRef), currentRef.build, rootProject, settings)
|
2011-01-25 13:24:52 +01:00
|
|
|
val newSession = session.appendSettings( append map (a => (a, arg)))
|
2011-02-03 04:56:11 +01:00
|
|
|
reapply(newSession, structure, s)
|
2011-01-25 13:24:52 +01:00
|
|
|
}
|
2011-03-30 02:25:12 +02:00
|
|
|
def inspect = Command(InspectCommand, inspectBrief, inspectDetailed)(inspectParser) { case (s,(actual,sk)) =>
|
|
|
|
|
val detailString = Project.details(Project.structure(s), actual, sk.scope, sk.key)
|
2011-02-06 18:26:20 +01:00
|
|
|
logger(s).info(detailString)
|
|
|
|
|
s
|
2011-02-12 22:23:40 +01:00
|
|
|
}
|
2011-02-16 00:41:01 +01:00
|
|
|
def lastGrep = Command(LastGrepCommand, lastGrepBrief, lastGrepDetailed)(lastGrepParser) { case (s,(pattern,sk)) =>
|
2011-06-11 05:40:25 +02:00
|
|
|
val (str, ref) = extractLast(s)
|
|
|
|
|
Output.lastGrep(sk, str, pattern, ref)
|
2011-02-12 22:23:40 +01:00
|
|
|
s
|
2011-02-16 00:41:01 +01:00
|
|
|
}
|
2011-06-11 05:40:25 +02:00
|
|
|
def extractLast(s: State) = {
|
|
|
|
|
val ext = Project.extract(s)
|
|
|
|
|
(ext.structure.streams, Select(ext.currentRef))
|
|
|
|
|
}
|
2011-03-30 02:25:12 +02:00
|
|
|
def inspectParser = (s: State) => token((Space ~> ("actual" ^^^ true)) ?? false) ~ spacedKeyParser(s)
|
2011-02-20 04:22:09 +01:00
|
|
|
val spacedKeyParser = (s: State) => Act.requireSession(s, token(Space) ~> Act.scopedKeyParser(s))
|
2011-03-22 01:26:04 +01:00
|
|
|
val optSpacedKeyParser = (s: State) => spacedKeyParser(s).?
|
|
|
|
|
def lastGrepParser(s: State) = Act.requireSession(s, (token(Space) ~> token(NotSpace, "<pattern>")) ~ optSpacedKeyParser(s))
|
|
|
|
|
def last = Command(LastCommand, lastBrief, lastDetailed)(optSpacedKeyParser) { (s,sk) =>
|
2011-06-11 05:40:25 +02:00
|
|
|
val (str, ref) = extractLast(s)
|
|
|
|
|
Output.last(sk, str, ref)
|
2011-02-06 18:26:20 +01:00
|
|
|
s
|
2011-01-25 13:24:52 +01:00
|
|
|
}
|
2011-02-16 00:41:01 +01:00
|
|
|
|
2011-01-27 01:49:54 +01:00
|
|
|
def autoImports(extracted: Extracted): EvalImports = new EvalImports(imports(extracted), "<auto-imports>")
|
|
|
|
|
def imports(extracted: Extracted): Seq[(String,Int)] =
|
|
|
|
|
{
|
2011-03-21 22:56:41 +01:00
|
|
|
val curi = extracted.currentRef.build
|
|
|
|
|
extracted.structure.units(curi).imports.map(s => (s, -1))
|
2011-01-27 01:49:54 +01:00
|
|
|
}
|
2011-01-25 13:24:52 +01:00
|
|
|
|
2011-01-24 04:34:17 +01:00
|
|
|
def listBuild(uri: URI, build: Load.LoadedBuildUnit, current: Boolean, currentID: String, log: Logger) =
|
|
|
|
|
{
|
|
|
|
|
log.info("In " + uri)
|
|
|
|
|
def prefix(id: String) = if(currentID != id) " " else if(current) " * " else "(*)"
|
|
|
|
|
for(id <- build.defined.keys) log.info("\t" + prefix(id) + id)
|
|
|
|
|
}
|
2010-07-28 05:01:45 +02:00
|
|
|
|
2011-02-09 02:38:18 +01:00
|
|
|
def act = Command.custom(Act.actParser)
|
2011-01-26 04:23:03 +01:00
|
|
|
|
2011-01-22 20:01:59 +01:00
|
|
|
def projects = Command.command(ProjectsCommand, projectsBrief, projectsDetailed ) { s =>
|
2011-01-25 13:24:52 +01:00
|
|
|
val extracted = Project extract s
|
|
|
|
|
import extracted._
|
2011-03-21 22:56:41 +01:00
|
|
|
import currentRef.{build => curi, project => cid}
|
2010-12-19 18:03:10 +01:00
|
|
|
val log = logger(s)
|
2011-01-24 04:34:17 +01:00
|
|
|
listBuild(curi, structure.units(curi), true, cid, log)
|
|
|
|
|
for( (uri, build) <- structure.units if curi != uri) listBuild(uri, build, false, cid, log)
|
2011-01-19 00:24:11 +01:00
|
|
|
s
|
2010-07-28 05:01:45 +02:00
|
|
|
}
|
2010-12-19 18:03:10 +01:00
|
|
|
def withAttribute[T](s: State, key: AttributeKey[T], ifMissing: String)(f: T => State): State =
|
|
|
|
|
(s get key) match {
|
|
|
|
|
case None => logger(s).error(ifMissing); s.fail
|
|
|
|
|
case Some(nav) => f(nav)
|
|
|
|
|
}
|
2010-07-28 05:01:45 +02:00
|
|
|
|
2011-02-09 02:38:18 +01:00
|
|
|
def project = Command.make(ProjectCommand, projectBrief, projectDetailed)(ProjectNavigation.command)
|
2010-10-30 19:24:23 +02:00
|
|
|
|
2011-01-22 20:01:59 +01:00
|
|
|
def exit = Command.command(TerminateAction, Help(exitBrief) :: Nil ) ( doExit )
|
|
|
|
|
|
2011-02-05 04:02:39 +01:00
|
|
|
def doExit(s: State): State = s.runExitHooks().exit(true)
|
2010-07-19 18:38:42 +02:00
|
|
|
|
2011-02-20 04:22:09 +01:00
|
|
|
def loadFailed = Command.command(LoadFailed)(handleLoadFailed)
|
|
|
|
|
@tailrec def handleLoadFailed(s: State): State =
|
|
|
|
|
{
|
|
|
|
|
val result = (SimpleReader.readLine("Project loading failed: (r)etry, (q)uit, or (i)gnore? ") getOrElse Quit).toLowerCase
|
|
|
|
|
def matches(s: String) = !result.isEmpty && (s startsWith result)
|
|
|
|
|
|
|
|
|
|
if(matches("retry"))
|
|
|
|
|
LoadProject :: s
|
|
|
|
|
else if(matches(Quit))
|
|
|
|
|
s.exit(ok = false)
|
|
|
|
|
else if(matches("ignore"))
|
|
|
|
|
s
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
println("Invalid response.")
|
|
|
|
|
handleLoadFailed(s)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-12 04:33:30 +01:00
|
|
|
def loadProjectCommands(arg: String) = (OnFailure + " " + LoadFailed) :: (LoadProjectImpl + " " + arg).trim :: ClearOnFailure :: FailureWall :: Nil
|
|
|
|
|
def loadProject = Command(LoadProject, LoadProjectBrief, LoadProjectDetailed)(_ => matched(Project.loadActionParser)) { (s,arg) => loadProjectCommands(arg) ::: s }
|
2011-02-20 04:22:09 +01:00
|
|
|
|
2011-03-12 04:33:30 +01:00
|
|
|
def loadProjectImpl = Command(LoadProjectImpl)(_ => Project.loadActionParser) { (s0, action) =>
|
|
|
|
|
val (s, base) = Project.loadAction(s0, action)
|
|
|
|
|
IO.createDirectory(base)
|
|
|
|
|
val (eval, structure) = Load.defaultLoad(s, base, logger(s))
|
2011-01-25 13:24:52 +01:00
|
|
|
val session = Load.initialSession(structure, eval)
|
2011-02-05 04:02:39 +01:00
|
|
|
Project.setProject(session, structure, s)
|
2011-01-19 00:24:11 +01:00
|
|
|
}
|
2010-07-17 18:07:41 +02:00
|
|
|
|
2011-03-21 03:54:01 +01:00
|
|
|
def handleException(e: Throwable, s: State): State =
|
|
|
|
|
handleException(e, s, logger(s))
|
|
|
|
|
def handleException(e: Throwable, s: State, log: Logger): State =
|
|
|
|
|
{
|
|
|
|
|
e match
|
|
|
|
|
{
|
2011-04-04 03:08:06 +02:00
|
|
|
case _: Incomplete => () // already handled by evaluateTask
|
|
|
|
|
case _: NoMessageException => ()
|
2011-05-31 04:10:01 +02:00
|
|
|
case ite: InvocationTargetException =>
|
|
|
|
|
val cause = ite.getCause
|
|
|
|
|
if(cause == null || cause == ite) logFullException(ite, log) else handleException(cause, s, log)
|
|
|
|
|
case _: MessageOnlyException => log.error(e.toString)
|
|
|
|
|
case _ => logFullException(e, log)
|
2011-03-21 03:54:01 +01:00
|
|
|
}
|
2010-08-05 01:41:11 +02:00
|
|
|
s.fail
|
|
|
|
|
}
|
2011-05-31 04:10:01 +02:00
|
|
|
def logFullException(e: Throwable, log: Logger)
|
|
|
|
|
{
|
|
|
|
|
log.trace(e)
|
|
|
|
|
log.error(ErrorHandling reducedToString e)
|
|
|
|
|
log.error("Use 'last' for the full log.")
|
|
|
|
|
}
|
2010-08-05 01:41:11 +02:00
|
|
|
|
2010-07-28 05:01:45 +02:00
|
|
|
def addAlias(s: State, name: String, value: String): State =
|
2011-01-22 20:01:59 +01:00
|
|
|
if(Command validID name) {
|
2010-07-28 05:01:45 +02:00
|
|
|
val removed = removeAlias(s, name)
|
2011-03-21 22:56:41 +01:00
|
|
|
if(value.isEmpty) removed else removed.copy(definedCommands = newAlias(name, value) +: removed.definedCommands)
|
2010-07-28 05:01:45 +02:00
|
|
|
} else {
|
|
|
|
|
System.err.println("Invalid alias name '" + name + "'.")
|
|
|
|
|
s.fail
|
|
|
|
|
}
|
2011-01-24 04:34:17 +01:00
|
|
|
|
2011-02-09 02:38:18 +01:00
|
|
|
def removeAliases(s: State): State = removeTagged(s, CommandAliasKey)
|
2011-03-21 22:56:41 +01:00
|
|
|
def removeAlias(s: State, name: String): State = s.copy(definedCommands = s.definedCommands.filter(c => !isAliasNamed(name, c)) )
|
2011-02-09 02:38:18 +01:00
|
|
|
|
2011-03-21 22:56:41 +01:00
|
|
|
def removeTagged(s: State, tag: AttributeKey[_]): State = s.copy(definedCommands = removeTagged(s.definedCommands, tag))
|
2011-02-09 02:38:18 +01:00
|
|
|
def removeTagged(as: Seq[Command], tag: AttributeKey[_]): Seq[Command] = as.filter(c => ! (c.tags contains tag))
|
|
|
|
|
|
2011-01-22 20:01:59 +01:00
|
|
|
def isAliasNamed(name: String, c: Command): Boolean = isNamed(name, getAlias(c))
|
2011-01-24 04:34:17 +01:00
|
|
|
def isNamed(name: String, alias: Option[(String,String)]): Boolean = alias match { case None => false; case Some((n,_)) => name == n }
|
2011-01-22 20:01:59 +01:00
|
|
|
|
|
|
|
|
def getAlias(c: Command): Option[(String,String)] = c.tags get CommandAliasKey
|
|
|
|
|
def printAlias(s: State, name: String): Unit = printAliases(aliases(s,(n,v) => n == name) )
|
|
|
|
|
def printAliases(s: State): Unit = printAliases(allAliases(s))
|
|
|
|
|
def printAliases(as: Seq[(String,String)]): Unit =
|
|
|
|
|
for( (name,value) <- as)
|
|
|
|
|
println("\t" + name + " = " + value)
|
|
|
|
|
|
|
|
|
|
def aliasNames(s: State): Seq[String] = allAliases(s).map(_._1)
|
|
|
|
|
def allAliases(s: State): Seq[(String,String)] = aliases(s, (n,v) => true)
|
|
|
|
|
def aliases(s: State, pred: (String,String) => Boolean): Seq[(String,String)] =
|
2011-03-21 22:56:41 +01:00
|
|
|
s.definedCommands.flatMap(c => getAlias(c).filter(tupled(pred)))
|
2011-01-22 20:01:59 +01:00
|
|
|
|
|
|
|
|
def newAlias(name: String, value: String): Command =
|
2011-02-09 02:38:18 +01:00
|
|
|
Command.make(name, (name, "'" + value + "'"), "Alias of '" + value + "'")(aliasBody(name, value)).tag(CommandAliasKey, (name, value))
|
2011-01-22 21:01:10 +01:00
|
|
|
def aliasBody(name: String, value: String)(state: State): Parser[() => State] =
|
2011-03-21 22:56:41 +01:00
|
|
|
OptSpace ~> Parser(Command.combine(removeAlias(state,name).definedCommands)(state))(value)
|
2011-01-24 04:34:17 +01:00
|
|
|
|
2011-04-21 02:18:58 +02:00
|
|
|
val CommandAliasKey = AttributeKey[(String,String)]("is-command-alias", "Internal: marker for Commands created as aliases for another command.")
|
2010-07-17 18:07:41 +02:00
|
|
|
}
|