Skip to content

Commit

Permalink
Move some server pieces from main-command to main
Browse files Browse the repository at this point in the history
  • Loading branch information
dwijnand committed Jan 20, 2017
1 parent bf12a99 commit e83564a
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 35 deletions.
13 changes: 1 addition & 12 deletions main-command/src/main/scala/sbt/BasicCommands.scala
Expand Up @@ -21,7 +21,7 @@ import scala.util.control.NonFatal

object BasicCommands {
lazy val allBasicCommands = Seq(nop, ignore, help, completionsCommand, multi, ifLast, append, setOnFailure, clearOnFailure,
stashOnFailure, popOnFailure, reboot, call, early, exit, continuous, history, shell, server, client, read, alias) ++ compatCommands
stashOnFailure, popOnFailure, reboot, call, early, exit, continuous, history, shell, client, read, alias) ++ compatCommands

def nop = Command.custom(s => success(() => s))
def ignore = Command.command(FailureWall)(idFun)
Expand Down Expand Up @@ -193,17 +193,6 @@ object BasicCommands {
}
}

def server = Command.command(Server, Help.more(Server, ServerDetailed)) { s0 =>
val exchange = State.exchange
val s1 = exchange.run(s0)
exchange.publishEventMessage(ConsolePromptEvent(s0))
val exec: Exec = exchange.blockUntilNextExec
val newState = s1.copy(onFailure = Some(Exec(Server, None)), remainingCommands = exec +: Exec(Server, None) +: s1.remainingCommands).setInteractive(true)
exchange.publishEventMessage(ConsoleUnpromptEvent(exec.source))
if (exec.commandLine.trim.isEmpty) newState
else newState.clearGlobalLog
}

def client = Command.make(Client, Help.more(Client, ClientDetailed))(clientParser)
def clientParser(s0: State) =
{
Expand Down
15 changes: 0 additions & 15 deletions main-command/src/main/scala/sbt/Command.scala
Expand Up @@ -88,21 +88,6 @@ object Command {
}
}

/** This is the main function State transfer function of the sbt command processing, called by MainLoop.next, */
def process(exec: Exec, state: State): State =
{
val channelName = exec.source map { _.channelName }
State.exchange.publishEventMessage(ExecStatusEvent("Processing", channelName, exec.execId, Vector()))
val parser = combine(state.definedCommands)
val newState = parse(exec.commandLine, parser(state)) match {
case Right(s) => s() // apply command. command side effects happen here
case Left(errMsg) =>
state.log.error(errMsg)
state.fail
}
State.exchange.publishEventMessage(ExecStatusEvent("Done", channelName, exec.execId, newState.remainingCommands.toVector map { _.commandLine }))
newState
}

This comment has been minimized.

Copy link
@eed3si9n

eed3si9n Jul 28, 2017

Member

This should be mentioned in the migration guide. Can people use

"cmd" :: state

def invalidValue(label: String, allowed: Iterable[String])(value: String): String =
"Not a valid " + label + ": " + value + similar(value, allowed)
def similar(value: String, allowed: Iterable[String]): String =
Expand Down
3 changes: 0 additions & 3 deletions main-command/src/main/scala/sbt/State.scala
Expand Up @@ -8,7 +8,6 @@ import java.util.concurrent.Callable
import sbt.util.Logger
import sbt.internal.util.{ AttributeKey, AttributeMap, ErrorHandling, ExitHook, ExitHooks, GlobalLogging }
import sbt.internal.util.complete.HistoryCommands
import sbt.internal.CommandExchange
import sbt.internal.inc.classpath.ClassLoaderCache

