Skip to content
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

feat(readme): add section #1773

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static tech.jhipster.lite.common.domain.FileUtils.getPath;
import static tech.jhipster.lite.common.domain.WordUtils.CRLF;
import static tech.jhipster.lite.generator.project.domain.Constants.PACKAGE_JSON;
import static tech.jhipster.lite.generator.project.domain.Constants.README_MD;
import static tech.jhipster.lite.generator.project.domain.DefaultConfig.BASE_NAME;
import static tech.jhipster.lite.generator.project.domain.DefaultConfig.PRETTIER_DEFAULT_INDENT;
import static tech.jhipster.lite.generator.project.domain.DefaultConfig.PROJECT_NAME;
Expand Down Expand Up @@ -93,7 +94,7 @@ private void addScripts(Project project) {
public void addReadme(Project project) {
project.addDefaultConfig(PROJECT_NAME);

projectRepository.template(ProjectFile.forProject(project).withSource(SOURCE, "README.md").withSameDestination());
projectRepository.template(ProjectFile.forProject(project).withSource(SOURCE, README_MD).withSameDestination());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ private Constants() {}
public static final String HISTORY_JSON = "history.json";
public static final String ROUTER_TYPESCRIPT = "router.ts";
public static final String TSCONFIG_JSON = "tsconfig.json";
public static final String README_MD = "README.md";

public static final String COMMENT_PROPERTIES_PREFIX = "#";
public static final String KEY_VALUE_PROPERTIES_SEPARATOR = "=";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package tech.jhipster.lite.generator.readme.application;

import org.springframework.stereotype.Service;
import tech.jhipster.lite.generator.project.domain.Project;
import tech.jhipster.lite.generator.readme.domain.ReadMeService;

@Service
public class ReadMeApplicationService {

private final ReadMeService readMeService;

public ReadMeApplicationService(ReadMeService readMeService) {
this.readMeService = readMeService;
}

public void addSection(final Project project, final String header, final String body) {
readMeService.addSection(project, header, body);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package tech.jhipster.lite.generator.readme.domain;

import static tech.jhipster.lite.generator.project.domain.Constants.README_MD;

import tech.jhipster.lite.generator.project.domain.Project;
import tech.jhipster.lite.generator.project.domain.ProjectRepository;

public class ReadMeDomainService implements ReadMeService {

private final ProjectRepository projectRepository;

public ReadMeDomainService(ProjectRepository projectRepository) {
this.projectRepository = projectRepository;
}

@Override
public void addSection(final Project project, final String header, final String body) {
if (!projectRepository.containsRegexp(project, "", README_MD, header)) {
projectRepository.replaceText(
project,
"",
README_MD,
"<!-- jhipster-needle-readme -->",
header + "\n\n" + body + "\n\n<!-- jhipster-needle-readme -->"
);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package tech.jhipster.lite.generator.readme.domain;

import tech.jhipster.lite.generator.project.domain.Project;

public interface ReadMeService {
void addSection(Project project, String header, String body);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package tech.jhipster.lite.generator.readme.infrastructure.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import tech.jhipster.lite.generator.project.domain.ProjectRepository;
import tech.jhipster.lite.generator.readme.domain.ReadMeDomainService;
import tech.jhipster.lite.generator.readme.domain.ReadMeService;

@Configuration
public class ReadMeBeanConfiguration {

private final ProjectRepository projectRepository;

public ReadMeBeanConfiguration(ProjectRepository projectRepository) {
this.projectRepository = projectRepository;
}

@Bean
public ReadMeService readMeService() {
return new ReadMeDomainService(projectRepository);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@tech.jhipster.lite.SharedKernel
package tech.jhipster.lite.generator.readme;
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import static tech.jhipster.lite.generator.init.application.InitAssertFiles.assertFilesPrettier;
import static tech.jhipster.lite.generator.init.application.InitAssertFiles.assertFilesReadme;
import static tech.jhipster.lite.generator.project.domain.Constants.PACKAGE_JSON;
import static tech.jhipster.lite.generator.project.domain.Constants.README_MD;
import static tech.jhipster.lite.generator.project.domain.DefaultConfig.BASE_NAME;
import static tech.jhipster.lite.generator.project.domain.DefaultConfig.PRETTIER_DEFAULT_INDENT;
import static tech.jhipster.lite.generator.project.domain.DefaultConfig.PROJECT_NAME;
Expand Down Expand Up @@ -47,7 +48,7 @@ void shouldInitWithConfig() {
initApplicationService.init(project);

assertFilesInit(project);
assertFileContent(project, "README.md", "JHipster Lite");
assertFileContent(project, README_MD, "JHipster Lite");
assertFileContent(project, PACKAGE_JSON, "jhipster-lite");
assertFileContent(project, ".editorconfig", "end_of_line = crlf");
assertFileContent(project, ".prettierrc", "endOfLine: \"crlf\"");
Expand Down Expand Up @@ -79,7 +80,7 @@ void shouldInitMinimalWithConfig() {
initApplicationService.initMinimal(project);

assertFilesInitMinimal(project);
assertFileContent(project, "README.md", "JHipster Lite");
assertFileContent(project, README_MD, "JHipster Lite");
assertFileContent(project, ".editorconfig", "end_of_line = crlf");
assertFileGitInit(project);
}
Expand All @@ -91,7 +92,7 @@ void shouldInitWithDefaultConfig() {
initApplicationService.init(project);

assertFilesInit(project);
assertFileContent(project, "README.md", "JHipster Project");
assertFileContent(project, README_MD, "JHipster Project");
assertFileContent(project, PACKAGE_JSON, "jhipster");
assertFileContent(project, ".editorconfig", "end_of_line = lf");
assertFileContent(project, ".prettierrc", "endOfLine: \"lf\"");
Expand All @@ -114,7 +115,7 @@ void shouldInitMinimalWithDefaultConfig() {
initApplicationService.initMinimal(project);

assertFilesInitMinimal(project);
assertFileContent(project, "README.md", "JHipster Project");
assertFileContent(project, README_MD, "JHipster Project");
assertFileContent(project, ".editorconfig", "end_of_line = lf");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static tech.jhipster.lite.TestUtils.assertFileExist;
import static tech.jhipster.lite.common.domain.FileUtils.getPathOf;
import static tech.jhipster.lite.generator.project.domain.Constants.PACKAGE_JSON;
import static tech.jhipster.lite.generator.project.domain.Constants.README_MD;

import java.io.IOException;
import java.nio.file.Files;
Expand All @@ -19,7 +20,7 @@ public static void assertFilesPackageJson(Project project) {
}

public static void assertFilesReadme(Project project) {
assertFileExist(project, "README.md");
assertFileExist(project, README_MD);
}

public static void assertFilesGitConfiguration(Project project) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import static tech.jhipster.lite.generator.init.application.InitAssertFiles.assertFilesInit;
import static tech.jhipster.lite.generator.init.application.InitAssertFiles.assertFilesInitMinimal;
import static tech.jhipster.lite.generator.project.domain.Constants.PACKAGE_JSON;
import static tech.jhipster.lite.generator.project.domain.Constants.README_MD;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -40,7 +41,7 @@ void shouldInit() throws Exception {

Project project = ProjectDTO.toProject(projectDTO);
assertFilesInit(project);
assertFileContent(project, "README.md", "Chips Project");
assertFileContent(project, README_MD, "Chips Project");
assertFileContent(project, ".prettierrc", "tabWidth: 2");
assertFileContent(project, PACKAGE_JSON, "chips");
}
Expand All @@ -55,6 +56,6 @@ void shouldInitMinimal() throws Exception {

Project project = ProjectDTO.toProject(projectDTO);
assertFilesInitMinimal(project);
assertFileContent(project, "README.md", "Chips Project");
assertFileContent(project, README_MD, "Chips Project");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ void shouldHandleTemplatingError() {
try (MockedStatic<MustacheUtils> mustacheUtils = Mockito.mockStatic(MustacheUtils.class)) {
mustacheUtils.when(() -> MustacheUtils.template(anyString(), any())).thenThrow(new IOException());

List<ProjectFile> files = List.of(ProjectFile.forProject(project).withSource("mustache", "README.md").withSameDestination());
List<ProjectFile> files = List.of(ProjectFile.forProject(project).withSource("mustache", README_MD).withSameDestination());
assertThatThrownBy(() -> repository.template(files)).isExactlyInstanceOf(GeneratorException.class);
}
}
Expand All @@ -116,11 +116,11 @@ void shouldNotTemplateForUnknownFile() {
@Test
void shouldTemplatizeFile() {
Project project = tmpProject();
List<ProjectFile> files = List.of(ProjectFile.forProject(project).withSource("mustache", "README.md").withSameDestination());
List<ProjectFile> files = List.of(ProjectFile.forProject(project).withSource("mustache", README_MD).withSameDestination());

repository.template(files);

assertFileExist(project, "README.md");
assertFileExist(project, README_MD);
}

@Test
Expand All @@ -130,7 +130,7 @@ void shouldTemplatizeFileWithMustacheExtension() {

repository.template(files);

assertFileExist(project, "README.md");
assertFileExist(project, README_MD);
}

@Test
Expand Down Expand Up @@ -165,7 +165,7 @@ void shouldNotGetComputedTemplate() {
try (MockedStatic<MustacheUtils> mustacheUtils = Mockito.mockStatic(MustacheUtils.class)) {
mustacheUtils.when(() -> MustacheUtils.template(anyString(), any())).thenThrow(new IOException());

assertThatThrownBy(() -> repository.getComputedTemplate(project, "mustache", "README.md"))
assertThatThrownBy(() -> repository.getComputedTemplate(project, "mustache", README_MD))
.isExactlyInstanceOf(GeneratorException.class);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package tech.jhipster.lite.generator.readme.application;

import static tech.jhipster.lite.TestUtils.assertFileContent;
import static tech.jhipster.lite.TestUtils.tmpProject;
import static tech.jhipster.lite.generator.project.domain.Constants.README_MD;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import tech.jhipster.lite.IntegrationTest;
import tech.jhipster.lite.generator.init.application.InitApplicationService;
import tech.jhipster.lite.generator.project.domain.Project;

@IntegrationTest
class ReadMeApplicationServiceIT {

@Autowired
InitApplicationService initApplicationService;

@Autowired
ReadMeApplicationService readMeApplicationService;

@Test
void shouldAddSection() {
Project project = tmpProject();

initApplicationService.addReadme(project);
readMeApplicationService.addSection(
project,
"# Apache Kafka",
"""
# Apache Kafka

Description of the tasks to be launched.
"""
);

assertFileContent(project, README_MD, "Description of the tasks to be launched.");
}

@Test
void shouldNotAddSection() {
Project project = tmpProject();

initApplicationService.addReadme(project);
readMeApplicationService.addSection(
project,
"# Apache Kafka",
"""
# Apache Kafka

Description of the tasks to be launched.
"""
);
readMeApplicationService.addSection(project, "# Apache Kafka", """
# Apache Kafka

Update section content.
""");

// Section header is already existing so it won't add a new section
assertFileContent(project, README_MD, "Description of the tasks to be launched.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package tech.jhipster.lite.generator.readme.domain;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.verify;
import static tech.jhipster.lite.TestUtils.tmpProject;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import tech.jhipster.lite.UnitTest;
import tech.jhipster.lite.generator.project.domain.Project;
import tech.jhipster.lite.generator.project.domain.ProjectRepository;

@UnitTest
@ExtendWith(MockitoExtension.class)
class ReadMeDomainServiceTest {

@Mock
private ProjectRepository projectRepository;

@InjectMocks
private ReadMeDomainService readMeDomainService;

@Test
void shouldAddSection() {
Project project = tmpProject();

readMeDomainService.addSection(
project,
"# Apache Kafka",
"""
# Apache Kafka

Description of the tasks to be launched.
"""
);

verify(projectRepository).containsRegexp(any(Project.class), anyString(), anyString(), anyString());
verify(projectRepository).replaceText(any(Project.class), anyString(), anyString(), anyString(), anyString());
}
}