Skip to content

Commit

Permalink
Align with new Remote API in rewrite-core (#590)
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden authored Jul 3, 2023
1 parent bf2736a commit 701ef03
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Parameter;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Result;
import org.openrewrite.internal.lang.Nullable;

Expand Down Expand Up @@ -57,7 +58,8 @@ public void execute() throws MojoExecutionException {
return;
}

ResultsContainer results = listResults();
ExecutionContext ctx = executionContext();
ResultsContainer results = listResults(ctx);

RuntimeException firstException = results.getFirstException();
if (firstException != null) {
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/openrewrite/maven/AbstractRewriteMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ private void collectBasePaths(MavenProject project, Set<Path> paths, Path localR
}
}

protected ResultsContainer listResults() throws MojoExecutionException {
protected ResultsContainer listResults(ExecutionContext ctx) throws MojoExecutionException {
try (MeterRegistryProvider meterRegistryProvider = new MeterRegistryProvider(getLog(),
metricsUri, metricsUsername, metricsPassword)) {
Metrics.addRegistry(meterRegistryProvider.registry());
Expand Down Expand Up @@ -224,7 +224,6 @@ protected ResultsContainer listResults() throws MojoExecutionException {
}
}

ExecutionContext ctx = executionContext();
LargeSourceSet sourceSet = loadSourceSet(repositoryRoot, env, ctx);

List<Result> results = runRecipe(recipe, sourceSet, ctx);
Expand Down
18 changes: 8 additions & 10 deletions src/main/java/org/openrewrite/maven/AbstractRewriteRunMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@
package org.openrewrite.maven;

import org.apache.maven.plugin.MojoExecutionException;
import org.openrewrite.FileAttributes;
import org.openrewrite.PrintOutputCapture;
import org.openrewrite.Result;
import org.openrewrite.*;
import org.openrewrite.binary.Binary;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.ipc.http.HttpUrlConnectionSender;
import org.openrewrite.quark.Quark;
import org.openrewrite.remote.Remote;

Expand Down Expand Up @@ -52,7 +49,8 @@ public void execute() throws MojoExecutionException {
return;
}

ResultsContainer results = listResults();
ExecutionContext ctx = executionContext();
ResultsContainer results = listResults(ctx);
@Nullable RuntimeException firstException = results.getFirstException();
if (firstException != null) {
getLog().error("The recipe produced an error. Please report this to the recipe author.");
Expand Down Expand Up @@ -95,7 +93,7 @@ public void execute() throws MojoExecutionException {
try {
for (Result result : results.generated) {
assert result.getAfter() != null;
writeAfter(results.getProjectRoot(), result);
writeAfter(results.getProjectRoot(), result, ctx);
}
for (Result result : results.deleted) {
assert result.getBefore() != null;
Expand Down Expand Up @@ -132,13 +130,13 @@ public void execute() throws MojoExecutionException {
// On Mac this can return "false" even when the file was deleted, so skip the check
//noinspection ResultOfMethodCallIgnored
originalLocation.toFile().delete();
writeAfter(results.getProjectRoot(), result);
writeAfter(results.getProjectRoot(), result, ctx);
}
}
for (Result result : results.refactoredInPlace) {
assert result.getBefore() != null;
assert result.getAfter() != null;
writeAfter(results.getProjectRoot(), result);
writeAfter(results.getProjectRoot(), result, ctx);
}
List<Path> emptyDirectories = results.newlyEmptyDirectories();
if (!emptyDirectories.isEmpty()) {
Expand All @@ -154,7 +152,7 @@ public void execute() throws MojoExecutionException {
}
}

private static void writeAfter(Path root, Result result) {
private static void writeAfter(Path root, Result result, ExecutionContext ctx) {
if (result.getAfter() == null || result.getAfter() instanceof Quark) {
return;
}
Expand All @@ -173,7 +171,7 @@ private static void writeAfter(Path root, Result result) {
} else if (result.getAfter() instanceof Remote) {
Remote remote = (Remote) result.getAfter();
try (FileOutputStream sourceFileWriter = new FileOutputStream(targetFile)) {
InputStream source = remote.getInputStream(new HttpUrlConnectionSender());
InputStream source = remote.getInputStream(ctx);
byte[] buf = new byte[4096];
int length;
while ((length = source.read(buf)) > 0) {
Expand Down

0 comments on commit 701ef03

Please sign in to comment.