Skip to content

Commit 06c8a9e

Browse files
authored
feat: Apply recipes by number (#819)
- Downgrade spring-shell to 2.x - Recipes can be applied by name or number of recipe
1 parent 5be3c16 commit 06c8a9e

File tree

6 files changed

+31
-19
lines changed

6 files changed

+31
-19
lines changed

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.jline.utils.AttributedString;
2222
import org.jline.utils.AttributedStringBuilder;
2323
import org.jline.utils.AttributedStyle;
24+
import org.jline.utils.Colors;
2425
import org.springframework.sbm.engine.commands.ApplicableRecipeListCommand;
2526
import org.springframework.sbm.engine.commands.ApplyCommand;
2627
import org.springframework.sbm.engine.context.ProjectContext;
@@ -30,15 +31,13 @@
3031
import org.springframework.shell.Availability;
3132
import org.springframework.shell.CompletionContext;
3233
import org.springframework.shell.CompletionProposal;
33-
import org.springframework.shell.standard.ShellComponent;
34-
import org.springframework.shell.standard.ShellMethod;
35-
import org.springframework.shell.standard.ShellMethodAvailability;
36-
import org.springframework.shell.standard.ShellOption;
37-
import org.springframework.shell.standard.ValueProvider;
34+
import org.springframework.shell.standard.*;
3835
import org.springframework.stereotype.Component;
3936

4037
import java.util.List;
4138

39+
import static java.lang.Integer.parseUnsignedInt;
40+
4241
@ShellComponent
4342
@RequiredArgsConstructor
4443
public class ApplyShellCommand {
@@ -53,10 +52,17 @@ public class ApplyShellCommand {
5352
@ShellMethod(key = {"apply", "a"}, value = "Apply a given recipe to the target application.")
5453
@ShellMethodAvailability("availabilityCheck")
5554
public AttributedString apply(@ShellOption(arity = 1, valueProvider = ApplyRecipeValueProvider.class,
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();
55+
help = "The number of the recipe to apply.") String recipe) {
56+
// allow passing in number of position or name of recipe (meh)
57+
String recipeName = recipe;
58+
try {
59+
int recipeIndex = parseUnsignedInt(recipe);
60+
int realIndex = recipeIndex - 1;
61+
Recipe matchingRecipe = applicableRecipeListHolder.getRecipeByIndex(realIndex);
62+
recipeName = matchingRecipe.getName();
63+
} catch (NumberFormatException nfe) {
64+
65+
}
6066
AttributedStringBuilder header = buildHeader(recipeName);
6167
System.out.println(header.toAnsi());
6268

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import org.jline.utils.AttributedString;
1919
import org.jline.utils.AttributedStringBuilder;
2020
import org.jline.utils.AttributedStyle;
21-
import org.jline.utils.Colors;
2221
import org.springframework.sbm.engine.recipe.Recipe;
2322
import org.springframework.stereotype.Component;
2423

@@ -49,7 +48,7 @@ public AttributedString renderRecipesList(String noRecipesTitle, String title, L
4948
}
5049

5150
public AttributedStringBuilder buildRecipePresentation(int index, AttributedStringBuilder builder, Recipe recipe) {
52-
builder.style(AttributedStyle.BOLD);//.foreground(Colors.rgbColor("yellow")));
51+
builder.style(AttributedStyle.BOLD);
5352
builder.append(" ").append(Integer.toString(index + 1)).append(") ");
5453
builder.append(recipe.getName());
5554
builder.style(AttributedStyle.DEFAULT);

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import org.jline.utils.AttributedString;
2020
import org.jline.utils.AttributedStringBuilder;
2121
import org.jline.utils.AttributedStyle;
22-
import org.jline.utils.Colors;
2322
import org.junit.jupiter.api.Test;
2423
import org.junit.jupiter.api.extension.ExtendWith;
2524
import org.mockito.junit.jupiter.MockitoExtension;
@@ -47,16 +46,15 @@ void shouldRenderRecipe() {
4746
when(recipe.getDescription()).thenReturn(recipeDescription);
4847

4948
AttributedStringBuilder builder = new AttributedStringBuilder();
50-
builder.style(AttributedStyle.DEFAULT);
51-
builder.append(" - ");
52-
builder.style(AttributedStyle.DEFAULT.boldDefault().foreground(Colors.rgbColor("yellow")));
49+
builder.style(AttributedStyle.BOLD);
50+
builder.append(" 2) ");
5351
builder.append(recipe.getName());
5452
builder.style(AttributedStyle.DEFAULT);
5553
builder.append("\n -> " + recipe.getDescription());
5654
builder.append("\n");
5755

58-
AttributedStringBuilder builder2 = new AttributedStringBuilder();
59-
AttributedString attributedString = sut.buildRecipePresentation(builder2, recipe).toAttributedString();
56+
AttributedString attributedString = sut.buildRecipePresentation(1, new AttributedStringBuilder(), recipe).toAttributedString();
57+
6058
AttributedString expectedAttributedString = builder.toAttributedString();
6159
assertThat(attributedString)
6260
.as(attributedString + " " + expectedAttributedString)

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ class ScanShellCommandTest {
6161
@Mock
6262
private ConsolePrinter consolePrinter;
6363

64+
@Mock
65+
ApplicableRecipesListHolder applicableRecipesListHolder;
66+
6467
@InjectMocks
6568
ScanShellCommand sut;
6669

@@ -86,7 +89,11 @@ void testScanWithFailingPreconditionChecksShouldPrintResult() {
8689

8790
String result = sut.scan(projectRoot);
8891

92+
verify(applicableRecipesListHolder).clear();
93+
verify(applicableRecipesListHolder, never()).setRecipes(any());
94+
8995
ArgumentCaptor<String> capturedOutput = ArgumentCaptor.forClass(String.class);
96+
9097
verify(consolePrinter).println(capturedOutput.capture());
9198

9299
// header and validation result rendered
@@ -126,6 +133,8 @@ void testScanWithSucceedingPreconditionChecksShouldPrintResultAndRenderApplicabl
126133

127134
String result = sut.scan(projectRoot);
128135

136+
verify(applicableRecipesListHolder).clear();
137+
verify(applicableRecipesListHolder).setRecipes(recipes);
129138
// list of recipes returned
130139
assertThat(result).isEqualTo("\u001B[91mThe applicable recipe\u001B[0m");
131140
// header and validation result rendered

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ void beforeEach() {
5959
recipes = List.of();
6060
projectContext = mock(ProjectContext.class);
6161

62-
sut = new ScanShellCommand(scanCommand, applicationRecipeListRenderer, applicableRecipeListCommand, contextHolder, preconditionVerificationRenderer, scanCommandHeaderRenderer, consolePrinter);
62+
sut = new ScanShellCommand(scanCommand, applicationRecipeListRenderer, applicableRecipeListCommand, contextHolder, preconditionVerificationRenderer, scanCommandHeaderRenderer, consolePrinter, new ApplicableRecipesListHolder());
6363
}
6464

6565
private void recordMocks(String projectRoot, ScanCommand scanCommand, ApplicableRecipeListRenderer applicationRecipeListRenderer, ApplicableRecipeListCommand applicableRecipeListCommand, List<Resource> resources, List<Recipe> recipes, ProjectContext projectContext) {

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<maven.compiler.source>17</maven.compiler.source>
1717
<maven-compiler-plugin.version>3.10.1</maven-compiler-plugin.version>
1818
<java.version>17</java.version>
19-
<spring-shell.version>3.1.1</spring-shell.version>
19+
<spring-shell.version>2.1.10</spring-shell.version>
2020
<openrewrite.version>7.35.0</openrewrite.version>
2121
<openrewrite.spring.version>4.32.0</openrewrite.spring.version>
2222
<rewrite-migrate-java.version>1.17.0</rewrite-migrate-java.version>

0 commit comments

Comments
 (0)