From a9b0a74a087cc300f2143501e80c04d4b3b69522 Mon Sep 17 00:00:00 2001 From: Andy Damevin Date: Fri, 14 Jun 2024 12:20:07 +0200 Subject: [PATCH] Introduce version check in base codestarts and use them --- .../buildtool/maven/base/pom.tpl.qute.xml | 2 +- .../core/reader/QuteCodestartFileReader.java | 43 ++++++++++++++----- .../reader/QuteCodestartFileReaderTest.java | 24 +++++++++++ 3 files changed, 57 insertions(+), 12 deletions(-) create mode 100644 independent-projects/tools/codestarts/src/test/java/io/quarkus/devtools/codestarts/core/reader/QuteCodestartFileReaderTest.java diff --git a/independent-projects/tools/base-codestarts/src/main/resources/codestarts/quarkus/buildtool/maven/base/pom.tpl.qute.xml b/independent-projects/tools/base-codestarts/src/main/resources/codestarts/quarkus/buildtool/maven/base/pom.tpl.qute.xml index 511b172afc41b5..3d32afccb8efdd 100644 --- a/independent-projects/tools/base-codestarts/src/main/resources/codestarts/quarkus/buildtool/maven/base/pom.tpl.qute.xml +++ b/independent-projects/tools/base-codestarts/src/main/resources/codestarts/quarkus/buildtool/maven/base/pom.tpl.qute.xml @@ -140,7 +140,7 @@ build generate-code generate-code-tests - {#if generate-native} + {#if generate-native and quarkus.platform.version.compareVersionTo("3.12") >= 0} native-image-agent {/if} diff --git a/independent-projects/tools/codestarts/src/main/java/io/quarkus/devtools/codestarts/core/reader/QuteCodestartFileReader.java b/independent-projects/tools/codestarts/src/main/java/io/quarkus/devtools/codestarts/core/reader/QuteCodestartFileReader.java index d26b1ec9774c9e..5365404e82aefe 100644 --- a/independent-projects/tools/codestarts/src/main/java/io/quarkus/devtools/codestarts/core/reader/QuteCodestartFileReader.java +++ b/independent-projects/tools/codestarts/src/main/java/io/quarkus/devtools/codestarts/core/reader/QuteCodestartFileReader.java @@ -6,23 +6,15 @@ import java.util.Map; import java.util.Optional; import java.util.concurrent.CompletionStage; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.apache.commons.io.FilenameUtils; import io.quarkus.devtools.codestarts.CodestartException; import io.quarkus.devtools.codestarts.CodestartResource; import io.quarkus.devtools.codestarts.CodestartResource.Source; -import io.quarkus.qute.CompletedStage; -import io.quarkus.qute.Engine; -import io.quarkus.qute.EvalContext; -import io.quarkus.qute.Expression; -import io.quarkus.qute.ResultMapper; -import io.quarkus.qute.Results; -import io.quarkus.qute.TemplateException; -import io.quarkus.qute.TemplateLocator; -import io.quarkus.qute.TemplateNode; -import io.quarkus.qute.ValueResolver; -import io.quarkus.qute.Variant; +import io.quarkus.qute.*; final class QuteCodestartFileReader implements CodestartFileReader { @@ -30,6 +22,7 @@ final class QuteCodestartFileReader implements CodestartFileReader { private static final String ENTRY_QUTE_FLAG = ".entry.qute"; public static final String INCLUDE_QUTE_FLAG = ".include.qute"; public static final String SKIP_TAG = ""; + private static final Pattern VERSION_PATTERN = Pattern.compile("^((\\d+)\\.(\\d+)).*"); @Override public boolean matches(String fileName) { @@ -187,9 +180,37 @@ public CompletionStage resolve(EvalContext context) { return CompletedStage.of(value.endsWith((String) e)); }); } + + case "compareVersionTo": + if (context.getParams().size() == 1) { + return context.evaluate(context.getParams().get(0)).thenCompose(e -> { + return CompletedStage.of(compareVersionTo(value, (String) e)); + }); + } default: return Results.notFound(context); } } } + + static int compareVersionTo(String currentVersionString, String comparedVersionString) { + if (currentVersionString.contains("SNAPSHOT")) { + return 1; + } + final Runtime.Version currentVersion = Runtime.Version.parse(extractMajorMinor(currentVersionString)); + final Runtime.Version comparedVersion = Runtime.Version.parse(extractMajorMinor(comparedVersionString)); + return currentVersion.compareTo(comparedVersion); + } + + static String extractMajorMinor(String version) { + if (version == null || version.isEmpty()) { + throw new IllegalArgumentException("Version string cannot be null or empty"); + } + final Matcher matcher = VERSION_PATTERN.matcher(version); + if (!matcher.matches()) { + throw new IllegalArgumentException("Invalid version: " + version); + } + return matcher.replaceAll("$1"); + } + } diff --git a/independent-projects/tools/codestarts/src/test/java/io/quarkus/devtools/codestarts/core/reader/QuteCodestartFileReaderTest.java b/independent-projects/tools/codestarts/src/test/java/io/quarkus/devtools/codestarts/core/reader/QuteCodestartFileReaderTest.java new file mode 100644 index 00000000000000..b8a84d49ff19f7 --- /dev/null +++ b/independent-projects/tools/codestarts/src/test/java/io/quarkus/devtools/codestarts/core/reader/QuteCodestartFileReaderTest.java @@ -0,0 +1,24 @@ +package io.quarkus.devtools.codestarts.core.reader; + +import static io.quarkus.devtools.codestarts.core.reader.QuteCodestartFileReader.compareVersionTo; +import static io.quarkus.devtools.codestarts.core.reader.QuteCodestartFileReader.extractMajorMinor; +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.Test; + +class QuteCodestartFileReaderTest { + + @Test + void testExtractVersion() { + assertThat(extractMajorMinor("3.12.0.Final")).isEqualTo("3.12"); + assertThat(extractMajorMinor("3.12")).isEqualTo("3.12"); + } + + @Test + void testCompareVersion() { + assertThat(compareVersionTo("3.12.0.Final", "3.12")).isEqualTo(0); + assertThat(compareVersionTo("3.13.0", "3.12")).isGreaterThan(0); + assertThat(compareVersionTo("3.2.1", "3.12")).isLessThan(0); + assertThat(compareVersionTo("999-SNAPSHOT", "3.12")).isGreaterThan(0); + } +}