From 3d30a7237e3b31bdae225cf580753c2a69388210 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Wed, 20 Nov 2024 22:40:06 +0100 Subject: [PATCH] Find Yaml recipes as well --- .../java/recipes/FindRecipesTest.java | 73 +++++++++++++++++++ .../openrewrite/java/recipes/FindRecipes.java | 49 ++++++++++++- 2 files changed, 120 insertions(+), 2 deletions(-) diff --git a/rewrite-java-test/src/test/java/org/openrewrite/java/recipes/FindRecipesTest.java b/rewrite-java-test/src/test/java/org/openrewrite/java/recipes/FindRecipesTest.java index 988d0b18deb..3624f6bfe8e 100644 --- a/rewrite-java-test/src/test/java/org/openrewrite/java/recipes/FindRecipesTest.java +++ b/rewrite-java-test/src/test/java/org/openrewrite/java/recipes/FindRecipesTest.java @@ -24,6 +24,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.openrewrite.java.Assertions.java; +import static org.openrewrite.yaml.Assertions.yaml; class FindRecipesTest implements RewriteTest { @@ -174,4 +175,76 @@ class SomeRefasterRule { ) ); } + + @Test + void findYamlRecipe() { + rewriteRun( + spec -> spec.parser(JavaParser.fromJavaVersion().dependsOn( + """ + package org.openrewrite.java.template; + import java.lang.annotation.ElementType; + import java.lang.annotation.Target; + @Target(ElementType.TYPE) + public @interface RecipeDescriptor { + String name(); + String description(); + } + """ + )) + .dataTable(RewriteRecipeSource.Row.class, rows -> { + assertThat(rows).hasSize(2); + assertThat(rows.get(0).getDisplayName()).isEqualTo("Migrates to Apache Commons Lang 3.x"); + assertThat(rows.get(0).getSourceCode()).startsWith( + "---\ntype: specs.openrewrite.org/v1beta/recipe\nname: org.openrewrite.apache.commons.lang.UpgradeApacheCommonsLang_2_3"); + assertThat(rows.get(1).getDisplayName()).isEqualTo("Migrates to Apache POI 3.17"); + assertThat(rows.get(1).getSourceCode()).startsWith( + "---\ntype: specs.openrewrite.org/v1beta/recipe\nname: org.openrewrite.apache.poi.UpgradeApachePoi_3_17"); + }), + yaml( + """ + # Apache Commons Lang + --- + type: specs.openrewrite.org/v1beta/recipe + name: org.openrewrite.apache.commons.lang.UpgradeApacheCommonsLang_2_3 + displayName: Migrates to Apache Commons Lang 3.x + description: >- + Migrate applications to the latest Apache Commons Lang 3.x release. This recipe modifies\s + application's build files, and changes the package as per [the migration release notes](https://commons.apache.org/proper/commons-lang/article3_0.html). + tags: + - apache + - commons + - lang + recipeList: + - org.openrewrite.java.dependencies.ChangeDependency: + oldGroupId: commons-lang + oldArtifactId: commons-lang + newGroupId: org.apache.commons + newArtifactId: commons-lang3 + newVersion: 3.x + - org.openrewrite.java.ChangePackage: + oldPackageName: org.apache.commons.lang + newPackageName: org.apache.commons.lang3 + --- + type: specs.openrewrite.org/v1beta/recipe + name: org.openrewrite.apache.poi.UpgradeApachePoi_3_17 + displayName: Migrates to Apache POI 3.17 + description: Migrates to the last Apache POI 3.x release. This recipe modifies build files and makes changes to deprecated/preferred APIs that have changed between versions. + tags: + - apache + - poi + recipeList: + - org.openrewrite.java.dependencies.ChangeDependency: + oldGroupId: poi + oldArtifactId: poi + newGroupId: org.apache.poi + newArtifactId: poi + newVersion: 3.x + """, + spec -> spec.path("rewrite.yml").after(after -> { + assertThat(after).contains("~~>"); + return after; + }) + ) + ); + } } diff --git a/rewrite-java/src/main/java/org/openrewrite/java/recipes/FindRecipes.java b/rewrite-java/src/main/java/org/openrewrite/java/recipes/FindRecipes.java index b00f5e9c14e..d460a4ffa7a 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/recipes/FindRecipes.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/recipes/FindRecipes.java @@ -31,9 +31,13 @@ import org.openrewrite.java.tree.TypeUtils; import org.openrewrite.marker.SearchResult; import org.openrewrite.table.RewriteRecipeSource; +import org.openrewrite.yaml.YamlIsoVisitor; +import org.openrewrite.yaml.search.FindProperty; +import org.openrewrite.yaml.tree.Yaml; import java.util.ArrayList; import java.util.List; +import java.util.Set; import static java.util.Objects.requireNonNull; @@ -53,14 +57,16 @@ public String getDescription() { @Override public TreeVisitor getVisitor() { - TreeVisitor findRefasterRecipes = findRefasterRecipes(); TreeVisitor findImperativeRecipes = findImperativeRecipes(); + TreeVisitor findRefasterRecipes = findRefasterRecipes(); + TreeVisitor findYamlRecipes = findYamlRecipes(); return new TreeVisitor() { @Override public @Nullable Tree preVisit(@NonNull Tree tree, ExecutionContext ctx) { stopAfterPreVisit(); - tree = findRefasterRecipes.visit(tree, ctx); tree = findImperativeRecipes.visit(tree, ctx); + tree = findRefasterRecipes.visit(tree, ctx); + tree = findYamlRecipes.visit(tree, ctx); return tree; } }; @@ -213,4 +219,43 @@ private ValueNode mapValue(@Nullable Object value) { } }); } + + private TreeVisitor findYamlRecipes() { + return Preconditions.check( + new FindProperty("type", false, "specs.openrewrite.org/v1beta/recipe"), + new YamlIsoVisitor() { + @Override + public Yaml.Document visitDocument(Yaml.Document document, ExecutionContext ctx) { + Yaml.Document doc = super.visitDocument(document, ctx); + + if ("specs.openrewrite.org/v1beta/recipe".equals(extractValue(doc, "type"))) { + String displayName = extractValue(doc, "displayName"); + String description = extractValue(doc, "description"); + if (displayName != null && description != null) { + recipeSource.insertRow(ctx, new RewriteRecipeSource.Row( + displayName, + description, + RewriteRecipeSource.RecipeType.Yaml, + doc.withPrefix("").printTrimmed(getCursor()), + "[]" + )); + return SearchResult.found(doc); + } + } + return doc; + } + + private @Nullable String extractValue(Yaml yaml, String key) { + Set blocks = FindProperty.find(yaml, key, false); + if (!blocks.isEmpty()) { + Yaml.Block first = blocks.iterator().next(); + if (first instanceof Yaml.Scalar) { + return ((Yaml.Scalar) first).getValue(); + } + } + return null; + } + } + ); + } }