Skip to content

Commit

Permalink
Merge pull request #245 from chkl/243-add-include-and-exclude-options
Browse files Browse the repository at this point in the history
add include and exclude patterns to jte-model (#243 Part 2)
  • Loading branch information
casid authored Jul 6, 2023
2 parents 9517966 + e677593 commit 62bd05a
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 1 deletion.
11 changes: 11 additions & 0 deletions jte-models/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ To use jte-models, set up your build script to include one of these:
<implementationAnnotation>@foo.bar.MyAnnotation</implementationAnnotation>
</settings>
-->
<!-- optional settings to include (or exclude) certain templates.
The value is a regular expression matched against the class name of the generated JTE class.
<settings>
<includePattern>\.pages\..*</includePattern>
<excludePattern>\.components\..*</excludePattern>
</settings>
-->
</extension>
</extensions>
</configuration>
Expand Down Expand Up @@ -70,6 +77,8 @@ jte {
jteExtension('gg.jte.models.generator.ModelExtension') {
interfaceAnnotation = '@foo.bar.MyAnnotation'
implementationAnnotation = '@foo.bar.MyAnnotation'
includePattern = '\.pages\..*'
excludePattern = '\.components\..*'
}
*/
}
Expand Down Expand Up @@ -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\..*")
}
*/
}
Expand Down
17 changes: 17 additions & 0 deletions jte-models/src/main/java/gg/jte/models/generator/ModelConfig.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gg.jte.models.generator;

import java.util.Map;
import java.util.regex.Pattern;

public class ModelConfig {
private final Map<String, String> map;
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -31,11 +33,20 @@ public JteExtension init(Map<String, String> value) {
@Override
public Collection<Path> generate(JteConfig config, Set<TemplateDescription> 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());
}
}
2 changes: 2 additions & 0 deletions test/jte-runtime-cp-test-models-gradle/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ jte {
binaryStaticContent = true
jteExtension('gg.jte.models.generator.ModelExtension') {
implementationAnnotation = '@test.Dummy'
includePattern = '.*'
excludePattern = '.*Excluded.*'
}
}

Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions test/jte-runtime-cp-test-models/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@
<className>gg.jte.models.generator.ModelExtension</className>
<settings>
<implementationAnnotation>@test.Dummy</implementationAnnotation>
<includePattern>.*</includePattern>
<excludePattern>.*Exclude.*</excludePattern>
</settings>
</extension>
</extensions>
Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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")
Expand All @@ -80,6 +86,7 @@ void nestedContent(Templates templates) {
assertThat(output).containsIgnoringWhitespaces("Header", "Hello World", "Footer");
}


@ParameterizedTest
@MethodSource("templates")
void hasAnnotation(Templates templates) {
Expand Down

0 comments on commit 62bd05a

Please sign in to comment.