Skip to content

Commit

Permalink
Find Yaml recipes as well (#4695)
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek authored Nov 20, 2024
1 parent 98064c3 commit a303b79
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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;
})
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -53,14 +57,16 @@ public String getDescription() {

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
TreeVisitor<?, ExecutionContext> findRefasterRecipes = findRefasterRecipes();
TreeVisitor<?, ExecutionContext> findImperativeRecipes = findImperativeRecipes();
TreeVisitor<?, ExecutionContext> findRefasterRecipes = findRefasterRecipes();
TreeVisitor<?, ExecutionContext> findYamlRecipes = findYamlRecipes();
return new TreeVisitor<Tree, ExecutionContext>() {
@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;
}
};
Expand Down Expand Up @@ -213,4 +219,43 @@ private ValueNode mapValue(@Nullable Object value) {
}
});
}

private TreeVisitor<?, ExecutionContext> findYamlRecipes() {
return Preconditions.check(
new FindProperty("type", false, "specs.openrewrite.org/v1beta/recipe"),
new YamlIsoVisitor<ExecutionContext>() {
@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<Yaml.Block> 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;
}
}
);
}
}

0 comments on commit a303b79

Please sign in to comment.