/**
Expand Down Expand Up @@ -195,8 +194,6 @@ object State {
)
}

private[sbt] lazy val exchange = new CommandExchange()

/** Provides operations and transformations on State. */
implicit def stateOps(s: State): StateOps = new StateOps {
def process(f: (Exec, State) => State): State =
Expand Down
17 changes: 16 additions & 1 deletion main/src/main/scala/sbt/Main.scala
Expand Up @@ -8,6 +8,7 @@ import sbt.internal.{
Aggregation,
BuildStructure,
BuildUnit,
CommandExchange,
CommandStrings,
EvaluateConfigurations,
Inspect,
Expand Down Expand Up @@ -78,6 +79,8 @@ final class ConsoleMain extends xsbti.AppMain {
}

object StandardMain {
private[sbt] lazy val exchange = new CommandExchange()

def runManaged(s: State): xsbti.MainResult =
{
val previous = TrapExit.installManager()
Expand Down Expand Up @@ -117,7 +120,7 @@ object BuiltinCommands {
def DefaultCommands: Seq[Command] = Seq(ignore, help, completionsCommand, about, tasks, settingsCommand, loadProject, templateCommand,
projects, project, reboot, read, history, set, sessionCommand, inspect, loadProjectImpl, loadFailed, Cross.crossBuild, Cross.switchVersion,
Cross.crossRestoreSession, setOnFailure, clearOnFailure, stashOnFailure, popOnFailure, setLogLevel, plugin, plugins,
ifLast, multi, shell, BasicCommands.server, BasicCommands.client, continuous, eval, alias, append, last, lastGrep, export, boot, nop, call, exit, early, initialize, act) ++
ifLast, multi, shell, server, BasicCommands.client, continuous, eval, alias, append, last, lastGrep, export, boot, nop, call, exit, early, initialize, act) ++
compatCommands
def DefaultBootCommands: Seq[String] = LoadProject :: (IfLast + " " + Shell) :: Nil

Expand Down Expand Up @@ -544,4 +547,16 @@ object BuiltinCommands {
}
s.put(Keys.stateCompilerCache, cache)
}

def server = Command.command(Server, Help.more(Server, ServerDetailed)) { s0 =>
import sbt.internal.{ ConsolePromptEvent, ConsoleUnpromptEvent }
val exchange = StandardMain.exchange
val s1 = exchange run s0
exchange publishEventMessage ConsolePromptEvent(s0)
val exec: Exec = exchange.blockUntilNextExec
val newState = s1.copy(onFailure = Some(Exec(Server, None)), remainingCommands = exec +: Exec(Server, None) +: s1.remainingCommands).setInteractive(true)
exchange publishEventMessage ConsoleUnpromptEvent(exec.source)
if (exec.commandLine.trim.isEmpty) newState
else newState.clearGlobalLog
}
}
Expand Up @@ -9,7 +9,9 @@ import jline.TerminalFactory

import sbt.io.Using
import sbt.internal.util.{ ErrorHandling, GlobalLogBacking, GlobalLogging }
import sbt.internal.util.complete.DefaultParsers
import sbt.util.{ AbstractLogger, Logger }
import sbt.protocol._

object MainLoop {
/** Entry point to run the remaining commands in State with managed global logging.*/
Expand Down Expand Up @@ -98,12 +100,28 @@ object MainLoop {
}

def next(state: State): State =
ErrorHandling.wideConvert { state.process(Command.process) } match {
ErrorHandling.wideConvert { state.process(processCommand) } match {
case Right(s) => s
case Left(t: xsbti.FullReload) => throw t
case Left(t) => state.handleError(t)
}

/** This is the main function State transfer function of the sbt command processing. */
def processCommand(exec: Exec, state: State): State = {
import DefaultParsers._
val channelName = exec.source map (_.channelName)
StandardMain.exchange publishEventMessage ExecStatusEvent("Processing", channelName, exec.execId, Vector())
val parser = Command combine state.definedCommands
val newState = parse(exec.commandLine, parser(state)) match {
case Right(s) => s() // apply command. command side effects happen here
case Left(errMsg) =>
state.log error errMsg
state.fail
}
StandardMain.exchange publishEventMessage ExecStatusEvent("Done", channelName, exec.execId, newState.remainingCommands.toVector map (_.commandLine))
newState
}

@deprecated("Use State.handleError", "0.13.0")
def handleException(e: Throwable, s: State): State = s.handleError(e)

Expand Down
Expand Up @@ -26,10 +26,10 @@ class RelayAppender(name: String) extends AbstractAppender(name, null, PatternLa
}
}
def appendLog(level: Level.Value, message: => String): Unit = {
State.exchange.publishEventMessage(LogEvent(level.toString, message))
StandardMain.exchange.publishEventMessage(LogEvent(level.toString, message))
}
def appendEvent(level: Level.Value, event: AnyRef): Unit =
event match {
case x: ChannelLogEntry => State.exchange.publishEvent(x: AbstractEntry)
case x: ChannelLogEntry => StandardMain.exchange.publishEvent(x: AbstractEntry)
}
}
2 changes: 1 addition & 1 deletion main/src/test/scala/PluginCommandTest.scala
Expand Up @@ -52,7 +52,7 @@ object FakeState {
try {
System.setOut(new PrintStream(outBuffer, true))
val state = FakeState(enabledPlugins: _*)
Command.process(Exec(input, None), state)
MainLoop.processCommand(Exec(input, None), state)
new String(outBuffer.toByteArray)
} finally {
System.setOut(previousOut)
Expand Down

0 comments on commit e83564a

Please sign in to comment.