Skip to content

Commit

Permalink
For now only apply precondition to edit phase
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden committed Feb 16, 2024
1 parent c9b6394 commit 7e64cb2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,6 @@ public String getDescription() {
@NonFinal
transient boolean preconditionApplicable;

@NonFinal
transient boolean preconditionEverApplicable;

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new TreeVisitor<Tree, ExecutionContext>() {
Expand All @@ -159,7 +156,6 @@ public boolean isAcceptable(SourceFile sourceFile, ExecutionContext ctx) {
public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
Tree t = precondition.visit(tree, ctx);
preconditionApplicable = t != tree;
preconditionEverApplicable |= preconditionApplicable;
return tree;
}
};
Expand Down Expand Up @@ -201,7 +197,7 @@ public List<Recipe> getRecipeList() {

@Value
@EqualsAndHashCode(callSuper = false)
static class BellwetherDecoratedScanningRecipe<T> extends ScanningRecipe<T> {
static class BellwetherDecoratedScanningRecipe<T> extends ScanningRecipe<T> {

DeclarativeRecipe.PreconditionBellwether bellwether;
ScanningRecipe<T> delegate;
Expand All @@ -228,16 +224,12 @@ public T getInitialValue(ExecutionContext ctx) {

@Override
public TreeVisitor<?, ExecutionContext> getScanner(T acc) {
return Preconditions.check(bellwether.getVisitor(), delegate.getScanner(acc));
return delegate.getScanner(acc);
}

@Override
public Collection<? extends SourceFile> generate(T acc, ExecutionContext ctx) {
try {
return bellwether.isPreconditionEverApplicable() ? delegate.generate(acc, ctx) : super.generate(acc, ctx);
} finally {
bellwether.preconditionEverApplicable = false;
}
return delegate.generate(acc, ctx);
}

@Override
Expand All @@ -253,18 +245,18 @@ public List<Recipe> getRecipeList() {

@Override
public final List<Recipe> getRecipeList() {
if(preconditions.isEmpty()) {
if (preconditions.isEmpty()) {
return recipeList;
}

TreeVisitor<?, ExecutionContext> andPreconditions = null;
for (Recipe precondition : preconditions) {
if(isScanningRecipe(precondition)) {
if (isScanningRecipe(precondition)) {
throw new IllegalArgumentException(
getName() + " declares the ScanningRecipe " + precondition.getName() + " as a precondition." +
"ScanningRecipe cannot be used as Preconditions.");
}
if(andPreconditions == null) {
if (andPreconditions == null) {
andPreconditions = precondition.getVisitor();
} else {
andPreconditions = Preconditions.and(andPreconditions, precondition.getVisitor());
Expand All @@ -278,11 +270,11 @@ public final List<Recipe> getRecipeList() {
}

private static boolean isScanningRecipe(Recipe recipe) {
if(recipe instanceof ScanningRecipe) {
if (recipe instanceof ScanningRecipe) {
return true;
}
for (Recipe r : recipe.getRecipeList()) {
if(isScanningRecipe(r)) {
if (isScanningRecipe(r)) {
return true;
}
}
Expand All @@ -292,7 +284,7 @@ private static boolean isScanningRecipe(Recipe recipe) {
private static List<Recipe> decorateWithPreconditionBellwether(PreconditionBellwether bellwether, List<Recipe> recipeList) {
List<Recipe> mappedRecipeList = new ArrayList<>(recipeList.size());
for (Recipe recipe : recipeList) {
if(recipe instanceof ScanningRecipe) {
if (recipe instanceof ScanningRecipe) {
mappedRecipeList.add(new BellwetherDecoratedScanningRecipe<>(bellwether, (ScanningRecipe<?>) recipe));
} else {
mappedRecipeList.add(new BellwetherDecoratedRecipe(bellwether, recipe));
Expand Down Expand Up @@ -324,8 +316,8 @@ public void addValidation(Validated<Object> validated) {
@Override
public Validated<Object> validate() {
return Validated.<Object>test("initialization",
"initialize(..) must be called on DeclarativeRecipe prior to use.",
this, r -> initValidation != null)
"initialize(..) must be called on DeclarativeRecipe prior to use.",
this, r -> initValidation != null)
.and(validation)
.and(initValidation);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,18 +88,37 @@ void yamlPrecondition() {
toText: 2
- org.openrewrite.text.ChangeText:
toText: 3
""", "org.openrewrite.PreconditionTest"),
text("1", "3"),
text("2")
);
}

@Test
void yamlPreconditionWithScanningRecipe() {
rewriteRun(
spec -> spec.recipeFromYaml("""
---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.PreconditionTest
preconditions:
- org.openrewrite.text.Find:
find: 1
recipeList:
- org.openrewrite.text.CreateTextFile:
relativeFileName: test.txt
fileContents: "test"
""", "org.openrewrite.PreconditionTest")
.afterRecipe(run -> {
assertThat(run.getChangeset().getAllResults()).anySatisfy(
s -> assertThat(s.getAfter().getSourcePath()).isEqualTo(Paths.get("test.txt"))
s -> {
assertThat(s.getAfter()).isNotNull();
assertThat(s.getAfter().getSourcePath()).isEqualTo(Paths.get("test.txt"));
}
);
System.out.println(run);
}),
text("1", "3"),
text("2")
})
.expectedCyclesThatMakeChanges(1),
text("1")
);
}

Expand Down

0 comments on commit 7e64cb2

Please sign in to comment.