Skip to content

Commit

Permalink
Merge pull request #638 from Bolo89/add-history
Browse files Browse the repository at this point in the history
Add history
  • Loading branch information
pascalgrimaud authored Feb 3, 2022
2 parents e34b3f7 + 4e65603 commit fac6918
Show file tree
Hide file tree
Showing 43 changed files with 652 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/tech/jhipster/lite/common/domain/JsonUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package tech.jhipster.lite.common.domain;

import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonUtils {

public static final String CARRIAGE_RETURN = System.lineSeparator();

private static ObjectMapper objectMapper = null;

private JsonUtils() {
// Cannot be instantiated
}

public static ObjectMapper getObjectMapper() {
if (objectMapper != null) {
return objectMapper;
}
objectMapper = new ObjectMapper();
return objectMapper;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import tech.jhipster.lite.generator.buildtool.maven.application.MavenApplicationService;
import tech.jhipster.lite.generator.project.domain.Project;
import tech.jhipster.lite.generator.project.infrastructure.primary.dto.ProjectDTO;
import tech.jhipster.lite.technical.infrastructure.primary.annotation.GeneratorStep;

@RestController
@RequestMapping("/api/build-tools/maven")
Expand All @@ -25,6 +26,7 @@ public MavenResource(MavenApplicationService mavenApplicationService) {
@Operation(summary = "Init", description = "Init Maven project with pom.xml and wrapper")
@ApiResponse(responseCode = "500", description = "An error occurred while initializing Maven project")
@PostMapping
@GeneratorStep(id = "maven")
public void init(@RequestBody ProjectDTO projectDTO) {
Project project = ProjectDTO.toProject(projectDTO);
mavenApplicationService.init(project);
Expand All @@ -33,6 +35,7 @@ public void init(@RequestBody ProjectDTO projectDTO) {
@Operation(summary = "Add pom.xml")
@ApiResponse(responseCode = "500", description = "An error occurred while adding pom.xml to project")
@PostMapping("/pom-xml")
@GeneratorStep(id = "pom.xml")
public void addPomXml(@RequestBody ProjectDTO projectDTO) {
Project project = ProjectDTO.toProject(projectDTO);
mavenApplicationService.addPomXml(project);
Expand All @@ -41,6 +44,7 @@ public void addPomXml(@RequestBody ProjectDTO projectDTO) {
@Operation(summary = "Add Maven Wrapper")
@ApiResponse(responseCode = "500", description = "An error occurred while adding Maven Wrapper to project")
@PostMapping("/wrapper")
@GeneratorStep(id = "maven-wrapper")
public void addMavenWrapper(@RequestBody ProjectDTO projectDTO) {
Project project = ProjectDTO.toProject(projectDTO);
mavenApplicationService.addMavenWrapper(project);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package tech.jhipster.lite.generator.history.application;

import org.springframework.stereotype.Service;
import tech.jhipster.lite.generator.history.domain.GeneratorHistoryRepository;
import tech.jhipster.lite.generator.history.domain.GeneratorHistoryValue;
import tech.jhipster.lite.generator.project.domain.Project;

@Service
public class GeneratorHistoryApplicationService {

private final GeneratorHistoryRepository generatorHistoryRepository;

public GeneratorHistoryApplicationService(GeneratorHistoryRepository generatorHistoryRepository) {
this.generatorHistoryRepository = generatorHistoryRepository;
}

public void addHistoryValue(Project project, GeneratorHistoryValue generatorHistoryValue) {
generatorHistoryRepository.addHistoryValue(project, generatorHistoryValue);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package tech.jhipster.lite.generator.history.domain;

import java.util.ArrayList;
import java.util.List;

public class GeneratorHistoryData {

private final List<GeneratorHistoryValue> values = new ArrayList<>();

public List<GeneratorHistoryValue> getValues() {
return values;
}

@Override
public String toString() {
return "GeneratorHistoryData{" + "values=" + values + '}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package tech.jhipster.lite.generator.history.domain;

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

public interface GeneratorHistoryRepository {
GeneratorHistoryData getHistoryData(Project project);

void addHistoryValue(Project project, GeneratorHistoryValue generatorHistoryValue);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package tech.jhipster.lite.generator.history.domain;

public record GeneratorHistoryValue(String serviceId) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package tech.jhipster.lite.generator.history.infrastructure.primary;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.context.annotation.Configuration;
import tech.jhipster.lite.generator.history.application.GeneratorHistoryApplicationService;
import tech.jhipster.lite.generator.history.domain.GeneratorHistoryValue;
import tech.jhipster.lite.generator.project.domain.Project;
import tech.jhipster.lite.generator.project.infrastructure.primary.dto.ProjectDTO;
import tech.jhipster.lite.technical.infrastructure.primary.annotation.GeneratorStep;

@Aspect
@Configuration
public class GeneratorHistoryInterceptor {

private final GeneratorHistoryApplicationService generatorHistoryApplicationService;

public GeneratorHistoryInterceptor(GeneratorHistoryApplicationService generatorHistoryApplicationService) {
this.generatorHistoryApplicationService = generatorHistoryApplicationService;
}

@After(value = "@annotation(generatorStep)")
public void addInHistory(JoinPoint joinPoint, GeneratorStep generatorStep) {
String serviceId = generatorStep.id();
ProjectDTO projectDTO = (ProjectDTO) joinPoint.getArgs()[0];
Project project = ProjectDTO.toProject(projectDTO);
GeneratorHistoryValue generatorHistoryValue = new GeneratorHistoryValue(serviceId);
generatorHistoryApplicationService.addHistoryValue(project, generatorHistoryValue);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package tech.jhipster.lite.generator.history.infrastructure.secondary;

import com.fasterxml.jackson.core.JsonProcessingException;
import java.io.IOException;
import org.springframework.stereotype.Repository;
import tech.jhipster.lite.common.domain.FileUtils;
import tech.jhipster.lite.common.domain.JsonUtils;
import tech.jhipster.lite.common.domain.WordUtils;
import tech.jhipster.lite.error.domain.GeneratorException;
import tech.jhipster.lite.generator.history.domain.GeneratorHistoryData;
import tech.jhipster.lite.generator.history.domain.GeneratorHistoryRepository;
import tech.jhipster.lite.generator.history.domain.GeneratorHistoryValue;
import tech.jhipster.lite.generator.project.domain.Project;
import tech.jhipster.lite.generator.project.domain.ProjectRepository;

@Repository
public class GeneratorHistoryLocalRepository implements GeneratorHistoryRepository {

private static final String HISTORY_FILE_NAME = "history.json";
private static final String HISTORY_FILE_FOLDER_SOURCE = "history";
private static final String HISTORY_FOLDER_PATH_DEST = ".jhipster";

private final ProjectRepository projectRepository;

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

@Override
public GeneratorHistoryData getHistoryData(Project project) {
String filePath = getHistoryFilePath(project);
String historyFileContent;
try {
historyFileContent = FileUtils.read(filePath);
} catch (IOException e) {
throw new GeneratorException("Error on file " + filePath + " : " + e.getMessage());
}
return deserializeHistoryFile(filePath, historyFileContent);
}

@Override
public void addHistoryValue(Project project, GeneratorHistoryValue generatorHistoryValue) {
String filePath = getHistoryFilePath(project);
GeneratorHistoryData generatorHistoryData;
try {
String historyFileContent = FileUtils.read(filePath);
generatorHistoryData = deserializeHistoryFile(filePath, historyFileContent);
} catch (IOException e) {
// The file does not exist
createHistoryFile(project);
generatorHistoryData = getHistoryData(project);
}

generatorHistoryData.getValues().add(generatorHistoryValue);
String newHistoryFileContent;
try {
newHistoryFileContent = JsonUtils.getObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(generatorHistoryData);
} catch (JsonProcessingException e) {
throw new GeneratorException("Cannot serialize the history data " + generatorHistoryData + " : " + e.getMessage());
}
try {
FileUtils.write(filePath, newHistoryFileContent, WordUtils.CRLF);
} catch (IOException e) {
throw new GeneratorException("Cannot write JSON content in file " + filePath + " : " + e.getMessage());
}
}

private void createHistoryFile(Project project) {
projectRepository.add(project, HISTORY_FILE_FOLDER_SOURCE, HISTORY_FILE_NAME, HISTORY_FOLDER_PATH_DEST, HISTORY_FILE_NAME);
}

private String getHistoryFilePath(Project project) {
return FileUtils.getPath(project.getFolder(), HISTORY_FOLDER_PATH_DEST, HISTORY_FILE_NAME);
}

private GeneratorHistoryData deserializeHistoryFile(String filePath, String historyFileContent) {
try {
return JsonUtils.getObjectMapper().readValue(historyFileContent, GeneratorHistoryData.class);
} catch (JsonProcessingException e) {
throw new GeneratorException("Cannot deserialize JSON of file " + filePath + " : " + e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@tech.jhipster.lite.BusinessContext
package tech.jhipster.lite.generator.history;
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import tech.jhipster.lite.generator.init.application.InitApplicationService;
import tech.jhipster.lite.generator.project.domain.Project;
import tech.jhipster.lite.generator.project.infrastructure.primary.dto.ProjectDTO;
import tech.jhipster.lite.technical.infrastructure.primary.annotation.GeneratorStep;

@RestController
@RequestMapping("/api/projects")
Expand All @@ -25,6 +26,7 @@ public InitResource(InitApplicationService initApplicationService) {
@Operation(summary = "Init project")
@ApiResponse(responseCode = "500", description = "An error occurred while initializing project")
@PostMapping("/init")
@GeneratorStep(id = "init")
public void init(@RequestBody ProjectDTO projectDTO) {
Project project = ProjectDTO.toProject(projectDTO);
initApplicationService.init(project);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import tech.jhipster.lite.generator.packagemanager.npm.application.NpmApplicationService;
import tech.jhipster.lite.generator.project.domain.Project;
import tech.jhipster.lite.generator.project.infrastructure.primary.dto.ProjectDTO;
import tech.jhipster.lite.technical.infrastructure.primary.annotation.GeneratorStep;

@RestController
@RequestMapping("/api/npm")
Expand All @@ -25,6 +26,7 @@ public NpmResource(NpmApplicationService npmApplicationService) {
@Operation(summary = "Run: npm install")
@ApiResponse(responseCode = "500", description = "An error occurred while installing project")
@PostMapping("/install")
@GeneratorStep(id = "npm-install")
public void install(@RequestBody ProjectDTO projectDTO) {
Project project = ProjectDTO.toProject(projectDTO);
npmApplicationService.install(project);
Expand All @@ -33,6 +35,7 @@ public void install(@RequestBody ProjectDTO projectDTO) {
@Operation(summary = "Run: npm run prettier:format")
@ApiResponse(responseCode = "500", description = "An error occurred while prettifying project")
@PostMapping("/prettier-format")
@GeneratorStep(id = "npm-prettify")
public void prettify(@RequestBody ProjectDTO projectDTO) {
Project project = ProjectDTO.toProject(projectDTO);
npmApplicationService.prettify(project);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import tech.jhipster.lite.generator.project.domain.Project;
import tech.jhipster.lite.generator.project.infrastructure.primary.dto.ProjectDTO;
import tech.jhipster.lite.generator.server.javatool.base.application.JavaBaseApplicationService;
import tech.jhipster.lite.technical.infrastructure.primary.annotation.GeneratorStep;

@RestController
@RequestMapping("/api/servers/java/base")
Expand All @@ -25,6 +26,7 @@ public JavaBaseResource(JavaBaseApplicationService javaBaseApplicationService) {
@Operation(summary = "Add Base classes and Error domain to project")
@ApiResponse(responseCode = "500", description = "An error occurred while adding base classes and error domain to project")
@PostMapping
@GeneratorStep(id = "java-base")
public void init(@RequestBody ProjectDTO projectDTO) {
Project project = ProjectDTO.toProject(projectDTO);
javaBaseApplicationService.init(project);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import tech.jhipster.lite.generator.project.domain.Project;
import tech.jhipster.lite.generator.project.infrastructure.primary.dto.ProjectDTO;
import tech.jhipster.lite.generator.server.javatool.jacoco.application.JacocoApplicationService;
import tech.jhipster.lite.technical.infrastructure.primary.annotation.GeneratorStep;

@RestController
@RequestMapping("/api/servers/java/jacoco-minimum-coverage")
Expand All @@ -25,6 +26,7 @@ public JacocoResource(JacocoApplicationService jacocoApplicationService) {
@Operation(summary = "Add JaCoCo configuration to check minimum coverage")
@ApiResponse(responseCode = "500", description = "An error occurred while adding JaCoco configuration")
@PostMapping
@GeneratorStep(id = "jacoco-check-min-coverage")
public void addCheckMinimumCoverage(@RequestBody ProjectDTO projectDTO) {
Project project = ProjectDTO.toProject(projectDTO);
jacocoApplicationService.addCheckMinimumCoverage(project);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import tech.jhipster.lite.generator.project.domain.Project;
import tech.jhipster.lite.generator.project.infrastructure.primary.dto.ProjectDTO;
import tech.jhipster.lite.generator.server.sonar.application.SonarApplicationService;
import tech.jhipster.lite.technical.infrastructure.primary.annotation.GeneratorStep;

@RestController
@RequestMapping("/api/servers/sonar")
Expand All @@ -25,6 +26,7 @@ public SonarResource(SonarApplicationService sonarApplicationService) {
@Operation(summary = "Add Sonar configuration to inspect code quality")
@ApiResponse(responseCode = "500", description = "An error occurred while adding Sonar configuration")
@PostMapping
@GeneratorStep(id = "sonar")
public void addSonar(@RequestBody ProjectDTO projectDTO) {
Project project = ProjectDTO.toProject(projectDTO);
sonarApplicationService.init(project);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import tech.jhipster.lite.generator.project.domain.Project;
import tech.jhipster.lite.generator.project.infrastructure.primary.dto.ProjectDTO;
import tech.jhipster.lite.generator.server.springboot.async.application.SpringBootAsyncApplicationService;
import tech.jhipster.lite.technical.infrastructure.primary.annotation.GeneratorStep;

@RestController
@RequestMapping("/api/servers/spring-boot/async")
Expand All @@ -25,6 +26,7 @@ public SpringBootAsyncResource(SpringBootAsyncApplicationService springBootAsync
@Operation(summary = "Add asynchronous execution and scheduling configuration")
@ApiResponse(responseCode = "500", description = "An error occurred while adding asynchronous execution and scheduling configuration")
@PostMapping
@GeneratorStep(id = "springboot-async")
public void init(@RequestBody ProjectDTO projectDTO) {
Project project = ProjectDTO.toProject(projectDTO);
springBootAsyncApplicationService.init(project);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import tech.jhipster.lite.generator.project.domain.Project;
import tech.jhipster.lite.generator.project.infrastructure.primary.dto.ProjectDTO;
import tech.jhipster.lite.generator.server.springboot.banner.application.BannerApplicationService;
import tech.jhipster.lite.technical.infrastructure.primary.annotation.GeneratorStep;

@RestController
@RequestMapping("/api/servers/spring-boot/banner")
Expand All @@ -25,6 +26,7 @@ public BannerResource(BannerApplicationService bannerApplicationService) {
@Operation(summary = "Add banner JHipster v7 for Angular")
@ApiResponse(responseCode = "500", description = "An error occurred while adding banner")
@PostMapping("/jhipster-v7")
@GeneratorStep(id = "banner-jhipster-v7")
public void addBannerJHipsterV7(@RequestBody ProjectDTO projectDTO) {
Project project = ProjectDTO.toProject(projectDTO);
bannerApplicationService.addBannerJHipsterV7(project);
Expand All @@ -33,6 +35,7 @@ public void addBannerJHipsterV7(@RequestBody ProjectDTO projectDTO) {
@Operation(summary = "Add banner JHipster v7 for React")
@ApiResponse(responseCode = "500", description = "An error occurred while adding banner")
@PostMapping("/jhipster-v7-react")
@GeneratorStep(id = "banner-jhipster-v7-react")
public void addBannerJHipsterV7React(@RequestBody ProjectDTO projectDTO) {
Project project = ProjectDTO.toProject(projectDTO);
bannerApplicationService.addBannerJHipsterV7React(project);
Expand All @@ -41,6 +44,7 @@ public void addBannerJHipsterV7React(@RequestBody ProjectDTO projectDTO) {
@Operation(summary = "Add banner JHipster v7 for Vue")
@ApiResponse(responseCode = "500", description = "An error occurred while adding banner")
@PostMapping("/jhipster-v7-vue")
@GeneratorStep(id = "banner-jhipster-v7-vue")
public void addBannerJHipsterV7Vue(@RequestBody ProjectDTO projectDTO) {
Project project = ProjectDTO.toProject(projectDTO);
bannerApplicationService.addBannerJHipsterV7Vue(project);
Expand All @@ -49,6 +53,7 @@ public void addBannerJHipsterV7Vue(@RequestBody ProjectDTO projectDTO) {
@Operation(summary = "Add banner JHipster v2")
@ApiResponse(responseCode = "500", description = "An error occurred while adding banner")
@PostMapping("/jhipster-v2")
@GeneratorStep(id = "banner-jhipster-v2")
public void addBannerJHipsterV2(@RequestBody ProjectDTO projectDTO) {
Project project = ProjectDTO.toProject(projectDTO);
bannerApplicationService.addBannerJHipsterV2(project);
Expand All @@ -57,6 +62,7 @@ public void addBannerJHipsterV2(@RequestBody ProjectDTO projectDTO) {
@Operation(summary = "Add banner JHipster v3")
@ApiResponse(responseCode = "500", description = "An error occurred while adding banner")
@PostMapping("/jhipster-v3")
@GeneratorStep(id = "banner-jhipster-v3")
public void addBannerJHipsterV3(@RequestBody ProjectDTO projectDTO) {
Project project = ProjectDTO.toProject(projectDTO);
bannerApplicationService.addBannerJHipsterV3(project);
Expand Down
Loading

0 comments on commit fac6918

Please sign in to comment.