|
15 | 15 | */
|
16 | 16 | package org.springframework.sbm.shell;
|
17 | 17 |
|
| 18 | +import lombok.RequiredArgsConstructor; |
| 19 | +import org.jline.utils.AttributedString; |
| 20 | +import org.jline.utils.AttributedStringBuilder; |
| 21 | +import org.springframework.core.io.Resource; |
18 | 22 | import org.springframework.sbm.engine.commands.ApplicableRecipeListCommand;
|
19 | 23 | import org.springframework.sbm.engine.commands.ScanCommand;
|
20 | 24 | import org.springframework.sbm.engine.context.ProjectContext;
|
21 | 25 | import org.springframework.sbm.engine.context.ProjectContextHolder;
|
| 26 | +import org.springframework.sbm.engine.precondition.PreconditionVerificationResult; |
22 | 27 | import org.springframework.sbm.engine.recipe.Recipe;
|
23 |
| -import lombok.RequiredArgsConstructor; |
24 |
| -import org.jetbrains.annotations.NotNull; |
25 |
| -import org.jline.utils.AttributedString; |
26 |
| -import org.jline.utils.AttributedStringBuilder; |
27 |
| -import org.jline.utils.AttributedStyle; |
28 |
| -import org.jline.utils.Colors; |
29 | 28 | import org.springframework.shell.standard.ShellComponent;
|
30 | 29 | import org.springframework.shell.standard.ShellMethod;
|
31 | 30 | import org.springframework.shell.standard.ShellOption;
|
|
36 | 35 | @RequiredArgsConstructor
|
37 | 36 | public class ScanShellCommand {
|
38 | 37 |
|
39 |
| - private final ScanCommand scanCommand; |
40 |
| - private final ApplicableRecipeListRenderer applicableRecipeListRenderer; |
41 |
| - private final ApplicableRecipeListCommand applicableRecipeListCommand; |
42 |
| - private final ProjectContextHolder contextHolder; |
| 38 | + private final ScanCommand scanCommand; |
| 39 | + |
| 40 | + private final ApplicableRecipeListRenderer applicableRecipeListRenderer; |
| 41 | + |
| 42 | + private final ApplicableRecipeListCommand applicableRecipeListCommand; |
| 43 | + |
| 44 | + private final ProjectContextHolder contextHolder; |
| 45 | + private final PreconditionVerificationRenderer preconditionVerificationRenderer; |
| 46 | + private final ScanCommandHeaderRenderer scanCommandHeaderRenderer; |
| 47 | + private final ConsolePrinter consolePrinter; |
| 48 | + |
| 49 | + @ShellMethod(key = { "scan", "s" }, |
| 50 | + value = "Scans the target project directory and get the list of applicable recipes.") |
| 51 | + public AttributedString scan(@ShellOption(defaultValue = ".", |
| 52 | + help = "The root directory of the target application.") String projectRoot) { |
| 53 | + |
43 | 54 |
|
44 |
| - @ShellMethod(key = {"scan", "s"}, value = "Scans the target project directory and get the list of applicable recipes.") |
45 |
| - public AttributedString scan( |
46 |
| - @ShellOption( |
47 |
| - defaultValue = ".", |
48 |
| - help = "The root directory of the target application." |
49 |
| - ) String projectRoot) { |
| 55 | + List<Resource> resources = scanCommand.scanProjectRoot(projectRoot); |
| 56 | + String scanCommandHeader = scanCommandHeaderRenderer.renderHeader(projectRoot); |
| 57 | + PreconditionVerificationResult result = scanCommand.checkPreconditions(projectRoot, resources); |
| 58 | + String renderedPreconditionCheckResults = preconditionVerificationRenderer.renderPreconditionCheckResults(result); |
| 59 | + AttributedStringBuilder stringBuilder = new AttributedStringBuilder(); |
| 60 | + String output = stringBuilder |
| 61 | + .append(scanCommandHeader) |
| 62 | + .append(renderedPreconditionCheckResults) |
| 63 | + .toAnsi(); |
50 | 64 |
|
51 |
| - AttributedStringBuilder header = buildHeader(projectRoot); |
52 |
| - System.out.println(header.toAnsi()); |
| 65 | + consolePrinter.println(output); |
53 | 66 |
|
54 |
| - ProjectContext projectContext = scanCommand.execute(projectRoot); |
55 |
| - contextHolder.setProjectContext(projectContext); |
| 67 | + stringBuilder = new AttributedStringBuilder(); |
| 68 | + if ( ! result.hasError()) { |
| 69 | + ProjectContext projectContext = scanCommand.execute(projectRoot); |
| 70 | + contextHolder.setProjectContext(projectContext); |
| 71 | + List<Recipe> recipes = applicableRecipeListCommand.execute(projectContext); |
| 72 | + AttributedString recipeList = applicableRecipeListRenderer.render(recipes); |
| 73 | + stringBuilder.append(recipeList); |
| 74 | + } |
56 | 75 |
|
57 |
| - List<Recipe> recipes = applicableRecipeListCommand.execute(projectContext); |
58 |
| - return applicableRecipeListRenderer.render(recipes); |
59 |
| - } |
| 76 | + return stringBuilder.toAttributedString(); |
| 77 | + } |
60 | 78 |
|
61 |
| - @NotNull |
62 |
| - private AttributedStringBuilder buildHeader(String projectRoot) { |
63 |
| - AttributedStringBuilder builder = new AttributedStringBuilder(); |
64 |
| - builder.append("\n"); |
65 |
| - builder.style(AttributedStyle.DEFAULT.italicDefault().boldDefault().foreground(Colors.rgbColor("green"))); |
66 |
| - builder.append("scanning '" + projectRoot + "'"); |
67 |
| - return builder; |
68 |
| - } |
69 | 79 | }
|
0 commit comments