Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OpenRewrite 8.0 upgrade #549

Merged
merged 10 commits into from
May 31, 2023
12 changes: 6 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@

<properties>
<!-- Pinned versions, as RELEASE would make it into the published pom.xml -->
<rewrite.version>7.40.8</rewrite.version>
<rewrite.python.version>0.4.0</rewrite.python.version>
<rewrite.version>7.41.0-SNAPSHOT</rewrite.version>
<rewrite.python.version>0.5.0-SNAPSHOT</rewrite.python.version>

<!-- using 'ssh' url scheme by default, which assumes a human is performing git operations leveraging an ssh key -->
<developerConnectionUrl>scm:git:ssh://[email protected]/openrewrite/rewrite-maven-plugin.git
Expand All @@ -79,7 +79,7 @@
<netty.version>4.1.89.Final</netty.version>
<rocksdbjni.version>8.0.0</rocksdbjni.version>
<itf-maven.version>0.12.0</itf-maven.version>
<maven-dependencies.version>3.9.2</maven-dependencies.version>
<maven-dependencies.version>3.9.1</maven-dependencies.version>
<maven-release-plugin.version>3.0.0</maven-release-plugin.version>
<maven-plugin-tools.version>3.9.0</maven-plugin-tools.version>
<kotlin.version>1.8.10</kotlin.version>
Expand Down Expand Up @@ -210,7 +210,7 @@
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-core</artifactId>
<version>1.11.0</version>
<version>1.10.6</version>
</dependency>
<dependency>
<groupId>io.rsocket</groupId>
Expand Down Expand Up @@ -414,7 +414,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.1.0</version>
<version>3.0.0</version>
<executions>
<execution>
<goals>
Expand Down Expand Up @@ -521,7 +521,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>3.1.0</version>
<version>3.0.1</version>
<executions>
<execution>
<id>sign-artifacts</id>
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.InMemoryLargeSourceSet;
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 InMemoryLargeSourceSet(sourceFiles), ctx).getChangeset().getAllResults().stream()
.filter(source -> {
// Remove ASTs originating from generated files
if (source.getBefore() != null) {
Expand Down
27 changes: 22 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,10 @@
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.SourceFile;
import org.openrewrite.internal.InMemoryLargeSourceSet;
import org.openrewrite.xml.tree.Xml;

import javax.annotation.Nullable;
Expand Down Expand Up @@ -62,11 +65,25 @@ 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 String getDescription() {
return ConfigureMojo.class.getName() + " recipe.";
}

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

List<SourceFile> poms = Collections.singletonList(maven);
List<Result> results = recipe.run(new InMemoryLargeSourceSet(poms), ctx).getChangeset().getAllResults();
if (results.isEmpty()) {
getLog().warn("No changes made to plugin " + artifactId + " configuration");
return;
Expand Down
30 changes: 25 additions & 5 deletions src/main/java/org/openrewrite/maven/InitMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
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.xml.tree.Xml;
import org.openrewrite.SourceFile;
import org.openrewrite.internal.InMemoryLargeSourceSet;

import javax.annotation.Nullable;
import java.io.BufferedWriter;
Expand Down Expand Up @@ -70,10 +72,28 @@ 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 String getDescription() {
return InitMojo.class.getName() + " recipe.";
}

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

List<SourceFile> poms = mp.parse(Collections.singleton(project.getFile().toPath()), baseDir, ctx).collect(Collectors.toList());
List<Result> results = recipe.run(new InMemoryLargeSourceSet(poms), ctx).getChangeset().getAllResults();
if (results.isEmpty()) {
getLog().warn("Plugin " + artifactId + " is already part of the build");
return;
Expand Down
58 changes: 22 additions & 36 deletions src/main/java/org/openrewrite/maven/MavenMojoProjectParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
import org.openrewrite.java.marker.JavaVersion;
import org.openrewrite.java.style.Autodetect;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaSourceFile;
import org.openrewrite.java.tree.JavaType;
import org.openrewrite.marker.*;
import org.openrewrite.marker.ci.BuildEnvironment;
import org.openrewrite.maven.cache.CompositeMavenPomCache;
Expand Down Expand Up @@ -220,16 +218,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 @@ -260,10 +258,10 @@ 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));
mainProjectProvenance.add(sourceSet("main", dependencies));

List<J.CompilationUnit> parsedJava = ListUtils.map(cus,
addProvenance(baseDir, mainProjectProvenance, generatedSourcePaths));
Expand Down Expand Up @@ -304,10 +302,10 @@ 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));
markers.add(sourceSet("test", testDependencies));

List<J.CompilationUnit> parsedJava = ListUtils.map(
cus,
Expand All @@ -327,20 +325,8 @@ 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);
Set<JavaType.FullyQualified> typesInUse = new LinkedHashSet<>();
for (JavaSourceFile cu : cus) {
for (JavaType type : cu.getTypesInUse().getTypesInUse()) {
if (type instanceof JavaType.FullyQualified) {
typesInUse.add((JavaType.FullyQualified) type);
}
}
}
List<JavaType.FullyQualified> classpath = testSourceSet.getClasspath();
classpath.addAll(typesInUse);
testSourceSet = testSourceSet.withClasspath(classpath);
return testSourceSet;
private static JavaSourceSet sourceSet(String name, List<Path> dependencies) {
return JavaSourceSet.build(name, dependencies, typeCache, false);
}

@Nullable
Expand Down Expand Up @@ -382,7 +368,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 @@ -433,7 +419,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 @@ -461,7 +447,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 @@ -578,9 +564,9 @@ 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) {
Autodetect.Detector autodetect = Autodetect.detect(sourceFiles.stream());
NamedStyles merged = NamedStyles.merge(ListUtils.concat(styles, autodetect.build()));
if (merged == null) {
return sourceFiles;
}
return map(sourceFiles, cu -> cu.withMarkers(cu.getMarkers().add(merged)));
Expand Down
10 changes: 6 additions & 4 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.LargeSourceSet;
import org.openrewrite.Result;
import org.openrewrite.internal.InMemoryLargeSourceSet;
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,10 +43,11 @@ 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);
LargeSourceSet poms = new InMemoryLargeSourceSet(Collections.singletonList(maven));
List<Result> results = new RemovePlugin(groupId, artifactId)
.run(poms)
.getResults();
.run(poms, ctx)
.getChangeset()
.getAllResults();
if (!results.isEmpty()) {
Result result = results.get(0);
assert result.getBefore() != null;
Expand Down
27 changes: 14 additions & 13 deletions src/main/java/org/openrewrite/maven/ResourceParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
import org.openrewrite.properties.PropertiesParser;
import org.openrewrite.protobuf.ProtoParser;
import org.openrewrite.python.PythonParser;
import org.openrewrite.quark.QuarkParser;
import org.openrewrite.text.PlainText;
import org.openrewrite.text.PlainTextParser;
import org.openrewrite.quark.QuarkParser;
import org.openrewrite.tree.ParsingExecutionContextView;
import org.openrewrite.xml.XmlParser;
import org.openrewrite.yaml.YamlParser;
Expand All @@ -24,6 +24,7 @@
import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class ResourceParser {
private static final Set<String> DEFAULT_IGNORED_DIRECTORIES = new HashSet<>(Arrays.asList("build", "target", "out", ".sonar", ".gradle", ".idea", ".project", "node_modules", ".git", ".metadata", ".DS_Store"));
Expand Down Expand Up @@ -120,7 +121,7 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
}
});

