From 7764dc42ae1b94323d53a4e6edf9ea64a3c30b8a Mon Sep 17 00:00:00 2001 From: Ethan Atkins Date: Thu, 23 Aug 2018 18:43:07 -0700 Subject: [PATCH] Move executeContinuously into LegacyWatched This helps keep Watched.scala more manageable until we can potentially remove this code. --- main-command/src/main/scala/sbt/Watched.scala | 52 +++------------ .../scala/sbt/internal/LegacyWatched.scala | 64 +++++++++++++++++++ .../src/test/scala/sbt/WatchedSpec.scala | 5 +- 3 files changed, 76 insertions(+), 45 deletions(-) create mode 100644 main-command/src/main/scala/sbt/internal/LegacyWatched.scala diff --git a/main-command/src/main/scala/sbt/Watched.scala b/main-command/src/main/scala/sbt/Watched.scala index dcddda9d6..3d80f64ca 100644 --- a/main-command/src/main/scala/sbt/Watched.scala +++ b/main-command/src/main/scala/sbt/Watched.scala @@ -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 = {} diff --git a/main-command/src/main/scala/sbt/internal/LegacyWatched.scala b/main-command/src/main/scala/sbt/internal/LegacyWatched.scala new file mode 100644 index 000000000..624372efc --- /dev/null +++ b/main-command/src/main/scala/sbt/internal/LegacyWatched.scala @@ -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) + } + } + } + +} diff --git a/main-command/src/test/scala/sbt/WatchedSpec.scala b/main-command/src/test/scala/sbt/WatchedSpec.scala index 6f47f8880..5add4ba86 100644 --- a/main-command/src/test/scala/sbt/WatchedSpec.scala +++ b/main-command/src/test/scala/sbt/WatchedSpec.scala @@ -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 )