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
18 changes: 12 additions & 6 deletions src/main/java/org/openrewrite/java/migrate/UpdateSdkMan.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.openrewrite.binary.Binary;
import org.openrewrite.quark.Quark;
import org.openrewrite.remote.Remote;
import org.openrewrite.semver.LatestRelease;
import org.openrewrite.text.PlainText;
import org.openrewrite.text.PlainTextParser;

Expand Down Expand Up @@ -92,11 +93,17 @@ public Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
Matcher matcher = pattern.matcher(plainText.getText());
if (matcher.find()) {
String ver = newVersion == null ? matcher.group(1) : newVersion;
String dist = newDistribution == null ? matcher.group(2) : newDistribution;
for (String candidate : readSdkmanJavaCandidates()) {
if (candidate.startsWith(ver) && candidate.endsWith(dist)) {
return plainText.withText(matcher.replaceFirst("java=" + candidate));
}
String dist = newDistribution == null ? matcher.group(2) : "-" + newDistribution;
String newBasis = ver + dist;
Pattern majorPattern = Pattern.compile("^" + ver + "[.-].*");
LatestRelease releaseComparator = new LatestRelease(dist);
String idealCandidate = readSdkmanJavaCandidates().stream()
.filter(candidate -> majorPattern.matcher(candidate).matches())
.filter(candidate -> releaseComparator.isValid(newBasis, candidate))
.max(releaseComparator)
.orElse(null);
if (idealCandidate != null) {
return plainText.withText(matcher.replaceFirst("java=" + idealCandidate));
}
}
return sourceFile;
Expand All @@ -110,7 +117,6 @@ private List<String> readSdkmanJavaCandidates() {
} catch (IOException e) {
throw new RuntimeException(e);
}

}
};
return Preconditions.check(new FindSourceFiles(".sdkmanrc"), visitor);
Expand Down
68 changes: 42 additions & 26 deletions src/test/java/org/openrewrite/java/migrate/UpdateSdkManTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ void updateVersionUsingMajorOnly() {
spec -> spec.recipe(new UpdateSdkMan("17", null)),
text(
"""
java=11.0.25-tem
""",
java=11.0.25-tem
""",
"""
java=17.0.14-tem
""",
java=17.0.15-tem
""",
spec -> spec.path(".sdkmanrc")
)
);
Expand All @@ -49,11 +49,11 @@ void updateVersionExact() {
spec -> spec.recipe(new UpdateSdkMan("17.0.14", null)),
text(
"""
java=11.1.2-tem
""",
java=11.1.2-tem
""",
"""
java=17.0.14-tem
""",
java=17.0.14-tem
""",
spec -> spec.path(".sdkmanrc")
)
);
Expand All @@ -65,11 +65,11 @@ void updateDistributionOnly() {
spec -> spec.recipe(new UpdateSdkMan(null, "amzn")),
text(
"""
java=11.0.26-tem
""",
java=11.0.26-tem
""",
"""
java=11.0.26-amzn
""",
java=11.0.26-amzn
""",
spec -> spec.path(".sdkmanrc")
)
);
Expand All @@ -81,11 +81,11 @@ void updateBoth() {
spec -> spec.recipe(new UpdateSdkMan("17", "graalce")),
text(
"""
java=11.0.25-amzn
""",
java=11.0.25-amzn
""",
"""
java=17.0.9-graalce
""",
java=17.0.9-graalce
""",
spec -> spec.path(".sdkmanrc")
)
);
Expand All @@ -97,8 +97,8 @@ void nonExistingVersion() {
spec -> spec.recipe(new UpdateSdkMan("42", null)),
text(
"""
java=11.1.2-tem
""",
java=11.1.2-tem
""",
spec -> spec.path(".sdkmanrc")
)
);
Expand All @@ -110,8 +110,8 @@ void nonExistingDist() {
spec -> spec.recipe(new UpdateSdkMan(null, "notreal")),
text(
"""
java=11.1.2-tem
""",
java=11.1.2-tem
""",
spec -> spec.path(".sdkmanrc")
)
);
Expand All @@ -128,8 +128,8 @@ void onlyUpdateSdkManRCFiles() {
spec -> spec.recipe(new UpdateSdkMan("17", "tem")),
text(
"""
java=11.1.2-tem
""",
java=11.1.2-tem
""",
spec -> spec.path(".not-sdkmanrc")
)
);
Expand All @@ -141,11 +141,27 @@ void nonNumericalVersionPart() {
spec -> spec.recipe(new UpdateSdkMan("17", null)),
text(
"""
java=11.0.25.fx-zulu
""",
java=11.0.25.fx-zulu
""",
"""
java=17.0.14.fx-zulu
""",
java=17.0.15.fx-zulu
""",
spec -> spec.path(".sdkmanrc")
)
);
}

@Test
void zuluNonCrac() {
rewriteRun(
spec -> spec.recipe(new UpdateSdkMan("17", null)),
text(
"""
java=11.0.26-zulu
""",
"""
java=17.0.15-zulu
""",
spec -> spec.path(".sdkmanrc")
)
);
Expand Down