Skip to content

Commit

Permalink
Move recipe printing to separate classes
Browse files Browse the repository at this point in the history
  • Loading branch information
ammachado committed Dec 20, 2023
1 parent c4de091 commit f7deb8b
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@
import lombok.Value;
import lombok.With;
import org.openrewrite.Contributor;
import org.openrewrite.Incubating;
import org.openrewrite.Maintainer;
import org.openrewrite.internal.lang.Nullable;

import java.net.URI;
import java.time.Duration;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.function.Consumer;
import java.util.stream.Collectors;

@Value
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
Expand Down Expand Up @@ -58,4 +62,23 @@ public class RecipeDescriptor {
List<RecipeExample> examples;

URI source;

@Incubating(since = "8.11.3")
public void print(Consumer<String> consumer) {
StringBuilder recipeString = new StringBuilder(name);

if (options != null && !options.isEmpty()) {
String opts = options.stream().map(option -> {
if (option.getValue() != null) {
return String.format("%s=%s", option.getName(), option.getValue());
}
return null;
}).filter(Objects::nonNull).collect(Collectors.joining(", "));
if (!opts.isEmpty()) {
recipeString.append(String.format(": {%s}", opts));
}
}

consumer.accept(recipeString.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ public static RecipeSpec defaults() {
@Nullable
Function<List<SourceFile>, LargeSourceSet> sourceSet;

@Nullable
RecipeTreePrinter recipeTreePrinter;

/**
* Configuration that applies to all source file inputs.
*/
Expand Down Expand Up @@ -282,4 +285,10 @@ public RecipeSpec afterTypeValidationOptions(TypeValidation typeValidation) {
ExecutionContext getExecutionContext() {
return executionContext;
}

@Incubating(since = "8.11.3")
public RecipeSpec printRecipe(RecipeTreePrinter recipeTreePrinter) {
this.recipeTreePrinter = recipeTreePrinter;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright 2023 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.test;

import lombok.Value;
import org.openrewrite.Incubating;
import org.openrewrite.Recipe;
import org.openrewrite.config.RecipeDescriptor;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Consumer;

@Incubating(since = "8.11.3")
public interface RecipeTreePrinter {

RecipeTreePrinter DEFAULT = new SystemOutRecipeTreePrinter();

Consumer<String> consumeRecipeDescriptor(String prefix);

/**
* Print the recipe tree for a recipe.
*
* @param recipe the
*/
default void printTree(Recipe recipe) {
printRecipe(recipe.getDescriptor(), "");
}

/**
* @return a set of recipe names to skip when printing the recipe tree.
*/
default Set<String> getRecipesToSkipChildren() {
return Collections.emptySet();
}

/**
* Print the recipe tree for a recipe descriptor.
*
* @param rd the recipe descriptor
* @param prefix the indentation prefix
*/
default void printRecipe(RecipeDescriptor rd, String prefix) {
Set<String> recipesToSkipChildren = getRecipesToSkipChildren();
if (recipesToSkipChildren.contains(rd.getName())) {
return;
}

rd.print(consumeRecipeDescriptor(prefix));

for (RecipeDescriptor child : rd.getRecipeList()) {
printRecipe(child, prefix + " ");
}
}

@Value
class SystemOutRecipeTreePrinter implements RecipeTreePrinter {

Set<String> recipesToSkipChildren = new HashSet<>();

@Override
public Consumer<String> consumeRecipeDescriptor(String prefix) {
return s -> {
System.out.print(prefix);
System.out.println(s);
};
}
}
}
75 changes: 9 additions & 66 deletions rewrite-test/src/main/java/org/openrewrite/test/RewriteTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.openrewrite.config.CompositeRecipe;
import org.openrewrite.config.Environment;
import org.openrewrite.config.OptionDescriptor;
import org.openrewrite.config.RecipeDescriptor;
import org.openrewrite.internal.InMemoryDiffEntry;
import org.openrewrite.internal.RecipeIntrospectionUtils;
import org.openrewrite.internal.StringUtils;
Expand Down Expand Up @@ -183,8 +182,15 @@ default void rewriteRun(Consumer<RecipeSpec> spec, SourceSpec<?>... sourceSpecs)
validateRecipeOptions(recipe);
}

if (printTree()) {
printTree(recipe);
RecipeTreePrinter recipeTreePrinter = null;
if (testMethodSpec.getRecipeTreePrinter() != null) {
recipeTreePrinter = testMethodSpec.getRecipeTreePrinter();
} else if (testClassSpec.getRecipeTreePrinter() != null) {
recipeTreePrinter = testClassSpec.getRecipeTreePrinter();
}

if (recipeTreePrinter != null) {
recipeTreePrinter.printTree(recipe);
}

int cycles = testMethodSpec.cycles == null ? testClassSpec.getCycles() : testMethodSpec.getCycles();
Expand Down Expand Up @@ -628,69 +634,6 @@ default void validateRecipeOptions(Recipe recipe) {
.isNotEqualTo("name");
}
}

/**
* @return true if the recipe tree should be printed for a recipe during test.
*/
default boolean printTree() {
return false;
}

/**
* Print the recipe tree for a recipe.
*
* @param recipe the
*/
default void printTree(Recipe recipe) {
printRecipe(recipe.getDescriptor(), "");
}

/**
* Print the recipe tree for a recipe descriptor.
*
* @param rd the recipe descriptor
* @param prefix the indentation prefix
*/
default void printRecipe(RecipeDescriptor rd, String prefix) {
Set<String> recipesToSkipChildren = recipesToSkipChildren();
if (recipesToSkipChildren.contains(rd.getName())) {
return;
}

StringBuilder recipeString = new StringBuilder(prefix + rd.getName());

if (!rd.getOptions().isEmpty()) {
String opts = rd.getOptions().stream().map(option -> {
if (option.getValue() != null) {
return String.format("%s=%s", option.getName(), option.getValue());
}
return null;
}).filter(Objects::nonNull).collect(Collectors.joining(", "));
if (!opts.isEmpty()) {
recipeString.append(String.format(": {%s}", opts));
}
}

consumeRecipeTree().accept(recipeString.toString());

for (RecipeDescriptor child : rd.getRecipeList()) {
printRecipe(child, prefix + " ");
}
}

/**
* @return a set of recipe names to skip when printing the recipe tree.
*/
default Set<String> recipesToSkipChildren() {
return Collections.emptySet();
}

/**
* @return a consumer that accepts a string to print the recipe tree to. Defaults to printing to stdout.
*/
default Consumer<String> consumeRecipeTree() {
return System.out::println;
}
}

class RewriteTestUtils {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
package org.openrewrite.test.internal;
/*
* Copyright 2023 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.test;

import lombok.EqualsAndHashCode;
import lombok.Value;
Expand All @@ -8,27 +23,18 @@
import org.openrewrite.Recipe;
import org.openrewrite.config.CompositeRecipe;
import org.openrewrite.java.recipes.SelectRecipeExamples;
import org.openrewrite.test.RewriteTest;

import java.util.Arrays;
import java.util.Collections;
import java.util.function.Consumer;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

class PrintRecipeTreeTest implements RewriteTest {
class RecipeTreePrinterTest implements RewriteTest {

private final StringBuilder sb = new StringBuilder();

@Override
public boolean printTree() {
return true;
}

@Override
public Consumer<String> consumeRecipeTree() {
return s -> sb.append(s).append(System.lineSeparator());
}
private final RecipeTreePrinter recipeTreePrinter = prefix ->
s -> sb.append(prefix).append(s).append(System.lineSeparator());

@BeforeEach
void beforeEach() {
Expand All @@ -38,7 +44,9 @@ void beforeEach() {
@Test
void printRecipeTreeForSimpleRecipe() {
rewriteRun(
spec -> spec.recipe(new SelectRecipeExamples())
spec -> spec
.recipe(new SelectRecipeExamples())
.printRecipe(recipeTreePrinter)
);

assertThat(sb.toString()).isEqualTo(SelectRecipeExamples.class.getName() + System.lineSeparator());
Expand All @@ -52,7 +60,9 @@ void printRecipeTreeForRecipeWithNestedRecipes() {
new CompositeRecipe(Collections.singletonList(new SelectRecipeExamples()))
));
rewriteRun(
spec -> spec.recipe(recipe)
spec -> spec
.recipe(recipe)
.printRecipe(recipeTreePrinter)
);

String output = sb.toString();
Expand Down

0 comments on commit f7deb8b

Please sign in to comment.