Skip to content

Commit

Permalink
Introduce version check in base codestarts and use them
Browse files Browse the repository at this point in the history
  • Loading branch information
ia3andy committed Jun 14, 2024
1 parent 59bad99 commit 16d9c88
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@
<goal>build</goal>
<goal>generate-code</goal>
<goal>generate-code-tests</goal>
{#if generate-native}
{#if generate-native and quarkus.platform.version.compareVersionTo("3.12") >= 0}
<goal>native-image-agent</goal>
{/if}
</goals>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
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 com.fasterxml.jackson.core.Version;

import io.quarkus.devtools.codestarts.CodestartException;
import io.quarkus.devtools.codestarts.CodestartResource;
import io.quarkus.devtools.codestarts.CodestartResource.Source;
Expand All @@ -30,6 +34,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 = "<SKIP>";
private static final Pattern VERSION_PATTERN = Pattern.compile("^(\\d+)(\\.(\\d+))?(\\.(\\d+))?.*");

@Override
public boolean matches(String fileName) {
Expand Down Expand Up @@ -187,9 +192,39 @@ public CompletionStage<Object> 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 Version currentVersion = extractVersion(currentVersionString);
final Version comparedVersion = extractVersion(comparedVersionString);
return currentVersion.compareTo(comparedVersion);
}

static Version extractVersion(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);
}
final String[] split = version.split("\\.");
return new Version(Integer.parseInt(split[0]), split.length > 1 ? Integer.parseInt(split[1]) : 0,
split.length > 2 ? Integer.parseInt(split[2]) : 0, null, null, null);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.quarkus.devtools.codestarts.core.reader;

import static io.quarkus.devtools.codestarts.core.reader.QuteCodestartFileReader.*;
import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.core.Version;

class QuteCodestartFileReaderTest {

@Test
void testExtractVersion() {
assertThat(extractVersion("3.12.3.Final")).isEqualTo(new Version(3, 12, 3, null, null, null));
assertThat(extractVersion("3.12.1")).isEqualTo(new Version(3, 12, 1, null, null, null));
assertThat(extractVersion("3.12")).isEqualTo(new Version(3, 12, 0, null, null, null));
assertThat(extractVersion("3")).isEqualTo(new Version(3, 0, 0, null, null, null));
}

@Test
void testCompareVersion() {
assertThat(compareVersionTo("3.12.0.Final", "1.0")).isGreaterThan(0);
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);
}
}

0 comments on commit 16d9c88

Please sign in to comment.