Repository: jhipster/jhipster-lite
Pull request #10053 (2024-06-12 17:16:13)

feat(custom-jhlite): generate slug shared kernel with feature and module enum slugs skeleton
See https://github.com/jhipster/jhipster-lite-extension/pull/1#discussion_r1636042962


Patch:
diff --git a/src/main/java/tech/jhipster/lite/generator/server/springboot/customjhlite/domain/CustomJHLiteModuleFactory.java b/src/main/java/tech/jhipster/lite/generator/server/springboot/customjhlite/domain/CustomJHLiteModuleFactory.java
index 1c3df2bfb..12c4cde69 100644
--- a/src/main/java/tech/jhipster/lite/generator/server/springboot/customjhlite/domain/CustomJHLiteModuleFactory.java
+++ b/src/main/java/tech/jhipster/lite/generator/server/springboot/customjhlite/domain/CustomJHLiteModuleFactory.java
@@ -16,6 +16,8 @@ import tech.jhipster.lite.shared.error.domain.Assert;
 public class CustomJHLiteModuleFactory {
 
   private static final JHipsterSource SOURCE = from("server/springboot/custom-jhlite");
+  private static final JHipsterSource MAIN_SOURCE = SOURCE.append("main");
+  private static final JHipsterSource TEST_SOURCE = SOURCE.append("test");
   private static final JHipsterSource CUCUMBER_SOURCE = from("server/springboot/cucumber");
 
   private static final String SRC_MAIN_JAVA = "src/main/java";
@@ -31,6 +33,8 @@ public class CustomJHLiteModuleFactory {
     Assert.notNull("properties", properties);
 
     String packagePath = properties.packagePath();
+    String baseName = properties.projectBaseName().capitalized();
+    JHipsterDestination slugDestination = toSrcMainJava().append(packagePath).append("shared").append("slug");
     JHipsterDestination cucumberDestination = toSrcTestJava().append(packagePath).append("cucumber");
 
     //@formatter:off
@@ -62,8 +66,11 @@ public class CustomJHLiteModuleFactory {
         .set(BEAN_DEFINITION_OVERRIDING_PROPERTY_KEY, propertyValue(true))
         .and()
       .files()
-        .add(SOURCE.template("CucumberTest.java"), cucumberDestination.append("CucumberTest.java"))
-        .add(SOURCE.template("CucumberConfiguration.java"), cucumberDestination.append("CucumberConfiguration.java"))
+        .add(MAIN_SOURCE.template("package-info.java"), slugDestination.append("package-info.java"))
+        .add(MAIN_SOURCE.template("FeatureSlug.java"), slugDestination.append("domain").append(baseName + "FeatureSlug.java"))
+        .add(MAIN_SOURCE.template("ModuleSlug.java"), slugDestination.append("domain").append(baseName + "ModuleSlug.java"))
+        .add(TEST_SOURCE.template("CucumberTest.java"), cucumberDestination.append("CucumberTest.java"))
+        .add(TEST_SOURCE.template("CucumberConfiguration.java"), cucumberDestination.append("CucumberConfiguration.java"))
         .add(CUCUMBER_SOURCE.append("rest").template("CucumberRestTemplate.java"), cucumberDestination.append("rest").append("CucumberRestTemplate.java"))
         .add(CUCUMBER_SOURCE.file("gitkeep"), to("src/test/features/.gitkeep"))
         .batch(SOURCE.append("tests-ci"),to("tests-ci"))
diff --git a/src/main/resources/generator/server/springboot/custom-jhlite/main/FeatureSlug.java.mustache b/src/main/resources/generator/server/springboot/custom-jhlite/main/FeatureSlug.java.mustache
new file mode 100644
index 000000000..532291149
--- /dev/null
+++ b/src/main/resources/generator/server/springboot/custom-jhlite/main/FeatureSlug.java.mustache
@@ -0,0 +1,23 @@
+package {{ packageName }}.shared.slug.domain;
+
+import tech.jhipster.lite.module.domain.resource.JHipsterFeatureSlugFactory;
+import {{packageName}}.shared.generation.domain.ExcludeFromGeneratedCodeCoverage;
+
+@ExcludeFromGeneratedCodeCoverage
+public enum {{ baseName }}FeatureSlug implements JHipsterFeatureSlugFactory {
+  // Add here the slugs of your features
+  // e.g.: MY_FEATURE("my-feature")
+  ;
+
+  private final String slug;
+
+  @SuppressWarnings("java:S1144")
+  {{ baseName }}FeatureSlug(String slug) {
+    this.slug = slug;
+  }
+
+  @Override
+  public String get() {
+    return slug;
+  }
+}
diff --git a/src/main/resources/generator/server/springboot/custom-jhlite/main/ModuleSlug.java.mustache b/src/main/resources/generator/server/springboot/custom-jhlite/main/ModuleSlug.java.mustache
new file mode 100644
index 000000000..b96ef8508
--- /dev/null
+++ b/src/main/resources/generator/server/springboot/custom-jhlite/main/ModuleSlug.java.mustache
@@ -0,0 +1,35 @@
+package {{ packageName }}.shared.slug.domain;
+
+import java.util.Map;
+import java.util.Optional;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+import tech.jhipster.lite.module.domain.resource.JHipsterModuleSlugFactory;
+import {{packageName}}.shared.generation.domain.ExcludeFromGeneratedCodeCoverage;
+
+@ExcludeFromGeneratedCodeCoverage
+public enum {{ baseName }}ModuleSlug implements JHipsterModuleSlugFactory {
+  // Add here the slugs of your modules
+  // e.g.: MY_MODULE("my-module")
+  ;
+
+  private static final Map<String, {{ baseName }}ModuleSlug> moduleSlugMap = Stream.of(values()).collect(
+    Collectors.toMap({{ baseName }}ModuleSlug::get, Function.identity())
+  );
+  private final String slug;
+
+  @SuppressWarnings("java:S1144")
+  {{ baseName }}ModuleSlug(String slug) {
+    this.slug = slug;
+  }
+
+  @Override
+  public String get() {
+    return slug;
+  }
+
+  public static Optional<{{ baseName }}ModuleSlug> fromString(String slug) {
+    return Optional.ofNullable(moduleSlugMap.get(slug));
+  }
+}
diff --git a/src/main/resources/generator/server/springboot/custom-jhlite/main/package-info.java.mustache b/src/main/resources/generator/server/springboot/custom-jhlite/main/package-info.java.mustache
new file mode 100644
index 000000000..8530e27e5
--- /dev/null
+++ b/src/main/resources/generator/server/springboot/custom-jhlite/main/package-info.java.mustache
@@ -0,0 +1,2 @@
+@{{ packageName }}.SharedKernel
+package {{ packageName }}.shared.slug;


Test patch:
diff --git a/src/main/resources/generator/server/springboot/custom-jhlite/CucumberConfiguration.java.mustache b/src/main/resources/generator/server/springboot/custom-jhlite/test/CucumberConfiguration.java.mustache
similarity index 100%
rename from src/main/resources/generator/server/springboot/custom-jhlite/CucumberConfiguration.java.mustache
rename to src/main/resources/generator/server/springboot/custom-jhlite/test/CucumberConfiguration.java.mustache
diff --git a/src/main/resources/generator/server/springboot/custom-jhlite/CucumberTest.java.mustache b/src/main/resources/generator/server/springboot/custom-jhlite/test/CucumberTest.java.mustache
similarity index 100%
rename from src/main/resources/generator/server/springboot/custom-jhlite/CucumberTest.java.mustache
rename to src/main/resources/generator/server/springboot/custom-jhlite/test/CucumberTest.java.mustache
diff --git a/src/test/java/tech/jhipster/lite/generator/server/springboot/customjhlite/domain/CustomJHLiteModuleFactoryTest.java b/src/test/java/tech/jhipster/lite/generator/server/springboot/customjhlite/domain/CustomJHLiteModuleFactoryTest.java
index 79e8f845c..5c009e33e 100644
--- a/src/test/java/tech/jhipster/lite/generator/server/springboot/customjhlite/domain/CustomJHLiteModuleFactoryTest.java
+++ b/src/test/java/tech/jhipster/lite/generator/server/springboot/customjhlite/domain/CustomJHLiteModuleFactoryTest.java
@@ -106,6 +106,7 @@ class CustomJHLiteModuleFactoryTest {
       .hasFile("src/test/java/com/jhipster/test/cucumber/CucumberConfiguration.java")
         .containing("import com.jhipster.test.MyappApp;")
         .and()
+      .hasPrefixedFiles("src/main/java/com/jhipster/test/shared/slug", "package-info.java", "domain/MyappFeatureSlug.java", "domain/MyappModuleSlug.java")
       .hasFiles("src/test/java/com/jhipster/test/cucumber/rest/CucumberRestTemplate.java")
       .hasFiles("src/test/features/.gitkeep");
     //@formatter:on