From 5ce561d0081d730b51ffe8e468dda2abdba1b02b Mon Sep 17 00:00:00 2001 From: Christian Klinger Date: Tue, 4 Jul 2023 14:19:52 +0200 Subject: [PATCH 1/3] #243: add jte-model options 'includePattern' and 'excludePattern' --- jte-models/README.md | 11 +++++++++++ .../gg/jte/models/generator/ModelConfig.java | 17 +++++++++++++++++ .../gg/jte/models/generator/ModelExtension.java | 13 ++++++++++++- 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/jte-models/README.md b/jte-models/README.md index a8a4ebe5..c9286072 100644 --- a/jte-models/README.md +++ b/jte-models/README.md @@ -26,6 +26,13 @@ To use jte-models, set up your build script to include one of these: @foo.bar.MyAnnotation --> + @@ -70,6 +77,8 @@ jte { jteExtension('gg.jte.models.generator.ModelExtension') { interfaceAnnotation = '@foo.bar.MyAnnotation' implementationAnnotation = '@foo.bar.MyAnnotation' + includePattern = '\.pages\..*' + excludePattern = '\.components\..*' } */ } @@ -99,6 +108,8 @@ jte { jteExtension("gg.jte.models.generator.ModelExtension") { property("interfaceAnnotation", "@foo.bar.MyAnnotation") property("implementationAnnotation", "@foo.bar.MyAnnotation") + property("includePattern", "\.pages\..*") + property("excludePattern", '\.components\..*") } */ } diff --git a/jte-models/src/main/java/gg/jte/models/generator/ModelConfig.java b/jte-models/src/main/java/gg/jte/models/generator/ModelConfig.java index a14abf71..c4147646 100644 --- a/jte-models/src/main/java/gg/jte/models/generator/ModelConfig.java +++ b/jte-models/src/main/java/gg/jte/models/generator/ModelConfig.java @@ -1,6 +1,7 @@ package gg.jte.models.generator; import java.util.Map; +import java.util.regex.Pattern; public class ModelConfig { private final Map map; @@ -16,4 +17,20 @@ public String interfaceAnnotation() { public String implementationAnnotation() { return map.getOrDefault("implementationAnnotation", ""); } + + public Pattern includePattern() { + String includePattern = map.get("includePattern"); + if (includePattern == null) { + return null; + } + return Pattern.compile(includePattern); + } + + public Pattern excludePattern() { + String excludePattern = map.get("excludePattern"); + if (excludePattern == null) { + return null; + } + return Pattern.compile(excludePattern); + } } diff --git a/jte-models/src/main/java/gg/jte/models/generator/ModelExtension.java b/jte-models/src/main/java/gg/jte/models/generator/ModelExtension.java index cf136b71..c9032a57 100644 --- a/jte-models/src/main/java/gg/jte/models/generator/ModelExtension.java +++ b/jte-models/src/main/java/gg/jte/models/generator/ModelExtension.java @@ -8,8 +8,10 @@ import java.nio.file.Path; import java.util.Collection; +import java.util.List; import java.util.Map; import java.util.Set; +import java.util.regex.Pattern; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -31,11 +33,20 @@ public JteExtension init(Map value) { @Override public Collection generate(JteConfig config, Set templateDescriptions) { TemplateEngine engine = TemplateEngine.createPrecompiled(ContentType.Plain); + + Pattern includePattern = modelConfig.includePattern(); + Pattern excludePattern = modelConfig.excludePattern(); + + var templateDescriptionsFiltered = templateDescriptions.stream() // + .filter(x -> includePattern == null || includePattern.matcher(x.fullyQualifiedClassName()).matches()) // + .filter(x -> excludePattern == null || !excludePattern.matcher(x.fullyQualifiedClassName()).matches()) // + .collect(Collectors.toSet()); + return Stream.of( new ModelGenerator(engine, "interfacetemplates", "Templates", "Templates"), new ModelGenerator(engine, "statictemplates", "StaticTemplates", "Templates"), new ModelGenerator(engine, "dynamictemplates", "DynamicTemplates", "Templates") - ).map(g -> g.generate(config, templateDescriptions, modelConfig)) + ).map(g -> g.generate(config, templateDescriptionsFiltered, modelConfig)) .collect(Collectors.toList()); } } From 4e1c121e5cd6d9ef17a92879a9973aeaa777309f Mon Sep 17 00:00:00 2001 From: Christian Klinger Date: Wed, 5 Jul 2023 09:23:27 +0200 Subject: [PATCH 2/3] #243: configure integration test with includePattern,excludePattern --- test/jte-runtime-cp-test-models/pom.xml | 2 ++ .../src/main/jte/ModelExampleExclude.jte | 0 .../src/test/java/gg/jte/TemplateEngineTest.java | 7 +++++++ 3 files changed, 9 insertions(+) create mode 100644 test/jte-runtime-cp-test-models/src/main/jte/ModelExampleExclude.jte diff --git a/test/jte-runtime-cp-test-models/pom.xml b/test/jte-runtime-cp-test-models/pom.xml index e4685407..bca18020 100644 --- a/test/jte-runtime-cp-test-models/pom.xml +++ b/test/jte-runtime-cp-test-models/pom.xml @@ -89,6 +89,8 @@ gg.jte.models.generator.ModelExtension @test.Dummy + .* + .*Exclude.* diff --git a/test/jte-runtime-cp-test-models/src/main/jte/ModelExampleExclude.jte b/test/jte-runtime-cp-test-models/src/main/jte/ModelExampleExclude.jte new file mode 100644 index 00000000..e69de29b diff --git a/test/jte-runtime-cp-test-models/src/test/java/gg/jte/TemplateEngineTest.java b/test/jte-runtime-cp-test-models/src/test/java/gg/jte/TemplateEngineTest.java index 351d08f2..6644539d 100644 --- a/test/jte-runtime-cp-test-models/src/test/java/gg/jte/TemplateEngineTest.java +++ b/test/jte-runtime-cp-test-models/src/test/java/gg/jte/TemplateEngineTest.java @@ -14,6 +14,7 @@ import test.Model; import java.lang.annotation.Annotation; +import java.util.Arrays; import java.util.stream.Stream; import static org.assertj.core.api.Assertions.assertThat; @@ -65,6 +66,11 @@ void unusedTag(gg.jte.generated.precompiled.Templates templates) { String output = templates.tagUnused("One", "Two").render(); assertThat(output).isEqualTo("One is One, two is Two."); } + @ParameterizedTest + @MethodSource("templates") + void excludedTemplates(gg.jte.generated.precompiled.Templates templates){ + assertThat(templates.getClass().getMethods()).noneMatch(m -> m.getName().contains("Exclude")); + } @ParameterizedTest @MethodSource("templates") @@ -80,6 +86,7 @@ void nestedContent(Templates templates) { assertThat(output).containsIgnoringWhitespaces("Header", "Hello World", "Footer"); } + @ParameterizedTest @MethodSource("templates") void hasAnnotation(Templates templates) { From e6775938947d9707136aae2cd3b422b4baae3c50 Mon Sep 17 00:00:00 2001 From: Christian Klinger Date: Wed, 5 Jul 2023 09:38:10 +0200 Subject: [PATCH 3/3] #243: configure gradle integration test with includePattern,excludePattern --- test/jte-runtime-cp-test-models-gradle/build.gradle | 2 ++ .../src/main/jte/modelExampleExcluded.jte | 0 .../src/test/java/gg/jte/TemplateEngineTest.java | 6 ++++++ 3 files changed, 8 insertions(+) create mode 100644 test/jte-runtime-cp-test-models-gradle/src/main/jte/modelExampleExcluded.jte diff --git a/test/jte-runtime-cp-test-models-gradle/build.gradle b/test/jte-runtime-cp-test-models-gradle/build.gradle index fc12df34..5015d778 100644 --- a/test/jte-runtime-cp-test-models-gradle/build.gradle +++ b/test/jte-runtime-cp-test-models-gradle/build.gradle @@ -32,6 +32,8 @@ jte { binaryStaticContent = true jteExtension('gg.jte.models.generator.ModelExtension') { implementationAnnotation = '@test.Dummy' + includePattern = '.*' + excludePattern = '.*Excluded.*' } } diff --git a/test/jte-runtime-cp-test-models-gradle/src/main/jte/modelExampleExcluded.jte b/test/jte-runtime-cp-test-models-gradle/src/main/jte/modelExampleExcluded.jte new file mode 100644 index 00000000..e69de29b diff --git a/test/jte-runtime-cp-test-models-gradle/src/test/java/gg/jte/TemplateEngineTest.java b/test/jte-runtime-cp-test-models-gradle/src/test/java/gg/jte/TemplateEngineTest.java index 351d08f2..0a62c25f 100644 --- a/test/jte-runtime-cp-test-models-gradle/src/test/java/gg/jte/TemplateEngineTest.java +++ b/test/jte-runtime-cp-test-models-gradle/src/test/java/gg/jte/TemplateEngineTest.java @@ -73,6 +73,12 @@ void normalContent(Templates templates) { assertThat(output).containsIgnoringWhitespaces("Header", "Main", "Footer"); } + @ParameterizedTest + @MethodSource("templates") + void excludedTemplates(gg.jte.generated.precompiled.Templates templates){ + assertThat(templates.getClass().getMethods()).noneMatch(m -> m.getName().contains("Exclude")); + } + @ParameterizedTest @MethodSource("templates") void nestedContent(Templates templates) {