-
-
Notifications
You must be signed in to change notification settings - Fork 216
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #638 from Bolo89/add-history
Add history
- Loading branch information
Showing
43 changed files
with
652 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/main/java/tech/jhipster/lite/common/domain/JsonUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
.../tech/jhipster/lite/generator/history/application/GeneratorHistoryApplicationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/tech/jhipster/lite/generator/history/domain/GeneratorHistoryData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + '}'; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/tech/jhipster/lite/generator/history/domain/GeneratorHistoryRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
3 changes: 3 additions & 0 deletions
3
src/main/java/tech/jhipster/lite/generator/history/domain/GeneratorHistoryValue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) {} |
31 changes: 31 additions & 0 deletions
31
...h/jhipster/lite/generator/history/infrastructure/primary/GeneratorHistoryInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
...ster/lite/generator/history/infrastructure/secondary/GeneratorHistoryLocalRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
src/main/java/tech/jhipster/lite/generator/history/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
@tech.jhipster.lite.BusinessContext | ||
package tech.jhipster.lite.generator.history; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.