Skip to content

Commit 5ae152c

Browse files
committed
Apply suggestions from code review
1 parent bc1798e commit 5ae152c

File tree

8 files changed

+105
-101
lines changed

8 files changed

+105
-101
lines changed

modules/cli/src/main/scala/scala/cli/ScalaCli.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ object ScalaCli extends CommandsEntryPoint {
3131
Package,
3232
Run,
3333
SetupIde,
34-
Test
34+
Test,
35+
Version
3536
)
3637

3738
lazy val progName = (new Argv0).get("scala-cli")

modules/cli/src/main/scala/scala/cli/commands/About.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ object About extends ScalaCommand[AboutOptions] {
99
def run(options: AboutOptions, args: RemainingArgs): Unit = {
1010
val version = Constants.version
1111
val detailedVersionOpt = Some(Constants.detailedVersion).filter(_ != version)
12-
if (options.version) println(version)
13-
else println(s"Scala CLI version $version" + detailedVersionOpt.fold("")(" (" + _ + ")"))
12+
println(s"Scala CLI version $version" + detailedVersionOpt.fold("")(" (" + _ + ")"))
1413
}
1514
}

modules/cli/src/main/scala/scala/cli/commands/AboutOptions.scala

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,7 @@ package scala.cli.commands
33
import caseapp._
44

55
@HelpMessage("Print details about this application")
6-
final case class AboutOptions(
7-
@Group("About")
8-
@Name("v")
9-
@HelpMessage("Print only the scala-cli version")
10-
version: Boolean = false
11-
)
6+
final case class AboutOptions()
127

