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

improve presets configuration #12007

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,8 @@ src/main/resources/config/application-perso.*
# Approvals testing
src/test/resources/**/*.received.txt
.approval_tests_temp/

######################
# Personal Presets Definitions
######################
src/main/resources/presets/personal/
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import tech.jhipster.lite.module.domain.javadependency.JavaDependenciesVersionsRepository;
import tech.jhipster.lite.module.domain.javadependency.ProjectJavaDependenciesRepository;
import tech.jhipster.lite.module.domain.landscape.JHipsterLandscape;
import tech.jhipster.lite.module.domain.preset.Preset;
import tech.jhipster.lite.module.domain.preset.Presets;
import tech.jhipster.lite.module.domain.resource.JHipsterModulesResources;

@Service
Expand Down Expand Up @@ -56,7 +56,7 @@ public JHipsterLandscape landscape() {
return modules.landscape();
}

public Collection<Preset> getPresets() {
public Presets getPresets() {
return preset.getPresets();
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package tech.jhipster.lite.module.domain;

import java.util.Collection;
import tech.jhipster.lite.module.domain.preset.Preset;
import tech.jhipster.lite.module.domain.preset.Presets;

public interface JHipsterPresetRepository {
Collection<Preset> getPresets();
Presets getPresets();
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package tech.jhipster.lite.module.domain;

import java.util.Collection;

public interface ProjectFiles {
String readString(String path);

byte[] readBytes(String path);

Collection<String> findPaths(String path);
}
23 changes: 23 additions & 0 deletions src/main/java/tech/jhipster/lite/module/domain/preset/Presets.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package tech.jhipster.lite.module.domain.preset;

import java.util.Collection;
import java.util.Comparator;
import tech.jhipster.lite.shared.error.domain.Assert;

public record Presets(Collection<Preset> presets) {
public Presets {
Assert.notNull("presets", presets);
}

public static Presets from(Collection<Preset> presets) {
return new Presets(sortByAlphabeticalOrder(presets));
}

private static Collection<Preset> sortByAlphabeticalOrder(Collection<Preset> presets) {
return presets.stream().sorted(alphabeticalOrder()).toList();
}

private static Comparator<Preset> alphabeticalOrder() {
return Comparator.comparing(preset -> preset.name().name());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,6 @@ public RestJHipsterModulePropertiesDefinition propertiesDefinition(@PathVariable
@Operation(summary = "Get presets configuration")
@GetMapping(path = "/presets", produces = MediaType.APPLICATION_JSON_VALUE)
ResponseEntity<RestPresets> getPresets() {
return ResponseEntity.ok(RestPresets.from(modules.getPresets()));
return ResponseEntity.ok(RestPresets.from(modules.getPresets().presets()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.function.Function;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;
import tech.jhipster.lite.module.domain.JHipsterPresetRepository;
import tech.jhipster.lite.module.domain.ProjectFiles;
import tech.jhipster.lite.module.domain.preset.Preset;
import tech.jhipster.lite.module.domain.preset.PresetName;
import tech.jhipster.lite.module.domain.preset.Presets;
import tech.jhipster.lite.shared.error.domain.GeneratorException;

@Repository
class FileSystemJHipsterPresetRepository implements JHipsterPresetRepository {

private static final String PRESET_FOLDER = "/";
private static final String ROOT_FOLDER = "/";

private final ObjectMapper json;
private final ProjectFiles projectFiles;
Expand All @@ -23,23 +26,39 @@ class FileSystemJHipsterPresetRepository implements JHipsterPresetRepository {
public FileSystemJHipsterPresetRepository(
ObjectMapper json,
ProjectFiles projectFiles,
@Value("${jhlite.preset-file.name:preset.json}") String presetFileName
@Value("${jhlite.preset.folder:presets}") String presetFolderName
) {
this.json = json;
this.projectFiles = projectFiles;
this.presetName = PresetName.from(presetFileName);
this.presetName = PresetName.from(presetFolderName);
}

@Override
public Collection<Preset> getPresets() {
try {
return json.readValue(projectFiles.readBytes(presetFilePath()), PersistedPresets.class).toDomain();
} catch (IOException e) {
throw GeneratorException.technicalError("Can't read presets: " + e.getMessage(), e);
}
public Presets getPresets() {
return Presets.from(readAllPresets());
}

private String presetFilePath() {
return PRESET_FOLDER + presetName.name();
private List<Preset> readAllPresets() {
return projectFiles
.findPaths(presetFolderPath())
.stream()
.map(readPresetFile())
.map(PersistedPresets::toDomain)
.flatMap(Collection::stream)
.toList();
}

private Function<String, PersistedPresets> readPresetFile() {
return path -> {
try {
return json.readValue(projectFiles.readBytes(path), PersistedPresets.class);
} catch (IOException e) {
throw GeneratorException.technicalError("Can't read preset file at " + path + ": " + e.getMessage(), e);
}
};
}

private String presetFolderPath() {
return ROOT_FOLDER + presetName.name();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@

import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collection;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Stream;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Service;
import tech.jhipster.lite.module.domain.ProjectFiles;
Expand All @@ -29,6 +37,16 @@ public String readString(String path) {
}
}

private InputStream getInputStream(String path) {
return FileSystemProjectFiles.class.getResourceAsStream(path.replace("\\", SLASH));
}

private void assertFileExist(String path, InputStream input) {
if (input == null) {
throw GeneratorException.technicalError("Can't find file: " + path);
}
}

@ExcludeFromGeneratedCodeCoverage(reason = "The error handling is an hard to test implementation detail")
private static String toString(InputStream input) {
try {
Expand All @@ -52,22 +70,68 @@ public byte[] readBytes(String path) {
}
}

private void assertFileExist(String path, InputStream input) {
if (input == null) {
throw GeneratorException.technicalError("Can't find file: " + path);
@ExcludeFromGeneratedCodeCoverage(reason = "The error handling is an hard to test implementation detail")
private static byte[] toByteArray(InputStream input) {
try {
return IOUtils.toByteArray(input);
} catch (IOException e) {
throw GeneratorException.technicalError("Error reading file: " + e.getMessage(), e);
}
}

private InputStream getInputStream(String path) {
return FileSystemProjectFiles.class.getResourceAsStream(path.replace("\\", SLASH));
@Override
public Collection<String> findPaths(String rootFolder) {
Assert.notBlank("rootFolder", rootFolder);

Path rootPath = rootPathFrom(rootFolder);

assertFolder(rootPath);

return buildRelativePath(rootFolder, rootPath);
}

@ExcludeFromGeneratedCodeCoverage(reason = "The error handling is an hard to test implementation detail")
private static byte[] toByteArray(InputStream input) {
private Path rootPathFrom(String resourcePath) {
URL folderUrl = getURL(resourcePath);
assertFolderExist(resourcePath, folderUrl);

try {
return IOUtils.toByteArray(input);
return Path.of(folderUrl.toURI());
} catch (URISyntaxException e) {
throw GeneratorException.technicalError("Unable to read folder %s: %s".formatted(resourcePath, e.getMessage()), e);
}
}

private void assertFolderExist(String path, URL url) {
if (url == null) {
throw GeneratorException.technicalError("Can't find folder: " + path);
}
}

private void assertFolder(Path rootPath) {
if (!Files.isDirectory(rootPath)) {
throw GeneratorException.technicalError("Path %s is not a folder".formatted(rootPath));
}
}

@ExcludeFromGeneratedCodeCoverage(reason = "The error handling is an hard to test implementation detail")
private static List<String> buildRelativePath(String rootFolder, Path rootPath) {
try (Stream<Path> walkStream = getWalkStream(rootPath)) {
return walkStream.filter(Files::isRegularFile).map(relativePathFrom(rootFolder, rootPath)).toList();
} catch (IOException e) {
throw GeneratorException.technicalError("Error reading file: " + e.getMessage(), e);
throw GeneratorException.technicalError("Error closing %s: %s".formatted(rootFolder, e.getMessage()), e);
}
}

private static Stream<Path> getWalkStream(Path folder) throws IOException {
return Files.walk(folder);
}

private static Function<Path, String> relativePathFrom(String rootFolder, Path rootPath) {
return path -> Path.of(rootFolder).resolve(rootPath.relativize(path)).toString().replace("\\", SLASH);
}

private URL getURL(String path) {
return FileSystemProjectFiles.class.getResource(path.replace("\\", SLASH));
}
}
4 changes: 2 additions & 2 deletions src/main/resources/config/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ jhlite:
hidden-resources:
slugs:
- svelte-core
preset-file:
name: preset.json
preset:
folder: presets

server:
port: 7471
Expand Down
Loading