Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse JVM 9 version correctly #439

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
package scala.build.blooprifle


object VersionUtil {
def jvmRelease(jvmVersion: String): Option[Int] = {
val jvmReleaseRegex = "(1[.])?(\\d+)".r
jvmReleaseRegex.findFirstMatchIn(jvmVersion).map(_.group(2)).map(_.toInt)
}

/** @param jvmVersion,
* for example '1.8.0.22' or '11.0.2'
* @return
* jvm release version (8, 11)
*/
val jvmReleaseRegex = "(1[.])?(\\d+)"
def jvmRelease(jvmVersion: String): Option[Int] =
jvmReleaseRegex.r.findFirstMatchIn(jvmVersion).map(_.group(2)).map(_.toInt)
tpasternak marked this conversation as resolved.
Show resolved Hide resolved

/** @param input
* `java -version` output`
* @return
* jvm release version (8, 11)
*/
def parseJavaVersion(input: String): Option[Int] = for {
firstMatch <- s""".*version .($jvmReleaseRegex).*""".r.findFirstMatchIn(input)
versionNumberGroup <- Option(firstMatch.group(1))
versionInt <- jvmRelease(versionNumberGroup)
} yield versionInt

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package scala.build.blooprifle

import com.eed3si9n.expecty.Expecty.expect

import scala.build.blooprifle.VersionUtil.{jvmRelease, parseBloopAbout}
import scala.build.blooprifle.VersionUtil.{jvmRelease, parseBloopAbout, parseJavaVersion}

class ParsingTests extends munit.FunSuite {

implicit class BV(s: String) {
implicit def v = BloopVersion(s)
implicit def p = parseBloopAbout(s)
implicit def j = jvmRelease(s)
implicit def v = BloopVersion(s)
implicit def p = parseBloopAbout(s)
implicit def j = jvmRelease(s)
implicit def jv = parseJavaVersion(s)
}

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

test("parse jvm version") {
expect("""openjdk version "1.8.0_292" """.jv == Some(8))
expect("""openjdk version "9" """.jv == Some(9))
expect("""openjdk version "11.0.11" 2021-04-20 """.jv == Some(11))
expect("""openjdk version "16" 2021-03-16 """.jv == Some(16))
}

val jreBloopOutput =
"""|bloop v1.4.11
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import java.nio.file.Path
import java.security.MessageDigest

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

val javaV0 = os.proc(javaCmd, "-version").call(
val javaVersionOutput = os.proc(javaCmd, "-version").call(
cwd = os.pwd,
stdout = os.Pipe,
stderr = os.Pipe,
mergeErrIntoOut = true
).out.text().trim()
val javaFullVersion = javaV0.split(" ").lift(2).map(_.replace("\"", ""))
val javaReleaseVersion =
javaFullVersion.flatMap(_.trim.stripPrefix("1.").split("[.]").headOption)

val javaVersion =
try javaReleaseVersion.get.toInt
catch {
case e: Throwable =>
throw new Exception(s"Could not parse java version from output: $javaV0", e)
}
val javaVersion = parseJavaVersion(javaVersionOutput).getOrElse {
throw new Exception(s"Could not parse java version from output: $javaVersionOutput")
}

JavaHomeInfo(javaCmd, javaVersion)
}

Expand Down