Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions rewrite-core/src/main/java/org/openrewrite/Recipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.openrewrite.internal.StringUtils;
import org.openrewrite.internal.lang.NullUtils;
import org.openrewrite.table.RecipeRunStats;
import org.openrewrite.table.SearchResults;
import org.openrewrite.table.SourcesFileErrors;
import org.openrewrite.table.SourcesFileResults;

Expand Down Expand Up @@ -315,6 +316,7 @@ private List<OptionDescriptor> getOptionDescriptors() {

private static final List<DataTableDescriptor> GLOBAL_DATA_TABLES = Arrays.asList(
dataTableDescriptorFromDataTable(new SourcesFileResults(Recipe.noop())),
dataTableDescriptorFromDataTable(new SearchResults(Recipe.noop())),
dataTableDescriptorFromDataTable(new SourcesFileErrors(Recipe.noop())),
dataTableDescriptorFromDataTable(new RecipeRunStats(Recipe.noop()))
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.openrewrite.scheduling.RecipeRunCycle;
import org.openrewrite.scheduling.WatchableExecutionContext;
import org.openrewrite.table.RecipeRunStats;
import org.openrewrite.table.SearchResults;
import org.openrewrite.table.SourcesFileErrors;
import org.openrewrite.table.SourcesFileResults;

Expand Down Expand Up @@ -56,6 +57,7 @@ private LargeSourceSet runRecipeCycles(Recipe recipe, LargeSourceSet sourceSet,

RecipeRunStats recipeRunStats = new RecipeRunStats(Recipe.noop());
SourcesFileErrors errorsTable = new SourcesFileErrors(Recipe.noop());
SearchResults searchResults = new SearchResults(Recipe.noop());
SourcesFileResults sourceFileResults = new SourcesFileResults(Recipe.noop());

LargeSourceSet after = sourceSet;
Expand All @@ -72,7 +74,7 @@ private LargeSourceSet runRecipeCycles(Recipe recipe, LargeSourceSet sourceSet,
Cursor rootCursor = new Cursor(null, Cursor.ROOT_VALUE);
try {
RecipeRunCycle<LargeSourceSet> cycle = new RecipeRunCycle<>(recipe, i, rootCursor, ctxWithWatch,
recipeRunStats, sourceFileResults, errorsTable, LargeSourceSet::edit);
recipeRunStats, searchResults, sourceFileResults, errorsTable, LargeSourceSet::edit);
ctxWithWatch.putCycle(cycle);
after.beforeCycle(i == maxCycles);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.openrewrite.scheduling.RecipeRunCycle;
import org.openrewrite.scheduling.WatchableExecutionContext;
import org.openrewrite.table.RecipeRunStats;
import org.openrewrite.table.SearchResults;
import org.openrewrite.table.SourcesFileErrors;
import org.openrewrite.table.SourcesFileResults;

Expand Down Expand Up @@ -58,8 +59,9 @@ protected Object handle(Generate request) throws Exception {
if (ctx.getMessage(CURRENT_RECIPE) == null) {
WatchableExecutionContext wctx = new WatchableExecutionContext(ctx);
wctx.putCycle(new RecipeRunCycle<>(recipe, 0, new Cursor(null, Cursor.ROOT_VALUE), wctx,
new RecipeRunStats(Recipe.noop()), new SourcesFileResults(Recipe.noop()),
new SourcesFileErrors(Recipe.noop()), LargeSourceSet::edit));
new RecipeRunStats(Recipe.noop()), new SearchResults(Recipe.noop()),
new SourcesFileResults(Recipe.noop()), new SourcesFileErrors(Recipe.noop()),
LargeSourceSet::edit));
ctx.putCurrentRecipe(recipe);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.openrewrite.scheduling.RecipeRunCycle;
import org.openrewrite.scheduling.WatchableExecutionContext;
import org.openrewrite.table.RecipeRunStats;
import org.openrewrite.table.SearchResults;
import org.openrewrite.table.SourcesFileErrors;
import org.openrewrite.table.SourcesFileResults;

Expand Down Expand Up @@ -91,8 +92,9 @@ private Object getVisitorP(Visit request) {
// removed from OpenRewrite in the future.
WatchableExecutionContext ctx = new WatchableExecutionContext((ExecutionContext) p);
ctx.putCycle(new RecipeRunCycle<>(recipe, 0, new Cursor(null, Cursor.ROOT_VALUE), ctx,
new RecipeRunStats(Recipe.noop()), new SourcesFileResults(Recipe.noop()),
new SourcesFileErrors(Recipe.noop()), LargeSourceSet::edit));
new RecipeRunStats(Recipe.noop()), new SearchResults(Recipe.noop()),
new SourcesFileResults(Recipe.noop()), new SourcesFileErrors(Recipe.noop()),
LargeSourceSet::edit));
ctx.putCurrentRecipe(recipe);
return ctx;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@
import org.openrewrite.internal.ExceptionUtils;
import org.openrewrite.internal.FindRecipeRunException;
import org.openrewrite.internal.RecipeRunException;
import org.openrewrite.marker.Generated;
import org.openrewrite.marker.Markers;
import org.openrewrite.marker.RecipesThatMadeChanges;
import org.openrewrite.marker.*;
import org.openrewrite.quark.Quark;
import org.openrewrite.table.RecipeRunStats;
import org.openrewrite.table.SearchResults;
import org.openrewrite.table.SourcesFileErrors;
import org.openrewrite.table.SourcesFileResults;

Expand All @@ -41,8 +40,7 @@
import java.util.function.BiFunction;
import java.util.function.UnaryOperator;

import static java.util.Collections.newSetFromMap;
import static java.util.Collections.unmodifiableList;
import static java.util.Collections.*;
import static org.openrewrite.ExecutionContext.SCANNING_MUTATION_VALIDATION;
import static org.openrewrite.Recipe.PANIC;

Expand All @@ -64,6 +62,7 @@ public class RecipeRunCycle<LSS extends LargeSourceSet> {
Cursor rootCursor;
WatchableExecutionContext ctx;
RecipeRunStats recipeRunStats;
SearchResults searchResults;
SourcesFileResults sourcesFileResults;
SourcesFileErrors errorsTable;
BiFunction<LSS, UnaryOperator<@Nullable SourceFile>, LSS> sourceSetEditor;
Expand Down Expand Up @@ -152,7 +151,7 @@ public LSS generateSources(LSS sourceSet) {
generated.replaceAll(source -> addRecipesThatMadeChanges(recipeStack, source));
if (!generated.isEmpty()) {
acc.addAll(generated);
generated.forEach(source -> recordSourceFileResult(null, source, recipeStack, ctx));
generated.forEach(source -> recordSourceFileResultAndSearchResults(null, source, recipeStack, ctx));
madeChangesInThisCycle.add(recipe);
}
} catch (Throwable t) {
Expand Down Expand Up @@ -214,7 +213,7 @@ public LSS editSources(LSS sourceSet) {

if (after != source) {
madeChangesInThisCycle.add(recipe);
recordSourceFileResult(source, after, recipeStack, ctx);
recordSourceFileResultAndSearchResults(source, after, recipeStack, ctx);
if (source.getMarkers().findFirst(Generated.class).isPresent()) {
// skip edits made to generated source files so that they don't show up in a diff
// that later fails to apply on a freshly cloned repository
Expand All @@ -234,11 +233,11 @@ public LSS editSources(LSS sourceSet) {
}
return after;
}, sourceFile);
}
}
);
}

private void recordSourceFileResult(@Nullable SourceFile before, @Nullable SourceFile after, Stack<Recipe> recipeStack, ExecutionContext ctx) {
private void recordSourceFileResultAndSearchResults(@Nullable SourceFile before, @Nullable SourceFile after, Stack<Recipe> recipeStack, ExecutionContext ctx) {
String beforePath = (before == null) ? "" : before.getSourcePath().toString();
String afterPath = (after == null) ? "" : after.getSourcePath().toString();
Recipe recipe = recipeStack.peek();
Expand All @@ -250,14 +249,19 @@ private void recordSourceFileResult(@Nullable SourceFile before, @Nullable Sourc
if (hierarchical) {
parentName = recipeStack.get(recipeStack.size() - 2).getName();
}
String recipeName = recipe.getName();
sourcesFileResults.insertRow(ctx, new SourcesFileResults.Row(
beforePath,
afterPath,
parentName,
recipeName,
recipe.getName(),
effortSeconds,
cycle));

List<String> searchMarkers = collectSearchResults(before, after);
for (String searchResult : searchMarkers) {
searchResults.insertRow(ctx, new SearchResults.Row(beforePath, afterPath, searchResult, recipe.getInstanceName()));
}

if (hierarchical) {
recordSourceFileResult(beforePath, afterPath, recipeStack.subList(0, recipeStack.size() - 1), effortSeconds, ctx);
}
Expand Down Expand Up @@ -347,4 +351,35 @@ private static boolean isScanningRequired(Recipe recipe) {
}
return false;
}

private List<String> collectSearchResults(@Nullable SourceFile before, @Nullable SourceFile after) {
if (after == null) {
return emptyList();
}
Set<SearchResult> alreadyPresentMarkers = new TreeVisitor<Tree, Set<SearchResult>>() {
@Override
public <M extends Marker> M visitMarker(Marker marker, Set<SearchResult> ctx) {
if (marker instanceof SearchResult) {
ctx.add((SearchResult) marker);
}
return super.visitMarker(marker, ctx);
}
}.reduce(before, newSetFromMap(new IdentityHashMap<>()));

return new TreeVisitor<Tree, List<String>>() {
@Override
public <M extends Marker> M visitMarker(Marker marker, List<String> ctx) {
if (marker instanceof SearchResult && !alreadyPresentMarkers.contains(marker)) {
Cursor cursor = getCursor();
if (!(cursor.getValue() instanceof Tree)) {
cursor = cursor.getParentTreeCursor();
}
if (cursor.getValue() instanceof Tree) {
ctx.add(((Tree) cursor.getValue()).print(getCursor(), new PrintOutputCapture<>(0, PrintOutputCapture.MarkerPrinter.SANITIZED)));
}
}
return super.visitMarker(marker, ctx);
}
}.reduce(after, new ArrayList<>());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2022 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.table;

import lombok.Value;
import org.jspecify.annotations.Nullable;
import org.openrewrite.Column;
import org.openrewrite.DataTable;
import org.openrewrite.Recipe;

public class SearchResults extends DataTable<SearchResults.Row> {

public SearchResults(Recipe recipe) {
super(recipe, "Source files that had search results",
"Search results that were found during the recipe run.");
}

@Value
public static class Row {
@Column(displayName = "Source path of search result before the run",
description = "The source path of the file with the search result markers present.")
@Nullable
String sourcePath;

@Column(displayName = "Source path of search result after run the run",
description = "A recipe may modify the source path. This is the path after the run. `null` when a source file was deleted during the run.")
@Nullable
String afterSourcePath;

@Column(displayName = "Result",
description = "The trimmed printed tree of the LST element that the marker is attached to.")
String result;

@Column(displayName = "Recipe that added the search marker",
description = "The specific recipe that added the Search marker.")
String recipe;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public PlainText visitText(PlainText text, ExecutionContext ctx) {
void descriptor() {
Recipe recipe = toRecipe();
new WordTable(recipe);
assertThat(recipe.getDataTableDescriptors()).hasSize(4);
assertThat(recipe.getDataTableDescriptors()).hasSize(5);
assertThat(recipe.getDataTableDescriptors().getFirst().getColumns()).hasSize(2);
}

Expand Down
Loading