Skip to content

Commit 5be3c16

Browse files
authored
Apply recipes by number (#818)
1 parent 9cfa11a commit 5be3c16

File tree

6 files changed

+64
-18
lines changed

6 files changed

+64
-18
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2021 - 2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.sbm.shell;
17+
18+
import org.springframework.sbm.engine.recipe.Recipe;
19+
import org.springframework.stereotype.Component;
20+
21+
import java.util.ArrayList;
22+
import java.util.List;
23+
24+
/**
25+
* @author Fabian Krüger
26+
*/
27+
@Component
28+
public class ApplicableRecipesListHolder {
29+
private List<Recipe> recipes = new ArrayList<>();
30+
31+
public void clear() {
32+
recipes.clear();
33+
}
34+
35+
public void setRecipes(List<Recipe> recipes) {
36+
this.recipes = recipes;
37+
}
38+
39+
public Recipe getRecipeByIndex(int index) {
40+
return recipes.get(index);
41+
}
42+
}

applications/spring-shell/src/main/java/org/springframework/sbm/shell/ApplyShellCommand.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,17 @@
1616
*/
1717
package org.springframework.sbm.shell;
1818

19+
import lombok.RequiredArgsConstructor;
20+
import org.jetbrains.annotations.NotNull;
21+
import org.jline.utils.AttributedString;
22+
import org.jline.utils.AttributedStringBuilder;
23+
import org.jline.utils.AttributedStyle;
1924
import org.springframework.sbm.engine.commands.ApplicableRecipeListCommand;
2025
import org.springframework.sbm.engine.commands.ApplyCommand;
2126
import org.springframework.sbm.engine.context.ProjectContext;
2227
import org.springframework.sbm.engine.context.ProjectContextHolder;
2328
import org.springframework.sbm.engine.recipe.Action;
2429
import org.springframework.sbm.engine.recipe.Recipe;
25-
import lombok.RequiredArgsConstructor;
26-
import org.jetbrains.annotations.NotNull;
27-
import org.jline.utils.AttributedString;
28-
import org.jline.utils.AttributedStringBuilder;
29-
import org.jline.utils.AttributedStyle;
30-
import org.jline.utils.Colors;
3130
import org.springframework.shell.Availability;
3231
import org.springframework.shell.CompletionContext;
3332
import org.springframework.shell.CompletionProposal;
@@ -49,11 +48,15 @@ public class ApplyShellCommand {
4948
private final ApplicableRecipeListRenderer applicableRecipeListRenderer;
5049
private final ProjectContextHolder projectContextHolder;
5150
private final ApplyCommandRenderer applyCommandRenderer;
51+
private final ApplicableRecipesListHolder applicableRecipeListHolder;
5252

5353
@ShellMethod(key = {"apply", "a"}, value = "Apply a given recipe to the target application.")
5454
@ShellMethodAvailability("availabilityCheck")
5555
public AttributedString apply(@ShellOption(arity = 1, valueProvider = ApplyRecipeValueProvider.class,
56-
help = "The name of the recipe to apply.") String recipeName) {
56+
help = "The number of the recipe to apply.") String recipeIndex) {
57+
int realIndex = Integer.parseInt(recipeIndex) - 1;
58+
Recipe recipe = applicableRecipeListHolder.getRecipeByIndex(realIndex);
59+
String recipeName = recipe.getName();
5760
AttributedStringBuilder header = buildHeader(recipeName);
5861
System.out.println(header.toAnsi());
5962

applications/spring-shell/src/main/java/org/springframework/sbm/shell/RecipeRenderer.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,18 @@ public AttributedString renderRecipesList(String noRecipesTitle, String title, L
3939
AttributedString titleString = renderTitle(title);
4040
builder.append(titleString);
4141

42-
foundRecipes.forEach(recipe -> this.buildRecipePresentation(builder, recipe));
42+
foundRecipes.forEach(recipe -> this.buildRecipePresentation(foundRecipes.indexOf(recipe), builder, recipe));
4343

4444
builder.append("\n");
45-
builder.append("Run command '> apply recipe-name' to apply a recipe.");
45+
builder.append("Run command '> apply <recipe-number>' to apply a recipe.");
4646
builder.append("\n");
4747
}
4848
return builder.toAttributedString();
4949
}
5050

51-
public AttributedStringBuilder buildRecipePresentation(AttributedStringBuilder builder, Recipe recipe) {
52-
builder.style(AttributedStyle.DEFAULT);
53-
builder.append(" - ");
54-
builder.style(AttributedStyle.DEFAULT.italicDefault().boldDefault().foreground(Colors.rgbColor("yellow")));
51+
public AttributedStringBuilder buildRecipePresentation(int index, AttributedStringBuilder builder, Recipe recipe) {
52+
builder.style(AttributedStyle.BOLD);//.foreground(Colors.rgbColor("yellow")));
53+
builder.append(" ").append(Integer.toString(index + 1)).append(") ");
5554
builder.append(recipe.getName());
5655
builder.style(AttributedStyle.DEFAULT);
5756
builder.append("\n -> ").append(recipe.getDescription());

applications/spring-shell/src/main/java/org/springframework/sbm/shell/ScanShellCommand.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public class ScanShellCommand {
4242
private final PreconditionVerificationRenderer preconditionVerificationRenderer;
4343
private final ScanCommandHeaderRenderer scanCommandHeaderRenderer;
4444
private final ConsolePrinter consolePrinter;
45+
private final ApplicableRecipesListHolder applicableRecipesListHolder;
4546

4647
@ShellMethod(key = {"scan", "s"},
4748
value = "Scans the target project directory and get the list of applicable recipes.")
@@ -50,6 +51,8 @@ public String scan(
5051
help = "The root directory of the target application.")
5152
String projectRoot) {
5253

54+
applicableRecipesListHolder.clear();
55+
5356
List<Resource> resources = scanCommand.scanProjectRoot(projectRoot);
5457
String scanCommandHeader = scanCommandHeaderRenderer.renderHeader(projectRoot);
5558
PreconditionVerificationResult result = scanCommand.checkPreconditions(projectRoot, resources);
@@ -67,6 +70,7 @@ public String scan(
6770
ProjectContext projectContext = scanCommand.execute(projectRoot);
6871
contextHolder.setProjectContext(projectContext);
6972
List<Recipe> recipes = applicableRecipeListCommand.execute(projectContext);
73+
applicableRecipesListHolder.setRecipes(recipes);
7074
AttributedString recipeList = applicableRecipeListRenderer.render(recipes);
7175
stringBuilder.append(recipeList);
7276
}

applications/spring-shell/src/test/java/org/springframework/sbm/shell/RecipeRendererTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.sbm.shell;
1717

18+
import org.springframework.sbm.engine.recipe.Recipe;
1819
import org.jline.utils.AttributedString;
1920
import org.jline.utils.AttributedStringBuilder;
2021
import org.jline.utils.AttributedStyle;
@@ -75,8 +76,8 @@ void shouldRenderListOfRecipesWhenNotEmpty() {
7576

7677
AttributedStringBuilder builder = new AttributedStringBuilder();
7778

78-
sut.buildRecipePresentation(builder, recipe1);
79-
sut.buildRecipePresentation(builder, recipe2);
79+
sut.buildRecipePresentation(1, builder, recipe1);
80+
sut.buildRecipePresentation(2, builder, recipe2);
8081

8182

8283
AttributedString attributedString = sut.renderRecipesList("", title, List.of(recipe1, recipe2));

components/sbm-core/src/main/java/org/springframework/sbm/engine/commands/ApplyCommand.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@
1515
*/
1616
package org.springframework.sbm.engine.commands;
1717

18-
import org.openrewrite.ExecutionContext;
19-
import org.springframework.beans.factory.annotation.Qualifier;
2018
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
21-
import org.springframework.context.annotation.Scope;
2219
import org.springframework.sbm.common.filter.DeletedResourcePathStringFilter;
2320
import org.springframework.sbm.common.filter.ModifiedResourcePathStringFilter;
2421
import org.springframework.sbm.engine.context.ProjectContext;

0 commit comments

Comments
 (0)