From b8bf36d46b8a1481ddbcd262f4726c78f343ecbc Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Fri, 27 Dec 2024 14:15:40 +0100 Subject: [PATCH] Read SDKMAN! Java candidates from a file maintained on disk --- .github/workflows/sdkman-candidates.yml | 37 +++++++ .../java/migrate/UpdateSdkMan.java | 31 +++--- src/main/resources/sdkman-java.csv | 99 +++++++++++++++++++ 3 files changed, 155 insertions(+), 12 deletions(-) create mode 100644 .github/workflows/sdkman-candidates.yml create mode 100644 src/main/resources/sdkman-java.csv diff --git a/.github/workflows/sdkman-candidates.yml b/.github/workflows/sdkman-candidates.yml new file mode 100644 index 0000000000..011213154b --- /dev/null +++ b/.github/workflows/sdkman-candidates.yml @@ -0,0 +1,37 @@ +--- +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 + - uses: actions/setup-java@v4 + with: + cache: 'gradle' + distribution: 'temurin' + java-version: '17' + + - 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 "team@moderne.io" + 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) diff --git a/src/main/java/org/openrewrite/java/migrate/UpdateSdkMan.java b/src/main/java/org/openrewrite/java/migrate/UpdateSdkMan.java index 979ddefe70..c540c2ef06 100644 --- a/src/main/java/org/openrewrite/java/migrate/UpdateSdkMan.java +++ b/src/main/java/org/openrewrite/java/migrate/UpdateSdkMan.java @@ -25,7 +25,11 @@ import org.openrewrite.text.PlainText; import org.openrewrite.text.PlainTextParser; -import java.util.Arrays; +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; @@ -35,15 +39,6 @@ @Value @EqualsAndHashCode(callSuper = false) public class UpdateSdkMan extends Recipe { - private static final String SDKMAN_CONFIG_FILE_PATTERN = ".sdkmanrc"; - - /** - * Retrieve from https://api.sdkman.io/2/candidates/java/linuxx64/versions/all - * TODO: Investifate the best way to get the latest versions from SDKMAN - */ - private static final List SDKMAN_CANDIDATES_LIST = Arrays.asList( - "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.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-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" - .split(",")); @Option(displayName = "Java version", description = "The Java version to update to.", example = "17") @Nullable @@ -91,7 +86,7 @@ public Tree visit(@Nullable Tree tree, ExecutionContext ctx) { if (matcher.find()) { String ver = newVersion == null ? matcher.group(1) : newVersion; String dist = newDistribution == null ? matcher.group(2) : newDistribution; - for (String candidate : SDKMAN_CANDIDATES_LIST) { + for (String candidate : readSdkmanJavaCandidates()) { if (candidate.startsWith(ver) && candidate.endsWith(dist)) { return plainText.withText(matcher.replaceFirst("java=" + candidate)); } @@ -99,7 +94,19 @@ public Tree visit(@Nullable Tree tree, ExecutionContext ctx) { } return sourceFile; } + + private List 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(SDKMAN_CONFIG_FILE_PATTERN), visitor); + return Preconditions.check(new FindSourceFiles(".sdkmanrc"), visitor); } } diff --git a/src/main/resources/sdkman-java.csv b/src/main/resources/sdkman-java.csv new file mode 100644 index 0000000000..ed0b5eee77 --- /dev/null +++ b/src/main/resources/sdkman-java.csv @@ -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 \ No newline at end of file