diff --git a/main-command/src/main/scala/sbt/BasicCommandStrings.scala b/main-command/src/main/scala/sbt/BasicCommandStrings.scala index 6d7b6c52e..efa853903 100644 --- a/main-command/src/main/scala/sbt/BasicCommandStrings.scala +++ b/main-command/src/main/scala/sbt/BasicCommandStrings.scala @@ -161,6 +161,12 @@ $AliasCommand name= def ShellDetailed = "Provides an interactive prompt and network server from which commands can be run." + def StartServer = "startServer" + def StartServerDetailed = + s"""$StartServer + Starts the server if it has not been started. This is intended to be used with + -Dsbt.server.autostart=false.""" + def OldShell = "oldshell" def OldShellDetailed = "Provides an interactive prompt from which commands can be run." diff --git a/main/src/main/scala/sbt/Main.scala b/main/src/main/scala/sbt/Main.scala index 493f88117..f4043f989 100644 --- a/main/src/main/scala/sbt/Main.scala +++ b/main/src/main/scala/sbt/Main.scala @@ -195,6 +195,7 @@ object BuiltinCommands { multi, shell, oldshell, + startServer, BasicCommands.client, continuous, eval, @@ -737,6 +738,12 @@ object BuiltinCommands { else newState.clearGlobalLog } + def startServer: Command = + Command.command(StartServer, Help.more(StartServer, StartServerDetailed)) { s0 => + val exchange = StandardMain.exchange + exchange.runServer(s0) + } + private val sbtVersionRegex = """sbt\.version\s*=.*""".r private def isSbtVersionLine(s: String) = sbtVersionRegex.pattern matcher s matches () diff --git a/main/src/main/scala/sbt/internal/CommandExchange.scala b/main/src/main/scala/sbt/internal/CommandExchange.scala index 8cc17a909..d56edb245 100644 --- a/main/src/main/scala/sbt/internal/CommandExchange.scala +++ b/main/src/main/scala/sbt/internal/CommandExchange.scala @@ -23,6 +23,9 @@ import scala.util.{ Success, Failure } * this exchange, which could serve command request from either of the channel. */ private[sbt] final class CommandExchange { + private val autoStartServer = sys.props.get("sbt.server.autostart") map { + _.toLowerCase == "true" + } getOrElse true private val lock = new AnyRef {} private var server: Option[ServerInstance] = None private var consoleChannel: Option[ConsoleChannel] = None @@ -61,13 +64,17 @@ private[sbt] final class CommandExchange { consoleChannel = Some(x) subscribe(x) } - runServer(s) + if (autoStartServer) runServer(s) + else s } private def newChannelName: String = s"channel-${nextChannelId.incrementAndGet()}" - private def runServer(s: State): State = { - val port = (s get serverPort) match { + /** + * Check if a server instance is running already, and start one if it isn't. + */ + private[sbt] def runServer(s: State): State = { + def port = (s get serverPort) match { case Some(x) => x case None => 5001 } diff --git a/notes/1.0.2.markdown b/notes/1.0.2.markdown index 433965dc4..44cad0df7 100644 --- a/notes/1.0.2.markdown +++ b/notes/1.0.2.markdown @@ -11,14 +11,16 @@ This is a hotfix release for sbt 1.0.x series. - Fixes sbt server missing some messages. [#3523][3523] by [@guillaumebort][@guillaumebort] - Refixes `consoleProject`. [zinc#386][zinc386] by [@dwijnand][@dwijnand] - Adds JVM flag `sbt.gigahorse` to enable/disable the internal use of Gigahorse to workaround NPE in `JavaNetAuthenticator` when used in conjunction with `repositories` override. [lm#167][lm167] by [@cunei][@cunei] +- Adds JVM flag `sbt.server.autostart` to enable/disable the automatic starting of sbt server with the sbt shell. This also adds new `startServer` command to manually start the server. by [@eed3si9n][@eed3si9n] ### Internal - Fixes unused import warnings. [#3533][3533] by [@razvan-panda][@razvan-panda] - [@dpratt]: https://github.com/dpratt [@dwijnand]: https://github.com/dwijnand [@cunei]: https://github.com/cunei + [@eed3si9n]: https://github.com/eed3si9n + [@dpratt]: https://github.com/dpratt [@kczulko]: https://github.com/kczulko [@razvan-panda]: https://github.com/razvan-panda [@guillaumebort]: https://github.com/guillaumebort