implement -client option

This commit is contained in:
Eugene Yokota 2018-06-24 23:37:28 -04:00
parent 18c6b04b47
commit 1a1f530985
5 changed files with 34 additions and 16 deletions

View File

@ -194,6 +194,8 @@ $AliasCommand name=
def Client = "client" def Client = "client"
def ClientDetailed = "Provides an interactive prompt from which commands can be run on a server." def ClientDetailed = "Provides an interactive prompt from which commands can be run on a server."
def DashClient = "-client"
def DashDashClient = "--client"
def StashOnFailure = "sbtStashOnFailure" def StashOnFailure = "sbtStashOnFailure"
def PopOnFailure = "sbtPopOnFailure" def PopOnFailure = "sbtPopOnFailure"

View File

@ -297,7 +297,7 @@ object BasicCommands {
case e :: Nil if e.commandLine == "shell" => Nil case e :: Nil if e.commandLine == "shell" => Nil
case xs => xs map (_.commandLine) case xs => xs map (_.commandLine)
}) })
NetworkClient.run(arguments) NetworkClient.run(s0.configuration.baseDirectory, arguments)
"exit" :: s0.copy(remainingCommands = Nil) "exit" :: s0.copy(remainingCommands = Nil)
} }

View File

@ -15,6 +15,7 @@ import sbt.internal.util.complete.Parser
import sbt.internal.util.complete.DefaultParsers._ import sbt.internal.util.complete.DefaultParsers._
import sbt.io.IO import sbt.io.IO
import sbt.io.syntax._
object CommandUtil { object CommandUtil {
def readLines(files: Seq[File]): Seq[String] = def readLines(files: Seq[File]): Seq[String] =
@ -89,4 +90,7 @@ object CommandUtil {
details.map { case (k, v) => k + "\n\n " + v } mkString ("\n", "\n\n", "\n") details.map { case (k, v) => k + "\n\n " + v } mkString ("\n", "\n\n", "\n")
final val HelpPatternFlags = Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE final val HelpPatternFlags = Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE
private[sbt] def isSbtBuild(baseDir: File) =
(baseDir / "project").exists() || (baseDir * "*.sbt").get.nonEmpty
} }

View File

@ -24,7 +24,7 @@ import sbt.io.syntax._
import sbt.io.IO import sbt.io.IO
import sjsonnew.support.scalajson.unsafe.Converter import sjsonnew.support.scalajson.unsafe.Converter
class NetworkClient(arguments: List[String]) { self => class NetworkClient(baseDirectory: File, arguments: List[String]) { self =>
private val channelName = new AtomicReference("_") private val channelName = new AtomicReference("_")
private val status = new AtomicReference("Ready") private val status = new AtomicReference("Ready")
private val lock: AnyRef = new AnyRef {} private val lock: AnyRef = new AnyRef {}
@ -39,7 +39,7 @@ class NetworkClient(arguments: List[String]) { self =>
// Open server connection based on the portfile // Open server connection based on the portfile
def init(): ServerConnection = { def init(): ServerConnection = {
val portfile = (file("project") / "target" / "active.json").getAbsoluteFile val portfile = baseDirectory / "project" / "target" / "active.json"
if (!portfile.exists) sys.error("server does not seem to be running.") if (!portfile.exists) sys.error("server does not seem to be running.")
val (sk, tkn) = ClientSocket.socket(portfile) val (sk, tkn) = ClientSocket.socket(portfile)
val conn = new ServerConnection(sk) { val conn = new ServerConnection(sk) {
@ -190,9 +190,9 @@ class NetworkClient(arguments: List[String]) { self =>
} }
object NetworkClient { object NetworkClient {
def run(arguments: List[String]): Unit = def run(baseDirectory: File, arguments: List[String]): Unit =
try { try {
new NetworkClient(arguments) new NetworkClient(baseDirectory, arguments)
() ()
} catch { } catch {
case NonFatal(e) => println(e.getMessage) case NonFatal(e) => println(e.getMessage)

View File

@ -64,15 +64,30 @@ import CommandStrings.BootCommand
final class xMain extends xsbti.AppMain { final class xMain extends xsbti.AppMain {
def run(configuration: xsbti.AppConfiguration): xsbti.MainResult = { def run(configuration: xsbti.AppConfiguration): xsbti.MainResult = {
import BasicCommands.early import BasicCommands.early
import BasicCommandStrings.runEarly import BasicCommandStrings.{ runEarly, DashClient, DashDashClient }
import BuiltinCommands.defaults import BuiltinCommands.defaults
import sbt.internal.CommandStrings.{ BootCommand, DefaultsCommand, InitCommand } import sbt.internal.CommandStrings.{ BootCommand, DefaultsCommand, InitCommand }
val state = StandardMain.initialState( import sbt.internal.client.NetworkClient
configuration,
Seq(defaults, early), // if we detect -Dsbt.client=true or -client, run thin client.
runEarly(DefaultsCommand) :: runEarly(InitCommand) :: BootCommand :: Nil val clientModByEnv = java.lang.Boolean.getBoolean("sbt.client")
) val userCommands = configuration.arguments.map(_.trim)
StandardMain.runManaged(state) if (clientModByEnv || (userCommands.exists { cmd =>
(cmd == DashClient) || (cmd == DashDashClient)
})) {
val args = userCommands.toList filterNot { cmd =>
(cmd == DashClient) || (cmd == DashDashClient)
}
NetworkClient.run(configuration.baseDirectory, args)
Exit(0)
} else {
val state = StandardMain.initialState(
configuration,
Seq(defaults, early),
runEarly(DefaultsCommand) :: runEarly(InitCommand) :: BootCommand :: Nil
)
StandardMain.runManaged(state)
}
} }
} }
@ -861,9 +876,6 @@ object BuiltinCommands {
private val sbtVersionRegex = """sbt\.version\s*=.*""".r private val sbtVersionRegex = """sbt\.version\s*=.*""".r
private def isSbtVersionLine(s: String) = sbtVersionRegex.pattern matcher s matches () private def isSbtVersionLine(s: String) = sbtVersionRegex.pattern matcher s matches ()
private def isSbtProject(baseDir: File, projectDir: File) =
projectDir.exists() || (baseDir * "*.sbt").get.nonEmpty
private def writeSbtVersionUnconditionally(state: State) = { private def writeSbtVersionUnconditionally(state: State) = {
val baseDir = state.baseDir val baseDir = state.baseDir
val sbtVersion = BuiltinCommands.sbtVersion(state) val sbtVersion = BuiltinCommands.sbtVersion(state)
@ -877,7 +889,7 @@ object BuiltinCommands {
if (sbtVersionAbsent) { if (sbtVersionAbsent) {
val warnMsg = s"No sbt.version set in project/build.properties, base directory: $baseDir" val warnMsg = s"No sbt.version set in project/build.properties, base directory: $baseDir"
try { try {
if (isSbtProject(baseDir, projectDir)) { if (isSbtBuild(baseDir)) {
val line = s"sbt.version=$sbtVersion" val line = s"sbt.version=$sbtVersion"
IO.writeLines(buildProps, line :: buildPropsLines) IO.writeLines(buildProps, line :: buildPropsLines)
state.log info s"Updated file $buildProps: set sbt.version to $sbtVersion" state.log info s"Updated file $buildProps: set sbt.version to $sbtVersion"