Skip to content

Commit

Permalink
properly sort archetype version
Browse files Browse the repository at this point in the history
Signed-off-by: tvallin <[email protected]>
  • Loading branch information
tvallin committed Aug 28, 2023
1 parent a61a6dd commit 40c9ac0
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 11 deletions.
10 changes: 5 additions & 5 deletions cli/common/src/main/java/io/helidon/build/cli/common/SemVer.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
*/
final class SemVer {

private static final Pattern MAJOR_PATTERN = Pattern.compile("^(?<major>[0-9]+).+$");
private static final Pattern MAJOR_PATTERN = Pattern.compile("^(?<major>[0-9]+)(.*)");

private SemVer() {
}
Expand All @@ -49,14 +49,14 @@ private SemVer() {
static List<String> latestMajorVersions(List<ArchetypesData.Version> versions) {
List<String> ids = versions.stream().map(ArchetypesData.Version::id).collect(Collectors.toList());
// versions grouped by major digit
Map<String, List<String>> groups = Lists.mappedBy(ids, SemVer::majorDigit);
Map<String, List<String>> groups = Lists.mappedBy(ids, SemVer::lowerVersionFromMajorDigit);

// Maven versions grouped by version range
Map<VersionRange, List<MavenVersion>> ranges =
Maps.mapEntry(groups, Strings::isValid, VersionRange::higherOrEqual, MavenVersion::toMavenVersion);

// the latest of each group
Collection<MavenVersion> latest = Maps.mapEntryValue(ranges, entry->entry.getKey().resolveLatest(entry.getValue()))
Collection<MavenVersion> latest = Maps.mapEntryValue(ranges, entry -> entry.getKey().resolveLatest(entry.getValue()))
.values();
List<ArchetypesData.Version> latestVersions = versions.stream()
.filter(version -> latest.contains(version.toMavenVersion()))
Expand All @@ -72,10 +72,10 @@ static List<ArchetypesData.Version> sortVersions(List<ArchetypesData.Version> ve
return versions;
}

private static String majorDigit(String version) {
private static String lowerVersionFromMajorDigit(String version) {
Matcher matcher = MAJOR_PATTERN.matcher(version);
if (matcher.find()) {
return matcher.group("major");
return matcher.group("major") + "-SNAPSHOT";
}
return "";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,30 @@ public void testVersionOrdering() {
Matchers.is(List.of("2.0.0", "1.0.1", "4.0.0")));
}

@Test
public void testVersionQualifierOrdering() {
String version1;
String version2;
List<String> versions = List.of(
"0-SNAPSHOT",
"0-ALPHA",
"0-BETA",
"0-MILESTONE",
"0-RC",
"0",
"0-sp");

for (int i = 0; i < versions.size() - 1; i += 2) {
version1 = versions.get(i);
version2 = versions.get(i + 1);
assertThat(String.format("%s should be picked over %s", version1, version2),
data(version(version1, 100),
version(version2, 100))
.latestMajorVersions(),
Matchers.is(List.of(version2)));
}
}

private static ArchetypesData data(ArchetypesData.Version... versions) {
return ArchetypesData.builder().versions(versions).build();
}
Expand Down
6 changes: 3 additions & 3 deletions common/common/src/main/java/io/helidon/build/common/Maps.java
Original file line number Diff line number Diff line change
Expand Up @@ -432,9 +432,9 @@ public static <T, U, K, V> Map<K, List<V>> mapEntry(
Function<T, K> keyMapper,
Function<U, V> valueMapper) {
return map.entrySet().stream()
.filter(entry-> keyFilter.test(entry.getKey()))
.filter(entry -> keyFilter.test(entry.getKey()))
.collect(Collectors.toMap(
entry->keyMapper.apply(entry.getKey()),
entry->entry.getValue().stream().map(valueMapper).collect(Collectors.toList())));
entry -> keyMapper.apply(entry.getKey()),
entry -> entry.getValue().stream().map(valueMapper).collect(Collectors.toList())));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, 2022 Oracle and/or its affiliates.
* Copyright (c) 2021, 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -259,11 +259,11 @@ public String toString() {
private static class StringItem implements Item {

private static final List<String> QUALIFIERS = Arrays.asList(
"snapshot",
"alpha",
"beta",
"milestone",
"rc",
"snapshot",
"",
"sp");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2021 Oracle and/or its affiliates.
* Copyright (c) 2020, 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,6 +16,8 @@

package io.helidon.build.common.maven;

import java.util.List;

import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -82,4 +84,25 @@ void testReleaseCandidateVersions() {
assertThat(toMavenVersion("2.0.0-RC2"), is(org.hamcrest.Matchers.greaterThan(toMavenVersion("2.0.0-RC1"))));
assertThat(toMavenVersion("2.0.0"), is(org.hamcrest.Matchers.greaterThan(toMavenVersion("2.0.0-RC2"))));
}

@Test
void testQualifierComparison() {
MavenVersion version1;
MavenVersion version2;
List<MavenVersion> versions = List.of(
toMavenVersion("0-SNAPSHOT"),
toMavenVersion("0-ALPHA"),
toMavenVersion("0-BETA"),
toMavenVersion("0-MILESTONE"),
toMavenVersion("0-RC"),
toMavenVersion("0"),
toMavenVersion("0-sp"));

for (int i = 0; i < versions.size() - 1; i += 2) {
version1 = versions.get(i);
version2 = versions.get(i + 1);
assertThat(String.format("%s should be lower than %s", version1, version2),
version1, is(org.hamcrest.Matchers.lessThan(version2)));
}
}
}

0 comments on commit 40c9ac0

Please sign in to comment.