Skip to content

Commit 3f52b88

Browse files
nielsdebruingithub-actions[bot]timtebeek
authored
Add recipe for updating SDKMAN configuration (#649)
* Add new UpdateSDK recipe * Update src/main/java/org/openrewrite/java/migrate/UpdateSdkMan.java Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update src/test/java/org/openrewrite/java/migrate/UpdateSdkManTest.java Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update src/test/java/org/openrewrite/java/migrate/UpdateSdkManTest.java Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update src/main/java/org/openrewrite/java/migrate/UpdateSdkMan.java Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Fix code * Inline `getSdkManJDKs()` to use static field * Demonstrate issue with some Azul images * Reduce to the bare minimum and support suffixed versions * Read SDKMAN! Java candidates from a file maintained on disk * No need for Gradle when using curl * Include `UpdateSdkMan` with `UpgradeJavaVersion` * Neither option should be required if nullable --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Tim te Beek <[email protected]>
1 parent ea8167f commit 3f52b88

File tree

5 files changed

+404
-1
lines changed

5 files changed

+404
-1
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
name: Update SDKMAN! candidates
3+
4+
on:
5+
workflow_dispatch: {}
6+
schedule:
7+
- cron: 0 10 * * MON
8+
9+
jobs:
10+
update-candidates:
11+
if: github.event_name != 'schedule' || github.repository_owner == 'openrewrite'
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Checkout
15+
uses: actions/checkout@v4
16+
with:
17+
fetch-depth: 0
18+
19+
- name: Update candidates
20+
run: curl https://api.sdkman.io/2/candidates/java/linux/versions/all | tr , '\n' > src/main/resources/sdkman-java.csv
21+
22+
# Commit and push
23+
- name: configure-git-user
24+
run: |
25+
git config user.email "[email protected]"
26+
git config user.name "team-moderne[bot]"
27+
- name: Create timestamp
28+
run: echo "NOW=$(date +'%Y-%m-%dT%H%M')" >> $GITHUB_ENV
29+
- name: Commit and push
30+
run: |
31+
git add src/main/resources/sdkman-java.csv
32+
git diff --quiet HEAD || (git commit -m "[Auto] SDKMAN! Java candidates as of ${{ env.NOW }}" && git push origin main)
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
* <p>
4+
* Licensed under the Moderne Source Available License (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://docs.moderne.io/licensing/moderne-source-available-license
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.openrewrite.java.migrate;
17+
18+
import lombok.EqualsAndHashCode;
19+
import lombok.Value;
20+
import org.jspecify.annotations.Nullable;
21+
import org.openrewrite.*;
22+
import org.openrewrite.binary.Binary;
23+
import org.openrewrite.quark.Quark;
24+
import org.openrewrite.remote.Remote;
25+
import org.openrewrite.text.PlainText;
26+
import org.openrewrite.text.PlainTextParser;
27+
28+
import java.io.IOException;
29+
import java.net.URISyntaxException;
30+
import java.net.URL;
31+
import java.nio.file.Files;
32+
import java.nio.file.Paths;
33+
import java.util.List;
34+
import java.util.regex.Matcher;
35+
import java.util.regex.Pattern;
36+
37+
import static java.util.Objects.requireNonNull;
38+
39+
@Value
40+
@EqualsAndHashCode(callSuper = false)
41+
public class UpdateSdkMan extends Recipe {
42+
43+
@Option(displayName = "Java version",
44+
description = "The Java version to update to.",
45+
required = false,
46+
example = "17")
47+
@Nullable
48+
String newVersion;
49+
50+
@Option(displayName = "Distribution",
51+
description = "The JVM distribution to use.",
52+
required = false,
53+
example = "tem")
54+
@Nullable
55+
String newDistribution;
56+
57+
@Override
58+
public String getDisplayName() {
59+
return "Update SDKMan Java version";
60+
}
61+
62+
@Override
63+
public String getDescription() {
64+
//language=markdown
65+
return "Update the SDKMAN JDK version in the `.sdkmanrc` file. Given a major release (e.g., 17), the recipe " +
66+
"will update the current distribution to the current default SDKMAN version of the specified major " +
67+
"release. The distribution option can be used to specify a specific JVM distribution. " +
68+
"Note that these must correspond to valid SDKMAN distributions.";
69+
}
70+
71+
@Override
72+
public Validated<Object> validate(ExecutionContext ctx) {
73+
return super.validate(ctx)
74+
.and(Validated.required("newVersion", newVersion)
75+
.or(Validated.required("newDistribution", newDistribution)));
76+
}
77+
78+
@Override
79+
public TreeVisitor<?, ExecutionContext> getVisitor() {
80+
TreeVisitor<?, ExecutionContext> visitor = new TreeVisitor<Tree, ExecutionContext>() {
81+
@Override
82+
public Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
83+
SourceFile sourceFile = (SourceFile) requireNonNull(tree);
84+
if (sourceFile instanceof Quark || sourceFile instanceof Remote || sourceFile instanceof Binary) {
85+
return sourceFile;
86+
}
87+
PlainText plainText = PlainTextParser.convert(sourceFile);
88+
89+
// Define a regex pattern to extract the version and distribution
90+
Pattern pattern = Pattern.compile("java=(.*?)([.a-z]*-.*)");
91+
Matcher matcher = pattern.matcher(plainText.getText());
92+
if (matcher.find()) {
93+
String ver = newVersion == null ? matcher.group(1) : newVersion;
94+
String dist = newDistribution == null ? matcher.group(2) : newDistribution;
95+
for (String candidate : readSdkmanJavaCandidates()) {
96+
if (candidate.startsWith(ver) && candidate.endsWith(dist)) {
97+
return plainText.withText(matcher.replaceFirst("java=" + candidate));
98+
}
99+
}
100+
}
101+
return sourceFile;
102+
}
103+
104+
private List<String> readSdkmanJavaCandidates() {
105+
URL resource = getClass().getResource("/sdkman-java.csv");
106+
if (resource != null) {
107+
try {
108+
return Files.readAllLines(Paths.get(resource.toURI()));
109+
} catch (IOException | URISyntaxException e) {
110+
throw new RuntimeException(e);
111+
}
112+
}
113+
throw new IllegalStateException("Could not find /sdkman-java.csv file");
114+
}
115+
};
116+
return Preconditions.check(new FindSourceFiles(".sdkmanrc"), visitor);
117+
}
118+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ public List<Recipe> getRecipeList() {
5959
new UseMavenCompilerPluginReleaseConfiguration(version),
6060
new UpdateMavenProjectPropertyJavaVersion(version),
6161
new org.openrewrite.jenkins.UpgradeJavaVersion(version, null),
62-
new UpdateJavaCompatibility(version, null, null, false, null)
62+
new UpdateJavaCompatibility(version, null, null, false, null),
63+
new UpdateSdkMan(String.valueOf(version), null)
6364
);
6465
}
6566

src/main/resources/sdkman-java.csv

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
11.0.14.1-jbr
2+
11.0.15-trava
3+
11.0.25-albba
4+
11.0.25-amzn
5+
11.0.25-kona
6+
11.0.25-librca
7+
11.0.25-ms
8+
11.0.25-sapmchn
9+
11.0.25-sem
10+
11.0.25-tem
11+
11.0.25-zulu
12+
11.0.25.fx-librca
13+
11.0.25.fx-zulu
14+
17.0.12-graal
15+
17.0.12-jbr
16+
17.0.12-oracle
17+
17.0.13-albba
18+
17.0.13-amzn
19+
17.0.13-kona
20+
17.0.13-librca
21+
17.0.13-ms
22+
17.0.13-sapmchn
23+
17.0.13-sem
24+
17.0.13-tem
25+
17.0.13-zulu
26+
17.0.13.crac-librca
27+
17.0.13.crac-zulu
28+
17.0.13.fx-librca
29+
17.0.13.fx-zulu
30+
17.0.9-graalce
31+
21.0.2-graalce
32+
21.0.2-open
33+
21.0.5-amzn
34+
21.0.5-graal
35+
21.0.5-jbr
36+
21.0.5-kona
37+
21.0.5-librca
38+
21.0.5-ms
39+
21.0.5-oracle
40+
21.0.5-sapmchn
41+
21.0.5-sem
42+
21.0.5-tem
43+
21.0.5-zulu
44+
21.0.5.crac-librca
45+
21.0.5.crac-zulu
46+
21.0.5.fx-librca
47+
21.0.5.fx-zulu
48+
22.0.2-oracle
49+
22.1.0.1.r11-gln
50+
22.1.0.1.r17-gln
51+
22.3.5.r11-nik
52+
22.3.5.r17-mandrel
53+
22.3.5.r17-nik
54+
23-open
55+
23.0.1-amzn
56+
23.0.1-graal
57+
23.0.1-graalce
58+
23.0.1-librca
59+
23.0.1-oracle
60+
23.0.1-sapmchn
61+
23.0.1-tem
62+
23.0.1-zulu
63+
23.0.1.crac-zulu
64+
23.0.1.fx-librca
65+
23.0.1.fx-zulu
66+
23.0.6.fx-nik
67+
23.0.6.r17-mandrel
68+
23.0.6.r17-nik
69+
23.1.5.fx-nik
70+
23.1.5.r21-mandrel
71+
23.1.5.r21-nik
72+
24.0.2.r22-mandrel
73+
24.1.1.r23-mandrel
74+
24.1.1.r23-nik
75+
24.ea.22-graal
76+
24.ea.23-graal
77+
24.ea.24-graal
78+
24.ea.25-graal
79+
24.ea.26-open
80+
24.ea.27-open
81+
24.ea.28-open
82+
24.ea.29-open
83+
25.ea.1-graal
84+
25.ea.1-open
85+
25.ea.2-graal
86+
25.ea.2-open
87+
25.ea.3-open
88+
6.0.119-zulu
89+
7.0.352-zulu
90+
8.0.282-trava
91+
8.0.432-albba
92+
8.0.432-amzn
93+
8.0.432-kona
94+
8.0.432-librca
95+
8.0.432-sem
96+
8.0.432-tem
97+
8.0.432-zulu
98+
8.0.432.fx-librca
99+
8.0.432.fx-zulu

0 commit comments

Comments
 (0)