Skip to content

Commit

Permalink
OpenRewrite 8.0 upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden committed May 5, 2023
1 parent 7a7f2b6 commit 9fc0f28
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 44 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
</scm>

<properties>
<rewrite.version>7.40.2</rewrite.version>
<rewrite.version>7.41.0-SNAPSHOT</rewrite.version>
<rewrite.python.version>0.4.0</rewrite.python.version>

<!-- using 'ssh' url scheme by default, which assumes a human is performing git operations leveraging an ssh key -->
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/openrewrite/maven/AbstractRewriteMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.openrewrite.config.Environment;
import org.openrewrite.config.RecipeDescriptor;
import org.openrewrite.config.YamlResourceLoader;
import org.openrewrite.internal.InMemoryLargeIterable;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.ipc.http.HttpSender;
import org.openrewrite.ipc.http.HttpUrlConnectionSender;
Expand Down Expand Up @@ -281,7 +282,7 @@ protected ResultsContainer listResults() throws MojoExecutionException {
}

getLog().info("Running recipe(s)...");
List<Result> results = recipe.run(sourceFiles, ctx).getResults().stream()
List<Result> results = recipe.run(new InMemoryLargeIterable<>(sourceFiles), ctx).getResults().stream()
.filter(source -> {
// Remove ASTs originating from generated files
if (source.getBefore() != null) {
Expand Down
21 changes: 16 additions & 5 deletions src/main/java/org/openrewrite/maven/ConfigureMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.Result;
import org.openrewrite.internal.InMemoryLargeIterable;
import org.openrewrite.xml.tree.Xml;

import javax.annotation.Nullable;
Expand Down Expand Up @@ -62,11 +64,20 @@ public void execute() throws MojoExecutionException, MojoFailureException {

ExecutionContext ctx = executionContext();
Xml.Document maven = new MavenMojoProjectParser(getLog(), baseDir, pomCacheEnabled, pomCacheDirectory, runtime, skipMavenParsing, getExclusions(), getPlainTextMasks(), sizeThresholdMb, mavenSession, settingsDecrypter).parseMaven(project, Collections.emptyList(), ctx);
List<Xml.Document> poms = Arrays.asList(maven);
List<Result> results = new ChangePluginConfiguration(groupId, artifactId, getConfiguration())
.doNext(new ChangePluginDependencies(groupId, artifactId, dependencies))
.doNext(new ChangePluginExecutions(groupId, artifactId, getExecutions()))
.run(poms).getResults();
Recipe recipe = new Recipe() {
@Override
public String getDisplayName() {
return ConfigureMojo.class.getName();
}

@Override
public List<Recipe> getRecipeList() {
return Arrays.asList(new ChangePluginDependencies(groupId, artifactId, dependencies), new ChangePluginExecutions(groupId, artifactId, getExecutions()));
}
};

List<Xml.Document> poms = Collections.singletonList(maven);
List<Result> results = recipe.run(new InMemoryLargeIterable<>(poms), ctx).getResults();
if (results.isEmpty()) {
getLog().warn("No changes made to plugin " + artifactId + " configuration");
return;
Expand Down
23 changes: 19 additions & 4 deletions src/main/java/org/openrewrite/maven/InitMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.Result;
import org.openrewrite.internal.InMemoryLargeIterable;
import org.openrewrite.xml.tree.Xml;

import javax.annotation.Nullable;
Expand Down Expand Up @@ -70,10 +72,23 @@ public void execute() throws MojoExecutionException {
MavenParser mp = MavenParser.builder()
.mavenConfig(baseDir.resolve(".mvn/maven.config"))
.build();
List<Xml.Document> poms = mp.parse(Collections.singleton(project.getFile().toPath()), baseDir, ctx);
List<Result> results = new AddPlugin(groupId, artifactId, getVersion(), getConfiguration(), null, getExecutions())
.doNext(new ChangePluginDependencies(groupId, artifactId, dependencies))
.run(poms).getResults();
Recipe recipe = new Recipe() {
@Override
public String getDisplayName() {
return InitMojo.class.getName();
}

@Override
public List<Recipe> getRecipeList() {
return Arrays.asList(
new AddPlugin(groupId, artifactId, getVersion(), getConfiguration(), null, getExecutions()),
new ChangePluginDependencies(groupId, artifactId, dependencies)
);
}
};

List<Xml.Document> poms = mp.parse(Collections.singleton(project.getFile().toPath()), baseDir, ctx).collect(Collectors.toList());
List<Result> results = recipe.run(new InMemoryLargeIterable<>(poms), ctx).getResults();
if (results.isEmpty()) {
getLog().warn("Plugin " + artifactId + " is already part of the build");
return;
Expand Down
40 changes: 20 additions & 20 deletions src/main/java/org/openrewrite/maven/MavenMojoProjectParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -219,16 +219,16 @@ public List<Marker> generateProvenance(MavenProject mavenProject) {

BuildEnvironment buildEnvironment = BuildEnvironment.build(System::getenv);
return Stream.of(
buildEnvironment,
gitProvenance(baseDir, buildEnvironment),
OperatingSystemProvenance.current(),
buildTool,
new JavaVersion(randomId(), javaRuntimeVersion, javaVendor, sourceCompatibility, targetCompatibility),
new JavaProject(randomId(), mavenProject.getName(), new JavaProject.Publication(
mavenProject.getGroupId(),
mavenProject.getArtifactId(),
mavenProject.getVersion()
)))
buildEnvironment,
gitProvenance(baseDir, buildEnvironment),
OperatingSystemProvenance.current(),
buildTool,
new JavaVersion(randomId(), javaRuntimeVersion, javaVendor, sourceCompatibility, targetCompatibility),
new JavaProject(randomId(), mavenProject.getName(), new JavaProject.Publication(
mavenProject.getGroupId(),
mavenProject.getArtifactId(),
mavenProject.getVersion()
)))
.filter(Objects::nonNull)
.collect(toList());
}
Expand Down Expand Up @@ -259,7 +259,7 @@ private List<SourceFile> processMainSources(
.collect(toList());
javaParser.setClasspath(dependencies);

List<J.CompilationUnit> cus = applyStyles(javaParser.parse(mainJavaSources, baseDir, ctx), styles);
List<J.CompilationUnit> cus = applyStyles(javaParser.parse(mainJavaSources, baseDir, ctx).collect(toList()), styles);

List<Marker> mainProjectProvenance = new ArrayList<>(projectProvenance);
mainProjectProvenance.add(sourceSet("main", dependencies, cus));
Expand Down Expand Up @@ -303,7 +303,7 @@ private List<SourceFile> processTestSources(
List<Path> testJavaSources = listJavaSources(mavenProject.getBuild().getTestSourceDirectory());
alreadyParsed.addAll(testJavaSources);

List<J.CompilationUnit> cus = applyStyles(javaParser.parse(testJavaSources, baseDir, ctx), styles);
List<J.CompilationUnit> cus = applyStyles(javaParser.parse(testJavaSources, baseDir, ctx).collect(toList()), styles);

List<Marker> markers = new ArrayList<>(projectProvenance);
markers.add(sourceSet("test", testDependencies, cus));
Expand All @@ -327,7 +327,7 @@ private List<SourceFile> processTestSources(

@NotNull
private static JavaSourceSet sourceSet(String name, List<Path> dependencies, List<? extends JavaSourceFile> cus) {
JavaSourceSet testSourceSet = JavaSourceSet.build(name, dependencies, typeCache, false);
JavaSourceSet sourceSet = JavaSourceSet.build(name, dependencies, typeCache, false);
Set<JavaType.FullyQualified> typesInUse = new LinkedHashSet<>();
for (JavaSourceFile cu : cus) {
for (JavaType type : cu.getTypesInUse().getTypesInUse()) {
Expand All @@ -336,10 +336,10 @@ private static JavaSourceSet sourceSet(String name, List<Path> dependencies, Lis
}
}
}
List<JavaType.FullyQualified> classpath = testSourceSet.getClasspath();
List<JavaType.FullyQualified> classpath = sourceSet.getClasspath();
classpath.addAll(typesInUse);
testSourceSet = testSourceSet.withClasspath(classpath);
return testSourceSet;
sourceSet = sourceSet.withClasspath(classpath);
return sourceSet;
}

@Nullable
Expand Down Expand Up @@ -381,7 +381,7 @@ public Map<MavenProject, Xml.Document> parseMaven(List<MavenProject> mavenProjec

List<Xml.Document> mavens = mavenParserBuilder
.build()
.parse(allPoms, baseDir, ctx);
.parse(allPoms, baseDir, ctx).collect(toList());

if (logger.isDebugEnabled()) {
logDebug(topLevelProject, "Base directory : '" + baseDir + "'");
Expand Down Expand Up @@ -432,7 +432,7 @@ public Map<MavenProject, Xml.Document> parseMaven(List<MavenProject> mavenProjec
* Recursively navigate the maven project to collect any poms that are local (on disk)
*
* @param project A maven project to examine for any children/parent poms.
* @param paths A list of paths to poms that have been collected so far.
* @param paths A list of paths to poms that have been collected so far.
*/
private void collectPoms(MavenProject project, Set<Path> paths) {
paths.add(pomPath(project));
Expand Down Expand Up @@ -460,7 +460,7 @@ private void collectPoms(MavenProject project, Set<Path> paths) {
private static Path pomPath(MavenProject mavenProject) {
Path pomPath = mavenProject.getFile().toPath();
// org.codehaus.mojo:flatten-maven-plugin produces a synthetic pom unsuitable for our purposes, use the regular pom instead
if(pomPath.endsWith(".flattened-pom.xml")) {
if (pomPath.endsWith(".flattened-pom.xml")) {
return mavenProject.getBasedir().toPath().resolve("pom.xml");
}
return pomPath;
Expand Down Expand Up @@ -579,7 +579,7 @@ private Set<Path> pathsToOtherMavenProjects(MavenProject mavenProject) {
private List<J.CompilationUnit> applyStyles(List<J.CompilationUnit> sourceFiles, List<NamedStyles> styles) {
Autodetect autodetect = Autodetect.detect(sourceFiles);
NamedStyles merged = NamedStyles.merge(ListUtils.concat(styles, autodetect));
if(merged == null) {
if (merged == null) {
return sourceFiles;
}
return map(sourceFiles, cu -> cu.withMarkers(cu.getMarkers().add(merged)));
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/org/openrewrite/maven/RemoveMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.openrewrite.ExecutionContext;
import org.openrewrite.LargeIterable;
import org.openrewrite.Result;
import org.openrewrite.internal.InMemoryLargeIterable;
import org.openrewrite.xml.tree.Xml;

import java.io.BufferedWriter;
Expand All @@ -15,7 +17,6 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

Expand All @@ -42,9 +43,9 @@ public void execute() throws MojoExecutionException, MojoFailureException {
Path baseDir = getBuildRoot();
ExecutionContext ctx = executionContext();
Xml.Document maven = new MavenMojoProjectParser(getLog(), baseDir, pomCacheEnabled, pomCacheDirectory, runtime, skipMavenParsing, getExclusions(), getPlainTextMasks(), sizeThresholdMb, mavenSession, settingsDecrypter).parseMaven(project, Collections.emptyList(), ctx);
List<Xml.Document> poms = Arrays.asList(maven);
LargeIterable<Xml.Document> poms = new InMemoryLargeIterable<>(Collections.singletonList(maven));
List<Result> results = new RemovePlugin(groupId, artifactId)
.run(poms)
.run(poms, ctx)
.getResults();
if (!results.isEmpty()) {
Result result = results.get(0);
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/org/openrewrite/maven/ResourceParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,34 +173,34 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
}
});

sourceFiles.addAll((List<S>) javaParser.parse(javaPaths, baseDir, ctx));
sourceFiles.addAll((List<S>) javaParser.parse(javaPaths, baseDir, ctx).collect(Collectors.toList()));
alreadyParsed.addAll(javaPaths);

sourceFiles.addAll((List<S>) jsonParser.parse(jsonPaths, baseDir, ctx));
sourceFiles.addAll((List<S>) jsonParser.parse(jsonPaths, baseDir, ctx).collect(Collectors.toList()));
alreadyParsed.addAll(jsonPaths);

sourceFiles.addAll((List<S>) xmlParser.parse(xmlPaths, baseDir, ctx));
sourceFiles.addAll((List<S>) xmlParser.parse(xmlPaths, baseDir, ctx).collect(Collectors.toList()));
alreadyParsed.addAll(xmlPaths);

sourceFiles.addAll((List<S>) yamlParser.parse(yamlPaths, baseDir, ctx));
sourceFiles.addAll((List<S>) yamlParser.parse(yamlPaths, baseDir, ctx).collect(Collectors.toList()));
alreadyParsed.addAll(yamlPaths);

sourceFiles.addAll((List<S>) propertiesParser.parse(propertiesPaths, baseDir, ctx));
sourceFiles.addAll((List<S>) propertiesParser.parse(propertiesPaths, baseDir, ctx).collect(Collectors.toList()));
alreadyParsed.addAll(propertiesPaths);

sourceFiles.addAll((List<S>) protoParser.parse(protoPaths, baseDir, ctx));
sourceFiles.addAll((List<S>) protoParser.parse(protoPaths, baseDir, ctx).collect(Collectors.toList()));
alreadyParsed.addAll(protoPaths);

sourceFiles.addAll((List<S>) pythonParser.parse(pythonPaths, baseDir, ctx));
sourceFiles.addAll((List<S>) pythonParser.parse(pythonPaths, baseDir, ctx).collect(Collectors.toList()));
alreadyParsed.addAll(pythonPaths);

sourceFiles.addAll((List<S>) hclParser.parse(hclPaths, baseDir, ctx));
sourceFiles.addAll((List<S>) hclParser.parse(hclPaths, baseDir, ctx).collect(Collectors.toList()));
alreadyParsed.addAll(hclPaths);

sourceFiles.addAll((List<S>) plainTextParser.parse(plainTextPaths, baseDir, ctx));
sourceFiles.addAll((List<S>) plainTextParser.parse(plainTextPaths, baseDir, ctx).collect(Collectors.toList()));
alreadyParsed.addAll(plainTextPaths);

sourceFiles.addAll((List<S>) quarkParser.parse(quarkPaths, baseDir, ctx));
sourceFiles.addAll((List<S>) quarkParser.parse(quarkPaths, baseDir, ctx).collect(Collectors.toList()));
alreadyParsed.addAll(quarkPaths);

return sourceFiles;
Expand Down

0 comments on commit 9fc0f28

Please sign in to comment.