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

Add module list to the JModuleProperties #9390

Open
wants to merge 1 commit 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
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import java.time.Instant;
import java.time.format.DateTimeParseException;
import java.util.Collection;
import java.util.Map;
import tech.jhipster.lite.module.domain.Indentation;
import tech.jhipster.lite.module.domain.JHipsterModuleSlug;
import tech.jhipster.lite.module.domain.javadependency.Version;
import tech.jhipster.lite.shared.error.domain.Assert;

Expand All @@ -21,6 +23,8 @@ public class JHipsterModuleProperties {
private final JHipsterProjectFolder projectFolder;
private final boolean commitModule;
private final JHipsterModuleParameters parameters;
private final Collection<JHipsterModuleSlug> newModules;
private final Collection<JHipsterModuleSlug> alreadyApplied;

private final Indentation indentation;
private final JHipsterBasePackage basePackage;
Expand All @@ -30,10 +34,18 @@ public class JHipsterModuleProperties {
private final SpringConfigurationFormat springConfigurationFormat;
private final Version javaVersion = new Version("21");

public JHipsterModuleProperties(String projectFolder, boolean commitModule, Map<String, Object> parameters) {
public JHipsterModuleProperties(
String projectFolder,
boolean commitModule,
Map<String, Object> parameters,
Collection<JHipsterModuleSlug> newModules,
Collection<JHipsterModuleSlug> alreadyApplied
) {
this.projectFolder = new JHipsterProjectFolder(projectFolder);
this.commitModule = commitModule;
this.parameters = new JHipsterModuleParameters(parameters);
this.newModules = newModules;
this.alreadyApplied = alreadyApplied;

indentation = Indentation.from(this.parameters.getOrDefault(INDENTATION_PARAMETER, null, Integer.class));
basePackage = new JHipsterBasePackage(this.parameters.getOrDefault(BASE_PACKAGE_PARAMETER, null, String.class));
Expand Down Expand Up @@ -61,6 +73,18 @@ public String getString(String key) {
return parameters.get(key, String.class);
}

public Collection<JHipsterModuleSlug> getNewModules() {
return newModules;
}

public Collection<JHipsterModuleSlug> getAlreadyAppliedModules() {
return alreadyApplied;
}

public boolean isAppliedOrBeingApplied(JHipsterModuleSlug name) {
return alreadyApplied.contains(name) || newModules.contains(name);
}

public String getOrDefaultString(String key, String defaultValue) {
Assert.notBlank("defaultValue", defaultValue);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.swagger.v3.oas.annotations.Hidden;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -15,7 +16,9 @@
import tech.jhipster.lite.module.domain.JHipsterModuleSlug;
import tech.jhipster.lite.module.domain.JHipsterModuleToApply;
import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties;
import tech.jhipster.lite.module.domain.properties.JHipsterProjectFolder;
import tech.jhipster.lite.module.domain.resource.JHipsterModuleResource;
import tech.jhipster.lite.project.application.ProjectsApplicationService;
import tech.jhipster.lite.shared.projectfolder.domain.ProjectFolder;

@RestController
Expand All @@ -25,13 +28,19 @@ class ModulesResource {

private final JHipsterModulesApplicationService modules;
private final ProjectFolder projectFolder;
private final ProjectsApplicationService projectsApplicationService;

private final RestJHipsterModules modulesList;
private final RestJHipsterLandscape modulesLandscape;

public ModulesResource(JHipsterModulesApplicationService modules, ProjectFolder projectFolder) {
public ModulesResource(
JHipsterModulesApplicationService modules,
ProjectFolder projectFolder,
ProjectsApplicationService projectsApplicationService
) {
this.modules = modules;
this.projectFolder = projectFolder;
this.projectsApplicationService = projectsApplicationService;

modulesList = RestJHipsterModules.from(modules.resources());
modulesLandscape = RestJHipsterLandscape.from(modules.landscape());
Expand All @@ -52,14 +61,19 @@ public ResponseEntity<RestJHipsterLandscape> modulesLandscape() {
@PostMapping("apply-patches")
@Operation(summary = "Apply multiple modules patches")
public void applyPatches(@RequestBody @Validated RestJHipsterModulesToApply modulesToApply) {
modules.apply(modulesToApply.toDomain(projectFolder));
var folder = create(modulesToApply.getProperties().getProjectFolder());
var alreadyApplied = getAlreadyAppliedModules(folder);
modules.apply(modulesToApply.toDomain(projectFolder, alreadyApplied));
}

@Hidden
@PostMapping("modules/{slug}/apply-patch")
public void applyPatch(@RequestBody @Validated RestJHipsterModuleProperties restProperties, @PathVariable("slug") String slug) {
JHipsterModuleProperties properties = restProperties.toDomain(projectFolder);
modules.apply(new JHipsterModuleToApply(new JHipsterModuleSlug(slug), properties));
var module = new JHipsterModuleSlug(slug);
var folder = create(restProperties.getProjectFolder());
var alreadyApplied = getAlreadyAppliedModules(folder);
JHipsterModuleProperties properties = restProperties.toDomain(projectFolder, List.of(module), alreadyApplied);
modules.apply(new JHipsterModuleToApply(module, properties));
}

@Hidden
Expand All @@ -68,4 +82,16 @@ public RestJHipsterModulePropertiesDefinition propertiesDefinition(@PathVariable
JHipsterModuleResource module = modules.resources().get(new JHipsterModuleSlug(slug));
return RestJHipsterModulePropertiesDefinition.from(module.propertiesDefinition());
}

private List<JHipsterModuleSlug> getAlreadyAppliedModules(JHipsterProjectFolder userSpecifiedProjectFolder) {
var moduleNames = projectsApplicationService.getAppliedModules(userSpecifiedProjectFolder);
return moduleNames.stream().map(JHipsterModuleSlug::new).toList();
}

private JHipsterProjectFolder create(String folderPath) {
if (projectFolder.isInvalid(folderPath)) {
throw new InvalidProjectFolderException();
}
return new JHipsterProjectFolder(folderPath);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.media.Schema.RequiredMode;
import java.util.Collection;
import java.util.Map;
import tech.jhipster.lite.module.domain.JHipsterModuleSlug;
import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties;
import tech.jhipster.lite.shared.error.domain.Assert;
import tech.jhipster.lite.shared.projectfolder.domain.ProjectFolder;
Expand All @@ -24,12 +26,18 @@ class RestJHipsterModuleProperties {
this.parameters = parameters;
}

public JHipsterModuleProperties toDomain(ProjectFolder jHipsterProjectFolderFactory) {
public JHipsterModuleProperties toDomain(
ProjectFolder jHipsterProjectFolderFactory,
Collection<JHipsterModuleSlug> newModules,
Collection<JHipsterModuleSlug> alreadyApplied
) {
Assert.notNull("jHipsterProjectFolderFactory", jHipsterProjectFolderFactory);
Assert.notNull("newModules", newModules);
Assert.notNull("alreadyApplied", alreadyApplied);

assertValidProjectFolder(jHipsterProjectFolderFactory);

return new JHipsterModuleProperties(getProjectFolder(), isCommit(), getParameters());
return new JHipsterModuleProperties(getProjectFolder(), isCommit(), getParameters(), newModules, alreadyApplied);
}

private void assertValidProjectFolder(ProjectFolder jHipsterProjectFolderFactory) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ class RestJHipsterModulesToApply {
this.properties = properties;
}

public JHipsterModulesToApply toDomain(ProjectFolder projectFolder) {
return new JHipsterModulesToApply(getModules().stream().map(JHipsterModuleSlug::new).toList(), properties.toDomain(projectFolder));
public JHipsterModulesToApply toDomain(ProjectFolder projectFolder, Collection<JHipsterModuleSlug> alreadyApplied) {
var newModules = getModules().stream().map(JHipsterModuleSlug::new).toList();
return new JHipsterModulesToApply(newModules, properties.toDomain(projectFolder, newModules, alreadyApplied));
}

@NotEmpty
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ private void addFileToProject(JHipsterSource source, JHipsterDestination destina
}

private JHipsterModuleContext context() {
JHipsterModuleProperties properties = new JHipsterModuleProperties(projectFolder.get(), false, null);
JHipsterModuleProperties properties = new JHipsterModuleProperties(projectFolder.get(), false, null, List.of(), List.of());
return JHipsterModuleContext.builder(moduleBuilder(properties)).build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package tech.jhipster.lite.project.application;

import java.util.Collection;
import org.springframework.stereotype.Service;
import tech.jhipster.lite.module.domain.properties.JHipsterProjectFolder;
import tech.jhipster.lite.project.domain.ProjectPath;
import tech.jhipster.lite.project.domain.ProjectsRepository;
import tech.jhipster.lite.project.domain.download.Project;
Expand Down Expand Up @@ -39,4 +41,8 @@ public void append(ProjectActionToAppend actionToAppend) {
public ProjectHistory getHistory(ProjectPath path) {
return projects.getHistory(path);
}

public Collection<String> getAppliedModules(JHipsterProjectFolder projectFolder) {
return projects.getHistory(new ProjectPath(projectFolder.folder())).getAppliedModules();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,8 @@ public Collection<ProjectAction> actions() {
public ModuleParameters latestProperties() {
return actions.stream().map(ProjectAction::parameters).reduce(ModuleParameters.EMPTY, ModuleParameters::merge);
}

public Collection<String> getAppliedModules() {
return actions.stream().map(ProjectAction::module).map(ModuleSlug::slug).toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import static tech.jhipster.lite.module.domain.JHipsterModule.*;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tech.jhipster.lite.TestFileUtils;
Expand Down Expand Up @@ -254,7 +256,7 @@ public static JHipsterModuleBuilder emptyModuleBuilder() {
}

public static JHipsterModuleProperties testModuleProperties() {
return new JHipsterModuleProperties(TestFileUtils.tmpDirForTest(), true, null);
return new JHipsterModuleProperties(TestFileUtils.tmpDirForTest(), true, null, List.of(), List.of());
}

public static JavaDependenciesVersions currentJavaDependenciesVersion() {
Expand Down Expand Up @@ -298,7 +300,9 @@ public static JHipsterModuleProperties allProperties() {
true,
"optionalBoolean",
true
)
),
List.of(),
List.of()
);
}

Expand Down Expand Up @@ -422,13 +426,17 @@ public static MavenPlugin asciidoctorPlugin() {
}

public static JHipsterModulesToApply modulesToApply() {
var maven = moduleSlug("maven-java");
var init = moduleSlug("init");
return new JHipsterModulesToApply(
List.of(moduleSlug("maven-java"), moduleSlug("init")),
List.of(maven, init),
propertiesBuilder("/dummy")
.projectName("Chips Project")
.basePackage("tech.jhipster.chips")
.put("baseName", "chips")
.put("serverPort", 8080)
.module(maven)
.module(init)
.build()
);
}
Expand All @@ -454,6 +462,8 @@ public static final class JHipsterModulePropertiesBuilder {
private boolean commitModules = false;
private final String projectFolder;
private final Map<String, Object> properties = new HashMap<>();
private final Set<JHipsterModuleSlug> newModules = new HashSet<>();
private final Set<JHipsterModuleSlug> alreadyApplied = new HashSet<>();

private JHipsterModulePropertiesBuilder(String projectFolder) {
this.projectFolder = projectFolder;
Expand Down Expand Up @@ -483,14 +493,26 @@ public JHipsterModulePropertiesBuilder projectName(String projectName) {
return this;
}

public JHipsterModulePropertiesBuilder module(JHipsterModuleSlug module) {
newModules.add(module);

return this;
}

public JHipsterModulePropertiesBuilder alreadyApplied(JHipsterModuleSlug module) {
alreadyApplied.add(module);

return this;
}

public JHipsterModulePropertiesBuilder put(String key, Object value) {
properties.put(key, value);

return this;
}

public JHipsterModuleProperties build() {
return new JHipsterModuleProperties(projectFolder, commitModules, properties);
return new JHipsterModuleProperties(projectFolder, commitModules, properties, newModules, alreadyApplied);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
Expand Down Expand Up @@ -193,7 +194,9 @@ private JHipsterModuleProperties properties() {
return new JHipsterModuleProperties(
"/tmp/folder",
false,
Map.of("string", "value", "boolean", true, "integer", 42, "blank", " ", "instant", today.toString())
Map.of("string", "value", "boolean", true, "integer", 42, "blank", " ", "instant", today.toString()),
List.of(),
List.of()
);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package tech.jhipster.lite.module.infrastructure.primary;

import static org.assertj.core.api.Assertions.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static tech.jhipster.lite.module.domain.JHipsterModulesFixture.*;
import static tech.jhipster.lite.module.domain.JHipsterModulesFixture.allProperties;

import java.util.List;
import org.junit.jupiter.api.Test;
import tech.jhipster.lite.JsonHelper;
import tech.jhipster.lite.UnitTest;
Expand All @@ -20,13 +22,15 @@ void shouldNotConvertToPropertiesWithInvalidProjectFolder() {
when(jHipsterProjectFolderFactory.isInvalid("/test")).thenReturn(true);

assertThatThrownBy(
() -> JsonHelper.readFromJson(json(), RestJHipsterModuleProperties.class).toDomain(jHipsterProjectFolderFactory)
() -> JsonHelper.readFromJson(json(), RestJHipsterModuleProperties.class).toDomain(jHipsterProjectFolderFactory, List.of(), List.of())
).isExactlyInstanceOf(InvalidProjectFolderException.class);
}

@Test
void shouldConvertToProperties() {
assertThat(JsonHelper.readFromJson(json(), RestJHipsterModuleProperties.class).toDomain(jHipsterProjectFolderFactory))
assertThat(
JsonHelper.readFromJson(json(), RestJHipsterModuleProperties.class).toDomain(jHipsterProjectFolderFactory, List.of(), List.of())
)
.usingRecursiveComparison()
.isEqualTo(allProperties());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class RestJHipsterModulesToApplyTest {

@Test
void shouldDeserializeFromJson() {
assertThat(JsonHelper.readFromJson(json(), RestJHipsterModulesToApply.class).toDomain(projectFolder))
assertThat(JsonHelper.readFromJson(json(), RestJHipsterModulesToApply.class).toDomain(projectFolder, List.of()))
.usingRecursiveComparison()
.isEqualTo(JHipsterModulesFixture.modulesToApply());
}
Expand Down
Loading