Skip to content

Commit 991c806

Browse files
authored
closes #15 add precondition check when scanning (#37)
1 parent 8203e16 commit 991c806

File tree

174 files changed

+2029
-1573
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

174 files changed

+2029
-1573
lines changed

.github/workflows/check-license-headers.yml

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
name: Check License Lines
22
on:
3-
pull_request:
4-
branches:
5-
- main
3+
push:
4+
branches: '**'
5+
# pull_request:
6+
# branches:
7+
# - main
68
jobs:
79
check-license-lines:
810
runs-on: ubuntu-latest

.github/workflows/mvn-build.yml

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
name: Java CI
22
on:
3-
pull_request:
4-
branches:
5-
- main
3+
push:
4+
branches: '**'
5+
# pull_request:
6+
# branches:
7+
# - main
68
jobs:
79
build:
810
runs-on: ubuntu-latest

components/sbm-core/src/main/java/org/springframework/sbm/project/parser/Resource.java renamed to applications/spring-shell/src/main/java/org/springframework/sbm/shell/ConsolePrinter.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.springframework.sbm.project.parser;
16+
package org.springframework.sbm.shell;
1717

18-
import java.io.InputStream;
19-
import java.nio.file.Path;
18+
import org.springframework.stereotype.Component;
2019

21-
public interface Resource {
22-
Path getPath();
23-
24-
InputStream getContent();
20+
@Component
21+
public class ConsolePrinter {
22+
public void println(String text) {
23+
System.out.println(text);
24+
}
2525
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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.jline.utils.AttributedStringBuilder;
19+
import org.jline.utils.AttributedStyle;
20+
import org.jline.utils.Colors;
21+
import org.springframework.sbm.engine.precondition.PreconditionCheck;
22+
import org.springframework.sbm.engine.precondition.PreconditionCheckResult;
23+
import org.springframework.sbm.engine.precondition.PreconditionVerificationResult;
24+
import org.springframework.stereotype.Component;
25+
26+
@Component
27+
public class PreconditionVerificationRenderer {
28+
29+
public String renderPreconditionCheckResults(PreconditionVerificationResult result) {
30+
AttributedStringBuilder stringBuilder = new AttributedStringBuilder();
31+
stringBuilder.style(stringBuilder.style().DEFAULT.bold().foreground(Colors.rgbColor("black")));
32+
stringBuilder.append("\n\n").append(String.format("Checked preconditions for '%s'", result.getProjectRoot())).append("\n");
33+
34+
result.getResults().forEach(r -> {
35+
stringBuilder.append(renderCheckResult(r));
36+
});
37+
stringBuilder.append("\n");
38+
return stringBuilder.toAnsi();
39+
}
40+
41+
private String renderCheckResult(PreconditionCheckResult r) {
42+
AttributedStringBuilder builder = new AttributedStringBuilder();
43+
44+
// TODO: move rendering of status into central place
45+
if(r.getState().equals(PreconditionCheck.ResultState.FAILED)) {
46+
builder.style(builder.style().DEFAULT.bold().foreground(Colors.rgbColor("red")));
47+
builder.append(" [X]");
48+
builder.style(builder.style().DEFAULT);
49+
}
50+
51+
if(r.getState().equals(PreconditionCheck.ResultState.PASSED)) {
52+
builder.style(AttributedStyle.DEFAULT.bold().foreground(Colors.rgbColor("green")));
53+
builder.append("[ok]");
54+
builder.style(AttributedStyle.DEFAULT);
55+
}
56+
57+
if(r.getState().equals(PreconditionCheck.ResultState.WARN)) {
58+
builder.style(AttributedStyle.DEFAULT.bold().foreground(Colors.rgbColor("yellow")));
59+
builder.append(" [!]");
60+
builder.style(AttributedStyle.DEFAULT);
61+
}
62+
63+
builder.append(" ").append(r.getMessage());
64+
builder.append("\n");
65+
66+
return builder.toAnsi();
67+
}
68+
69+
}

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

+10-5
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
*/
1616
package org.springframework.sbm.shell;
1717

18-
import org.springframework.sbm.engine.recipe.Recipe;
19-
import org.springframework.sbm.engine.recipe.RecipeAutomation;
2018
import org.jline.utils.AttributedString;
2119
import org.jline.utils.AttributedStringBuilder;
2220
import org.jline.utils.AttributedStyle;
2321
import org.jline.utils.Colors;
22+
import org.springframework.sbm.engine.recipe.Recipe;
23+
import org.springframework.sbm.engine.recipe.RecipeAutomation;
2424
import org.springframework.stereotype.Component;
2525

2626
import java.util.List;
@@ -37,13 +37,16 @@ public AttributedString renderRecipesList(String noRecipesTitle, String title, L
3737
if (foundRecipes.isEmpty()) {
3838
builder.append(noRecipesTitle);
3939
} else {
40+
AttributedString titleString = renderTitle(title);
41+
builder.append(titleString);
4042

4143
AttributedString emojiMapping = renderEmojiMapping();
4244
builder.append(emojiMapping);
4345
foundRecipes.forEach(recipe -> this.buildRecipePresentation(builder, recipe));
44-
AttributedString titleString = renderTitle(title);
45-
builder.append(titleString);
46-
builder.append("\n\n");
46+
47+
builder.append("\n");
48+
builder.append("Run command '> apply recipe-name' to apply a recipe.");
49+
builder.append("\n");
4750
}
4851
return builder.toAttributedString();
4952
}
@@ -71,7 +74,9 @@ public AttributedStringBuilder buildRecipePresentation(AttributedStringBuilder b
7174
private AttributedString renderTitle(String title) {
7275
AttributedStringBuilder builder = new AttributedStringBuilder();
7376
builder.style(AttributedStyle.DEFAULT.bold());
77+
builder.append("\n");
7478
builder.append(title);
79+
builder.append("\n\n");
7580
return builder.toAttributedString();
7681
}
7782

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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.jline.utils.AttributedStringBuilder;
19+
import org.jline.utils.AttributedStyle;
20+
import org.jline.utils.Colors;
21+
import org.springframework.stereotype.Component;
22+
23+
@Component
24+
public class ScanCommandHeaderRenderer {
25+
public String renderHeader(String projectRoot) {
26+
AttributedStringBuilder builder = new AttributedStringBuilder();
27+
builder.append("\n");
28+
builder.style(AttributedStyle.DEFAULT.italicDefault().boldDefault().foreground(Colors.rgbColor("green")));
29+
builder.append("scanning '" + projectRoot + "'");
30+
return builder.toAnsi();
31+
}
32+
}

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

+41-31
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,16 @@
1515
*/
1616
package org.springframework.sbm.shell;
1717

18+
import lombok.RequiredArgsConstructor;
19+
import org.jline.utils.AttributedString;
20+
import org.jline.utils.AttributedStringBuilder;
21+
import org.springframework.core.io.Resource;
1822
import org.springframework.sbm.engine.commands.ApplicableRecipeListCommand;
1923
import org.springframework.sbm.engine.commands.ScanCommand;
2024
import org.springframework.sbm.engine.context.ProjectContext;
2125
import org.springframework.sbm.engine.context.ProjectContextHolder;
26+
import org.springframework.sbm.engine.precondition.PreconditionVerificationResult;
2227
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;
2928
import org.springframework.shell.standard.ShellComponent;
3029
import org.springframework.shell.standard.ShellMethod;
3130
import org.springframework.shell.standard.ShellOption;
@@ -36,34 +35,45 @@
3635
@RequiredArgsConstructor
3736
public class ScanShellCommand {
3837

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+
4354

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();
5064

51-
AttributedStringBuilder header = buildHeader(projectRoot);
52-
System.out.println(header.toAnsi());
65+
consolePrinter.println(output);
5366

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+
}
5675

57-
List<Recipe> recipes = applicableRecipeListCommand.execute(projectContext);
58-
return applicableRecipeListRenderer.render(recipes);
59-
}
76+
return stringBuilder.toAttributedString();
77+
}
6078

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-
}
6979
}

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

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package org.springframework.sbm;
1717

