Skip to content

Commit

Permalink
Merge pull request #8327 from atomfrede/thymeleaf-htmx-template
Browse files Browse the repository at this point in the history
add webjars thymeleaf template modules
  • Loading branch information
murdos authored Dec 30, 2023
2 parents 3effa48 + 06cb484 commit 0f4b9c0
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,12 @@ public ThymeleafTemplateModuleApplicationService() {
public JHipsterModule buildThymeleafTemplateModule(JHipsterModuleProperties properties) {
return thymeleafTemplateFactory.buildModule(properties);
}

public JHipsterModule buildThymeleafHtmxWebjarsModule(JHipsterModuleProperties properties) {
return thymeleafTemplateFactory.buildHtmxWebjarsModule(properties);
}

public JHipsterModule buildThymeleafAlpinejsWebjarsModule(JHipsterModuleProperties properties) {
return thymeleafTemplateFactory.buildAlpineWebjarsModule(properties);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,68 @@
import static tech.jhipster.lite.module.domain.JHipsterModule.*;

import tech.jhipster.lite.module.domain.JHipsterModule;
import tech.jhipster.lite.module.domain.JHipsterProjectFilePath;
import tech.jhipster.lite.module.domain.file.JHipsterDestination;
import tech.jhipster.lite.module.domain.file.JHipsterSource;
import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties;
import tech.jhipster.lite.shared.error.domain.Assert;

public class ThymeleafTemplateModuleFactory {

private static final String PROPERTIES = "properties";
private static final JHipsterSource SOURCE = from("server/springboot/thymeleaf/template/src/main/resources");
private static final String TEMPLATES = "templates";
private static final String TEMPLATES_LAYOUT = "templates/layout";
private static final String STATIC_CSS = "static/css";
private static final String MAIN_HTML = "main.html";
private static final JHipsterProjectFilePath MAIN_RESOURCES_PATH = path("src/main/resources");
private static final JHipsterDestination DESTINATION = to(MAIN_RESOURCES_PATH.get());

private static final JHipsterDestination DESTINATION = to("src/main/resources");
private static final String MAIN_SCRIPT_NEEDLE = "<!-- jhipster-needle-thymeleaf-main-script -->";

public JHipsterModule buildModule(JHipsterModuleProperties properties) {
Assert.notNull("properties", properties);
Assert.notNull(PROPERTIES, properties);

//@formatter:off
return moduleBuilder(properties)
.files()
.add(SOURCE.append(TEMPLATES).template("index.html"), toSrcMainResources().append(TEMPLATES).append("index.html"))
.add(SOURCE.append(TEMPLATES_LAYOUT).template("main.html"), toSrcMainResources().append(TEMPLATES_LAYOUT).append("main.html"))
.add(SOURCE.append(TEMPLATES_LAYOUT).template(MAIN_HTML), toSrcMainResources().append(TEMPLATES_LAYOUT).append(MAIN_HTML))
.add(SOURCE.append(STATIC_CSS).template("application.css"), DESTINATION.append(STATIC_CSS).append("application.css"))
.and()
.build();
//@formatter:on
}

public JHipsterModule buildHtmxWebjarsModule(JHipsterModuleProperties properties) {
Assert.notNull(PROPERTIES, properties);

//@formatter:off
return moduleBuilder(properties)
.mandatoryReplacements()
.in(MAIN_RESOURCES_PATH.append(TEMPLATES_LAYOUT).append(MAIN_HTML))
.add(lineBeforeText(MAIN_SCRIPT_NEEDLE), webjarsScriptTag("htmx.org/dist/htmx.min.js"))
.and()
.and()
.build();
//@formatter:on
}

public JHipsterModule buildAlpineWebjarsModule(JHipsterModuleProperties properties) {
Assert.notNull(PROPERTIES, properties);

//@formatter:off
return moduleBuilder(properties)
.mandatoryReplacements()
.in(MAIN_RESOURCES_PATH.append(TEMPLATES_LAYOUT).append(MAIN_HTML))
.add(lineBeforeText(MAIN_SCRIPT_NEEDLE), webjarsScriptTag("alpinejs/dist/cdn.js"))
.and()
.and()
.build();
//@formatter:on
}

private String webjarsScriptTag(String webjarsLocation) {
return "<script type=\"text/javascript\" th:src=\"@{/webjars/%s}\"></script>".formatted(webjarsLocation);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package tech.jhipster.lite.generator.server.springboot.thymeleaf.template.infrastructure.primary;

import static org.apache.commons.lang3.StringUtils.*;
import static tech.jhipster.lite.generator.slug.domain.JHLiteModuleSlug.*;

import org.springframework.context.annotation.Bean;
Expand All @@ -12,6 +13,12 @@
@Configuration
class ThymeleafTemplateModuleConfiguration {

private static final String TAG_SERVER = "server";
private static final String TAG_SPRING = "spring";
private static final String TAG_BOOT = "spring-boot";
private static final String TAG_THYMELEAF = "thymeleaf";
private static final String TAG_WEBJAR = "webjar";

@Bean
public JHipsterModuleResource thymeleafTemplateModule(ThymeleafTemplateModuleApplicationService thymeleafTemplate) {
return JHipsterModuleResource
Expand All @@ -20,9 +27,37 @@ public JHipsterModuleResource thymeleafTemplateModule(ThymeleafTemplateModuleApp
.propertiesDefinition(
JHipsterModulePropertiesDefinition.builder().addBasePackage().addProjectBaseName().addConfigurationFormat().build()
)
.apiDoc("Thymeleaf", "Add thymeleaf skeleton layout files to the project")
.apiDoc(capitalize(TAG_THYMELEAF), "Add thymeleaf skeleton layout files to the project")
.organization(JHipsterModuleOrganization.builder().addDependency(SPRING_BOOT_THYMELEAF).build())
.tags("server", "spring", "spring-boot", "thymeleaf")
.tags(TAG_SERVER, TAG_SPRING, TAG_BOOT, TAG_THYMELEAF)
.factory(thymeleafTemplate::buildThymeleafTemplateModule);
}

@Bean
public JHipsterModuleResource thymeleafTemplateHtmxWebjarsModule(ThymeleafTemplateModuleApplicationService thymeleafTemplate) {
return JHipsterModuleResource
.builder()
.slug(THYMELEAF_TEMPLATE_HTMX_WEBJAR)
.propertiesDefinition(
JHipsterModulePropertiesDefinition.builder().addBasePackage().addProjectBaseName().addConfigurationFormat().build()
)
.apiDoc(capitalize(TAG_THYMELEAF), "Add htmx webjars scripts to thymeleaf layout")
.organization(JHipsterModuleOrganization.builder().addDependency(HTMX_WEBJARS).addDependency(THYMELEAF_TEMPLATE).build())
.tags(TAG_SERVER, TAG_SPRING, TAG_BOOT, TAG_THYMELEAF, TAG_WEBJAR)
.factory(thymeleafTemplate::buildThymeleafHtmxWebjarsModule);
}

@Bean
public JHipsterModuleResource thymeleafTemplateAlpineWebjarsModule(ThymeleafTemplateModuleApplicationService thymeleafTemplate) {
return JHipsterModuleResource
.builder()
.slug(THYMELEAF_TEMPLATE_ALPINEJS_WEBJAR)
.propertiesDefinition(
JHipsterModulePropertiesDefinition.builder().addBasePackage().addProjectBaseName().addConfigurationFormat().build()
)
.apiDoc(capitalize(TAG_THYMELEAF), "Add alpine webjars scripts to thymeleaf layout")
.organization(JHipsterModuleOrganization.builder().addDependency(ALPINE_JS_WEBJARS).addDependency(THYMELEAF_TEMPLATE).build())
.tags(TAG_SERVER, TAG_SPRING, TAG_BOOT, TAG_THYMELEAF, TAG_WEBJAR)
.factory(thymeleafTemplate::buildThymeleafAlpinejsWebjarsModule);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ public enum JHLiteModuleSlug implements JHipsterModuleSlugFactory {
SPRING_BOOT_MVC_EMPTY("spring-boot-mvc-empty"),
SPRING_BOOT_THYMELEAF("spring-boot-thymeleaf"),
THYMELEAF_TEMPLATE("thymeleaf-template"),
THYMELEAF_TEMPLATE_HTMX_WEBJAR("thymeleaf-template-htmx-webjars"),
THYMELEAF_TEMPLATE_ALPINEJS_WEBJAR("thymeleaf-template-alpinejs-webjars"),
SPRING_BOOT_TOMCAT("spring-boot-tomcat"),
SPRING_BOOT_UNDERTOW("spring-boot-undertow"),
SPRING_BOOT_WEBFLUX_EMPTY("spring-boot-webflux-empty"),
Expand Down
16 changes: 16 additions & 0 deletions src/test/features/server/springboot/springboot-thymeleaf.feature
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,19 @@ Feature: Spring Boot Thymeleaf module
| thymeleaf-template |
Then I should have files in "src/main/resources/templates"
| index.html |

Scenario: Should apply Thymeleaf Template htmx, alpinejs webjars
When I apply modules to default project
| maven-java |
| spring-boot |
| spring-boot-thymeleaf |
| thymeleaf-template |
| webjars-locator |
| htmx-webjars |
| alpinejs-webjars |
| thymeleaf-template-htmx-webjars |
| thymeleaf-template-alpinejs-webjars |
Then I should have files in "src/main/resources/templates/layout"
| main.html |
And I should have "@{/webjars/htmx.org/dist/htmx.min.js}" in "src/main/resources/templates/layout/main.html"
And I should have "@{/webjars/alpinejs/dist/cdn.js}" in "src/main/resources/templates/layout/main.html"
4 changes: 3 additions & 1 deletion tests-ci/generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,9 @@ elif [[ $application == 'thymeleafapp' ]]; then
"spring-boot-thymeleaf" \
"thymeleaf-template" \
"webjars-locator" \
"htmx-webjars"
"htmx-webjars" \
"thymeleaf-template" \
"thymeleaf-template-htmx-webjars"

else
echo "*** Unknown configuration..."
Expand Down

0 comments on commit 0f4b9c0

Please sign in to comment.