Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find Yaml recipes as well #4695

Merged
merged 2 commits into from
Nov 20, 2024
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
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;
}
}
);
}
}