Move executeContinuously into LegacyWatched

This helps keep Watched.scala more manageable until we can potentially
remove this code.
This commit is contained in:
Ethan Atkins 2018-08-23 18:43:07 -07:00
parent 28aa1de32a
commit 7764dc42ae
3 changed files with 76 additions and 45 deletions

View File

@ -10,9 +10,15 @@ package sbt
import java.io.File
import java.nio.file.FileSystems
import sbt.BasicCommandStrings._
import sbt.BasicCommandStrings.{
ContinuousExecutePrefix,
FailureWall,
continuousBriefHelp,
continuousDetail
}
import sbt.BasicCommands.otherCommandParser
import sbt.CommandUtil.withAttribute
import sbt.internal.LegacyWatched
import sbt.internal.io.{ EventMonitor, Source, WatchState }
import sbt.internal.util.AttributeKey
import sbt.internal.util.Types.const
@ -227,48 +233,8 @@ object Watched {
}
@deprecated("Replaced by Watched.command", "1.3.0")
def executeContinuously(watched: Watched, s: State, next: String, repeat: String): State = {
@tailrec def shouldTerminate: Boolean =
(System.in.available > 0) && (watched.terminateWatch(System.in.read()) || shouldTerminate)
val log = s.log
val logger = new EventMonitor.Logger {
override def debug(msg: => Any): Unit = log.debug(msg.toString)
}
s get ContinuousEventMonitor match {
case None =>
// This is the first iteration, so run the task and create a new EventMonitor
(ClearOnFailure :: next :: FailureWall :: repeat :: s)
.put(
ContinuousEventMonitor,
EventMonitor(
WatchState.empty(watched.watchService(), watched.watchSources(s)),
watched.pollInterval,
watched.antiEntropy,
shouldTerminate,
logger
)
)
case Some(eventMonitor) =>
printIfDefined(watched watchingMessage eventMonitor.state)
val triggered = try eventMonitor.awaitEvent()
catch {
case e: Exception =>
log.error(
"Error occurred obtaining files to watch. Terminating continuous execution..."
)
s.handleError(e)
false
}
if (triggered) {
printIfDefined(watched triggeredMessage eventMonitor.state)
ClearOnFailure :: next :: FailureWall :: repeat :: s
} else {
while (System.in.available() > 0) System.in.read()
eventMonitor.close()
s.remove(ContinuousEventMonitor)
}
}
}
def executeContinuously(watched: Watched, s: State, next: String, repeat: String): State =
LegacyWatched.executeContinuously(watched, s, next, repeat)
private[sbt] object NullLogger extends Logger {
override def trace(t: => Throwable): Unit = {}

View File

@ -0,0 +1,64 @@
/*
* sbt
* Copyright 2011 - 2018, Lightbend, Inc.
* Copyright 2008 - 2010, Mark Harrah
* Licensed under Apache License 2.0 (see LICENSE)
*/
package sbt.internal
import sbt.BasicCommandStrings.{ ClearOnFailure, FailureWall }
import sbt.internal.io.{ EventMonitor, WatchState }
import sbt.{ State, Watched }
import scala.annotation.tailrec
import Watched.ContinuousEventMonitor
import scala.util.control.NonFatal
private[sbt] object LegacyWatched {
@deprecated("Replaced by Watched.command", "1.3.0")
def executeContinuously(watched: Watched, s: State, next: String, repeat: String): State = {
@tailrec def shouldTerminate: Boolean =
(System.in.available > 0) && (watched.terminateWatch(System.in.read()) || shouldTerminate)
val log = s.log
val logger = new EventMonitor.Logger {
override def debug(msg: => Any): Unit = log.debug(msg.toString)
}
s get ContinuousEventMonitor match {
case None =>
// This is the first iteration, so run the task and create a new EventMonitor
(ClearOnFailure :: next :: FailureWall :: repeat :: s)
.put(
ContinuousEventMonitor,
EventMonitor(
WatchState.empty(watched.watchService(), watched.watchSources(s)),
watched.pollInterval,
watched.antiEntropy,
shouldTerminate,
logger
)
)
case Some(eventMonitor) =>
Watched.printIfDefined(watched watchingMessage eventMonitor.state)
val triggered = try eventMonitor.awaitEvent()
catch {
case NonFatal(e) =>
log.error(
"Error occurred obtaining files to watch. Terminating continuous execution..."
)
s.handleError(e)
false
}
if (triggered) {
Watched.printIfDefined(watched triggeredMessage eventMonitor.state)
ClearOnFailure :: next :: FailureWall :: repeat :: s
} else {
while (System.in.available() > 0) System.in.read()
eventMonitor.close()
s.remove(ContinuousEventMonitor)
}
}
}
}

View File

@ -11,7 +11,7 @@ import java.io.File
import java.util.concurrent.CountDownLatch
import org.scalatest.{ FlatSpec, Matchers }
import sbt.Watched.{ WatchConfig, NullLogger, WatchSource }
import sbt.Watched.{ NullLogger, WatchSource }
import sbt.internal.io.WatchState
import sbt.io.IO
@ -26,7 +26,8 @@ class WatchedSpec extends FlatSpec with Matchers {
() => latch.getCount == 0,
triggeredMessage = _ => { latch.countDown(); None },
watchingMessage = _ => { new File(dir, "foo").createNewFile(); None },
watchState = WatchState.empty(Watched.createWatchService, WatchSource(dir.toRealPath) :: Nil),
watchState =
WatchState.empty(Watched.createWatchService(), WatchSource(dir.toRealPath) :: Nil),
pollInterval = 5.millis,
antiEntropy = 5.millis
)