From e81272968a88690c0841fa113973449cf0599587 Mon Sep 17 00:00:00 2001 From: Florian Enner Date: Sun, 19 Nov 2023 19:23:05 +0100 Subject: [PATCH] added support for graalvm version strings without minor version (#1234) * added support for graalvm version strings without minor version * fixed support for GraalVM CE --- .../model/InternalProjectConfiguration.java | 27 ++++--- .../InternalProjectConfigurationTest.java | 71 +++++++++++++++++++ 2 files changed, 87 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/gluonhq/substrate/model/InternalProjectConfiguration.java b/src/main/java/com/gluonhq/substrate/model/InternalProjectConfiguration.java index f09e1f04..392d917f 100644 --- a/src/main/java/com/gluonhq/substrate/model/InternalProjectConfiguration.java +++ b/src/main/java/com/gluonhq/substrate/model/InternalProjectConfiguration.java @@ -122,21 +122,22 @@ public Path getGraalPath() { } public Version getGraalVersion() throws IOException { - String pattern = "GraalVM .*?(\\d\\d.\\d.\\d)"; ProcessRunner graalJava; try { - graalJava = new ProcessRunner( - getGraalVMBinPath().resolve("java").toString(), - "--version"); + graalJava = new ProcessRunner(getGraalVMBinPath().resolve("java").toString(), "-version"); graalJava.runProcess("java-version"); } catch (InterruptedException e) { throw new IOException("Couldn't determine GraalVM version, " + e.toString()); } - String output = graalJava.getResponse(); + return parseGraalVersion(graalJava.getResponse()); + } + + static Version parseGraalVersion(String versionString) { + String pattern = "GraalVM .*?(\\d{1,2}(\\.\\d+){0,2})"; Pattern r = Pattern.compile(pattern); - Matcher m = r.matcher(output); + Matcher m = r.matcher(versionString); if (!m.find()) - throw new IOException("Couldn't determine GraalVM version"); + throw new IllegalArgumentException("Couldn't determine GraalVM version from: " + versionString); return new Version(m.group(1)); } @@ -679,16 +680,20 @@ public Version getGraalVMJavaVersion() throws IOException { } catch (InterruptedException e) { throw new IllegalArgumentException("$GRAALVM_HOME/bin/java -version process failed"); } - String pattern = "version \"(\\d{1,2}(\\.\\d+){0,2})\""; - Pattern r = Pattern.compile(pattern); List responses = graalJava.getResponses(); if (responses == null || responses.isEmpty()) { throw new IOException("Couldn't determine GraalVM's Java version"); } - String realVersion = responses.get(0).replaceAll("-internal", ""); + return parseGraalVMJavaVersion(responses.get(0)); + } + + static Version parseGraalVMJavaVersion(String versionString) { + String realVersion = versionString.replaceAll("-internal", ""); + String pattern = "version \"(\\d{1,2}(\\.\\d+){0,2})\""; + Pattern r = Pattern.compile(pattern); Matcher m = r.matcher(realVersion); if (!m.find()) { - throw new IOException("Couldn't determine GraalVM's Java version for " + responses.get(0)); + throw new IllegalArgumentException("Couldn't determine GraalVM's Java version from: " + realVersion); } return new Version(m.group(1)); } diff --git a/src/test/java/com/gluonhq/substrate/model/InternalProjectConfigurationTest.java b/src/test/java/com/gluonhq/substrate/model/InternalProjectConfigurationTest.java index dedc03bf..d3501b60 100644 --- a/src/test/java/com/gluonhq/substrate/model/InternalProjectConfigurationTest.java +++ b/src/test/java/com/gluonhq/substrate/model/InternalProjectConfigurationTest.java @@ -71,4 +71,75 @@ static Stream versioningSchemeParameters() { Arguments.of("22.0.2", false) ); } + + @ParameterizedTest + @MethodSource("graalVersionOutputs") + void testParseGraalVMVersion(Version graalVersion, Version javaVersion, String output) { + assertEquals(graalVersion.toString(), InternalProjectConfiguration.parseGraalVersion(output).toString()); + } + + @ParameterizedTest + @MethodSource("graalVersionOutputs") + void testParseGraalVMJavaVersion(Version graalVersion, Version javaVersion, String output) { + assertEquals(javaVersion.toString(), InternalProjectConfiguration.parseGraalVMJavaVersion(output).toString()); + } + + static Stream graalVersionOutputs() { + // outputs of 'java -version' + return Stream.of( + // ======== Gluon Builds ======== + + // graalvm-svm-java17-linux-gluon-22.1.0.1-Final + Arguments.of(new Version("22.1.0"), new Version("17.0.3"), "openjdk version \"17.0.3\" 2022-04-19\n" + + "OpenJDK Runtime Environment GraalVM 22.1.0.1 (build 17.0.3+7-jvmci-22.1-b06)\n" + + "OpenJDK 64-Bit Server VM GraalVM 22.1.0.1 (build 17.0.3+7-jvmci-22.1-b06, mixed mode, sharing)"), + + // graalvm-svm-java17-windows-gluon-22.0.0.3-Final + Arguments.of(new Version("22.0.0"), new Version("17.0.2"), "openjdk version \"17.0.2\" 2022-01-18\n" + + "OpenJDK Runtime Environment GraalVM 22.0.0.2 (build 17.0.2+8-jvmci-22.0-b05)\n" + + "OpenJDK 64-Bit Server VM GraalVM 22.0.0.2 (build 17.0.2+8-jvmci-22.0-b05, mixed mode, sharing)"), + + // graalvm-svm-java17-windows-gluon-22.1.0.1-Final + Arguments.of(new Version("22.1.0"), new Version("17.0.3"), "openjdk version \"17.0.3\" 2022-04-19\n" + + "OpenJDK Runtime Environment GraalVM 22.1.0.1 (build 17.0.3+7-jvmci-22.1-b06)\n" + + "OpenJDK 64-Bit Server VM GraalVM 22.1.0.1 (build 17.0.3+7-jvmci-22.1-b06, mixed mode, sharing)"), + + // ======== Oracle Builds ======== + + // graalvm-jdk-17.0.7+8.1 + Arguments.of(new Version("17.0.7"), new Version("17.0.7"), "java version \"17.0.7\" 2023-04-18 LTS\n" + + "Java(TM) SE Runtime Environment Oracle GraalVM 17.0.7+8.1 (build 17.0.7+8-LTS-jvmci-23.0-b12)\n" + + "Java HotSpot(TM) 64-Bit Server VM Oracle GraalVM 17.0.7+8.1 (build 17.0.7+8-LTS-jvmci-23.0-b12, mixed mode, sharing)"), + + // graalvm-jdk-20.0.1+9.1 + Arguments.of(new Version("20.0.1"), new Version("20.0.1"), "java version \"20.0.1\" 2023-04-18\n" + + "Java(TM) SE Runtime Environment Oracle GraalVM 20.0.1+9.1 (build 20.0.1+9-jvmci-23.0-b12)\n" + + "Java HotSpot(TM) 64-Bit Server VM Oracle GraalVM 20.0.1+9.1 (build 20.0.1+9-jvmci-23.0-b12, mixed mode, sharing)"), + + // graalvm-jdk-21+35.1 + Arguments.of(new Version("21"), new Version("21"), "java version \"21\" 2023-09-19\n" + + "Java(TM) SE Runtime Environment Oracle GraalVM 21+35.1 (build 21+35-jvmci-23.1-b15)\n" + + "Java HotSpot(TM) 64-Bit Server VM Oracle GraalVM 21+35.1 (build 21+35-jvmci-23.1-b15, mixed mode, sharing)"), + + // graalvm-jdk-17.0.9+11.1 + Arguments.of(new Version("17.0.9"), new Version("17.0.9"), "java version \"17.0.9\" 2023-10-17 LTS\n" + + "Java(TM) SE Runtime Environment Oracle GraalVM 17.0.9+11.1 (build 17.0.9+11-LTS-jvmci-23.0-b21)\n" + + "Java HotSpot(TM) 64-Bit Server VM Oracle GraalVM 17.0.9+11.1 (build 17.0.9+11-LTS-jvmci-23.0-b21, mixed mode, sharing)"), + + // graalvm-jdk-21.0.1+12.1 + Arguments.of(new Version("21.0.1"), new Version("21.0.1"), "java version \"21.0.1\" 2023-10-17\n" + + "Java(TM) SE Runtime Environment Oracle GraalVM 21.0.1+12.1 (build 21.0.1+12-jvmci-23.1-b19)\n" + + "Java HotSpot(TM) 64-Bit Server VM Oracle GraalVM 21.0.1+12.1 (build 21.0.1+12-jvmci-23.1-b19, mixed mode, sharing)"), + + Arguments.of(new Version("17.0.7"), new Version("17.0.7"), "openjdk version \"17.0.7\" 2023-04-18\n" + + "OpenJDK Runtime Environment GraalVM CE 17.0.7+7.1 (build 17.0.7+7-jvmci-23.0-b12)\n" + + "OpenJDK 64-Bit Server VM GraalVM CE 17.0.7+7.1 (build 17.0.7+7-jvmci-23.0-b12, mixed mode, sharing)"), + + Arguments.of(new Version("21"), new Version("21"), "openjdk version \"21\" 2023-09-19\n" + + "OpenJDK Runtime Environment GraalVM CE 21+35.1 (build 21+35-jvmci-23.1-b15)\n" + + "OpenJDK 64-Bit Server VM GraalVM CE 21+35.1 (build 21+35-jvmci-23.1-b15, mixed mode, sharing)") + + ); + } + }