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
11 changes: 9 additions & 2 deletions src/main/java/org/openrewrite/java/migrate/UpdateSdkMan.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
public class UpdateSdkMan extends Recipe {

@Option(displayName = "Java version",
description = "The Java version to update to.",
description = "The Java version to update to. Use `latest.patch` to upgrade to the latest version within the current major version.",
required = false,
example = "17")
@Nullable
Expand Down Expand Up @@ -85,7 +85,14 @@ public Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
Pattern pattern = Pattern.compile("java=(.*?)([.a-z]*-.*)");
Matcher matcher = pattern.matcher(plainText.getText());
if (matcher.find()) {
String ver = newVersion == null ? matcher.group(1) : newVersion;
String ver;
if ("latest.patch".equalsIgnoreCase(newVersion)) {
ver = matcher.group(1).split("\\.")[0];
} else if (newVersion == null) {
ver = matcher.group(1);
} else {
ver = newVersion;
}
String dist = newDistribution == null ? matcher.group(2) : "-" + newDistribution;
String newBasis = ver + dist;
Pattern majorPattern = Pattern.compile("^" + ver + "[.-].*");
Expand Down
52 changes: 52 additions & 0 deletions src/test/java/org/openrewrite/java/migrate/UpdateSdkManTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,58 @@ void upgradeIfNewVersionIsStillSameVersionBasisAndDifferentDistribution() {
);
}

@Test
void minorUpgradesWithinSameMajorVersion() {
rewriteRun(
spec -> spec.recipe(new UpdateSdkMan("latest.patch", null)),
text(
"""
java=21.0.6-zulu
""",
spec -> spec.path(".sdkmanrc")
.after(str -> assertThat(str)
.startsWith("java=21.0.")
.doesNotContain("21.0.6")
.endsWith("-zulu")
.actual())
)
);
}

@Test
void minorUpgradeWithNewDistribution() {
rewriteRun(
spec -> spec.recipe(new UpdateSdkMan("latest.patch", "amzn")),
text(
"""
java=21.0.6-zulu
""",
spec -> spec.path(".sdkmanrc")
.after(str -> assertThat(str)
.startsWith("java=21.0.")
.endsWith("-amzn")
.actual())
)
);
}

@Test
void minorDoesNotCrossMajorVersion() {
rewriteRun(
spec -> spec.recipe(new UpdateSdkMan("latest.patch", null)),
text(
"""
java=11.0.25-tem
""",
spec -> spec.path(".sdkmanrc")
.after(str -> assertThat(str)
.startsWith("java=11.0.")
.endsWith("-tem")
.actual())
)
);
}

@Test
void doNotDowngradeVersionIfAlreadyHighEnoughSameDistribution() {
rewriteRun(
Expand Down
Loading