From c01c0a9d0bfaaa85bd80f7629e154e3059193b45 Mon Sep 17 00:00:00 2001 From: Didier Loiseau Date: Fri, 20 Sep 2024 02:44:16 +0200 Subject: [PATCH 1/5] Added tests for declarative recipes as preconditions Includes the example from the documentation. --- .../config/DeclarativeRecipeTest.java | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/rewrite-core/src/test/java/org/openrewrite/config/DeclarativeRecipeTest.java b/rewrite-core/src/test/java/org/openrewrite/config/DeclarativeRecipeTest.java index a70b9c7701d..572428023df 100644 --- a/rewrite-core/src/test/java/org/openrewrite/config/DeclarativeRecipeTest.java +++ b/rewrite-core/src/test/java/org/openrewrite/config/DeclarativeRecipeTest.java @@ -174,6 +174,62 @@ void yamlPrecondition() { ); } + @Test + void yamlDeclarativeRecipeAsPrecondition() { + rewriteRun( + spec -> spec.recipeFromYaml(""" + --- + type: specs.openrewrite.org/v1beta/recipe + name: org.openrewrite.PreconditionTest + description: Test. + preconditions: + - org.openrewrite.DeclarativePrecondition + recipeList: + - org.openrewrite.text.ChangeText: + toText: 2 + - org.openrewrite.text.ChangeText: + toText: 3 + --- + type: specs.openrewrite.org/v1beta/recipe + name: org.openrewrite.DeclarativePrecondition + recipeList: + - org.openrewrite.text.Find: + find: 1 + """, "org.openrewrite.PreconditionTest"), + text("1", "3"), + text("2") + ); + } + + @Test + void orPreconditions() { + // example from https://docs.openrewrite.org/reference/yaml-format-reference#creating-or-preconditions-instead-of-and + rewriteRun( + spec -> spec.recipeFromYaml(""" + type: specs.openrewrite.org/v1beta/recipe + name: org.sample.DoSomething + description: Test. + preconditions: + - org.sample.FindAnyJson + recipeList: + - org.openrewrite.text.ChangeText: + toText: 2 + --- + type: specs.openrewrite.org/v1beta/recipe + name: org.sample.FindAnyJson + recipeList: + - org.openrewrite.FindSourceFiles: + filePattern: "**/my.json" + - org.openrewrite.FindSourceFiles: + filePattern: "**/your.json" + - org.openrewrite.FindSourceFiles: + filePattern: "**/our.json" + """, "org.sample.DoSomething"), + text("1", "2", spec -> spec.path("a/my.json")), + text("a", spec -> spec.path("a/not-my.json")) + ); + } + @Test void yamlPreconditionWithScanningRecipe() { rewriteRun( From 3e98b1f6e0b3bc6c65ebf6afd5f5a3498f3f87fb Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Fri, 20 Sep 2024 16:06:32 +0200 Subject: [PATCH 2/5] Allow declarative recipes to be used as preconditions --- .../org/openrewrite/config/DeclarativeRecipe.java | 12 +++++++++++- .../openrewrite/config/DeclarativeRecipeTest.java | 2 -- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/rewrite-core/src/main/java/org/openrewrite/config/DeclarativeRecipe.java b/rewrite-core/src/main/java/org/openrewrite/config/DeclarativeRecipe.java index 150575a3217..765e0062b50 100644 --- a/rewrite-core/src/main/java/org/openrewrite/config/DeclarativeRecipe.java +++ b/rewrite-core/src/main/java/org/openrewrite/config/DeclarativeRecipe.java @@ -270,7 +270,8 @@ public final List getRecipeList() { getName() + " declares the ScanningRecipe " + precondition.getName() + " as a precondition." + "ScanningRecipe cannot be used as Preconditions."); } - andPreconditions.add(precondition::getVisitor); + andPreconditions.add(precondition instanceof DeclarativeRecipe ? + (() -> or((DeclarativeRecipe) precondition)) : precondition::getVisitor); } PreconditionBellwether bellwether = new PreconditionBellwether(Preconditions.and(andPreconditions.toArray(new Supplier[]{}))); List recipeListWithBellwether = new ArrayList<>(recipeList.size() + 1); @@ -279,6 +280,15 @@ public final List getRecipeList() { return recipeListWithBellwether; } + private static TreeVisitor or(DeclarativeRecipe recipe) { + List> conditions = new ArrayList<>(); + for (Recipe r : recipe.getRecipeList()) { + conditions.add(r instanceof DeclarativeRecipe ? or((DeclarativeRecipe) r) : r.getVisitor()); + } + //noinspection unchecked + return Preconditions.or(conditions.>toArray(new TreeVisitor[0])); + } + private static boolean isScanningRecipe(Recipe recipe) { if (recipe instanceof ScanningRecipe) { return true; diff --git a/rewrite-core/src/test/java/org/openrewrite/config/DeclarativeRecipeTest.java b/rewrite-core/src/test/java/org/openrewrite/config/DeclarativeRecipeTest.java index 572428023df..3a3de2c199e 100644 --- a/rewrite-core/src/test/java/org/openrewrite/config/DeclarativeRecipeTest.java +++ b/rewrite-core/src/test/java/org/openrewrite/config/DeclarativeRecipeTest.java @@ -185,8 +185,6 @@ void yamlDeclarativeRecipeAsPrecondition() { preconditions: - org.openrewrite.DeclarativePrecondition recipeList: - - org.openrewrite.text.ChangeText: - toText: 2 - org.openrewrite.text.ChangeText: toText: 3 --- From 587fe425592aa2ff40098108c866cdc1e0473349 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Fri, 20 Sep 2024 16:23:00 +0200 Subject: [PATCH 3/5] Remove arguments to `toArray` --- .../src/main/java/org/openrewrite/config/DeclarativeRecipe.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rewrite-core/src/main/java/org/openrewrite/config/DeclarativeRecipe.java b/rewrite-core/src/main/java/org/openrewrite/config/DeclarativeRecipe.java index 765e0062b50..968837954fa 100644 --- a/rewrite-core/src/main/java/org/openrewrite/config/DeclarativeRecipe.java +++ b/rewrite-core/src/main/java/org/openrewrite/config/DeclarativeRecipe.java @@ -286,7 +286,7 @@ private static TreeVisitor or(DeclarativeRecipe recipe) { conditions.add(r instanceof DeclarativeRecipe ? or((DeclarativeRecipe) r) : r.getVisitor()); } //noinspection unchecked - return Preconditions.or(conditions.>toArray(new TreeVisitor[0])); + return Preconditions.or(conditions.toArray(new TreeVisitor[0])); } private static boolean isScanningRecipe(Recipe recipe) { From be3e43bfb3b38852da97ace94bc6dedc66b15bc1 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Fri, 20 Sep 2024 16:28:22 +0200 Subject: [PATCH 4/5] Remove DeclarativeRecipe specificity --- .../java/org/openrewrite/config/DeclarativeRecipe.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/rewrite-core/src/main/java/org/openrewrite/config/DeclarativeRecipe.java b/rewrite-core/src/main/java/org/openrewrite/config/DeclarativeRecipe.java index 968837954fa..0ecfab865fd 100644 --- a/rewrite-core/src/main/java/org/openrewrite/config/DeclarativeRecipe.java +++ b/rewrite-core/src/main/java/org/openrewrite/config/DeclarativeRecipe.java @@ -270,8 +270,7 @@ public final List getRecipeList() { getName() + " declares the ScanningRecipe " + precondition.getName() + " as a precondition." + "ScanningRecipe cannot be used as Preconditions."); } - andPreconditions.add(precondition instanceof DeclarativeRecipe ? - (() -> or((DeclarativeRecipe) precondition)) : precondition::getVisitor); + andPreconditions.add(() -> orVisitors(precondition)); } PreconditionBellwether bellwether = new PreconditionBellwether(Preconditions.and(andPreconditions.toArray(new Supplier[]{}))); List recipeListWithBellwether = new ArrayList<>(recipeList.size() + 1); @@ -280,10 +279,11 @@ public final List getRecipeList() { return recipeListWithBellwether; } - private static TreeVisitor or(DeclarativeRecipe recipe) { + private static TreeVisitor orVisitors(Recipe recipe) { List> conditions = new ArrayList<>(); + conditions.add(recipe.getVisitor()); for (Recipe r : recipe.getRecipeList()) { - conditions.add(r instanceof DeclarativeRecipe ? or((DeclarativeRecipe) r) : r.getVisitor()); + conditions.add(orVisitors(r)); } //noinspection unchecked return Preconditions.or(conditions.toArray(new TreeVisitor[0])); From 456be8c4535b8ba92275df726182262675a60843 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Fri, 20 Sep 2024 16:37:13 +0200 Subject: [PATCH 5/5] Improve formatting --- .../config/DeclarativeRecipeTest.java | 53 ++++++++++--------- 1 file changed, 29 insertions(+), 24 deletions(-) diff --git a/rewrite-core/src/test/java/org/openrewrite/config/DeclarativeRecipeTest.java b/rewrite-core/src/test/java/org/openrewrite/config/DeclarativeRecipeTest.java index 3a3de2c199e..22f8f6ee263 100644 --- a/rewrite-core/src/test/java/org/openrewrite/config/DeclarativeRecipeTest.java +++ b/rewrite-core/src/test/java/org/openrewrite/config/DeclarativeRecipeTest.java @@ -177,8 +177,8 @@ void yamlPrecondition() { @Test void yamlDeclarativeRecipeAsPrecondition() { rewriteRun( - spec -> spec.recipeFromYaml(""" - --- + spec -> spec.recipeFromYaml( + """ type: specs.openrewrite.org/v1beta/recipe name: org.openrewrite.PreconditionTest description: Test. @@ -193,7 +193,9 @@ void yamlDeclarativeRecipeAsPrecondition() { recipeList: - org.openrewrite.text.Find: find: 1 - """, "org.openrewrite.PreconditionTest"), + """, + "org.openrewrite.PreconditionTest" + ), text("1", "3"), text("2") ); @@ -201,28 +203,31 @@ void yamlDeclarativeRecipeAsPrecondition() { @Test void orPreconditions() { - // example from https://docs.openrewrite.org/reference/yaml-format-reference#creating-or-preconditions-instead-of-and + // As documented https://docs.openrewrite.org/reference/yaml-format-reference#creating-or-preconditions-instead-of-and rewriteRun( - spec -> spec.recipeFromYaml(""" - type: specs.openrewrite.org/v1beta/recipe - name: org.sample.DoSomething - description: Test. - preconditions: - - org.sample.FindAnyJson - recipeList: - - org.openrewrite.text.ChangeText: - toText: 2 - --- - type: specs.openrewrite.org/v1beta/recipe - name: org.sample.FindAnyJson - recipeList: - - org.openrewrite.FindSourceFiles: - filePattern: "**/my.json" - - org.openrewrite.FindSourceFiles: - filePattern: "**/your.json" - - org.openrewrite.FindSourceFiles: - filePattern: "**/our.json" - """, "org.sample.DoSomething"), + spec -> spec.recipeFromYaml( + """ + type: specs.openrewrite.org/v1beta/recipe + name: org.sample.DoSomething + description: Test. + preconditions: + - org.sample.FindAnyJson + recipeList: + - org.openrewrite.text.ChangeText: + toText: 2 + --- + type: specs.openrewrite.org/v1beta/recipe + name: org.sample.FindAnyJson + recipeList: + - org.openrewrite.FindSourceFiles: + filePattern: "**/my.json" + - org.openrewrite.FindSourceFiles: + filePattern: "**/your.json" + - org.openrewrite.FindSourceFiles: + filePattern: "**/our.json" + """, + "org.sample.DoSomething" + ), text("1", "2", spec -> spec.path("a/my.json")), text("a", spec -> spec.path("a/not-my.json")) );