|
15 | 15 | */
|
16 | 16 | package org.openrewrite.config;
|
17 | 17 |
|
| 18 | +import org.apache.commons.lang3.StringUtils; |
18 | 19 | import org.openrewrite.Contributor;
|
19 | 20 | import org.openrewrite.Recipe;
|
20 | 21 | import org.openrewrite.RecipeException;
|
|
28 | 29 | import java.util.*;
|
29 | 30 |
|
30 | 31 | import static java.util.Collections.emptyList;
|
| 32 | +import static java.util.Comparator.comparingInt; |
| 33 | +import static java.util.function.Function.identity; |
31 | 34 | import static java.util.stream.Collectors.toList;
|
| 35 | +import static java.util.stream.Collectors.toMap; |
32 | 36 |
|
33 | 37 | public class Environment {
|
34 | 38 | private final Collection<? extends ResourceLoader> resourceLoaders;
|
@@ -134,24 +138,27 @@ public Collection<RecipeDescriptor> listRecipeDescriptors() {
|
134 | 138 | }
|
135 | 139 |
|
136 | 140 | public Recipe activateRecipes(Iterable<String> activeRecipes) {
|
137 |
| - List<Recipe> allRecipes = listRecipes(); |
| 141 | + Map<String, Recipe> recipesByName = listRecipes().stream().collect(toMap(Recipe::getName, identity())); |
138 | 142 | List<String> recipesNotFound = new ArrayList<>();
|
139 | 143 | List<Recipe> activatedRecipes = new ArrayList<>();
|
140 | 144 | for (String activeRecipe : activeRecipes) {
|
141 |
| - boolean foundRecipe = false; |
142 |
| - for (Recipe recipe : allRecipes) { |
143 |
| - if (activeRecipe.equals(recipe.getName())) { |
144 |
| - activatedRecipes.add(recipe); |
145 |
| - foundRecipe = true; |
146 |
| - break; |
147 |
| - } |
148 |
| - } |
149 |
| - if (!foundRecipe) { |
| 145 | + Recipe recipe = recipesByName.get(activeRecipe); |
| 146 | + if (recipe == null) { |
150 | 147 | recipesNotFound.add(activeRecipe);
|
| 148 | + } else { |
| 149 | + activatedRecipes.add(recipe); |
151 | 150 | }
|
152 | 151 | }
|
153 | 152 | if (!recipesNotFound.isEmpty()) {
|
154 |
| - throw new RecipeException("Recipes not found: " + String.join(", ", recipesNotFound)); |
| 153 | + List<String> suggestions = recipesNotFound.stream() |
| 154 | + .map(r -> recipesByName.keySet().stream() |
| 155 | + .min(comparingInt(a -> StringUtils.getLevenshteinDistance(a, r))) |
| 156 | + .orElse(r)) |
| 157 | + .collect(toList()); |
| 158 | + String message = String.format("Recipes not found: %s\nDid you mean: %s", |
| 159 | + String.join(", ", recipesNotFound), |
| 160 | + String.join(", ", suggestions)); |
| 161 | + throw new RecipeException(message); |
155 | 162 | }
|
156 | 163 | return new CompositeRecipe(activatedRecipes);
|
157 | 164 | }
|
|
0 commit comments