Skip to content

Commit 0816d65

Browse files
committed
NIT: Reuse javaVersion method
- remove dot as github includes it into the clickable URL - Move methods for downloading to Update.scala - apply fmt
1 parent 88981b6 commit 0816d65

File tree

4 files changed

+46
-53
lines changed

4 files changed

+46
-53
lines changed

modules/cli/src/main/scala/scala/cli/commands/update/Update.scala

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,19 @@ import com.github.plokhotnyuk.jsoniter_scala.core.*
66
import com.github.plokhotnyuk.jsoniter_scala.macros.*
77
import coursier.core
88

9+
import java.net.{HttpURLConnection, URL, URLConnection}
10+
import java.nio.charset.StandardCharsets
11+
912
import scala.build.Logger
1013
import scala.build.errors.CheckScalaCliVersionError
1114
import scala.build.internal.Constants.{ghName, ghOrg, version as scalaCliVersion}
1215
import scala.cli.CurrentParams
1316
import scala.cli.commands.shared.HelpGroup
1417
import scala.cli.commands.{CommandUtils, ScalaCommand, SpecificationLevel}
15-
import scala.cli.internal.ProcUtil
1618
import scala.cli.signing.shared.Secret
1719
import scala.cli.util.ArgHelpers.*
18-
import scala.util.Properties
1920
import scala.util.control.NonFatal
21+
import scala.util.{Properties, Try}
2022

