Skip to content

Commit 61019fb

Browse files
Pick better SDKMan versions (#754)
* Pick better SDKMan versions * Expanding restriction of candidate for upgrading version and correcting situation where `.crac-zulu` version could be chosen rather than `-zulu` when coming from `-zulu`. * No need for ListUtils unless it concerns a collection of LST elements --------- Co-authored-by: Steve Elliott <steve@moderne.io>
1 parent a130cbb commit 61019fb

File tree

2 files changed

+54
-32
lines changed

2 files changed

+54
-32
lines changed

src/main/java/org/openrewrite/java/migrate/UpdateSdkMan.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.openrewrite.binary.Binary;
2323
import org.openrewrite.quark.Quark;
2424
import org.openrewrite.remote.Remote;
25+
import org.openrewrite.semver.LatestRelease;
2526
import org.openrewrite.text.PlainText;
2627
import org.openrewrite.text.PlainTextParser;
2728

@@ -92,11 +93,17 @@ public Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
9293
Matcher matcher = pattern.matcher(plainText.getText());
9394
if (matcher.find()) {
9495
String ver = newVersion == null ? matcher.group(1) : newVersion;
95-
String dist = newDistribution == null ? matcher.group(2) : newDistribution;
96-
for (String candidate : readSdkmanJavaCandidates()) {
97-
if (candidate.startsWith(ver) && candidate.endsWith(dist)) {
98-
return plainText.withText(matcher.replaceFirst("java=" + candidate));
99-
}
96+
String dist = newDistribution == null ? matcher.group(2) : "-" + newDistribution;
97+
String newBasis = ver + dist;
98+
Pattern majorPattern = Pattern.compile("^" + ver + "[.-].*");
99+
LatestRelease releaseComparator = new LatestRelease(dist);
100+
String idealCandidate = readSdkmanJavaCandidates().stream()
101+
.filter(candidate -> majorPattern.matcher(candidate).matches())
102+
.filter(candidate -> releaseComparator.isValid(newBasis, candidate))
103+
.max(releaseComparator)
104+
.orElse(null);
105+
if (idealCandidate != null) {
106+
return plainText.withText(matcher.replaceFirst("java=" + idealCandidate));
100107
}
101108
}
102109
return sourceFile;
@@ -110,7 +117,6 @@ private List<String> readSdkmanJavaCandidates() {
110117
} catch (IOException e) {
111118
throw new RuntimeException(e);
112119
}
113-
114120
}
115121
};
116122
return Preconditions.check(new FindSourceFiles(".sdkmanrc"), visitor);

src/test/java/org/openrewrite/java/migrate/UpdateSdkManTest.java

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ void updateVersionUsingMajorOnly() {
3333
spec -> spec.recipe(new UpdateSdkMan("17", null)),
3434
text(
3535
"""
36-
java=11.0.25-tem
37-
""",
36+
java=11.0.25-tem
37+
""",
3838
"""
39-
java=17.0.14-tem
40-
""",
39+
java=17.0.15-tem
40+
""",
4141
spec -> spec.path(".sdkmanrc")
4242
)
4343
);
@@ -49,11 +49,11 @@ void updateVersionExact() {
4949
spec -> spec.recipe(new UpdateSdkMan("17.0.14", null)),
5050
text(
5151
"""
52-
java=11.1.2-tem
53-
""",
52+
java=11.1.2-tem
53+
""",
5454
"""
55-
java=17.0.14-tem
56-
""",
55+
java=17.0.14-tem
56+
""",
5757
spec -> spec.path(".sdkmanrc")
5858
)
5959
);
@@ -65,11 +65,11 @@ void updateDistributionOnly() {
6565
spec -> spec.recipe(new UpdateSdkMan(null, "amzn")),
6666
text(
6767
"""
68-
java=11.0.26-tem
69-
""",
68+
java=11.0.26-tem
69+
""",
7070
"""
71-
java=11.0.26-amzn
72-
""",
71+
java=11.0.26-amzn
72+
""",
7373
spec -> spec.path(".sdkmanrc")
7474
)
7575
);
@@ -81,11 +81,11 @@ void updateBoth() {
8181
spec -> spec.recipe(new UpdateSdkMan("17", "graalce")),
8282
text(
8383
"""
84-
java=11.0.25-amzn
85-
""",
84+
java=11.0.25-amzn
85+
""",
8686
"""
87-
java=17.0.9-graalce
88-
""",
87+
java=17.0.9-graalce
88+
""",
8989
spec -> spec.path(".sdkmanrc")
9090
)
9191
);
@@ -97,8 +97,8 @@ void nonExistingVersion() {
9797
spec -> spec.recipe(new UpdateSdkMan("42", null)),
9898
text(
9999
"""
100-
java=11.1.2-tem
101-
""",
100+
java=11.1.2-tem
101+
""",
102102
spec -> spec.path(".sdkmanrc")
103103
)
104104
);
@@ -110,8 +110,8 @@ void nonExistingDist() {
110110
spec -> spec.recipe(new UpdateSdkMan(null, "notreal")),
111111
text(
112112
"""
113-
java=11.1.2-tem
114-
""",
113+
java=11.1.2-tem
114+
""",
115115
spec -> spec.path(".sdkmanrc")
116116
)
117117
);
@@ -128,8 +128,8 @@ void onlyUpdateSdkManRCFiles() {
128128
spec -> spec.recipe(new UpdateSdkMan("17", "tem")),
129129
text(
130130
"""
131-
java=11.1.2-tem
132-
""",
131+
java=11.1.2-tem
132+
""",
133133
spec -> spec.path(".not-sdkmanrc")
134134
)
135135
);
@@ -141,11 +141,27 @@ void nonNumericalVersionPart() {
141141
spec -> spec.recipe(new UpdateSdkMan("17", null)),
142142
text(
143143
"""
144-
java=11.0.25.fx-zulu
145-
""",
144+
java=11.0.25.fx-zulu
145+
""",
146146
"""
147-
java=17.0.14.fx-zulu
148-
""",
147+
java=17.0.15.fx-zulu
148+
""",
149+
spec -> spec.path(".sdkmanrc")
150+
)
151+
);
152+
}
153+
154+
@Test
155+
void zuluNonCrac() {
156+
rewriteRun(
157+
spec -> spec.recipe(new UpdateSdkMan("17", null)),
158+
text(
159+
"""
160+
java=11.0.26-zulu
161+
""",
162+
"""
163+
java=17.0.15-zulu
164+
""",
149165
spec -> spec.path(".sdkmanrc")
150166
)
151167
);

0 commit comments

Comments
 (0)