1818
import com.rabbitmq.client.Channel;
19-
import com.rabbitmq.client.ConnectionFactory;
2019
import org.junit.jupiter.api.BeforeEach;
2120
import org.junit.jupiter.api.Tag;
2221
import org.junit.jupiter.api.Test;

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

+15-11
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,18 @@
1515
*/
1616
package org.springframework.sbm.recipes;
1717

18-
import org.springframework.sbm.IntegrationTestBaseClass;
19-
import org.springframework.sbm.engine.git.Commit;
20-
import org.springframework.sbm.engine.git.GitSupport;
21-
import org.springframework.sbm.project.resource.ApplicationProperties;
18+
import org.eclipse.jgit.api.Git;
2219
import org.jetbrains.annotations.NotNull;
2320
import org.junit.jupiter.api.BeforeAll;
2421
import org.junit.jupiter.api.Tag;
2522
import org.junit.jupiter.api.Test;
23+
import org.springframework.sbm.IntegrationTestBaseClass;
24+
import org.springframework.sbm.engine.git.Commit;
25+
import org.springframework.sbm.engine.git.GitSupport;
26+
import org.springframework.sbm.project.resource.ApplicationProperties;
2627

28+
import java.io.File;
2729
import java.io.IOException;
28-
import java.nio.file.Files;
29-
import java.util.List;
30-
import java.util.stream.Collectors;
3130

