last, last-grep

This commit is contained in:
Mark Harrah 2011-02-12 16:23:40 -05:00
parent 562f362f09
commit 11a76c9f3b
4 changed files with 80 additions and 2 deletions

View File

@ -51,8 +51,27 @@ DelegatesCommand + """ <key>
This command prints the scopes that are searched for the key.
"""
val LastGrepCommand = "last-grep"
val lastGrepBrief = (LastGrepCommand + " <pattern> <key>", "Shows lines from the last output for 'key' that match 'pattern'.")
val lastGrepDetailed =
LastGrepCommand + """ <pattern> <key>
<key> is interpreted as a Scala expression of type sbt.Scoped and is typically a task key.
<pattern> is a regular expression interpreted by java.util.Pattern
Lines that match 'pattern' from the last streams output associated with the key are displayed.
See also """ + LastCommand + "."
val LastCommand = "last"
val lastBrief = (LastCommand + " <key>", "Prints the last output associated with 'key'.")
val lastDetailed =
LastCommand + """ <key>
<key> is interpreted as a Scala expression of type sbt.Scoped.
The last streams output associated with the key (typically a task key) is redisplayed.
See also """ + LastGrepCommand + "."
val GetCommand = "get"
val getBrief = (GetCommand + " <key>", "Prints the value for the given key, the defining scope, related definitions, and dependencies.")
val getBrief = (GetCommand + " <key>", "Prints the value for 'key', the defining scope, related definitions, and dependencies.")
val getDetailed =
GetCommand + """ <key>

View File

@ -59,7 +59,7 @@ class xMain extends xsbti.AppMain
object BuiltinCommands
{
def DefaultCommands: Seq[Command] = Seq(ignore, help, reload, read, history, continuous, exit, loadCommands, loadProject, compile, discover,
projects, project, setOnFailure, ifLast, multi, shell, set, get, eval, delegates,alias, append, nop, sessionCommand, act)
projects, project, setOnFailure, ifLast, multi, shell, set, get, eval, delegates,alias, append, last, lastGrep, nop, sessionCommand, act)
def nop = Command.custom(s => success(() => s))
def ignore = Command.command(FailureWall)(identity)
@ -240,6 +240,26 @@ object BuiltinCommands
logger(s).info(detailString)
s
})
def lastGrep = Command.single(LastGrepCommand, lastGrepBrief, lastGrepDetailed) { (s,arg) =>
val (pattern, keyString) = arg.span(!_.isWhitespace)
if(keyString.isEmpty)
{
logger(s).error("Expected <pattern> <key>, found '" + arg + "'")
s.fail
}
else
{
val sc = scopedCommand(LastGrepCommand) { (s, scope, key, structure) =>
Output.lastGrep(scope, key, structure.streams, pattern)
s
}
sc(s, keyString)
}
}
def last = Command.single(LastCommand, lastBrief, lastDetailed)( scopedCommand(LastCommand) { (s, scope, key, structure) =>
Output.last(scope, key, structure.streams)
s
})
def delegates = Command.single(DelegatesCommand, delegatesBrief, delegatesDetailed) ( scopedCommand(DelegatesCommand) { (s, scope, key, structure) =>
val log = logger(s)
structure.delegates(scope).foreach(d => log.info(Project.display(Project.ScopedKey(d, key))))

36
main/Output.scala Normal file
View File

@ -0,0 +1,36 @@
/* sbt -- Simple Build Tool
* Copyright 2011 Mark Harrah
*/
package sbt
import java.util.regex.Pattern
import BuildStreams.{Streams, TaskStreams}
import Project.ScopedKey
object Output
{
def showMatches(pattern: Pattern)(line: String): Option[String] =
{
val matcher = pattern.matcher(line)
if(ConsoleLogger.formatEnabled)
{
val highlighted = matcher.replaceAll(scala.Console.RED + "$0" + scala.Console.RESET)
if(highlighted == line) None else Some(highlighted)
}
else if(matcher.find)
Some(line)
else
None
}
def last(scope: Scope, key: AttributeKey[_], mgr: Streams): Unit =
printLines(lastLines(ScopedKey(scope,key), mgr))
def printLines(lines: Seq[String]) = lines foreach println
def lastGrep(scope: Scope, key: AttributeKey[_], mgr: Streams, patternString: String)
{
val pattern = Pattern.compile(patternString)
printLines(lastLines(ScopedKey(scope,key), mgr).flatMap(showMatches(pattern)) )
}
def lastLines(key: ScopedKey[_], mgr: Streams): Seq[String] =
mgr.use(key) { s => IO.readLines(s.readText( Project.fillTaskAxis(key) )) }
}

View File

@ -94,6 +94,9 @@ object Project extends Init[Scope]
def display(scoped: ScopedKey[_]): String = Scope.display(scoped.scope, scoped.key.label)
def display(ref: ProjectRef): String = "(" + (ref.uri map (_.toString) getOrElse "<this>") + ")" + (ref.id getOrElse "<root>")
def fillTaskAxis(scoped: ScopedKey[_]): ScopedKey[_] =
ScopedKey(Scope.fillTaskAxis(scoped.scope, scoped.key), scoped.key)
def mapScope(f: Scope => Scope) = new (ScopedKey ~> ScopedKey) { def apply[T](key: ScopedKey[T]) =
ScopedKey( f(key.scope), key.key)
}