Skip to content

Commit

Permalink
recipe configuration reference information for discover help commands (
Browse files Browse the repository at this point in the history
  • Loading branch information
aegershman authored Apr 26, 2021
1 parent b41d568 commit 188eb90
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 147 deletions.
5 changes: 4 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.openrewrite.maven</groupId>
<artifactId>rewrite-maven-plugin</artifactId>
<version>4.1.4-SNAPSHOT</version>
<version>4.2.0-SNAPSHOT</version>
<name>rewrite-maven-plugin</name>

<packaging>maven-plugin</packaging>
Expand Down Expand Up @@ -472,6 +472,9 @@
</plugin>
</plugins>
</build>
<properties>
<maven.test.skip>true</maven.test.skip>
</properties>
</profile>
</profiles>

Expand Down
179 changes: 72 additions & 107 deletions src/main/java/org/openrewrite/maven/RewriteDiscoverMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,157 +3,122 @@
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.openrewrite.Recipe;
import org.openrewrite.config.Environment;
import org.openrewrite.config.OptionDescriptor;
import org.openrewrite.config.RecipeDescriptor;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.style.NamedStyles;

import java.util.Collection;
import java.util.HashSet;

/**
* Display available recipes found on the classpath.<br>
* {@code ./mvnw rewrite:discover -Drecipe=<recipe-name>} to display recipe configuration details. For example:<br>
* {@code ./mvnw rewrite:discover -Drecipe=org.openrewrite.java.format.AutoFormat}
* {@code ./mvnw rewrite:discover -Ddetail=true -Drecipe=<recipe-name>} to display recipe configuration details. For example:<br>
* {@code ./mvnw rewrite:discover -Ddetail=true -Drecipe=org.openrewrite.java.format.AutoFormat}
*/
@Mojo(name = "discover", threadSafe = true)
public class RewriteDiscoverMojo extends AbstractRewriteMojo {

private static final String RECIPE_NOT_FOUND_EXCEPTION_MSG = "Could not find recipe '%s' among available recipes";

/**
* The name of a specific recipe to show details for. For example:<br>
* {@code ./mvnw rewrite:discover -Drecipe=org.openrewrite.java.format.AutoFormat}
* {@code ./mvnw rewrite:discover -Ddetail=true -Drecipe=org.openrewrite.java.format.AutoFormat}
*/
@Nullable
@Parameter(property = "recipe")
String recipeFilter;
String recipe;

/**
* Whether to show verbose details of recipes and options.
* Whether to display recipe details such as displayName, description, and configuration options.
*/
@Parameter(property = "rewrite.discover.verbose", defaultValue = "false")
boolean verbose;
@Parameter(property = "detail", defaultValue = "false")
boolean detail;

/**
* Whether to show recipe details for recipes which use other recipes.
* The maximum level of recursion to display recipe descriptors under recipeList.
*/
@Parameter(property = "rewrite.discover.recursive", defaultValue = "false")
boolean recursive;
@Parameter(property = "recursion", defaultValue = "0")
int recursion;

@Override
public void execute() throws MojoExecutionException {
Environment env = environment();

if (recipeFilter != null) {
RecipeDescriptor recipeDescriptor = env.listRecipeDescriptors().stream().filter(r -> r.getName().equalsIgnoreCase(recipeFilter)).findAny().orElse(null);
if (recipeDescriptor == null) {
getLog().info("Recipe " + recipeFilter + " not found.");
} else {
logRecipeDescriptor(recipeDescriptor, verbose, recursive);
Collection<RecipeDescriptor> availableRecipeDescriptors = env.listRecipeDescriptors();
if (recipe != null) {
RecipeDescriptor rd = getRecipeDescriptor(recipe, availableRecipeDescriptors);
writeRecipeDescriptor(rd, detail, 0, 0);
} else {
Collection<RecipeDescriptor> activeRecipeDescriptors = new HashSet<>();
for (String activeRecipe : activeRecipes) {
RecipeDescriptor rd = getRecipeDescriptor(activeRecipe, availableRecipeDescriptors);
activeRecipeDescriptors.add(rd);
}
return;
writeDiscovery(availableRecipeDescriptors, activeRecipeDescriptors, env.listStyles());
}
Collection<Recipe> recipesByName = env.listRecipes();
getLog().info("Found " + activeRecipes.size() + " active recipes and " + recipesByName.size() + " activatable recipes.");
getLog().info("");
}

getLog().info("Active Recipes:");
for (String activeRecipe : activeRecipes) {
getLog().info(indent(1, activeRecipe));
private void writeDiscovery(Collection<RecipeDescriptor> availableRecipeDescriptors, Collection<RecipeDescriptor> activeRecipeDescriptors, Collection<NamedStyles> availableStyles) {
getLog().info(indent(0, "Available Recipes:"));
for (RecipeDescriptor recipeDescriptor : availableRecipeDescriptors) {
writeRecipeDescriptor(recipeDescriptor, detail, 0, 1);
}
getLog().info("");
getLog().info("Activatable Recipes:");
for (Recipe recipe : recipesByName) {
getLog().info(indent(1, recipe.getName()));

getLog().info(indent(0, "Available Styles:"));
for (NamedStyles style : availableStyles) {
getLog().info(indent(1, "name: " + style.getName()));
}
getLog().info("");
if (verbose) {
getLog().info("Descriptors:");
for (RecipeDescriptor recipeDescriptor : env.listRecipeDescriptors()) {
logRecipeDescriptor(recipeDescriptor, verbose, recursive);
}

getLog().info(indent(0, "Active Styles:"));
for (String activeStyle : activeStyles) {
getLog().info(indent(1, activeStyle));
}
}

private void logRecipeDescriptor(RecipeDescriptor recipeDescriptor, boolean verbose, boolean recursive) {
if (verbose) {
getLog().info(indent(1, "Name: " + recipeDescriptor.getName()));
getLog().info(indent(1, "Display name: " + recipeDescriptor.getDisplayName()));
getLog().info(indent(1, "Description: " + recipeDescriptor.getDescription()));
if (!recipeDescriptor.getTags().isEmpty()) {
getLog().info(indent(1, "Tags: " + String.join(",", recipeDescriptor.getTags())));
}
} else {
getLog().info(indent(1, recipeDescriptor.getName()));
getLog().info(indent(0, "Active Recipes:"));
for (RecipeDescriptor recipeDescriptor : activeRecipeDescriptors) {
writeRecipeDescriptor(recipeDescriptor, detail, 0, 1);
}
if (!recipeDescriptor.getOptions().isEmpty()) {

getLog().info(indent(0, ""));
getLog().info(indent(0, "Found " + availableRecipeDescriptors.size() + " available recipes and " + availableStyles.size() + " available styles."));
getLog().info(indent(0, "Configured with " + activeRecipeDescriptors.size() + " active recipes and " + activeStyles.size() + " active styles."));
}

private void writeRecipeDescriptor(RecipeDescriptor rd, boolean verbose, int currentRecursionLevel, int indentLevel) {
if (currentRecursionLevel <= recursion) {
getLog().info(indent(indentLevel, "name: " + rd.getName()));
if (verbose) {
getLog().info(indent(1, "Options:"));
}
for (OptionDescriptor optionDescriptor : recipeDescriptor.getOptions()) {
StringBuilder optionBuilder = new StringBuilder(optionDescriptor.getName())
.append(": ").append(optionDescriptor.getType());
if (optionDescriptor.isRequired()) {
optionBuilder.append("!");
}
getLog().info(indent(2, optionBuilder));
if (verbose) {
getLog().info(indent(2, "Display name: " + optionDescriptor.getDisplayName()));
getLog().info(indent(2, "Description: " + optionDescriptor.getDescription()));
getLog().info("");
getLog().info(indent(indentLevel, "displayName: " + rd.getDisplayName()));
getLog().info(indent(indentLevel, "description: " + rd.getDescription()));

getLog().info(indent(indentLevel, "options: " + (rd.getOptions().isEmpty() ? "[]" : "")));
for (OptionDescriptor od : rd.getOptions()) {
getLog().info(indent(indentLevel + 1, od.getName() + ": " + od.getType() + (od.isRequired() ? "!" : "")));
getLog().info(indent(indentLevel + 2, "displayName: " + od.getDisplayName()));
getLog().info(indent(indentLevel + 2, "description: " + od.getDescription()));
}
}
}
if (!recipeDescriptor.getRecipeList().isEmpty()) {
if (verbose) {
getLog().info(indent(1, "Recipe list:"));
}
for (RecipeDescriptor r : recipeDescriptor.getRecipeList()) {
logNestedRecipeDescriptor(r, verbose, recursive, 2);


if (!rd.getRecipeList().isEmpty() && (currentRecursionLevel + 1 <= recursion)) {
getLog().info(indent(indentLevel, "recipeList:"));
for (RecipeDescriptor r : rd.getRecipeList()) {
writeRecipeDescriptor(r, verbose, currentRecursionLevel + 1, indentLevel + 1);
}
}

if (verbose) {
getLog().info("");
getLog().info(indent(indentLevel, ""));
}
}
if (!verbose || (recipeDescriptor.getOptions().isEmpty() && recipeDescriptor.getRecipeList().isEmpty())) {
getLog().info("");
}

}

private void logNestedRecipeDescriptor(RecipeDescriptor recipeDescriptor, boolean verbose, boolean recursive, int indent) {
if (verbose) {
getLog().info(indent(indent, "Name: " + recipeDescriptor.getName()));
getLog().info(indent(indent, "Display name: " + recipeDescriptor.getDisplayName()));
getLog().info(indent(indent, "Description: " + recipeDescriptor.getDescription()));
if (!recipeDescriptor.getTags().isEmpty()) {
getLog().info(indent(indent, "Tags: " + String.join(",", recipeDescriptor.getTags())));
}
} else {
getLog().info(indent(indent, recipeDescriptor.getName()));
}
if (!recipeDescriptor.getOptions().isEmpty()) {
if (verbose) {
getLog().info(indent(indent, "Options:"));
}
for (OptionDescriptor optionDescriptor : recipeDescriptor.getOptions()) {
getLog().info(indent(indent + 1, optionDescriptor.getName() + ": " + optionDescriptor.getValue()));
}
if (verbose) {
getLog().info("");
}
}
if (recursive && !recipeDescriptor.getRecipeList().isEmpty()) {
if (verbose) {
getLog().info(indent(indent, "Recipe list:"));
}
for (RecipeDescriptor nestedRecipeDescriptor : recipeDescriptor.getRecipeList()) {
logNestedRecipeDescriptor(nestedRecipeDescriptor, verbose, true, indent + 1);
}
if (verbose) {
getLog().info("");
}
}
if (!verbose || (recipeDescriptor.getOptions().isEmpty() && recipeDescriptor.getRecipeList().isEmpty())) {
getLog().info("");
}
private static RecipeDescriptor getRecipeDescriptor(String recipe, Collection<RecipeDescriptor> recipeDescriptors) throws MojoExecutionException {
return recipeDescriptors.stream()
.filter(r -> r.getName().equalsIgnoreCase(recipe))
.findAny()
.orElseThrow(() -> new MojoExecutionException(String.format(RECIPE_NOT_FOUND_EXCEPTION_MSG, recipe)));
}
}
83 changes: 55 additions & 28 deletions src/test/java/org/openrewrite/maven/RewriteDiscoverIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,62 +5,89 @@
import com.soebes.itf.jupiter.extension.MavenTest;
import com.soebes.itf.jupiter.extension.SystemProperty;
import com.soebes.itf.jupiter.maven.MavenExecutionResult;
import org.junit.jupiter.api.Nested;

import static com.soebes.itf.extension.assertj.MavenITAssertions.assertThat;

@MavenJupiterExtension
@MavenGoal("${project.groupId}:${project.artifactId}:${project.version}:discover")
public class RewriteDiscoverIT {

@MavenTest
void rewrite_discover_default_output(MavenExecutionResult result) {
assertThat(result)
.isSuccessful()
.out()
.info()
.matches(logLines ->
logLines.stream().anyMatch(logLine -> logLine.contains("org.openrewrite.java.format.AutoFormat"))
)
.matches(logLines ->
logLines.stream().noneMatch(logLine -> logLine.contains("Descriptors"))
);
@Nested
@MavenGoal("${project.groupId}:${project.artifactId}:${project.version}:discover")
class RecipeLookup {

assertThat(result).out().warn().isEmpty();
}
@MavenTest
@SystemProperty(value = "detail", content = "true")
void rewrite_discover_detail(MavenExecutionResult result) {
assertThat(result)
.isSuccessful()
.out()
.info()
.matches(logLines ->
logLines.stream().anyMatch(logLine -> logLine.contains("displayName"))
)
.matches(logLines ->
logLines.stream().anyMatch(logLine -> logLine.contains("description"))
)
.matches(logLines ->
logLines.stream().anyMatch(logLine -> logLine.contains("options"))
);

@MavenTest
void rewrite_discover_reads_rewrite_yml(MavenExecutionResult result) {
assertThat(result)
.isSuccessful()
.out()
.info()
.matches(logLines ->
logLines.stream().anyMatch(logLine -> logLine.contains("com.example.RewriteDiscoverIT.CodeCleanup"))
);
assertThat(result).out().warn().isEmpty();
}

assertThat(result).out().warn().isEmpty();
@MavenTest
@SystemProperty(value = "recipe", content = "org.openrewrite.JAVA.format.AutoFormAT")
void rewrite_discover_recipe_lookup_case_insensitive(MavenExecutionResult result) {
assertThat(result)
.isSuccessful()
.out()
.info()
.matches(logLines ->
logLines.stream().anyMatch(logLine -> logLine.contains("org.openrewrite.java.format.AutoFormat"))
);
}

@MavenTest
@SystemProperty(value = "recursion", content = "1")
void rewrite_discover_recursion(MavenExecutionResult result) {
assertThat(result)
.isSuccessful()
.out()
.info()
.matches(logLines ->
logLines.stream().anyMatch(logLine -> logLine.contains("recipeList"))
);

assertThat(result).out().warn().isEmpty();
}
}

@MavenTest
@SystemProperty(value = "recipe", content = "org.openrewrite.JAVA.format.AutoFormAT")
void rewrite_discover_recipe_lookup_case_insensitive(MavenExecutionResult result) {
void rewrite_discover_default(MavenExecutionResult result) {
assertThat(result)
.isSuccessful()
.out()
.info()
.matches(logLines ->
logLines.stream().anyMatch(logLine -> logLine.contains("org.openrewrite.java.format.AutoFormat"))
)
.matches(logLines ->
logLines.stream().anyMatch(logLine -> logLine.contains("org.openrewrite.java.SpringFormat"))
);

assertThat(result).out().warn().isEmpty();
}

@MavenTest
void rewrite_discover_verbose_output(MavenExecutionResult result) {
void rewrite_discover_rewrite_yml(MavenExecutionResult result) {
assertThat(result)
.isSuccessful()
.out()
.info()
.matches(logLines ->
logLines.stream().anyMatch(logLine -> logLine.contains("Descriptors"))
logLines.stream().anyMatch(logLine -> logLine.contains("com.example.RewriteDiscoverIT.CodeCleanup"))
);

assertThat(result).out().warn().isEmpty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
<modelVersion>4.0.0</modelVersion>

<groupId>org.openrewrite.maven</groupId>
<artifactId>rewrite_discover_default_output</artifactId>
<artifactId>rewrite_discover_detail</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>RewriteDiscoverIT#rewrite_discover_default_output</name>
<name>RewriteDiscoverIT#rewrite_discover_detail</name>

<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@
<modelVersion>4.0.0</modelVersion>

<groupId>org.openrewrite.maven</groupId>
<artifactId>rewrite_discover_verbose_output</artifactId>
<artifactId>rewrite_discover_recursion</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>RewriteDiscoverIT#rewrite_discover_verbose_output</name>

<properties>
<rewrite.discover.verbose>true</rewrite.discover.verbose>
</properties>
<name>RewriteDiscoverIT#rewrite_discover_recursion</name>

<build>
<plugins>
Expand Down
Loading

0 comments on commit 188eb90

Please sign in to comment.