mirror of https://github.com/sbt/sbt.git
[2.x] feat: Add English aliases cross and switch for + / ++ (#9051)
Fixes #3137. Adds discoverable English-name alternatives for the symbolic cross-build commands using the | parser combinator, making them more beginner-friendly without adding separate commands.
This commit is contained in:
parent
8bcc6ae420
commit
dd93c92ebc
|
|
@ -41,7 +41,7 @@ object Cross {
|
||||||
|
|
||||||
private def switchParser(state: State): Parser[Switch] = {
|
private def switchParser(state: State): Parser[Switch] = {
|
||||||
import DefaultParsers.*
|
import DefaultParsers.*
|
||||||
def versionAndCommand(spacePresent: Boolean) = {
|
def versionAndCommand(commandName: String)(spacePresent: Boolean) = {
|
||||||
val x = Project.extract(state)
|
val x = Project.extract(state)
|
||||||
import x.*
|
import x.*
|
||||||
val knownVersions = crossVersions(x, currentRef)
|
val knownVersions = crossVersions(x, currentRef)
|
||||||
|
|
@ -56,7 +56,7 @@ object Cross {
|
||||||
ScalaHomeVersion(new File(home), Some(v).filterNot(_.isEmpty), force)
|
ScalaHomeVersion(new File(home), Some(v).filterNot(_.isEmpty), force)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
val spacedVersion = if (spacePresent) version else version & spacedFirst(SwitchCommand)
|
val spacedVersion = if (spacePresent) version else version & spacedFirst(commandName)
|
||||||
val verboseOpt = Parser.opt(token(Space ~> "-v"))
|
val verboseOpt = Parser.opt(token(Space ~> "-v"))
|
||||||
// Accept valid commands, or project/command patterns that may reference projects
|
// Accept valid commands, or project/command patterns that may reference projects
|
||||||
// not yet available after version switch (fixes #7574)
|
// not yet available after version switch (fixes #7574)
|
||||||
|
|
@ -74,19 +74,24 @@ object Cross {
|
||||||
switch1 | switch2
|
switch1 | switch2
|
||||||
}
|
}
|
||||||
|
|
||||||
token(SwitchCommand ~> OptSpace) flatMap { sp =>
|
def parse(commandName: String) =
|
||||||
versionAndCommand(sp.nonEmpty)
|
token(commandName ~> OptSpace) flatMap { sp =>
|
||||||
}
|
versionAndCommand(commandName)(sp.nonEmpty)
|
||||||
|
}
|
||||||
|
parse(SwitchCommand) | parse(SwitchAlias)
|
||||||
}
|
}
|
||||||
|
|
||||||
private case class CrossArgs(command: String, verbose: Boolean)
|
private case class CrossArgs(command: String, verbose: Boolean)
|
||||||
|
|
||||||
private def crossParser(state: State): Parser[CrossArgs] =
|
private def crossParser(state: State): Parser[CrossArgs] = {
|
||||||
token(CrossCommand <~ OptSpace) flatMap { _ =>
|
def parse(commandName: String) =
|
||||||
(token(Parser.opt("-v" <~ Space)) ~ token(matched(state.combinedParser))).map {
|
token(commandName <~ OptSpace) flatMap { _ =>
|
||||||
(verbose, command) => CrossArgs(command, verbose.isDefined)
|
(token(Parser.opt("-v" <~ Space)) ~ token(matched(state.combinedParser))).map {
|
||||||
|
(verbose, command) => CrossArgs(command, verbose.isDefined)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
parse(CrossCommand) | parse(CrossAlias)
|
||||||
|
}
|
||||||
|
|
||||||
private def crossRestoreSessionParser: Parser[String] = token(CrossRestoreSessionCommand)
|
private def crossRestoreSessionParser: Parser[String] = token(CrossRestoreSessionCommand)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -331,15 +331,19 @@ defaults
|
||||||
Nil
|
Nil
|
||||||
|
|
||||||
val CrossCommand = "+"
|
val CrossCommand = "+"
|
||||||
|
val CrossAlias = "cross"
|
||||||
val CrossRestoreSessionCommand = "+-"
|
val CrossRestoreSessionCommand = "+-"
|
||||||
val SwitchCommand = "++"
|
val SwitchCommand = "++"
|
||||||
|
val SwitchAlias = "switch"
|
||||||
|
|
||||||
def crossHelp: Help = Help.more(CrossCommand, CrossDetailed)
|
def crossHelp: Help =
|
||||||
|
Help.more(CrossCommand, CrossDetailed) ++ Help.more(CrossAlias, CrossDetailed)
|
||||||
def crossRestoreSessionHelp = Help.more(CrossRestoreSessionCommand, CrossRestoreSessionDetailed)
|
def crossRestoreSessionHelp = Help.more(CrossRestoreSessionCommand, CrossRestoreSessionDetailed)
|
||||||
def switchHelp: Help = Help.more(SwitchCommand, SwitchDetailed)
|
def switchHelp: Help =
|
||||||
|
Help.more(SwitchCommand, SwitchDetailed) ++ Help.more(SwitchAlias, SwitchDetailed)
|
||||||
|
|
||||||
def CrossDetailed =
|
def CrossDetailed =
|
||||||
s"""$CrossCommand [-v] <command>
|
s"""$CrossCommand (or $CrossAlias) [-v] <command>
|
||||||
Runs <command> for each Scala version specified for cross-building.
|
Runs <command> for each Scala version specified for cross-building.
|
||||||
|
|
||||||
For each string in `crossScalaVersions` in each project project, this command sets
|
For each string in `crossScalaVersions` in each project project, this command sets
|
||||||
|
|
@ -359,7 +363,7 @@ defaults
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def SwitchDetailed =
|
def SwitchDetailed =
|
||||||
s"""$SwitchCommand <scala-version>[!] [-v] [<command>]
|
s"""$SwitchCommand (or $SwitchAlias) <scala-version>[!] [-v] [<command>]
|
||||||
Changes the Scala version and runs a command.
|
Changes the Scala version and runs a command.
|
||||||
|
|
||||||
<scala-version> may be an actual Scala version such as 3.1.3, or a Semantic Version selector
|
<scala-version> may be an actual Scala version such as 3.1.3, or a Semantic Version selector
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,9 @@
|
||||||
|
lazy val scala212 = "2.12.21"
|
||||||
|
lazy val scala213 = "2.13.12"
|
||||||
|
|
||||||
|
ThisBuild / scalaVersion := scala212
|
||||||
|
|
||||||
|
lazy val root = (project in file("."))
|
||||||
|
.settings(
|
||||||
|
crossScalaVersions := Seq(scala212, scala213),
|
||||||
|
)
|
||||||
|
|
@ -0,0 +1,11 @@
|
||||||
|
# Test that "cross" works as an alias for "+"
|
||||||
|
> cross clean
|
||||||
|
|
||||||
|
# Test that "cross" works with verbose flag
|
||||||
|
> cross -v compile
|
||||||
|
|
||||||
|
# Test that "switch" works as an alias for "++"
|
||||||
|
> switch 2.13.12!
|
||||||
|
|
||||||
|
# Test that "switch" works with a command
|
||||||
|
> switch 2.12.21! compile
|
||||||
Loading…
Reference in New Issue