From 3f2f5c80703f6f83ce4ff6c67dda051e10afd318 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Sat, 3 Aug 2024 19:12:14 +0200 Subject: [PATCH] Drop interactivity from `rewrite:discover` Likely unused and somewhat problematic in the gradle plugin; removed here for parity & simplicity. --- pom.xml | 55 +------- .../maven/RewriteDiscoverMojo.java | 18 --- .../maven/ui/RecipeDescriptorTree.java | 104 ---------------- .../ui/RecipeDescriptorTreePrompter.java | 117 ------------------ .../openrewrite/maven/ui/package-info.java | 16 --- 5 files changed, 4 insertions(+), 306 deletions(-) delete mode 100644 src/main/java/org/openrewrite/maven/ui/RecipeDescriptorTree.java delete mode 100644 src/main/java/org/openrewrite/maven/ui/RecipeDescriptorTreePrompter.java delete mode 100644 src/main/java/org/openrewrite/maven/ui/package-info.java diff --git a/pom.xml b/pom.xml index ecd56f2d..76ab6d56 100644 --- a/pom.xml +++ b/pom.xml @@ -158,6 +158,10 @@ org.openrewrite rewrite-java-17 + + org.openrewrite + rewrite-java-21 + org.openrewrite rewrite-xml @@ -234,43 +238,11 @@ io.rsocket rsocket-core - - - org.apache.commons - commons-text - 1.12.0 - org.rocksdb rocksdbjni ${rocksdbjni.version} - - org.codehaus.plexus - plexus-interactivity-api - 1.3 - - - - org.codehaus.plexus - plexus-component-api - - - org.codehaus.plexus - plexus-container-default - - - junit - junit - - - - - - org.apache.maven.shared - maven-filtering - 3.3.2 - @@ -346,25 +318,6 @@ ${itf-maven.version} test - - - org.apache.maven.plugin-testing - maven-plugin-testing-harness - 3.3.0 - test - - - org.codehaus.plexus - plexus-container-default - - - - - org.apache.maven.plugins - maven-resources-plugin - 3.3.1 - test - diff --git a/src/main/java/org/openrewrite/maven/RewriteDiscoverMojo.java b/src/main/java/org/openrewrite/maven/RewriteDiscoverMojo.java index 7d59d825..31800281 100644 --- a/src/main/java/org/openrewrite/maven/RewriteDiscoverMojo.java +++ b/src/main/java/org/openrewrite/maven/RewriteDiscoverMojo.java @@ -16,16 +16,13 @@ package org.openrewrite.maven; import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugins.annotations.Component; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; -import org.codehaus.plexus.components.interactivity.Prompter; import org.openrewrite.config.Environment; import org.openrewrite.config.OptionDescriptor; import org.openrewrite.config.RecipeDescriptor; import org.openrewrite.internal.StringUtils; import org.openrewrite.internal.lang.Nullable; -import org.openrewrite.maven.ui.RecipeDescriptorTreePrompter; import org.openrewrite.style.NamedStyles; import java.util.*; @@ -60,16 +57,6 @@ public class RewriteDiscoverMojo extends AbstractRewriteMojo { @Parameter(property = "recursion", defaultValue = "0") int recursion; - /** - * Whether to enter an interactive shell to explore available recipes. For example:
- * {@code ./mvnw rewrite:discover -Dinteractive} - */ - @Parameter(property = "interactive", defaultValue = "false") - boolean interactive; - - @SuppressWarnings("NotNullFieldNotInitialized") - @Component - private Prompter prompter; @Override public void execute() throws MojoExecutionException { @@ -78,11 +65,6 @@ public void execute() throws MojoExecutionException { if (recipe != null) { RecipeDescriptor rd = getRecipeDescriptor(recipe, availableRecipeDescriptors); writeRecipeDescriptor(rd, detail, 0, 0); - } else if (interactive) { - getLog().info("Entering interactive mode, Ctrl-C to exit..."); - RecipeDescriptorTreePrompter treePrompter = new RecipeDescriptorTreePrompter(prompter); - RecipeDescriptor rd = treePrompter.execute(availableRecipeDescriptors); - writeRecipeDescriptor(rd, true, 0, 0); } else { Collection activeRecipeDescriptors = new HashSet<>(); for (String activeRecipe : getActiveRecipes()) { diff --git a/src/main/java/org/openrewrite/maven/ui/RecipeDescriptorTree.java b/src/main/java/org/openrewrite/maven/ui/RecipeDescriptorTree.java deleted file mode 100644 index 091bd410..00000000 --- a/src/main/java/org/openrewrite/maven/ui/RecipeDescriptorTree.java +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Copyright 2021 the original author or authors. - * - * 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 - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * 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.maven.ui; - -import org.openrewrite.config.RecipeDescriptor; - -import java.util.ArrayList; -import java.util.List; -import java.util.regex.Pattern; - -/** - * Used for traversing around the collection of available {@linkplain RecipeDescriptor}s in the environment. - *

- * It's not the most "proper" tree in the world, since these hold both data for a "directory" of recipes as well - * as RecipeDescriptors themselves. But it's not the end of the world. - */ -public class RecipeDescriptorTree implements Comparable { - private static final Pattern PATH_SEPARATOR = Pattern.compile("\\."); - - private String displayName; - private RecipeDescriptorTree parent; - private final List children = new ArrayList<>(); - private RecipeDescriptor recipeDescriptor; - - public RecipeDescriptorTree() { - this(null); - } - - public RecipeDescriptorTree(String displayName) { - this.displayName = displayName; - this.parent = null; - } - - public String getDisplayName() { - return this.displayName; - } - - public RecipeDescriptorTree addChild(RecipeDescriptorTree child) { - child.parent = this; - this.children.add(child); - return child; - } - - public RecipeDescriptorTree addChild(String displayName) { - return addChild(new RecipeDescriptorTree(displayName)); - } - - public List getChildren() { - return this.children; - } - - public RecipeDescriptorTree getParent() { - return this.parent; - } - - public RecipeDescriptor getRecipeDescriptor() { - return this.recipeDescriptor; - } - - public void setRecipeDescriptor(RecipeDescriptor recipeDescriptor) { - this.recipeDescriptor = recipeDescriptor; - this.displayName = recipeDescriptor.getDisplayName(); - } - - public void addPath(RecipeDescriptor path) { - String[] names = PATH_SEPARATOR.split(path.getName()); - RecipeDescriptorTree tree = this; - for (String name : names) { - if (tree.children.stream().anyMatch(r -> r.displayName.equalsIgnoreCase(name))) { - tree = tree.children.stream().filter(r -> r.displayName.equalsIgnoreCase(name)).findFirst().get(); - } else { - tree = tree.addChild(name); - } - } - - // we've traversed to a leaf node in the tree, which means we can tack on the - // RecipeDescriptor to this node and know it's a leaf node instead of a directory placeholder. - tree.setRecipeDescriptor(path); - } - - public void addPath(Iterable path) { - for (RecipeDescriptor rd : path) { - addPath(rd); - } - } - - @Override - public int compareTo(RecipeDescriptorTree o) { - return this.children.size() - o.children.size(); - } -} diff --git a/src/main/java/org/openrewrite/maven/ui/RecipeDescriptorTreePrompter.java b/src/main/java/org/openrewrite/maven/ui/RecipeDescriptorTreePrompter.java deleted file mode 100644 index 951c0222..00000000 --- a/src/main/java/org/openrewrite/maven/ui/RecipeDescriptorTreePrompter.java +++ /dev/null @@ -1,117 +0,0 @@ -/* - * Copyright 2021 the original author or authors. - * - * 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 - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * 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.maven.ui; - -import org.apache.maven.plugin.MojoExecutionException; -import org.codehaus.plexus.components.interactivity.Prompter; -import org.codehaus.plexus.components.interactivity.PrompterException; -import org.openrewrite.config.RecipeDescriptor; - -import java.util.Collection; -import java.util.SortedMap; -import java.util.TreeMap; -import java.util.stream.Collectors; - -/** - * Interactive shell to select and explore available options in the environment. - */ -public class RecipeDescriptorTreePrompter { - private static final String INDEX_TO_ANSWER_MAPPING_MSG = "[%s]: %s\n"; - private static final String AVAILABLE_OPTIONS_MSG = "Available options:\n"; - private static final String CHOOSE_NUMBER_PROMPT = "Choose a number (hint: enter to return to initial list)"; - private static final String INPUT_SELECTION_MUST_BE_NUMBER_PROMPT = "\nYour input selection must be a number, try again (hint: enter to return to initial list)"; - private static final String YOUR_SELECTION_NOT_IN_OPTIONS_PROMPT = "\nYour selection [%s] is not an option in the list, try again"; - private static final String HIGHEST_ROOT_NODE_PROMPT = "\nNo parent directory higher than this, try again"; - - private final Prompter prompter; - - public RecipeDescriptorTreePrompter(Prompter prompter) { - this.prompter = prompter; - } - - private RecipeDescriptorTree select(RecipeDescriptorTree tree) throws MojoExecutionException { - SortedMap answerSet = new TreeMap<>(); - RecipeDescriptorTree selection = null; - - do { - // build out set of selection choices - StringBuilder query = new StringBuilder(AVAILABLE_OPTIONS_MSG); - int counter = 0; - - for (RecipeDescriptorTree option : tree.getChildren().stream().sorted().collect(Collectors.toList())) { - counter++; - String answer = String.valueOf(counter); - answerSet.put(answer, option); - query.append(String.format(INDEX_TO_ANSWER_MAPPING_MSG, answer, option.getDisplayName())); - } - - String prompted; - query.append(CHOOSE_NUMBER_PROMPT); - do { - prompted = prompt(query.toString()); - if (prompted.isEmpty()) { - if (tree.getParent() != null) { - // navigate back up a layer - return select(tree.getParent()); - } else { - query.append(HIGHEST_ROOT_NODE_PROMPT); - } - } else if (!isNumber(prompted)) { - // is your input actually a number? - query.append(INPUT_SELECTION_MUST_BE_NUMBER_PROMPT); - } else if (answerSet.get(prompted) == null) { - // is your number input found in the set of options? - query.append(String.format(YOUR_SELECTION_NOT_IN_OPTIONS_PROMPT, prompted)); - } - } while (answerSet.get(prompted) == null); - selection = answerSet.get(prompted); - } while (selection == null); - - if (!selection.getChildren().isEmpty()) { - // if the selected tree has children, recurse - return select(selection); - } - // the selection tree has no children, thus is a leaf, thus our final stop on the recursion train - return selection; - } - - private static boolean isNumber(String message) { - try { - Integer.parseInt(message); - } catch (NumberFormatException e) { - return false; - } - return true; - } - - private String prompt(String message) throws MojoExecutionException { - String prompted; - try { - prompted = prompter.prompt(message); - } catch (PrompterException e) { - throw new MojoExecutionException(e.getMessage(), e); - } - return prompted; - } - - public RecipeDescriptor execute(Collection recipeDescriptors) throws MojoExecutionException { - RecipeDescriptorTree tree = new RecipeDescriptorTree(); - tree.addPath(recipeDescriptors); - RecipeDescriptorTree selection = select(tree); - return selection.getRecipeDescriptor(); - } - -} diff --git a/src/main/java/org/openrewrite/maven/ui/package-info.java b/src/main/java/org/openrewrite/maven/ui/package-info.java deleted file mode 100644 index 12910820..00000000 --- a/src/main/java/org/openrewrite/maven/ui/package-info.java +++ /dev/null @@ -1,16 +0,0 @@ -/* - * Copyright 2021 the original author or authors. - * - * 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 - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * 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.maven.ui;