-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #242 from timtebeek/rewrite-recipe-bom-3.x
Rewrite recipe bom 3.x
- Loading branch information
Showing
6 changed files
with
184 additions
and
14 deletions.
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
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
74 changes: 74 additions & 0 deletions
74
recipes/src/main/java/io/quarkus/updates/core/quarkus37/SetupJavaUpgradeJavaVersion.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,74 @@ | ||
package io.quarkus.updates.core.quarkus37; | ||
|
||
import org.openrewrite.*; | ||
import org.openrewrite.yaml.JsonPathMatcher; | ||
import org.openrewrite.yaml.YamlVisitor; | ||
import org.openrewrite.yaml.tree.Yaml; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import lombok.EqualsAndHashCode; | ||
import lombok.Value; | ||
|
||
@Value | ||
@EqualsAndHashCode(callSuper = false) | ||
public class SetupJavaUpgradeJavaVersion extends Recipe { | ||
|
||
@Option(displayName = "Java version", | ||
description = "The Java version to upgrade to.", | ||
example = "17") | ||
Integer minimumJavaMajorVersion; | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return "Upgrade `actions/setup-java` `java-version`"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Update the Java version used by `actions/setup-java` if it is below the expected version number."; | ||
} | ||
|
||
private static final JsonPathMatcher javaVersion = new JsonPathMatcher("..steps[?(@.uses =~ 'actions/setup-java@v*.*')].with.java-version"); | ||
private static final Pattern javaVersionPattern = Pattern.compile("([0-9]+)(\\.[0-9]+)*([-+].*)?"); | ||
|
||
@Override | ||
public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
return Preconditions.check( | ||
new FindSourceFiles(".github/workflows/*.yml"), | ||
new YamlVisitor<>() { | ||
@Override | ||
public Yaml visitMappingEntry(Yaml.Mapping.Entry entry, ExecutionContext ctx) { | ||
if (!javaVersion.matches(getCursor())) { | ||
return super.visitMappingEntry(entry, ctx); | ||
} | ||
|
||
Yaml.Scalar currentValue = (Yaml.Scalar) entry.getValue(); | ||
|
||
// specific versions are allowed by `actions/setup-java` | ||
Matcher matcher = javaVersionPattern.matcher(currentValue.getValue()); | ||
if (!matcher.matches()) { | ||
return super.visitMappingEntry(entry, ctx); | ||
} | ||
|
||
int currentMajorVersion; | ||
try { | ||
currentMajorVersion = Integer.parseInt(matcher.group(1)); | ||
} catch (NumberFormatException ex) { | ||
return super.visitMappingEntry(entry, ctx); | ||
} | ||
|
||
if (currentMajorVersion >= minimumJavaMajorVersion) { | ||
return super.visitMappingEntry(entry, ctx); | ||
} | ||
|
||
return super.visitMappingEntry( | ||
entry.withValue(currentValue.withValue(String.valueOf(minimumJavaMajorVersion))), | ||
ctx | ||
); | ||
} | ||
}); | ||
} | ||
|
||
} |
76 changes: 76 additions & 0 deletions
76
recipes/src/main/java/io/quarkus/updates/core/quarkus37/UpgradeJavaVersion.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,76 @@ | ||
package io.quarkus.updates.core.quarkus37; | ||
|
||
import lombok.EqualsAndHashCode; | ||
import lombok.Value; | ||
import org.openrewrite.ExecutionContext; | ||
import org.openrewrite.Option; | ||
import org.openrewrite.Recipe; | ||
import org.openrewrite.TreeVisitor; | ||
import org.openrewrite.gradle.UpdateJavaCompatibility; | ||
import org.openrewrite.java.JavaIsoVisitor; | ||
import org.openrewrite.java.marker.JavaVersion; | ||
import org.openrewrite.maven.UpdateMavenProjectPropertyJavaVersion; | ||
import org.openrewrite.maven.UseMavenCompilerPluginReleaseConfiguration; | ||
import org.openrewrite.java.tree.J; | ||
|
||
import java.time.Duration; | ||
import java.util.*; | ||
|
||
@Value | ||
@EqualsAndHashCode(callSuper = false) | ||
public class UpgradeJavaVersion extends Recipe { | ||
|
||
@Option(displayName = "Java version", | ||
description = "The Java version to upgrade to.", | ||
example = "17") | ||
Integer version; | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return "Upgrade Java version"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Upgrade build plugin configuration to use the specified Java version. " + | ||
"This recipe changes `java.toolchain.languageVersion` in `build.gradle(.kts)` of gradle projects, " + | ||
"or maven-compiler-plugin target version and related settings. " + | ||
"Will not downgrade if the version is newer than the specified version."; | ||
} | ||
|
||
@Override | ||
public List<Recipe> getRecipeList() { | ||
return Arrays.asList( | ||
new UseMavenCompilerPluginReleaseConfiguration(version), | ||
new UpdateMavenProjectPropertyJavaVersion(version), | ||
new UpdateJavaCompatibility(version, null, null, false, null) | ||
); | ||
} | ||
|
||
/** | ||
* This recipe only updates markers, so it does not correspond to human manual effort. | ||
* | ||
* @return Zero estimated time. | ||
*/ | ||
@Override | ||
public Duration getEstimatedEffortPerOccurrence() { | ||
return Duration.ofMinutes(0); | ||
} | ||
|
||
@Override | ||
public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
String newVersion = version.toString(); | ||
Map<JavaVersion, JavaVersion> updatedMarkers = new HashMap<>(); | ||
return new JavaIsoVisitor<>() { | ||
@Override | ||
public J preVisit(J tree, ExecutionContext ctx) { | ||
Optional<JavaVersion> maybeJavaVersion = tree.getMarkers().findFirst(JavaVersion.class); | ||
if (maybeJavaVersion.isPresent() && maybeJavaVersion.get().getMajorVersion() < version) { | ||
return tree.withMarkers(tree.getMarkers().setByType(updatedMarkers.computeIfAbsent(maybeJavaVersion.get(), | ||
m -> m.withSourceCompatibility(newVersion).withTargetCompatibility(newVersion)))); | ||
} | ||
return tree; | ||
} | ||
}; | ||
} | ||
} |
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