Skip to content

Commit

Permalink
Polish and disable recipe examples
Browse files Browse the repository at this point in the history
  • Loading branch information
jkschneider committed Aug 3, 2023
1 parent e034bfd commit dec3f04
Show file tree
Hide file tree
Showing 6 changed files with 884 additions and 856 deletions.
34 changes: 16 additions & 18 deletions src/main/java/org/openrewrite/gradle/ExamplesExtractor.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ public class ExamplesExtractor extends JavaIsoVisitor<ExecutionContext> {
private static final AnnotationMatcher DOCUMENT_EXAMPLE_ANNOTATION_MATCHER = new AnnotationMatcher("@org.openrewrite.DocumentExample");

private static final MethodMatcher REWRITE_RUN_METHOD_MATCHER_WITH_SPEC =
new MethodMatcher("org.openrewrite.test.RewriteTest rewriteRun(java.util.function.Consumer, org.openrewrite.test.SourceSpecs[])");
new MethodMatcher("org.openrewrite.test.RewriteTest rewriteRun(java.util.function.Consumer, org.openrewrite.test.SourceSpecs[])");
private static final MethodMatcher REWRITE_RUN_METHOD_MATCHER =
new MethodMatcher("org.openrewrite.test.RewriteTest rewriteRun(org.openrewrite.test.SourceSpecs[])");
new MethodMatcher("org.openrewrite.test.RewriteTest rewriteRun(org.openrewrite.test.SourceSpecs[])");

private static final MethodMatcher JAVA_METHOD_MATCHER = new MethodMatcher("org.openrewrite.java.Assertions java(..)");
private static final MethodMatcher BUILD_GRADLE_METHOD_MATCHER = new MethodMatcher("org.openrewrite.gradle.Assertions buildGradle(..)");
Expand All @@ -97,11 +97,9 @@ public class ExamplesExtractor extends JavaIsoVisitor<ExecutionContext> {
private static final MethodMatcher HCL_METHOD_MATCHER = new MethodMatcher("org.openrewrite.hcl.Assertions hcl(..)");
private static final MethodMatcher GROOVY_METHOD_MATCHER = new MethodMatcher("org.openrewrite.groovy.Assertions groovy(..)");
private static final MethodMatcher KOTLIN_METHOD_MATCHER = new MethodMatcher("org.openrewrite.kotlin.Assertions kotlin(..)");
private static final MethodMatcher SPEC_RECIPE_METHOD_MATCHER = new MethodMatcher("org.openrewrite.test.RecipeSpec recipe(..)");
private static final MethodMatcher ACTIVE_RECIPES_METHOD_MATCHER = new MethodMatcher("org.openrewrite.config.Environment activateRecipes(..)");
private static final MethodMatcher PATH_METHOD_MATCHER = new MethodMatcher("org.openrewrite.test.SourceSpec path(java.lang.String)");


private final String recipeType;
private RecipeNameAndParameters defaultRecipe;
private RecipeNameAndParameters specifiedRecipe;
Expand All @@ -122,19 +120,17 @@ public ExamplesExtractor() {
public String printRecipeExampleYaml() {
boolean usingDefaultRecipe = !specifiedRecipe.isValid();
return new ExamplesExtractor.YamlPrinter().print(recipeType,
usingDefaultRecipe ? defaultRecipe : specifiedRecipe,
usingDefaultRecipe,
recipeExamples);
usingDefaultRecipe ? defaultRecipe : specifiedRecipe,
usingDefaultRecipe,
recipeExamples);
}

@Override
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) {
if (method.getName().getSimpleName().equals("defaults") &&
method.getMethodType() != null &&
method.getMethodType().getDeclaringType() != null &&
!method.getMethodType().getDeclaringType().getInterfaces().isEmpty() &&
method.getMethodType().getDeclaringType().getInterfaces().get(0).getFullyQualifiedName().equals("org.openrewrite.test.RewriteTest")
) {
method.getMethodType().getDeclaringType().getInterfaces().get(0).getFullyQualifiedName().equals("org.openrewrite.test.RewriteTest")) {
defaultRecipe = findRecipe(method);
return method;
}
Expand Down Expand Up @@ -199,7 +195,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
if (!example.getSources().isEmpty()) {
example.setDescription(exampleDescription);
example.setParameters(recipe != null ? recipe.getParameters() :
defaultRecipe != null ? defaultRecipe.getParameters() : new ArrayList<>());
defaultRecipe != null ? defaultRecipe.getParameters() : new ArrayList<>());
this.recipeExamples.add(example);
}

Expand All @@ -212,7 +208,7 @@ private static boolean hasNotAnnotation(List<J.Annotation> annotations, Annotati

public static class YamlPrinter {
String print(String recipeType,
RecipeNameAndParameters recipe,
@Nullable RecipeNameAndParameters recipe,
boolean usingDefaultRecipe,
List<RecipeExample> examples) {
if (recipe == null ||
Expand All @@ -233,7 +229,8 @@ String print(String recipeType,

for (RecipeExample example : examples) {
Map<String, Object> exampleData = new LinkedHashMap<>();
exampleData.put("description", example.getDescription() == null ? "" : example.getDescription());
example.getDescription();
exampleData.put("description", example.getDescription());

List<String> params = usingDefaultRecipe ? recipe.getParameters() : example.getParameters();
if (params != null && !params.isEmpty()) {
Expand Down Expand Up @@ -386,9 +383,9 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method,

// arg0 is always `before`. arg1 is optional to be `after`, to adjust if code changed
J.Literal before = !args.isEmpty() ? (args.get(0) instanceof J.Literal ? (J.Literal) args.get(0) : null) : null;
J.Literal after = args.size() > 1? (args.get(1) instanceof J.Literal ? (J.Literal) args.get(1) : null) : null;
J.Literal after = args.size() > 1 ? (args.get(1) instanceof J.Literal ? (J.Literal) args.get(1) : null) : null;

if (before != null) {
if (before != null && before.getValue() != null) {
source.setBefore((String) before.getValue());
}

Expand All @@ -397,7 +394,8 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method,
}

if (StringUtils.isNullOrEmpty(source.getPath())) {
source.setPath(getPath(source.getBefore() != null ? source.getBefore() : source.getAfter(), language));
source.getBefore();
source.setPath(getPath(source.getBefore(), language));
}
return method;
}
Expand Down Expand Up @@ -429,11 +427,11 @@ String getPath(@Language("java") @Nullable String content, String language) {
if (language.equals("java")) {
try {
Stream<SourceFile> cusStream = JavaParser.fromJavaVersion()
.build().parse(content);
.build().parse(content);
Optional<SourceFile> firstElement = cusStream.findFirst();

if (firstElement.isPresent()) {
return firstElement.get().getSourcePath().toString();
return firstElement.get().getSourcePath().toString();
}
} catch (Exception e) {
// do nothing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ public void apply(Project project) {
project.getPlugins().apply(RewriteBuildInputLoggingPlugin.class);
project.getPlugins().apply(RewritePublishPlugin.class);
project.getPlugins().apply(RewriteRecipeAuthorAttributionPlugin.class);
project.getPlugins().apply(RewriteRecipeExamplesPlugin.class);
// project.getPlugins().apply(RewriteRecipeExamplesPlugin.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void apply(Project project) {
project.getPlugins().apply(RewritePublishPlugin.class);
project.getPlugins().apply(PublishVerificationPlugin.class);
project.getPlugins().apply(RewriteRecipeAuthorAttributionPlugin.class);
project.getPlugins().apply(RewriteRecipeExamplesPlugin.class);
// project.getPlugins().apply(RewriteRecipeExamplesPlugin.class);

project.getDependencies().add("testImplementation",
"org.openrewrite:rewrite-test:" + ext.getRewriteVersion().get());
Expand Down
5 changes: 5 additions & 0 deletions src/test/java/org/openrewrite/gradle/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
root = true

[*.java]
indent_size = 4
ij_continuation_indent_size = 2
Loading

0 comments on commit dec3f04

Please sign in to comment.