Skip to content

closes #15 add precondition check when scanning #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Mar 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions .github/workflows/check-license-headers.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
name: Check License Lines
on:
pull_request:
branches:
- main
push:
branches: '**'
# pull_request:
# branches:
# - main
jobs:
check-license-lines:
runs-on: ubuntu-latest
Expand Down
8 changes: 5 additions & 3 deletions .github/workflows/mvn-build.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
name: Java CI
on:
pull_request:
branches:
- main
push:
branches: '**'
# pull_request:
# branches:
# - main
jobs:
build:
runs-on: ubuntu-latest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.sbm.project.parser;
package org.springframework.sbm.shell;

import java.io.InputStream;
import java.nio.file.Path;
import org.springframework.stereotype.Component;

public interface Resource {
Path getPath();

InputStream getContent();
@Component
public class ConsolePrinter {
public void println(String text) {
System.out.println(text);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2021 - 2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.sbm.shell;

import org.jline.utils.AttributedStringBuilder;
import org.jline.utils.AttributedStyle;
import org.jline.utils.Colors;
import org.springframework.sbm.engine.precondition.PreconditionCheck;
import org.springframework.sbm.engine.precondition.PreconditionCheckResult;
import org.springframework.sbm.engine.precondition.PreconditionVerificationResult;
import org.springframework.stereotype.Component;

@Component
public class PreconditionVerificationRenderer {

public String renderPreconditionCheckResults(PreconditionVerificationResult result) {
AttributedStringBuilder stringBuilder = new AttributedStringBuilder();
stringBuilder.style(stringBuilder.style().DEFAULT.bold().foreground(Colors.rgbColor("black")));
stringBuilder.append("\n\n").append(String.format("Checked preconditions for '%s'", result.getProjectRoot())).append("\n");

result.getResults().forEach(r -> {
stringBuilder.append(renderCheckResult(r));
});
stringBuilder.append("\n");
return stringBuilder.toAnsi();
}

private String renderCheckResult(PreconditionCheckResult r) {
AttributedStringBuilder builder = new AttributedStringBuilder();

// TODO: move rendering of status into central place
if(r.getState().equals(PreconditionCheck.ResultState.FAILED)) {
builder.style(builder.style().DEFAULT.bold().foreground(Colors.rgbColor("red")));
builder.append(" [X]");
builder.style(builder.style().DEFAULT);
}

if(r.getState().equals(PreconditionCheck.ResultState.PASSED)) {
builder.style(AttributedStyle.DEFAULT.bold().foreground(Colors.rgbColor("green")));
builder.append("[ok]");
builder.style(AttributedStyle.DEFAULT);
}

if(r.getState().equals(PreconditionCheck.ResultState.WARN)) {
builder.style(AttributedStyle.DEFAULT.bold().foreground(Colors.rgbColor("yellow")));
builder.append(" [!]");
builder.style(AttributedStyle.DEFAULT);
}

builder.append(" ").append(r.getMessage());
builder.append("\n");

return builder.toAnsi();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
*/
package org.springframework.sbm.shell;

import org.springframework.sbm.engine.recipe.Recipe;
import org.springframework.sbm.engine.recipe.RecipeAutomation;
import org.jline.utils.AttributedString;
import org.jline.utils.AttributedStringBuilder;
import org.jline.utils.AttributedStyle;
import org.jline.utils.Colors;
import org.springframework.sbm.engine.recipe.Recipe;
import org.springframework.sbm.engine.recipe.RecipeAutomation;
import org.springframework.stereotype.Component;

import java.util.List;
Expand All @@ -37,13 +37,16 @@ public AttributedString renderRecipesList(String noRecipesTitle, String title, L
if (foundRecipes.isEmpty()) {
builder.append(noRecipesTitle);
} else {
AttributedString titleString = renderTitle(title);
builder.append(titleString);

AttributedString emojiMapping = renderEmojiMapping();
builder.append(emojiMapping);
foundRecipes.forEach(recipe -> this.buildRecipePresentation(builder, recipe));
AttributedString titleString = renderTitle(title);
builder.append(titleString);
builder.append("\n\n");

builder.append("\n");
builder.append("Run command '> apply recipe-name' to apply a recipe.");
builder.append("\n");
}
return builder.toAttributedString();
}
Expand Down Expand Up @@ -71,7 +74,9 @@ public AttributedStringBuilder buildRecipePresentation(AttributedStringBuilder b
private AttributedString renderTitle(String title) {
AttributedStringBuilder builder = new AttributedStringBuilder();
builder.style(AttributedStyle.DEFAULT.bold());
builder.append("\n");
builder.append(title);
builder.append("\n\n");
return builder.toAttributedString();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2021 - 2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.sbm.shell;

import org.jline.utils.AttributedStringBuilder;
import org.jline.utils.AttributedStyle;
import org.jline.utils.Colors;
import org.springframework.stereotype.Component;

@Component
public class ScanCommandHeaderRenderer {
public String renderHeader(String projectRoot) {
AttributedStringBuilder builder = new AttributedStringBuilder();
builder.append("\n");
builder.style(AttributedStyle.DEFAULT.italicDefault().boldDefault().foreground(Colors.rgbColor("green")));
builder.append("scanning '" + projectRoot + "'");
return builder.toAnsi();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,16 @@
*/
package org.springframework.sbm.shell;

import lombok.RequiredArgsConstructor;
import org.jline.utils.AttributedString;
import org.jline.utils.AttributedStringBuilder;
import org.springframework.core.io.Resource;
import org.springframework.sbm.engine.commands.ApplicableRecipeListCommand;
import org.springframework.sbm.engine.commands.ScanCommand;
import org.springframework.sbm.engine.context.ProjectContext;
import org.springframework.sbm.engine.context.ProjectContextHolder;
import org.springframework.sbm.engine.precondition.PreconditionVerificationResult;
import org.springframework.sbm.engine.recipe.Recipe;
import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.NotNull;
import org.jline.utils.AttributedString;
import org.jline.utils.AttributedStringBuilder;
import org.jline.utils.AttributedStyle;
import org.jline.utils.Colors;
import org.springframework.shell.standard.ShellComponent;
import org.springframework.shell.standard.ShellMethod;
import org.springframework.shell.standard.ShellOption;
Expand All @@ -36,34 +35,45 @@
@RequiredArgsConstructor
public class ScanShellCommand {

private final ScanCommand scanCommand;
private final ApplicableRecipeListRenderer applicableRecipeListRenderer;
private final ApplicableRecipeListCommand applicableRecipeListCommand;
private final ProjectContextHolder contextHolder;
private final ScanCommand scanCommand;

private final ApplicableRecipeListRenderer applicableRecipeListRenderer;

private final ApplicableRecipeListCommand applicableRecipeListCommand;

private final ProjectContextHolder contextHolder;
private final PreconditionVerificationRenderer preconditionVerificationRenderer;
private final ScanCommandHeaderRenderer scanCommandHeaderRenderer;
private final ConsolePrinter consolePrinter;

@ShellMethod(key = { "scan", "s" },
value = "Scans the target project directory and get the list of applicable recipes.")
public AttributedString scan(@ShellOption(defaultValue = ".",
help = "The root directory of the target application.") String projectRoot) {


@ShellMethod(key = {"scan", "s"}, value = "Scans the target project directory and get the list of applicable recipes.")
public AttributedString scan(
@ShellOption(
defaultValue = ".",
help = "The root directory of the target application."
) String projectRoot) {
List<Resource> resources = scanCommand.scanProjectRoot(projectRoot);
String scanCommandHeader = scanCommandHeaderRenderer.renderHeader(projectRoot);
PreconditionVerificationResult result = scanCommand.checkPreconditions(projectRoot, resources);
String renderedPreconditionCheckResults = preconditionVerificationRenderer.renderPreconditionCheckResults(result);
AttributedStringBuilder stringBuilder = new AttributedStringBuilder();
String output = stringBuilder
.append(scanCommandHeader)
.append(renderedPreconditionCheckResults)
.toAnsi();

AttributedStringBuilder header = buildHeader(projectRoot);
System.out.println(header.toAnsi());
consolePrinter.println(output);

ProjectContext projectContext = scanCommand.execute(projectRoot);
contextHolder.setProjectContext(projectContext);
stringBuilder = new AttributedStringBuilder();
if ( ! result.hasError()) {
ProjectContext projectContext = scanCommand.execute(projectRoot);
contextHolder.setProjectContext(projectContext);
List<Recipe> recipes = applicableRecipeListCommand.execute(projectContext);
AttributedString recipeList = applicableRecipeListRenderer.render(recipes);
stringBuilder.append(recipeList);
}

List<Recipe> recipes = applicableRecipeListCommand.execute(projectContext);
return applicableRecipeListRenderer.render(recipes);
}
return stringBuilder.toAttributedString();
}

@NotNull
private AttributedStringBuilder buildHeader(String projectRoot) {
AttributedStringBuilder builder = new AttributedStringBuilder();
builder.append("\n");
builder.style(AttributedStyle.DEFAULT.italicDefault().boldDefault().foreground(Colors.rgbColor("green")));
builder.append("scanning '" + projectRoot + "'");
return builder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package org.springframework.sbm;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConnectionFactory;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,18 @@
*/
package org.springframework.sbm.recipes;

import org.springframework.sbm.IntegrationTestBaseClass;
import org.springframework.sbm.engine.git.Commit;
import org.springframework.sbm.engine.git.GitSupport;
import org.springframework.sbm.project.resource.ApplicationProperties;
import org.eclipse.jgit.api.Git;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.springframework.sbm.IntegrationTestBaseClass;
import org.springframework.sbm.engine.git.Commit;
import org.springframework.sbm.engine.git.GitSupport;
import org.springframework.sbm.project.resource.ApplicationProperties;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;
import java.util.stream.Collectors;

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

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

scanProject();
assertApplicableRecipesContain(
Expand All @@ -75,7 +75,11 @@ private GitSupport initGitRepo() {
ApplicationProperties applicationProperties = new ApplicationProperties();
applicationProperties.setGitSupportEnabled(true);
GitSupport gitSupport = new GitSupport(applicationProperties);
gitSupport.initGit(getTestDir().toFile());
File repo = getTestDir().toFile();
Git git = gitSupport.initGit(repo);
gitSupport.add(repo, ".");
gitSupport.commit(repo, "initial commit");
gitSupport.switchToBranch(repo, "main");
return gitSupport;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2021 - 2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.sbm.shell;

import org.junit.jupiter.api.Test;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

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

class ConsolePrinterTest {

@Test
void shouldPrintToConsole() {
ByteArrayOutputStream sysOutBuffer = new ByteArrayOutputStream();
System.setOut(new PrintStream(sysOutBuffer));

ConsolePrinter sut = new ConsolePrinter();
sut.println("some text");
assertThat(sysOutBuffer.toString()).isEqualTo("some text\n");
}
}
Loading