Skip to content

Commit 746db09

Browse files
authored
Parse JVM 9 version correctly (#439)
* Parse JVM 9 version correctly
1 parent 3c81e1b commit 746db09

File tree

3 files changed

+43
-19
lines changed

3 files changed

+43
-19
lines changed

modules/bloop-rifle/src/main/scala/scala/build/blooprifle/VersionUtil.scala

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,31 @@
11
package scala.build.blooprifle
22

3+
import scala.util.Try
4+
35
object VersionUtil {
4-
def jvmRelease(jvmVersion: String): Option[Int] = {
5-
val jvmReleaseRegex = "(1[.])?(\\d+)".r
6-
jvmReleaseRegex.findFirstMatchIn(jvmVersion).map(_.group(2)).map(_.toInt)
7-
}
6+
7+
/** @param jvmVersion,
8+
* for example '1.8.0.22' or '11.0.2'
9+
* @return
10+
* jvm release version (8, 11)
11+
*/
12+
val jvmReleaseRegex = "(1[.])?(\\d+)"
13+
def jvmRelease(jvmVersion: String): Option[Int] = for {
14+
regexMatch <- jvmReleaseRegex.r.findFirstMatchIn(jvmVersion)
15+
versionString <- Option(regexMatch.group(2))
16+
versionInt <- Try(versionString.toInt).toOption
17+
} yield versionInt
18+
19+
/** @param input
20+
* `java -version` output`
21+
* @return
22+
* jvm release version (8, 11)
23+
*/
24+
def parseJavaVersion(input: String): Option[Int] = for {
25+
firstMatch <- s""".*version .($jvmReleaseRegex).*""".r.findFirstMatchIn(input)
26+
versionNumberGroup <- Option(firstMatch.group(1))
27+
versionInt <- jvmRelease(versionNumberGroup)
28+
} yield versionInt
829

930
def parseBloopAbout(stdoutFromBloopAbout: String): Option[BloopServerRuntimeInfo] = {
1031

modules/bloop-rifle/src/test/scala/scala/build/blooprifle/ParsingTests.scala

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ package scala.build.blooprifle
22

33
import com.eed3si9n.expecty.Expecty.expect
44

5-
import scala.build.blooprifle.VersionUtil.{jvmRelease, parseBloopAbout}
5+
import scala.build.blooprifle.VersionUtil.{jvmRelease, parseBloopAbout, parseJavaVersion}
66

77
class ParsingTests extends munit.FunSuite {
88

99
implicit class BV(s: String) {
10-
implicit def v = BloopVersion(s)
11-
implicit def p = parseBloopAbout(s)
12-
implicit def j = jvmRelease(s)
10+
implicit def v = BloopVersion(s)
11+
implicit def p = parseBloopAbout(s)
12+
implicit def j = jvmRelease(s)
13+
implicit def jv = parseJavaVersion(s)
1314
}
1415

1516
test("bloop version comparisons test") {
@@ -31,6 +32,13 @@ class ParsingTests extends munit.FunSuite {
3132
expect("17".j == Some(17))
3233
}
3334

35+
test("parse jvm version") {
36+
expect("""openjdk version "1.8.0_292" """.jv == Some(8))
37+
expect("""openjdk version "9" """.jv == Some(9))
38+
expect("""openjdk version "11.0.11" 2021-04-20 """.jv == Some(11))
39+
expect("""openjdk version "16" 2021-03-16 """.jv == Some(16))
40+
}
41+
3442
val jreBloopOutput =
3543
"""|bloop v1.4.11
3644
|

modules/build/src/main/scala/scala/build/options/BuildOptions.scala

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import java.nio.file.Path
1010
import java.security.MessageDigest
1111

1212
import scala.build.EitherCps.{either, value}
13+
import scala.build.blooprifle.VersionUtil.parseJavaVersion
1314
import scala.build.errors.{BuildException, Diagnostic, InvalidBinaryScalaVersionError}
1415
import scala.build.internal.Constants._
1516
import scala.build.internal.{Constants, OsLibc, Util}
@@ -127,22 +128,16 @@ final case class BuildOptions(
127128
val ext = if (Properties.isWin) ".exe" else ""
128129
val javaCmd = (javaHome / "bin" / s"java$ext").toString
129130

130-
val javaV0 = os.proc(javaCmd, "-version").call(
131+
val javaVersionOutput = os.proc(javaCmd, "-version").call(
131132
cwd = os.pwd,
132133
stdout = os.Pipe,
133134
stderr = os.Pipe,
134135
mergeErrIntoOut = true
135136
).out.text().trim()
136-
val javaFullVersion = javaV0.split(" ").lift(2).map(_.replace("\"", ""))
137-
val javaReleaseVersion =
138-
javaFullVersion.flatMap(_.trim.stripPrefix("1.").split("[.]").headOption)
139-
140-
val javaVersion =
141-
try javaReleaseVersion.get.toInt
142-
catch {
143-
case e: Throwable =>
144-
throw new Exception(s"Could not parse java version from output: $javaV0", e)
145-
}
137+
val javaVersion = parseJavaVersion(javaVersionOutput).getOrElse {
138+
throw new Exception(s"Could not parse java version from output: $javaVersionOutput")
139+
}
140+
146141
JavaHomeInfo(javaCmd, javaVersion)
147142
}
148143

0 commit comments

Comments
 (0)