|
| 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 | +} |
0 commit comments