Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 10 additions & 2 deletions src/main/java/org/openrewrite/java/migrate/UpdateSdkMan.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@
@Value
public class UpdateSdkMan extends Recipe {

/**
* Candidates carry the vendor build between the version and the distribution, as in `17.0.20+1.1-zulu`, and that
* lands in the qualifier the distribution is matched against. Only a build is tolerated ahead of the distribution,
* so a variant such as `.crac-zulu` is still a distinct distribution rather than a match for plain `-zulu`.
*/
private static final String BUILD_METADATA = "(\\+[^-]*)?";

@Option(displayName = "Java version",
description = "The Java version to update to. Use `latest.patch` to upgrade to the latest version within the current major version.",
required = false,
Expand Down Expand Up @@ -96,13 +103,14 @@ public Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
String dist = newDistribution == null ? matcher.group(2) : "-" + newDistribution;
String newBasis = ver + dist;
Pattern majorPattern = Pattern.compile("^" + ver + "[.-].*");
LatestRelease releaseComparator = new LatestRelease(dist);
String distPattern = BUILD_METADATA + dist;
LatestRelease releaseComparator = new LatestRelease(distPattern);
String idealCandidate = readSdkmanJavaCandidates().stream()
.filter(candidate -> majorPattern.matcher(candidate).matches())
.filter(candidate -> releaseComparator.isValid(newBasis, candidate))
.max(releaseComparator)
.orElse(null);
if (idealCandidate != null && !isRequestedDowngrade(matcher.group(1) + dist, idealCandidate, dist)) {
if (idealCandidate != null && !isRequestedDowngrade(matcher.group(1) + dist, idealCandidate, distPattern)) {
return plainText.withText(matcher.replaceFirst("java=" + idealCandidate));
}
}
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/org/openrewrite/java/migrate/UpdateSdkManTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,24 @@ void zuluNonCrac() {
"""
java=11.0.28-zulu
""",
spec -> spec.path(".sdkmanrc")
.after(str -> assertThat(str)
.startsWith("java=17.0.")
.endsWith("-zulu")
.doesNotContain(".crac")
.actual())
)
);
}

@Test
void distributionIsStillMatchedWhenTheCandidateCarriesAVendorBuild() {
rewriteRun(
spec -> spec.recipe(new UpdateSdkMan("17", null)),
text(
"""
java=11.0.28+1.1-zulu
""",
spec -> spec.path(".sdkmanrc")
.after(str -> assertThat(str)
.startsWith("java=17.0.")
Expand Down
Loading