List<S> sourceFiles = new ArrayList<>(resources.size() + quarkPaths.size());
Stream<S> sourceFiles = Stream.empty();

List<Path> javaPaths = new ArrayList<>();

Expand Down Expand Up @@ -173,37 +174,37 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
}
});

sourceFiles.addAll((List<S>) javaParser.parse(javaPaths, baseDir, ctx));
sourceFiles = Stream.concat(sourceFiles, (Stream<S>) javaParser.parse(javaPaths, baseDir, ctx));
alreadyParsed.addAll(javaPaths);

sourceFiles.addAll((List<S>) jsonParser.parse(jsonPaths, baseDir, ctx));
sourceFiles = Stream.concat(sourceFiles, (Stream<S>) jsonParser.parse(jsonPaths, baseDir, ctx));
alreadyParsed.addAll(jsonPaths);

sourceFiles.addAll((List<S>) xmlParser.parse(xmlPaths, baseDir, ctx));
sourceFiles = Stream.concat(sourceFiles, (Stream<S>) xmlParser.parse(xmlPaths, baseDir, ctx));
alreadyParsed.addAll(xmlPaths);

sourceFiles.addAll((List<S>) yamlParser.parse(yamlPaths, baseDir, ctx));
sourceFiles = Stream.concat(sourceFiles, (Stream<S>) yamlParser.parse(yamlPaths, baseDir, ctx));
alreadyParsed.addAll(yamlPaths);

sourceFiles.addAll((List<S>) propertiesParser.parse(propertiesPaths, baseDir, ctx));
sourceFiles = Stream.concat(sourceFiles, (Stream<S>) propertiesParser.parse(propertiesPaths, baseDir, ctx));
alreadyParsed.addAll(propertiesPaths);

sourceFiles.addAll((List<S>) protoParser.parse(protoPaths, baseDir, ctx));
sourceFiles = Stream.concat(sourceFiles, (Stream<S>) protoParser.parse(protoPaths, baseDir, ctx));
alreadyParsed.addAll(protoPaths);

sourceFiles.addAll((List<S>) pythonParser.parse(pythonPaths, baseDir, ctx));
sourceFiles = Stream.concat(sourceFiles, (Stream<S>) pythonParser.parse(pythonPaths, baseDir, ctx));
alreadyParsed.addAll(pythonPaths);

sourceFiles.addAll((List<S>) hclParser.parse(hclPaths, baseDir, ctx));
sourceFiles = Stream.concat(sourceFiles, (Stream<S>) hclParser.parse(hclPaths, baseDir, ctx));
alreadyParsed.addAll(hclPaths);

sourceFiles.addAll((List<S>) plainTextParser.parse(plainTextPaths, baseDir, ctx));
sourceFiles = Stream.concat(sourceFiles, (Stream<S>) plainTextParser.parse(plainTextPaths, baseDir, ctx));
alreadyParsed.addAll(plainTextPaths);

sourceFiles.addAll((List<S>) quarkParser.parse(quarkPaths, baseDir, ctx));
sourceFiles = Stream.concat(sourceFiles, (Stream<S>) quarkParser.parse(quarkPaths, baseDir, ctx));
alreadyParsed.addAll(quarkPaths);

return sourceFiles;
return sourceFiles.collect(Collectors.toList());
}

private boolean isOverSizeThreshold(long fileSize) {
Expand Down
Loading