mirror of https://github.com/sbt/sbt.git
Merge pull request #6870 from eatkins/watch-on-termination
Restore watchOnTermination callbacks
This commit is contained in:
commit
c00a2eacf2
|
|
@ -1128,7 +1128,28 @@ private[sbt] object Continuous extends DeprecatedContinuous {
|
||||||
val callbacks: Callbacks,
|
val callbacks: Callbacks,
|
||||||
val dynamicInputs: mutable.Set[DynamicInput],
|
val dynamicInputs: mutable.Set[DynamicInput],
|
||||||
val pending: Boolean,
|
val pending: Boolean,
|
||||||
|
var failAction: Option[Watch.Action],
|
||||||
) {
|
) {
|
||||||
|
def this(
|
||||||
|
count: Int,
|
||||||
|
commands: Seq[String],
|
||||||
|
beforeCommandImpl: (State, mutable.Set[DynamicInput]) => State,
|
||||||
|
afterCommand: State => State,
|
||||||
|
afterWatch: State => State,
|
||||||
|
callbacks: Callbacks,
|
||||||
|
dynamicInputs: mutable.Set[DynamicInput],
|
||||||
|
pending: Boolean,
|
||||||
|
) = this(
|
||||||
|
count,
|
||||||
|
commands,
|
||||||
|
beforeCommandImpl,
|
||||||
|
afterCommand,
|
||||||
|
afterWatch,
|
||||||
|
callbacks,
|
||||||
|
dynamicInputs,
|
||||||
|
pending,
|
||||||
|
None
|
||||||
|
)
|
||||||
def beforeCommand(state: State): State = beforeCommandImpl(state, dynamicInputs)
|
def beforeCommand(state: State): State = beforeCommandImpl(state, dynamicInputs)
|
||||||
def incremented: ContinuousState = withCount(count + 1)
|
def incremented: ContinuousState = withCount(count + 1)
|
||||||
def withPending(p: Boolean) =
|
def withPending(p: Boolean) =
|
||||||
|
|
@ -1323,7 +1344,8 @@ private[sbt] object ContinuousCommands {
|
||||||
case Watch.Prompt => stop.map(_ :: s"$PromptChannel ${channel.name}" :: Nil mkString ";")
|
case Watch.Prompt => stop.map(_ :: s"$PromptChannel ${channel.name}" :: Nil mkString ";")
|
||||||
case Watch.Run(commands) =>
|
case Watch.Run(commands) =>
|
||||||
stop.map(_ +: commands.map(_.commandLine).filter(_.nonEmpty) mkString "; ")
|
stop.map(_ +: commands.map(_.commandLine).filter(_.nonEmpty) mkString "; ")
|
||||||
case Watch.HandleError(_) =>
|
case a @ Watch.HandleError(_) =>
|
||||||
|
cs.failAction = Some(a)
|
||||||
stop.map(_ :: s"$failWatch ${channel.name}" :: Nil mkString "; ")
|
stop.map(_ :: s"$failWatch ${channel.name}" :: Nil mkString "; ")
|
||||||
case _ => stop
|
case _ => stop
|
||||||
}
|
}
|
||||||
|
|
@ -1353,7 +1375,8 @@ private[sbt] object ContinuousCommands {
|
||||||
}
|
}
|
||||||
cs.afterCommand(postState)
|
cs.afterCommand(postState)
|
||||||
}
|
}
|
||||||
private[sbt] val stopWatchCommand = watchCommand(stopWatch) { (channel, state) =>
|
private[this] val exitWatchShared = (error: Boolean) =>
|
||||||
|
(channel: String, state: State) =>
|
||||||
state.get(watchStates).flatMap(_.get(channel)) match {
|
state.get(watchStates).flatMap(_.get(channel)) match {
|
||||||
case Some(cs) =>
|
case Some(cs) =>
|
||||||
val afterWatchState = cs.afterWatch(state)
|
val afterWatchState = cs.afterWatch(state)
|
||||||
|
|
@ -1364,16 +1387,19 @@ private[sbt] object ContinuousCommands {
|
||||||
c.terminal.setPrompt(Prompt.Pending)
|
c.terminal.setPrompt(Prompt.Pending)
|
||||||
c.unprompt(ConsoleUnpromptEvent(Some(CommandSource(channel))))
|
c.unprompt(ConsoleUnpromptEvent(Some(CommandSource(channel))))
|
||||||
}
|
}
|
||||||
afterWatchState.get(watchStates) match {
|
val newState = afterWatchState.get(watchStates) match {
|
||||||
case None => afterWatchState
|
case None => afterWatchState
|
||||||
case Some(w) => afterWatchState.put(watchStates, w - channel)
|
case Some(w) => afterWatchState.put(watchStates, w - channel)
|
||||||
}
|
}
|
||||||
case _ => state
|
val commands = cs.commands.mkString("; ")
|
||||||
}
|
val count = cs.count
|
||||||
}
|
val action = cs.failAction.getOrElse(Watch.CancelWatch)
|
||||||
private[sbt] val failWatchCommand = watchCommand(failWatch) { (channel, state) =>
|
val st = cs.callbacks.onTermination(action, commands, count, newState)
|
||||||
state.fail
|
if (error) st.fail else st
|
||||||
|
case _ => if (error) state.fail else state
|
||||||
}
|
}
|
||||||
|
private[sbt] val stopWatchCommand = watchCommand(stopWatch)(exitWatchShared(false))
|
||||||
|
private[sbt] val failWatchCommand = watchCommand(failWatch)(exitWatchShared(true))
|
||||||
/*
|
/*
|
||||||
* Creates a FileTreeRepository where it is safe to call close without inadvertently cancelling
|
* Creates a FileTreeRepository where it is safe to call close without inadvertently cancelling
|
||||||
* still active watches.
|
* still active watches.
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
import sbt.watch.task.Build
|
||||||
|
|
||||||
|
val root = Build.root
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
# This tests that we can override the state transformation in the watch task
|
||||||
|
# In the build, watchOnEvent should return CancelWatch which should be successful, but we
|
||||||
|
# override watchTasks to fail the state instead
|
||||||
|
|
||||||
|
-> ~ root / setStringValue foo.txt bar
|
||||||
|
|
||||||
|
> checkStringValue foo.txt bar
|
||||||
|
|
@ -1,3 +1,15 @@
|
||||||
import sbt.watch.task.Build
|
watchOnIteration := { (count, project, commands) =>
|
||||||
|
Watch.CancelWatch
|
||||||
val root = Build.root
|
}
|
||||||
|
watchOnTermination := { (action, count, command, state) =>
|
||||||
|
action match {
|
||||||
|
case Watch.CancelWatch =>
|
||||||
|
java.nio.file.Files.delete(java.nio.file.Paths.get("foo.txt"))
|
||||||
|
case Watch.HandleError(e) =>
|
||||||
|
if (e.getMessage == "fail")
|
||||||
|
java.nio.file.Files.delete(java.nio.file.Paths.get("bar.txt"))
|
||||||
|
else
|
||||||
|
throw new IllegalStateException("unexpected error")
|
||||||
|
}
|
||||||
|
state
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,8 @@
|
||||||
# This tests that we can override the state transformation in the watch task
|
$ exists foo.txt
|
||||||
# In the build, watchOnEvent should return CancelWatch which should be successful, but we
|
> ~compile
|
||||||
# override watchTasks to fail the state instead
|
$ absent foo.txt
|
||||||
|
> set watchOnIteration := { (_, _, _) => new Watch.HandleError(new IllegalStateException("fail")) }
|
||||||
|
$ exists bar.txt
|
||||||
|
-> ~compile
|
||||||
|
$ absent bar.txt
|
||||||
|
|
||||||
-> ~ root / setStringValue foo.txt bar
|
|
||||||
|
|
||||||
> checkStringValue foo.txt bar
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue