Skip to content

Commit

Permalink
Add version of Scala to the output of version command
Browse files Browse the repository at this point in the history
  • Loading branch information
lwronski committed Aug 17, 2022
1 parent ce46b5e commit 1872ae0
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import caseapp._
@HelpMessage("Print `scala-cli` version")
final case class VersionOptions(
@Recurse
verbosity: VerbosityOptions = VerbosityOptions()
verbosity: VerbosityOptions = VerbosityOptions(),
@HelpMessage("Show only plain scala-cli version")
cliVersion: Boolean = false,
@HelpMessage("Show only plain scala version")
scalaVersion: Boolean = false
)
// format: on

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class ScalaCliCommands(
Uninstall,
UninstallCompletions,
Update,
Version
new Version(isSipScala = isSipScala)
) ++ (if (pgpUseBinaryCommands) Nil else pgpCommands.allScalaCommands.toSeq) ++
(if (pgpUseBinaryCommands) pgpBinaryCommands.allScalaCommands.toSeq else Nil)

Expand Down
7 changes: 1 addition & 6 deletions modules/cli/src/main/scala/scala/cli/commands/About.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@ class About(isSipScala: Boolean) extends ScalaCommand[AboutOptions] {

def run(options: AboutOptions, args: RemainingArgs): Unit = {
CurrentParams.verbosity = options.verbosity.verbosity
val version = Constants.version
val detailedVersionOpt = Constants.detailedVersion.filter(_ != version)
val appName =
if (isSipScala) "Scala command"
else "Scala CLI"
println(s"$appName version $version" + detailedVersionOpt.fold("")(" (" + _ + ")"))
println(Version.versionInfo(isSipScala))
val newestScalaCliVersion = Update.newestScalaCliVersion(options.ghToken.map(_.get()))
val isOutdated = CommandUtils.isOutOfDateVersion(
newestScalaCliVersion,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ object InstallHome extends ScalaCommand[InstallHomeOptions] {
val newScalaCliBinPath = os.Path(options.scalaCliBinaryPath, os.pwd)

val newVersion: String =
os.proc(newScalaCliBinPath, "version").call(cwd = os.pwd).out.text().trim
os.proc(newScalaCliBinPath, "version", "--cli-version").call(cwd = os.pwd).out.text().trim

// Backward compatibility - previous versions not have the `--version` parameter
val oldVersion: String =
if (os.isFile(destBinPath)) {
val res = os.proc(destBinPath, "version").call(cwd = os.pwd, check = false)
val res = os.proc(destBinPath, "version", "--cli-version").call(cwd = os.pwd, check = false)
if (res.exitCode == 0)
res.out.text().trim
else
Expand Down
2 changes: 1 addition & 1 deletion modules/cli/src/main/scala/scala/cli/commands/Update.scala
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ object Update extends ScalaCommand[UpdateOptions] {
}

private def getCurrentVersion(scalaCliBinPath: os.Path): String = {
val res = os.proc(scalaCliBinPath, "version").call(cwd = os.pwd, check = false)
val res = os.proc(scalaCliBinPath, "version", "--cli-version").call(cwd = os.pwd, check = false)
if (res.exitCode == 0)
res.out.text().trim
else
Expand Down
20 changes: 18 additions & 2 deletions modules/cli/src/main/scala/scala/cli/commands/Version.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,26 @@ import caseapp._
import scala.build.internal.Constants
import scala.cli.CurrentParams

object Version extends ScalaCommand[VersionOptions] {
class Version(isSipScala: Boolean) extends ScalaCommand[VersionOptions] {
override def group = "Miscellaneous"
def run(options: VersionOptions, args: RemainingArgs): Unit = {
CurrentParams.verbosity = options.verbosity.verbosity
println(Constants.version)
if (options.cliVersion)
println(Constants.version)
else if (options.scalaVersion)
println(Constants.defaultScalaVersion)
else
println(Version.versionInfo(isSipScala))
}
}

object Version {
def versionInfo(isSipScala: Boolean) =
val version = Constants.version
val detailedVersionOpt = Constants.detailedVersion.filter(_ != version).fold("")(" (" + _ + ")")
val appName =
if (isSipScala) "Scala code runner"
else "Scala CLI"
s"""$appName version $version$detailedVersionOpt
|Default Scala version: ${Constants.defaultScalaVersion}""".stripMargin
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class UpdateTests extends ScalaCliSuite {
stdout = os.Inherit
)

val nextVersion = os.proc(binDirPath / dummyScalaCliBinName, "version").call(
val nextVersion = os.proc(binDirPath / dummyScalaCliBinName, "version", "--cli-version").call(
cwd = root,
stdin = os.Inherit
).out.text().trim
Expand Down
16 changes: 16 additions & 0 deletions website/docs/reference/cli-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -1969,6 +1969,22 @@ Aliases: `-i`

Interactive mode

## Version options

Available in commands:
- [`version`](./commands.md#version)


<!-- Automatically generated, DO NOT EDIT MANUALLY -->

#### `--cli-version`

Show only plain scala-cli version

#### `--scala-version`

Show only plain scala version

## Watch options

Available in commands:
Expand Down
1 change: 1 addition & 0 deletions website/docs/reference/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ Print `scala-cli` version
Accepts options:
- [verbosity](./cli-options.md#verbosity-options)
- [version](./cli-options.md#version-options)
## Hidden commands
Expand Down

0 comments on commit 1872ae0

Please sign in to comment.