3231
import static org.assertj.core.api.Assertions.assertThat;
3332

@@ -50,9 +49,10 @@ void testRecipe() throws IOException {
5049
intializeTestProject();
5150
// create repo
5251
GitSupport gitSupport = initGitRepo();
53-
List<String> modifiedResources = Files.list(getTestDir()).map(f -> f.toAbsolutePath().toString()).collect(Collectors.toList());
54-
List<String> deletedResources = List.of();
55-
Commit initialCommit = gitSupport.addAllAndCommit(getTestDir().toFile(), "initial commit", modifiedResources, deletedResources);
52+
Commit initialCommit = gitSupport.getLatestCommit(getTestDir().toFile()).get();
53+
// List<String> modifiedResources = Files.list(getTestDir()).map(f -> f.toAbsolutePath().toString()).collect(Collectors.toList());
54+
// List<String> deletedResources = List.of();
55+
// Commit initialCommit = gitSupport.addAllAndCommit(getTestDir().toFile(), "initial commit", modifiedResources, deletedResources);
5656

5757
scanProject();
5858
assertApplicableRecipesContain(
@@ -75,7 +75,11 @@ private GitSupport initGitRepo() {
7575
ApplicationProperties applicationProperties = new ApplicationProperties();
7676
applicationProperties.setGitSupportEnabled(true);
7777
GitSupport gitSupport = new GitSupport(applicationProperties);
78-
gitSupport.initGit(getTestDir().toFile());
78+
File repo = getTestDir().toFile();
79+
Git git = gitSupport.initGit(repo);
80+
gitSupport.add(repo, ".");
81+
gitSupport.commit(repo, "initial commit");
82+
gitSupport.switchToBranch(repo, "main");
7983
return gitSupport;
8084
}
8185
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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.junit.jupiter.api.Test;
19+
20+
import java.io.ByteArrayOutputStream;
21+
import java.io.PrintStream;
22+
23+
import static org.assertj.core.api.Assertions.assertThat;
24+
25+
class ConsolePrinterTest {
26+
27+
@Test
28+
void shouldPrintToConsole() {
29+
ByteArrayOutputStream sysOutBuffer = new ByteArrayOutputStream();
30+
System.setOut(new PrintStream(sysOutBuffer));
31+
32+
ConsolePrinter sut = new ConsolePrinter();
33+
sut.println("some text");
34+
assertThat(sysOutBuffer.toString()).isEqualTo("some text\n");
35+
}
36+
}

0 commit comments

Comments
 (0)