forked from openrewrite/rewrite-migrate-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add recipe for updating SDKMAN configuration (openrewrite#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]>
- Loading branch information
1 parent
ea8167f
commit 3f52b88
Showing
5 changed files
with
404 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
--- | ||
name: Update SDKMAN! candidates | ||
|
||
on: | ||
workflow_dispatch: {} | ||
schedule: | ||
- cron: 0 10 * * MON | ||
|
||
jobs: | ||
update-candidates: | ||
if: github.event_name != 'schedule' || github.repository_owner == 'openrewrite' | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v4 | ||
with: | ||
fetch-depth: 0 | ||
|
||
- name: Update candidates | ||
run: curl https://api.sdkman.io/2/candidates/java/linux/versions/all | tr , '\n' > src/main/resources/sdkman-java.csv | ||
|
||
# Commit and push | ||
- name: configure-git-user | ||
run: | | ||
git config user.email "[email protected]" | ||
git config user.name "team-moderne[bot]" | ||
- name: Create timestamp | ||
run: echo "NOW=$(date +'%Y-%m-%dT%H%M')" >> $GITHUB_ENV | ||
- name: Commit and push | ||
run: | | ||
git add src/main/resources/sdkman-java.csv | ||
git diff --quiet HEAD || (git commit -m "[Auto] SDKMAN! Java candidates as of ${{ env.NOW }}" && git push origin main) |
118 changes: 118 additions & 0 deletions
118
src/main/java/org/openrewrite/java/migrate/UpdateSdkMan.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* <p> | ||
* Licensed under the Moderne Source Available License (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p> | ||
* https://docs.moderne.io/licensing/moderne-source-available-license | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.openrewrite.java.migrate; | ||
|
||
import lombok.EqualsAndHashCode; | ||
import lombok.Value; | ||
import org.jspecify.annotations.Nullable; | ||
import org.openrewrite.*; | ||
import org.openrewrite.binary.Binary; | ||
import org.openrewrite.quark.Quark; | ||
import org.openrewrite.remote.Remote; | ||
import org.openrewrite.text.PlainText; | ||
import org.openrewrite.text.PlainTextParser; | ||
|
||
import java.io.IOException; | ||
import java.net.URISyntaxException; | ||
import java.net.URL; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.util.List; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
@Value | ||
@EqualsAndHashCode(callSuper = false) | ||
public class UpdateSdkMan extends Recipe { | ||
|
||
@Option(displayName = "Java version", | ||
description = "The Java version to update to.", | ||
required = false, | ||
example = "17") | ||
@Nullable | ||
String newVersion; | ||
|
||
@Option(displayName = "Distribution", | ||
description = "The JVM distribution to use.", | ||
required = false, | ||
example = "tem") | ||
@Nullable | ||
String newDistribution; | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return "Update SDKMan Java version"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
//language=markdown | ||
return "Update the SDKMAN JDK version in the `.sdkmanrc` file. Given a major release (e.g., 17), the recipe " + | ||
"will update the current distribution to the current default SDKMAN version of the specified major " + | ||
"release. The distribution option can be used to specify a specific JVM distribution. " + | ||
"Note that these must correspond to valid SDKMAN distributions."; | ||
} | ||
|
||
@Override | ||
public Validated<Object> validate(ExecutionContext ctx) { | ||
return super.validate(ctx) | ||
.and(Validated.required("newVersion", newVersion) | ||
.or(Validated.required("newDistribution", newDistribution))); | ||
} | ||
|
||
@Override | ||
public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
TreeVisitor<?, ExecutionContext> visitor = new TreeVisitor<Tree, ExecutionContext>() { | ||
@Override | ||
public Tree visit(@Nullable Tree tree, ExecutionContext ctx) { | ||
SourceFile sourceFile = (SourceFile) requireNonNull(tree); | ||
if (sourceFile instanceof Quark || sourceFile instanceof Remote || sourceFile instanceof Binary) { | ||
return sourceFile; | ||
} | ||
PlainText plainText = PlainTextParser.convert(sourceFile); | ||
|
||
// Define a regex pattern to extract the version and distribution | ||
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 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)); | ||
} | ||
} | ||
} | ||
return sourceFile; | ||
} | ||
|
||
private List<String> readSdkmanJavaCandidates() { | ||
URL resource = getClass().getResource("/sdkman-java.csv"); | ||
if (resource != null) { | ||
try { | ||
return Files.readAllLines(Paths.get(resource.toURI())); | ||
} catch (IOException | URISyntaxException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
throw new IllegalStateException("Could not find /sdkman-java.csv file"); | ||
} | ||
}; | ||
return Preconditions.check(new FindSourceFiles(".sdkmanrc"), visitor); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
11.0.14.1-jbr | ||
11.0.15-trava | ||
11.0.25-albba | ||
11.0.25-amzn | ||
11.0.25-kona | ||
11.0.25-librca | ||
11.0.25-ms | ||
11.0.25-sapmchn | ||
11.0.25-sem | ||
11.0.25-tem | ||
11.0.25-zulu | ||
11.0.25.fx-librca | ||
11.0.25.fx-zulu | ||
17.0.12-graal | ||
17.0.12-jbr | ||
17.0.12-oracle | ||
17.0.13-albba | ||
17.0.13-amzn | ||
17.0.13-kona | ||
17.0.13-librca | ||
17.0.13-ms | ||
17.0.13-sapmchn | ||
17.0.13-sem | ||
17.0.13-tem | ||
17.0.13-zulu | ||
17.0.13.crac-librca | ||
17.0.13.crac-zulu | ||
17.0.13.fx-librca | ||
17.0.13.fx-zulu | ||
17.0.9-graalce | ||
21.0.2-graalce | ||
21.0.2-open | ||
21.0.5-amzn | ||
21.0.5-graal | ||
21.0.5-jbr | ||
21.0.5-kona | ||
21.0.5-librca | ||
21.0.5-ms | ||
21.0.5-oracle | ||
21.0.5-sapmchn | ||
21.0.5-sem | ||
21.0.5-tem | ||
21.0.5-zulu | ||
21.0.5.crac-librca | ||
21.0.5.crac-zulu | ||
21.0.5.fx-librca | ||
21.0.5.fx-zulu | ||
22.0.2-oracle | ||
22.1.0.1.r11-gln | ||
22.1.0.1.r17-gln | ||
22.3.5.r11-nik | ||
22.3.5.r17-mandrel | ||
22.3.5.r17-nik | ||
23-open | ||
23.0.1-amzn | ||
23.0.1-graal | ||
23.0.1-graalce | ||
23.0.1-librca | ||
23.0.1-oracle | ||
23.0.1-sapmchn | ||
23.0.1-tem | ||
23.0.1-zulu | ||
23.0.1.crac-zulu | ||
23.0.1.fx-librca | ||
23.0.1.fx-zulu | ||
23.0.6.fx-nik | ||
23.0.6.r17-mandrel | ||
23.0.6.r17-nik | ||
23.1.5.fx-nik | ||
23.1.5.r21-mandrel | ||
23.1.5.r21-nik | ||
24.0.2.r22-mandrel | ||
24.1.1.r23-mandrel | ||
24.1.1.r23-nik | ||
24.ea.22-graal | ||
24.ea.23-graal | ||
24.ea.24-graal | ||
24.ea.25-graal | ||
24.ea.26-open | ||
24.ea.27-open | ||
24.ea.28-open | ||
24.ea.29-open | ||
25.ea.1-graal | ||
25.ea.1-open | ||
25.ea.2-graal | ||
25.ea.2-open | ||
25.ea.3-open | ||
6.0.119-zulu | ||
7.0.352-zulu | ||
8.0.282-trava | ||
8.0.432-albba | ||
8.0.432-amzn | ||
8.0.432-kona | ||
8.0.432-librca | ||
8.0.432-sem | ||
8.0.432-tem | ||
8.0.432-zulu | ||
8.0.432.fx-librca | ||
8.0.432.fx-zulu |
Oops, something went wrong.