2123
object Update extends ScalaCommand[UpdateOptions] {
2224

@@ -46,7 +48,7 @@ object Update extends ScalaCommand[UpdateOptions] {
4648
tokenOpt.toSeq.map(tk => "Authorization" -> s"token ${tk.value}")
4749

4850
try {
49-
val resp = ProcUtil.download(url, headers: _*)
51+
val resp = download(url, headers: _*)
5052
readFromArray(resp)(releaseListCodec).filter(_.actualRelease)
5153
.maxByOption(_.version)
5254
.map(_.version.repr)
@@ -79,7 +81,7 @@ object Update extends ScalaCommand[UpdateOptions] {
7981
}
8082

8183
val installationScript =
82-
ProcUtil.downloadFile("https://scala-cli.virtuslab.org/get")
84+
downloadFile("https://scala-cli.virtuslab.org/get")
8385

8486
// format: off
8587
val res = os.proc(
@@ -188,4 +190,41 @@ object Update extends ScalaCommand[UpdateOptions] {
188190

189191
classesDir.contains(binRepoDir)
190192
}
193+
194+
// from https://github.com/coursier/coursier/blob/7b7c2c312aea26e850f0cd2cf15e688d0777f819/modules/cache/jvm/src/main/scala/coursier/cache/CacheUrl.scala#L489-L497
195+
private def closeConn(conn: URLConnection): Unit = {
196+
Try(conn.getInputStream).toOption.filter(_ != null).foreach(_.close())
197+
conn match {
198+
case conn0: HttpURLConnection =>
199+
Try(conn0.getErrorStream).toOption.filter(_ != null).foreach(_.close())
200+
conn0.disconnect()
201+
case _ =>
202+
}
203+
}
204+
205+
private def download(
206+
url: String,
207+
headers: (String, String)*
208+
): Array[Byte] = {
209+
var conn: URLConnection = null
210+
val url0 = new URL(url)
211+
try {
212+
conn = url0.openConnection()
213+
for ((k, v) <- headers)
214+
conn.setRequestProperty(k, v)
215+
conn.getInputStream.readAllBytes()
216+
}
217+
catch {
218+
case NonFatal(ex) =>
219+
throw new RuntimeException(s"Error downloading $url: $ex", ex)
220+
}
221+
finally
222+
if (conn != null)
223+
closeConn(conn)
224+
}
225+
226+
private def downloadFile(url: String): String = {
227+
val data = download(url)
228+
new String(data, StandardCharsets.UTF_8)
229+
}
191230
}

modules/cli/src/main/scala/scala/cli/internal/ProcUtil.scala

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -35,43 +35,6 @@ object ProcUtil {
3535
usesSh
3636
}
3737

38-
// from https://github.com/coursier/coursier/blob/7b7c2c312aea26e850f0cd2cf15e688d0777f819/modules/cache/jvm/src/main/scala/coursier/cache/CacheUrl.scala#L489-L497
39-
private def closeConn(conn: URLConnection): Unit = {
40-
Try(conn.getInputStream).toOption.filter(_ != null).foreach(_.close())
41-
conn match {
42-
case conn0: HttpURLConnection =>
43-
Try(conn0.getErrorStream).toOption.filter(_ != null).foreach(_.close())
44-
conn0.disconnect()
45-
case _ =>
46-
}
47-
}
48-
49-
def download(
50-
url: String,
51-
headers: (String, String)*
52-
): Array[Byte] = {
53-
var conn: URLConnection = null
54-
val url0 = new URL(url)
55-
try {
56-
conn = url0.openConnection()
57-
for ((k, v) <- headers)
58-
conn.setRequestProperty(k, v)
59-
conn.getInputStream.readAllBytes()
60-
}
61-
catch {
62-
case NonFatal(ex) =>
63-
throw new RuntimeException(s"Error downloading $url: $ex", ex)
64-
}
65-
finally
66-
if (conn != null)
67-
closeConn(conn)
68-
}
69-
70-
def downloadFile(url: String): String = {
71-
val data = download(url)
72-
new String(data, StandardCharsets.UTF_8)
73-
}
74-
7538
def forceKillProcess(process: Process, logger: Logger): Unit = {
7639
if (process.isAlive) {
7740
process.destroyForcibly()

modules/cli/src/test/scala/cli/tests/ScalafmtTest.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class ScalafmtTests extends munit.FunSuite {
5151
"scalafmt-x86_64-pc-win32.zip"
5252
)
5353
val errorMsg =
54-
s"""scalafmt native images missing for v$scalaFmtVersion, make a release at https://github.com/VirtusLab/scalafmt-native-image.
54+
s"""scalafmt native images missing for v$scalaFmtVersion, make a release at https://github.com/VirtusLab/scalafmt-native-image
5555
|Ensure that all expected assets are available in the release:
5656
| ${expectedAssets.mkString(", ")}
5757
|for scalafmt-native-image under tag v$scalaFmtVersion.""".stripMargin
@@ -68,7 +68,7 @@ class ScalafmtTests extends munit.FunSuite {
6868
catch {
6969
case e: JsonReaderException => throw new Exception(s"Error reading $url", e)
7070
case e: Throwable => throw new Exception(
71-
s"""Failed to check for the ScalaFmt native launcher assets: ${e.getMessage}.
71+
s"""Failed to check for the ScalaFmt native launcher assets: ${e.getMessage}
7272
|
7373
|$errorMsg
7474
|""".stripMargin,

modules/core/src/main/scala/scala/build/internals/OsLibc.scala

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,7 @@ object OsLibc {
100100
val ext = if (Properties.isWin) ".exe" else ""
101101
val javaCmd = (javaHome / "bin" / s"java$ext").toString
102102

103-
val javaVersionOutput = os.proc(javaCmd, "-version").call(
104-
cwd = os.pwd,
105-
stdout = os.Pipe,
106-
stderr = os.Pipe,
107-
mergeErrIntoOut = true
108-
).out.trim()
109-
val javaVersion = parseJavaVersion(javaVersionOutput).getOrElse {
110-
throw new Exception(s"Could not parse java version from output: $javaVersionOutput")
111-
}
112-
(javaVersion, javaCmd)
103+
(javaVersion(javaCmd), javaCmd)
113104
}
114105

115106
}

0 commit comments

Comments
 (0)