Skip to content

Commit

Permalink
Add patch file/report to dryRun
Browse files Browse the repository at this point in the history
  • Loading branch information
sambsnyd committed Apr 27, 2021
1 parent f3b4d3f commit 9674292
Showing 1 changed file with 37 additions and 5 deletions.
42 changes: 37 additions & 5 deletions src/main/java/org/openrewrite/maven/RewriteDryRunMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,27 @@
package org.openrewrite.maven;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Execute;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.plugins.annotations.*;
import org.openrewrite.Result;

import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.stream.Stream;

/**
* Display warnings for any recipes that would suggest changes, but does not make any changes.
* Generates warnings in the console for any recipes that would suggest changes, but does not make any changes.
*/
@Mojo(name = "dryRun", requiresDependencyResolution = ResolutionScope.TEST, threadSafe = true,
defaultPhase = LifecyclePhase.PROCESS_TEST_CLASSES)
@Execute(phase = LifecyclePhase.PROCESS_TEST_CLASSES)
public class RewriteDryRunMojo extends AbstractRewriteMojo {

@Parameter(property = "reportOutputDirectory", defaultValue = "${project.reporting.outputDirectory}/rewrite")
private File reportOutputDirectory;

@Override
public void execute() throws MojoExecutionException {
ResultsContainer results = listResults();
Expand Down Expand Up @@ -63,6 +71,30 @@ public void execute() throws MojoExecutionException {
" by:");
logRecipesThatMadeChanges(result);
}

//noinspection ResultOfMethodCallIgnored
reportOutputDirectory.mkdirs();

Path patchFile = reportOutputDirectory.toPath().resolve("rewrite.patch");
try (BufferedWriter writer = Files.newBufferedWriter(patchFile)) {
Stream.concat(
Stream.concat(results.generated.stream(), results.deleted.stream()),
Stream.concat(results.moved.stream(), results.refactoredInPlace.stream())
)
.map(Result::diff)
.forEach(diff -> {
try {
writer.write(diff + "\n");
} catch (IOException e) {
throw new RuntimeException(e);
}
});

} catch (Exception e) {
throw new MojoExecutionException("Unable to generate rewrite result file.", e);
}
getLog().warn("Report available:");
getLog().warn(" " + patchFile.normalize().toString());
getLog().warn("Run 'mvn rewrite:run' to apply the fixes. Afterwards, review and commit the results.");
}
}
Expand Down

0 comments on commit 9674292

Please sign in to comment.