Skip to content

Commit

Permalink
#278 add support for parent module, similar to parent-pom
Browse files Browse the repository at this point in the history
  • Loading branch information
casid committed Oct 1, 2023
1 parent cc17d60 commit fd09d6d
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 20 deletions.
23 changes: 14 additions & 9 deletions jte/src/main/java/gg/jte/compiler/TemplateCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public Template load(String name) {

@Override
public Template hotReload(String name) {
LinkedHashSet<ClassDefinition> classDefinitions = generate(Collections.singletonList(name), true);
LinkedHashSet<ClassDefinition> classDefinitions = generate(Collections.singletonList(name), true, readModuleInformation("", codeResolver));
classDefinitions.removeIf(c -> !c.isChanged());

if (!classDefinitions.isEmpty()) {
Expand All @@ -85,18 +85,24 @@ public void cleanAll() {

@Override
public List<String> generateAll() {
LinkedHashSet<ClassDefinition> classDefinitions = generate(codeResolver.resolveAllTemplateNames(), false);
Module module = readModuleInformation("", codeResolver);
Collection<String> names = module.resolveAllTemplateNames();
LinkedHashSet<ClassDefinition> classDefinitions = generate(names, false, module);

return classDefinitions.stream().map(ClassDefinition::getSourceFileName).collect(Collectors.toList());
}

@Override
public List<String> precompileAll() {
LinkedHashSet<ClassDefinition> classDefinitions = generate(codeResolver.resolveAllTemplateNames(), false);
Module module = readModuleInformation("", codeResolver);
Collection<String> names = module.resolveAllTemplateNames();
LinkedHashSet<ClassDefinition> classDefinitions = generate(names, false, module);

return precompileClasses(classDefinitions);
}

public List<String> precompile(List<String> names) {
LinkedHashSet<ClassDefinition> classDefinitions = generate(names, false);
LinkedHashSet<ClassDefinition> classDefinitions = generate(names, false, readModuleInformation("", codeResolver));
return precompileClasses(classDefinitions);
}

Expand Down Expand Up @@ -167,8 +173,7 @@ ClassCompiler createCompiler(String extension) {
}
}

private LinkedHashSet<ClassDefinition> generate(List<String> names, boolean trackChanges ) {
Module module = readModuleInformation("", codeResolver);
private LinkedHashSet<ClassDefinition> generate(Collection<String> names, boolean trackChanges, Module module) {

LinkedHashSet<ClassDefinition> classDefinitions = new LinkedHashSet<>();
for (String name : names) {
Expand Down Expand Up @@ -234,11 +239,11 @@ private LinkedHashSet<ClassDefinition> generate(List<String> names, boolean trac
private Module readModuleInformation(String alias, CodeResolver codeResolver) {
String jteRootContent = codeResolver.resolve(".jteroot");
if (jteRootContent == null) {
return new Module(alias, codeResolver, Map.of());
return new Module(alias, codeResolver, Map.of(), false);
}

if (!(codeResolver instanceof DirectoryCodeResolver directoryCodeResolver)) {
return new Module(alias, codeResolver, Map.of());
return new Module(alias, codeResolver, Map.of(), false);
}

ModuleInfo moduleInfo = ModuleInfoParser.parse(jteRootContent);
Expand All @@ -250,7 +255,7 @@ private Module readModuleInformation(String alias, CodeResolver codeResolver) {
children.put(moduleImport.alias(), readModuleInformation(moduleImport.alias(), moduleDirectoryResolver));
}

return new Module(alias, codeResolver, children);
return new Module(alias, codeResolver, children, moduleInfo.parent());
}

private LinkedHashSet<TemplateDependency> initTemplateDependencies(String name) {
Expand Down
30 changes: 25 additions & 5 deletions jte/src/main/java/gg/jte/compiler/module/Module.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package gg.jte.compiler.module;

import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;

import gg.jte.CodeResolver;
Expand All @@ -19,11 +22,13 @@ public static String getModuleAlias(String name) {
private final String alias;
private final CodeResolver codeResolver;
private final Map<String, Module> children;
private final boolean parent;

public Module( String alias, CodeResolver codeResolver, Map<String, Module> children ) {
public Module( String alias, CodeResolver codeResolver, Map<String, Module> children, boolean parent ) {
this.alias = alias;
this.codeResolver = isRoot() ? codeResolver : new ModuleCodeResolver(alias, codeResolver);
this.children = children;
this.parent = parent;
}

public Module resolve(String name) {
Expand Down Expand Up @@ -60,11 +65,26 @@ public boolean isRoot() {
return alias.isEmpty();
}

public String getAlias() {
return alias;
}

public CodeResolver getCodeResolver() {
return codeResolver;
}

public Collection<String> resolveAllTemplateNames() {
LinkedHashSet<String> result = new LinkedHashSet<>();
resolveAllTemplateNames(result);
return result;
}

private void resolveAllTemplateNames(LinkedHashSet<String> result) {
if (!parent) {
List<String> names = codeResolver.resolveAllTemplateNames();
for (String name : names) {
result.add(normalize(name));
}
}

for (Module module : children.values()) {
module.resolveAllTemplateNames(result);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import gg.jte.CodeResolver;
import gg.jte.TemplateNotFoundException;

import java.util.List;


public class ModuleCodeResolver implements CodeResolver {

Expand All @@ -29,6 +31,11 @@ public long getLastModified( String name ) {
return codeResolver.getLastModified(removeAlias(name));
}

@Override
public List<String> resolveAllTemplateNames() {
return codeResolver.resolveAllTemplateNames();
}

@Override
public boolean exists( String name ) {
return codeResolver.exists(removeAlias(name));
Expand Down
2 changes: 1 addition & 1 deletion jte/src/main/java/gg/jte/compiler/module/ModuleInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
import java.util.List;


public record ModuleInfo(List<ModuleImport> imports) {}
public record ModuleInfo(boolean parent, List<ModuleImport> imports) {}
11 changes: 8 additions & 3 deletions jte/src/main/java/gg/jte/compiler/module/ModuleInfoParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;


public class ModuleInfoParser {
Expand All @@ -29,7 +30,8 @@ private ModuleInfoParser( String moduleInfo ) {
}

private ModuleInfo parse() {
ModuleInfo result = new ModuleInfo(new ArrayList<>());
boolean parent = false;
List<ModuleImport> imports = new ArrayList<>();

push(Mode.Root);

Expand All @@ -44,13 +46,16 @@ private ModuleInfo parse() {
importMode.alias = extractFromLastIndex(-4).trim();
} else if ( currentChar == '\n' || i == endIndex - 1) {
importMode.from = extractFromLastIndex(1).trim();
result.imports().add(new ModuleImport(importMode.alias, importMode.from));
imports.add(new ModuleImport(importMode.alias, importMode.from));
pop();
}
} else if (currentMode == Mode.Root && regionMatches("@parent")) {
parent = true;
lastIndex = i + 1;
}
}

return result;
return new ModuleInfo(parent, imports);
}

private String extractFromLastIndex(int offset) {
Expand Down
31 changes: 29 additions & 2 deletions jte/src/test/java/gg/jte/TemplateEngine_ModulesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ <p>line chart (checkout)</p>
}

@Test
void emptyTopLevelModule_generateAll() {
void generateAll_emptyTopLevelModule() {
DirectoryCodeResolver codeResolver = new DirectoryCodeResolver(Path.of("src/test/modules/empty-top-level-module"));
TemplateEngine templateEngine = TemplateEngine.create(codeResolver, ContentType.Html);

Expand All @@ -87,5 +87,32 @@ void emptyTopLevelModule_generateAll() {
);
}

// TODO adjust precompileAll() and generateAll() to iterate over all module files as well!
@Test
void generateAll_threeModulesSameAlias() {
DirectoryCodeResolver codeResolver = new DirectoryCodeResolver(Path.of("src/test/modules/three-modules-same-alias/app"));
TemplateEngine templateEngine = TemplateEngine.create(codeResolver, ContentType.Html);

List<String> templates = templateEngine.generateAll();

assertThat(templates).containsExactlyInAnyOrder(
"gg/jte/generated/ondemand/JtepageGenerated.java",
"gg/jte/generated/ondemand/core/JtelayoutGenerated.java",
"gg/jte/generated/ondemand/apexcharts/JtelinechartGenerated.java"
);
}

@Test
void generateAll_threeModulesDifferentAlias() {
DirectoryCodeResolver codeResolver = new DirectoryCodeResolver(Path.of("src/test/modules/three-modules-different-alias/app"));
TemplateEngine templateEngine = TemplateEngine.create(codeResolver, ContentType.Html);

List<String> templates = templateEngine.generateAll();

assertThat(templates).containsExactlyInAnyOrder(
"gg/jte/generated/ondemand/JtepageGenerated.java",
"gg/jte/generated/ondemand/core/JtelayoutGenerated.java",
"gg/jte/generated/ondemand/apexcharts/JtelinechartGenerated.java",
"gg/jte/generated/ondemand/charts/JtelinechartGenerated.java"
);
}
}
1 change: 1 addition & 0 deletions jte/src/test/modules/empty-top-level-module/.jteroot
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@parent
@import apexcharts from apexcharts
@import checkout from checkout
@import core from core

0 comments on commit fd09d6d

Please sign in to comment.