From c195f943c0afacecc8299da4a650e7c6b179a327 Mon Sep 17 00:00:00 2001 From: Mark Harrah Date: Wed, 27 Jul 2011 19:50:59 -0400 Subject: [PATCH] aggregate 'last'. fixes #131 --- main/Main.scala | 2 +- main/Output.scala | 38 +++++++++++++++++++++++++++++++------- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/main/Main.scala b/main/Main.scala index 79a088baf..470a8a1bd 100644 --- a/main/Main.scala +++ b/main/Main.scala @@ -342,7 +342,7 @@ object BuiltinCommands } def extractLast(s: State) = { val ext = Project.extract(s) - (ext.structure.streams, Select(ext.currentRef)) + (ext.structure, Select(ext.currentRef)) } def inspectParser = (s: State) => token((Space ~> ("actual" ^^^ true)) ?? false) ~ spacedKeyParser(s) val spacedKeyParser = (s: State) => Act.requireSession(s, token(Space) ~> Act.scopedKeyParser(s)) diff --git a/main/Output.scala b/main/Output.scala index cc4624e7f..8946c6c58 100644 --- a/main/Output.scala +++ b/main/Output.scala @@ -7,7 +7,11 @@ package sbt import java.io.File import Keys.{Streams, TaskStreams} import Project.ScopedKey + import Load.BuildStructure + import Aggregation.{getTasks, KeyValue} + import Types.idFun import annotation.tailrec + import scala.Console.{BOLD, RESET} object Output { @@ -26,18 +30,38 @@ object Output } final val DefaultTail = "> " - def last(key: ScopedKey[_], mgr: Streams): Unit = printLines(lastLines(key, mgr)) + def last(key: ScopedKey[_], structure: BuildStructure): Unit = printLines( flatLines(lastLines(key, structure))(idFun) ) def last(file: File, tailDelim: String = DefaultTail): Unit = printLines(tailLines(file, tailDelim)) - def lastGrep(key: ScopedKey[_], mgr: Streams, patternString: String): Unit = - lastGrep(lastLines(key, mgr), patternString ) + def lastGrep(key: ScopedKey[_], structure: BuildStructure, patternString: String): Unit = + { + val pattern = Pattern compile patternString + val lines = flatLines( lastLines(key, structure) )(_ flatMap showMatches(pattern)) + printLines( lines ) + } def lastGrep(file: File, patternString: String, tailDelim: String = DefaultTail): Unit = - lastGrep( tailLines(file, tailDelim), patternString) - def lastGrep(lines: Seq[String], patternString: String): Unit = - printLines(lines flatMap showMatches(Pattern compile patternString)) + printLines(grep( tailLines(file, tailDelim), patternString) ) + def grep(lines: Seq[String], patternString: String): Seq[String] = + lines flatMap showMatches(Pattern compile patternString) + def flatLines(outputs: Seq[KeyValue[Seq[String]]])(f: Seq[String] => Seq[String]): Seq[String] = + { + val single = outputs.size == 1 + outputs flatMap { case KeyValue(key, lines) => + val flines = f(lines) + if(!single) bold(Project.display(key)) +: flines else flines + } + } def printLines(lines: Seq[String]) = lines foreach println - def lastLines(key: ScopedKey[_], mgr: Streams): Seq[String] = mgr.use(key) { s => IO.readLines(s.readText( Project.fillTaskAxis(key) )) } + def bold(s: String) = if(ConsoleLogger.formatEnabled) BOLD + s + RESET else s + + def lastLines(key: ScopedKey[_], structure: BuildStructure): Seq[KeyValue[Seq[String]]] = + { + val aggregated = Aggregation.getTasks(key, structure, true) + val outputs = aggregated map { case KeyValue(key, value) => KeyValue(key, lastLines(key, structure.streams)) } + outputs.filterNot(_.value.isEmpty) + } + def lastLines(key: ScopedKey[_], mgr: Streams): Seq[String] = mgr.use(key) { s => IO.readLines(s.readText( Project.fillTaskAxis(key) )) } def tailLines(file: File, tailDelim: String): Seq[String] = headLines(IO.readLines(file).reverse, tailDelim).reverse @tailrec def headLines(lines: Seq[String], tailDelim: String): Seq[String] = if(lines.isEmpty)