Skip to content

Commit

Permalink
Finish renaming "Change" to "Result"
Browse files Browse the repository at this point in the history
  • Loading branch information
sambsnyd committed Feb 10, 2021
1 parent a4220a9 commit ad3029c
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ protected Environment environment() {
return env.build();
}

protected ChangesContainer listChanges() {
protected ResultsContainer listResults() {
try (MeterRegistryProvider meterRegistryProvider = new MeterRegistryProvider(getLog(), metricsUri, metricsUsername, metricsPassword)) {
MeterRegistry meterRegistry = meterRegistryProvider.registry();

Expand All @@ -141,12 +141,11 @@ protected ChangesContainer listChanges() {
Environment env = environment();
Set<String> recipes = getActiveRecipes();
if (recipes == null || recipes.isEmpty()) {
return new ChangesContainer(baseDir, emptyList());
return new ResultsContainer(baseDir, emptyList());
}
List<NamedStyles> styles = env.activateStyles(getActiveStyles());
Recipe recipe = env.activateRecipes(recipes);


List<SourceFile> sourceFiles = new ArrayList<>();
List<Path> sourcePaths = getJavaSources().getFiles().stream()
.filter(it -> it.isFile() && it.getName().endsWith(".java"))
Expand Down Expand Up @@ -190,34 +189,34 @@ protected ChangesContainer listChanges() {

List<Result> results = recipe.run(sourceFiles);

return new ChangesContainer(baseDir, results);
return new ResultsContainer(baseDir, results);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

public static class ChangesContainer {
public static class ResultsContainer {
final Path projectRoot;
final List<Result> generated = new ArrayList<>();
final List<Result> deleted = new ArrayList<>();
final List<Result> moved = new ArrayList<>();
final List<Result> refactoredInPlace = new ArrayList<>();

public ChangesContainer(Path projectRoot, Collection<Result> changes) {
public ResultsContainer(Path projectRoot, Collection<Result> results) {
this.projectRoot = projectRoot;
for (Result change : changes) {
if (change.getBefore() == null && change.getAfter() == null) {
for (Result result : results) {
if (result.getBefore() == null && result.getAfter() == null) {
// This situation shouldn't happen / makes no sense, log and skip
continue;
}
if (change.getBefore() == null && change.getAfter() != null) {
generated.add(change);
} else if (change.getBefore() != null && change.getAfter() == null) {
deleted.add(change);
} else if (change.getBefore() != null && !change.getBefore().getSourcePath().equals(change.getAfter().getSourcePath())) {
moved.add(change);
if (result.getBefore() == null && result.getAfter() != null) {
generated.add(result);
} else if (result.getBefore() != null && result.getAfter() == null) {
deleted.add(result);
} else if (result.getBefore() != null && !result.getBefore().getSourcePath().equals(result.getAfter().getSourcePath())) {
moved.add(result);
} else {
refactoredInPlace.add(change);
refactoredInPlace.add(result);
}
}
}
Expand All @@ -231,8 +230,8 @@ public boolean isNotEmpty() {
}
}

protected void logVisitorsThatMadeChanges(Result change) {
for (String visitor : change.getRecipesThatMadeChanges()) {
protected void logVisitorsThatMadeChanges(Result result) {
for (String visitor : result.getRecipesThatMadeChanges()) {
getLog().warn(" " + visitor);
}
}
Expand Down
78 changes: 39 additions & 39 deletions plugin/src/main/java/org/openrewrite/gradle/RewriteFixTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,77 +44,77 @@ protected Logger getLog() {

@TaskAction
public void run() {
ChangesContainer changes = listChanges();
ResultsContainer results = listResults();

if (changes.isNotEmpty()) {
for(Result change : changes.generated) {
assert change.getAfter() != null;
if (results.isNotEmpty()) {
for(Result result : results.generated) {
assert result.getAfter() != null;
getLog().warn("Generated new file " +
change.getAfter().getSourcePath() +
result.getAfter().getSourcePath() +
" by:");
logVisitorsThatMadeChanges(change);
logVisitorsThatMadeChanges(result);
}
for(Result change : changes.deleted) {
assert change.getBefore() != null;
for(Result result : results.deleted) {
assert result.getBefore() != null;
getLog().warn("Deleted file " +
change.getBefore().getSourcePath() +
result.getBefore().getSourcePath() +
" by:");
logVisitorsThatMadeChanges(change);
logVisitorsThatMadeChanges(result);
}
for(Result change : changes.moved) {
assert change.getAfter() != null;
assert change.getBefore() != null;
for(Result result : results.moved) {
assert result.getAfter() != null;
assert result.getBefore() != null;
getLog().warn("File has been moved from " +
change.getBefore().getSourcePath() + " to " +
change.getAfter().getSourcePath() + " by:");
logVisitorsThatMadeChanges(change);
result.getBefore().getSourcePath() + " to " +
result.getAfter().getSourcePath() + " by:");
logVisitorsThatMadeChanges(result);
}
for(Result change : changes.refactoredInPlace) {
assert change.getBefore() != null;
for(Result result : results.refactoredInPlace) {
assert result.getBefore() != null;
getLog().warn("Changes have been made to " +
change.getBefore().getSourcePath() +
result.getBefore().getSourcePath() +
" by:");
logVisitorsThatMadeChanges(change);
logVisitorsThatMadeChanges(result);
}

getLog().warn("Please review and commit the changes.");
getLog().warn("Please review and commit the results.");

try {
for (Result change : changes.generated) {
assert change.getAfter() != null;
for (Result result : results.generated) {
assert result.getAfter() != null;
try (BufferedWriter sourceFileWriter = Files.newBufferedWriter(
changes.getProjectRoot().resolve(change.getAfter().getSourcePath()))) {
sourceFileWriter.write(change.getAfter().print());
results.getProjectRoot().resolve(result.getAfter().getSourcePath()))) {
sourceFileWriter.write(result.getAfter().print());
}
}
for (Result change: changes.deleted) {
assert change.getBefore() != null;
Path originalLocation = changes.getProjectRoot().resolve(change.getBefore().getSourcePath());
for (Result result: results.deleted) {
assert result.getBefore() != null;
Path originalLocation = results.getProjectRoot().resolve(result.getBefore().getSourcePath());
boolean deleteSucceeded = originalLocation.toFile().delete();
if(!deleteSucceeded) {
throw new IOException("Unable to delete file " + originalLocation.toAbsolutePath());
}
}
for (Result change : changes.moved) {
for (Result result : results.moved) {
// Should we try to use git to move the file first, and only if that fails fall back to this?
assert change.getBefore() != null;
Path originalLocation = changes.getProjectRoot().resolve(change.getBefore().getSourcePath());
assert result.getBefore() != null;
Path originalLocation = results.getProjectRoot().resolve(result.getBefore().getSourcePath());
boolean deleteSucceeded = originalLocation.toFile().delete();
if(!deleteSucceeded) {
throw new IOException("Unable to delete file " + originalLocation.toAbsolutePath());
}
assert change.getAfter() != null;
assert result.getAfter() != null;
try (BufferedWriter sourceFileWriter = Files.newBufferedWriter(
changes.getProjectRoot().resolve(change.getAfter().getSourcePath()))) {
sourceFileWriter.write(change.getAfter().print());
results.getProjectRoot().resolve(result.getAfter().getSourcePath()))) {
sourceFileWriter.write(result.getAfter().print());
}
}
for (Result change : changes.refactoredInPlace) {
assert change.getBefore() != null;
for (Result result : results.refactoredInPlace) {
assert result.getBefore() != null;
try (BufferedWriter sourceFileWriter = Files.newBufferedWriter(
changes.getProjectRoot().resolve(change.getBefore().getSourcePath()))) {
assert change.getAfter() != null;
sourceFileWriter.write(change.getAfter().print());
results.getProjectRoot().resolve(result.getBefore().getSourcePath()))) {
assert result.getAfter() != null;
sourceFileWriter.write(result.getAfter().print());
}
}
} catch (IOException e) {
Expand Down
46 changes: 23 additions & 23 deletions plugin/src/main/java/org/openrewrite/gradle/RewriteWarnTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class RewriteWarnTask extends AbstractRewriteTask{
public RewriteWarnTask(SourceSet sourceSet, RewriteExtension extension) {
super(sourceSet, extension);
setGroup("rewrite");
setDescription("Dry run the active refactoring recipes to sources within the " + sourceSet.getName() + " SourceSet. No changes will be made.");
setDescription("Dry run the active refactoring recipes to sources within the " + sourceSet.getName() + " SourceSet. No results will be made.");
}

@Override
Expand All @@ -40,39 +40,39 @@ protected Logger getLog() {

@TaskAction
public void run() {
ChangesContainer changes = listChanges();
ResultsContainer results = listResults();

if (changes.isNotEmpty()) {
for(Result change : changes.generated) {
assert change.getAfter() != null;
if (results.isNotEmpty()) {
for(Result result : results.generated) {
assert result.getAfter() != null;
getLog().warn("Applying fixes would generate new file " +
change.getAfter().getSourcePath() +
result.getAfter().getSourcePath() +
" by:");
logVisitorsThatMadeChanges(change);
logVisitorsThatMadeChanges(result);
}
for(Result change : changes.deleted) {
assert change.getBefore() != null;
for(Result result : results.deleted) {
assert result.getBefore() != null;
getLog().warn("Applying fixes would delete file " +
change.getBefore().getSourcePath() +
result.getBefore().getSourcePath() +
" by:");
logVisitorsThatMadeChanges(change);
logVisitorsThatMadeChanges(result);
}
for(Result change : changes.moved) {
assert change.getBefore() != null;
assert change.getAfter() != null;
for(Result result : results.moved) {
assert result.getBefore() != null;
assert result.getAfter() != null;
getLog().warn("Applying fixes would move file from " +
change.getBefore().getSourcePath() + " to " +
change.getAfter().getSourcePath() + " by:");
logVisitorsThatMadeChanges(change);
result.getBefore().getSourcePath() + " to " +
result.getAfter().getSourcePath() + " by:");
logVisitorsThatMadeChanges(result);
}
for(Result change : changes.refactoredInPlace) {
assert change.getBefore() != null;
getLog().warn("Applying fixes would make changes to " +
change.getBefore().getSourcePath() +
for(Result result : results.refactoredInPlace) {
assert result.getBefore() != null;
getLog().warn("Applying fixes would make results to " +
result.getBefore().getSourcePath() +
" by:");
logVisitorsThatMadeChanges(change);
logVisitorsThatMadeChanges(result);
}
getLog().warn("Run 'gradle rewriteFix' to apply the fixes. Afterwards, review and commit the changes.");
getLog().warn("Run 'gradle rewriteFix' to apply the fixes. Afterwards, review and commit the results.");
}
}
}

0 comments on commit ad3029c

Please sign in to comment.