138
object AboutOptions {
149
implicit lazy val parser: Parser[AboutOptions] = Parser.derive

modules/cli/src/main/scala/scala/cli/commands/InstallHome.scala

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,42 @@ object InstallHome extends ScalaCommand[InstallHomeOptions] {
1010
override def hidden: Boolean = true
1111

1212
private def isOutOfDate(newVersion: String, oldVersion: String): Boolean = {
13-
val versionOrdering = Ordering.by { (_: String).split("""\.""").map(_.toInt).toIterable }
14-
versionOrdering.gt(newVersion, oldVersion)
13+
import coursier.core.Version
14+
15+
Version(newVersion) > Version(oldVersion)
16+
}
17+
18+
private def loggingEqual(version: String) = {
19+
System.err.println(
20+
s"Scala-cli $version is already installed and up-to-date."
21+
)
22+
sys.exit(0)
23+
}
24+
25+
private def loggingUpdate(env: Boolean, newVersion: String, oldVersion: String) = {
26+
if (!env) println(
27+
s"""scala-cli $oldVersion is already installed and out-of-date.
28+
|scala-cli will be updated to version $newVersion
29+
|""".stripMargin
30+
)
31+
}
32+
33+
private def loggingDowngrade(env: Boolean, newVersion: String, oldVersion: String) = {
34+
if (!env && coursier.paths.Util.useAnsiOutput()) {
35+
println(s"scala-cli $oldVersion is already installed and up-to-date.")
36+
println(s"Do you want to downgrade scala-cli to version $newVersion [Y/n]")
37+
val response = readLine()
38+
if (response != "Y") {
39+
System.err.println("Abort")
40+
sys.exit(1)
41+
}
42+
}
43+
else {
44+
System.err.println(
45+
s"Error: scala-cli is already installed $oldVersion and up-to-date. Downgrade to $newVersion pass -f or --force."
46+
)
47+
sys.exit(1)
48+
}
1549
}
1650

1751
def run(options: InstallHomeOptions, args: RemainingArgs): Unit = {
@@ -21,57 +55,30 @@ object InstallHome extends ScalaCommand[InstallHomeOptions] {
2155
val newScalaCliBinPath = os.Path(options.scalaCliBinaryPath, os.pwd)
2256

2357
val newVersion: String =
24-
os.proc(newScalaCliBinPath, "about", "--version").call(cwd = os.pwd).out.text.trim
58+
os.proc(newScalaCliBinPath, "version").call(cwd = os.pwd).out.text.trim
2559

2660
// Backward compatibility - previous versions not have the `--version` parameter
2761
val oldVersion: String = Try {
28-
os.proc(binDirPath / options.binaryName, "about", "--version").call(cwd =
29-
os.pwd).out.text.trim
62+
os.proc(binDirPath / options.binaryName, "version").call(cwd = os.pwd).out.text.trim
3063
}.toOption.getOrElse("0.0.0")
3164

3265
if (os.exists(binDirPath)) {
33-
if (options.force) os.remove.all(binDirPath)
34-
else if (newVersion == oldVersion) {
35-
System.err.println(
36-
s"Scala-cli $newVersion is already installed and up-to-date."
37-
)
38-
sys.exit(1)
39-
}
40-
else if (isOutOfDate(newVersion, oldVersion)) {
41-
if (!options.env) println(
42-
s"""scala-cli $oldVersion is already installed and out-of-date.
43-
|scala-cli will be updated to version $newVersion
44-
|""".stripMargin
45-
)
46-
os.remove.all(binDirPath)
47-
}
48-
else {
49-
if (!options.env && coursier.paths.Util.useAnsiOutput()) {
50-
println(s"scala-cli $oldVersion is already installed and up-to-date.")
51-
println(s"Do you want to downgrade scala-cli to version $newVersion [Y/n]")
52-
val response = readLine()
53-
if (response != "Y") {
54-
System.err.println("Abort")
55-
sys.exit(1)
56-
}
57-
else
58-
os.remove.all(binDirPath)
59-
}
60-
else {
61-
System.err.println(
62-
s"Error: scala-cli is already installed $oldVersion and up-to-date. Downgrade to $newVersion pass -f or --force."
63-
)
64-
sys.exit(1)
65-
}
66-
}
66+
if (options.force) () // skip logging
67+
else if (newVersion == oldVersion) loggingEqual(newVersion)
68+
else if (isOutOfDate(newVersion, oldVersion))
69+
loggingUpdate(options.env, newVersion, oldVersion)
70+
else loggingDowngrade(options.env, newVersion, oldVersion)
71+
}
72+
73+
os.remove.all(binDirPath)
6774

6875
os.copy(
6976
from = newScalaCliBinPath,
7077
to = binDirPath / options.binaryName,
7178
createFolders = true
7279
)
7380
if (!Properties.isWin)
74-
os.perms.set(binDirPath / options.binaryName, os.PermSet.fromString("rwxrwxr-x"))
81+
os.perms.set(binDirPath / options.binaryName, os.PermSet.fromString("rwxr-xr-x"))
7582

7683
if (options.env) {
7784
println(s""" export PATH="$$PATH:$binDirPath" """)

modules/cli/src/main/scala/scala/cli/commands/SharedCompilationServerOptions.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package scala.cli.commands
22

33
import caseapp._
44
import com.google.gson.Gson
5-
import coursier.core.Version
65

76
import java.io.{BufferedReader, File, FileReader}
87
import java.nio.file.{AtomicMoveNotSupportedException, FileAlreadyExistsException, Files}
@@ -168,7 +167,9 @@ final case class SharedCompilationServerOptions(
168167
parseDuration("connection server startup timeout", bloopStartupTimeout)
169168

170169
def minimumBloopVersion = Constants.bloopVersion
171-
def acceptBloopVersion = Some((v: String) => Version(v) < Version(minimumBloopVersion))
170+
171+
import coursier.core.Version
172+
def acceptBloopVersion = Some((v: String) => Version(v) < Version(minimumBloopVersion))
172173

173174
def bloopDefaultJvmOptions(logger: Logger): List[String] = {
174175
val file = new File(bloopGlobalOptionsFile)

modules/integration/src/test/scala/scala/cli/integration/InstallHomeTests.scala

Lines changed: 46 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ class InstallHomeTests extends munit.FunSuite {
2525
|}""".stripMargin
2626
)
2727
)
28-
val binDirPath = os.home / ".scala" / "scala-cli"
2928

3029
private def packageDummyScalaCli(root: os.Path, dummyScalaCliFileName: String, output: String) = {
3130
// format: off
@@ -40,10 +39,19 @@ class InstallHomeTests extends munit.FunSuite {
4039
)
4140
}
4241

43-
private def installScalaCli(root: os.Path, binVersion: String, force: Boolean) = {
42+
private def installScalaCli(
43+
root: os.Path,
44+
binVersion: String,
45+
binDirPath: os.Path,
46+
force: Boolean
47+
) = {
4448
// format: off
4549
val cmdInstallVersion = Seq[os.Shellable](
46-
TestUtil.cli, "install-home", "--scala-cli-binary-path", binVersion , "--binary-name", dummyScalaCliBinName, "--bin-dir", binDirPath
50+
TestUtil.cli, "install-home",
51+
"--env",
52+
"--scala-cli-binary-path", binVersion ,
53+
"--binary-name", dummyScalaCliBinName,
54+
"--bin-dir", binDirPath
4755
) ++ (if(force) Seq[os.Shellable]("--force") else Seq.empty)
4856
// format: on
4957
os.proc(cmdInstallVersion).call(
@@ -53,46 +61,51 @@ class InstallHomeTests extends munit.FunSuite {
5361
)
5462
}
5563

56-
if (!Properties.isWin && TestUtil.isCI) {
57-
test("updating and downgrading dummy scala-cli using install-home command") {
64+
def runInstallHome(): Unit = {
5865

59-
testInputs.fromRoot { root =>
66+
testInputs.fromRoot { root =>
67+
val binDirPath = root / ".scala" / "scala-cli"
6068

61-
val binDummyScalaCliFirst = dummyScalaCliFirstName.stripSuffix(".scala").toLowerCase
62-
val binDummyScalaCliSecond = dummyScalaCliSecondName.stripSuffix(".scala").toLowerCase
69+
val binDummyScalaCliFirst = dummyScalaCliFirstName.stripSuffix(".scala").toLowerCase
70+
val binDummyScalaCliSecond = dummyScalaCliSecondName.stripSuffix(".scala").toLowerCase
6371

64-
packageDummyScalaCli(root, dummyScalaCliFirstName, binDummyScalaCliFirst)
65-
packageDummyScalaCli(root, dummyScalaCliSecondName, binDummyScalaCliSecond)
72+
packageDummyScalaCli(root, dummyScalaCliFirstName, binDummyScalaCliFirst)
73+
packageDummyScalaCli(root, dummyScalaCliSecondName, binDummyScalaCliSecond)
6674

67-
// install 1 version
68-
installScalaCli(root, binDummyScalaCliFirst, force = true)
75+
// install 1 version
76+
installScalaCli(root, binDummyScalaCliFirst, binDirPath, force = true)
6977

70-
println(binDirPath / dummyScalaCliBinName)
78+
println(binDirPath / dummyScalaCliBinName)
7179

72-
val v1Install = os.proc(binDirPath / dummyScalaCliBinName).call(
73-
cwd = root,
74-
stdin = os.Inherit
75-
).out.text.trim
76-
expect(v1Install == firstVersion)
80+
val v1Install = os.proc(binDirPath / dummyScalaCliBinName).call(
81+
cwd = root,
82+
stdin = os.Inherit
83+
).out.text.trim
84+
expect(v1Install == firstVersion)
7785

78-
// update to 2 version
79-
installScalaCli(root, binDummyScalaCliSecond, force = false)
86+
// update to 2 version
87+
installScalaCli(root, binDummyScalaCliSecond, binDirPath, force = false)
8088

81-
val v2Update = os.proc(binDirPath / dummyScalaCliBinName).call(
82-
cwd = root,
83-
stdin = os.Inherit
84-
).out.text.trim
85-
expect(v2Update == secondVersion)
89+
val v2Update = os.proc(binDirPath / dummyScalaCliBinName).call(
90+
cwd = root,
91+
stdin = os.Inherit
92+
).out.text.trim
93+
expect(v2Update == secondVersion)
8694

87-
// downgrade to 1 version with force
88-
installScalaCli(root, binDummyScalaCliFirst, force = true)
95+
// downgrade to 1 version with force
96+
installScalaCli(root, binDummyScalaCliFirst, binDirPath, force = true)
8997

90-
val v1Downgrade = os.proc(binDirPath / dummyScalaCliBinName).call(
91-
cwd = root,
92-
stdin = os.Inherit
93-
).out.text.trim
94-
expect(v1Downgrade == firstVersion)
95-
}
98+
val v1Downgrade = os.proc(binDirPath / dummyScalaCliBinName).call(
99+
cwd = root,
100+
stdin = os.Inherit
101+
).out.text.trim
102+
expect(v1Downgrade == firstVersion)
96103
}
97104
}
105+
106+
if (!Properties.isWin && TestUtil.isCI)
107+
test("updating and downgrading dummy scala-cli using install-home command") {
108+
runInstallHome()
109+
}
110+
98111
}

website/docs/reference/cli-options.md

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,6 @@ title: Command-line options
33
sidebar_position: 1
44
---
55

6-
## About options
7-
8-
Available in commands:
9-
- [`about`](./commands#about)
10-
11-
12-
<!-- Automatically generated, DO NOT EDIT MANUALLY -->
13-
14-
#### `--version`
15-
16-
Aliases: `-v`
17-
18-
Print only the scala-cli version
19-
206
## Add path options
217

228
Available in commands:
@@ -299,6 +285,7 @@ Available in commands:
299285
- [`run`](./commands#run)
300286
- [`setup-ide`](./commands#setup-ide)
301287
- [`test`](./commands#test)
288+
- [`version`](./commands#version)
302289

303290

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

website/docs/reference/commands.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ sidebar_position: 3
77

88
Print details about this application
99

10-
Accepts options:
11-
- [about](./cli-options.md#about-options)
12-
1310
## `bsp`
1411

1512
Accepts options:
@@ -219,6 +216,10 @@ Accepts options:
219216
- [test](./cli-options.md#test-options)
220217
- [watch](./cli-options.md#watch-options)
221218

219+
## `version`
220+
221+
Print scala-cli version
222+
222223
## Hidden commands
223224

224225
### `add-path`

0 commit comments

Comments
 (0)