spray / sbt-revolver

An SBT plugin for dangerously fast development turnaround in Scala
Apache License 2.0
846 stars 55 forks source link

Call `reStop` after hitting enter #107

Closed mrdziuban closed 2 years ago

mrdziuban commented 2 years ago

First off, thank you for the amazing tool! I've been using it for a long time and it makes running applications in SBT so much better.

I'm wondering if there's any built-in way to call reStop automatically after hitting enter rather than leaving the application running? I was hoping SBT's watchOnTermination setting would let me do this, but it seems to not work as I expected -- sbt/sbt#6861 -- so I'm looking for other solutions. Any ideas are much appreciated!

raboof commented 2 years ago

I'm not sure I understand where you are 'hitting enter' - is that on the sbt prompt, or is your application waiting for you to hit 'enter', or something else?

mrdziuban commented 2 years ago

Yup, in SBT after running ~reStart I hit enter to stop triggered execution (~) and return to the SBT prompt. I'm wondering if there's a way to call reStop when this happens instead of leaving the application running.

raboof commented 2 years ago

Aah, gotcha! I can see how that would be useful, but I don't know a way to do that. Perhaps with a custom command? Not sure..

mrdziuban commented 2 years ago

Now that watchOnTermination has been fixed (sbt/sbt#6870) and released as part of 1.7.0, I was able to accomplish this globally in my build.sbt with this code:

ThisBuild / watchOnTermination := { (action, cmd, times, state) =>
  val projNames = cmd.split(";").flatMap(Some(_).filter(_.contains("/reStart")).flatMap(_.trim.split("/reStart") match {
    case Array(projName) => Some(projName)
    case _ => None
  }))
  projNames.foldLeft(state) { (acc, projName) =>
    val projRef = ProjectRef((ThisBuild / baseDirectory).value, projName)
    Project.extract(state).runTask(projRef / reStop, state)._1
  }
}

Edited to support when multiple projects are run simultaneously