From 68dc70c17618b0feaaaebf3d9c258d25c9b29793 Mon Sep 17 00:00:00 2001 From: Johan Maasing Date: Fri, 2 May 2025 08:44:44 +0200 Subject: [PATCH] Makes it possible to put templates in subdirectories --- .../{ => nu/zoom/dsl}/Codecs.scala.ftl | 0 .../{ => nu/zoom/dsl}/Endpoints.scala.ftl | 0 .../{ => nu/zoom/dsl}/Protocol.scala.ftl | 0 .../nu/zoom/dsl/freemarker/Generator.java | 24 +++++++++++-------- 4 files changed, 14 insertions(+), 10 deletions(-) rename endpoints-templates/{ => nu/zoom/dsl}/Codecs.scala.ftl (100%) rename endpoints-templates/{ => nu/zoom/dsl}/Endpoints.scala.ftl (100%) rename endpoints-templates/{ => nu/zoom/dsl}/Protocol.scala.ftl (100%) diff --git a/endpoints-templates/Codecs.scala.ftl b/endpoints-templates/nu/zoom/dsl/Codecs.scala.ftl similarity index 100% rename from endpoints-templates/Codecs.scala.ftl rename to endpoints-templates/nu/zoom/dsl/Codecs.scala.ftl diff --git a/endpoints-templates/Endpoints.scala.ftl b/endpoints-templates/nu/zoom/dsl/Endpoints.scala.ftl similarity index 100% rename from endpoints-templates/Endpoints.scala.ftl rename to endpoints-templates/nu/zoom/dsl/Endpoints.scala.ftl diff --git a/endpoints-templates/Protocol.scala.ftl b/endpoints-templates/nu/zoom/dsl/Protocol.scala.ftl similarity index 100% rename from endpoints-templates/Protocol.scala.ftl rename to endpoints-templates/nu/zoom/dsl/Protocol.scala.ftl diff --git a/parser/src/main/java/nu/zoom/dsl/freemarker/Generator.java b/parser/src/main/java/nu/zoom/dsl/freemarker/Generator.java index dcfa86a..1ac2cb0 100644 --- a/parser/src/main/java/nu/zoom/dsl/freemarker/Generator.java +++ b/parser/src/main/java/nu/zoom/dsl/freemarker/Generator.java @@ -21,6 +21,7 @@ import nu.zoom.dsl.ast.DocumentNode; import java.io.IOException; import java.nio.charset.StandardCharsets; +import java.nio.file.FileVisitOption; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; @@ -50,17 +51,20 @@ public class Generator { } public List generate() throws IOException, TemplateException { - try (Stream files = Files.list(templatesDir)) { - List templates = files - .map(Path::getFileName) - .map(Path::toString) - .filter(p -> p.length() > TEMPLATE_EXTENSION_LENGTH && p.endsWith(TEMPLATE_EXTENSION) - ) - .toList(); + try (Stream files = Files.walk(templatesDir)) { + List templates = files + .filter(p -> { + var fname = p.getFileName().toString(); + return fname.length() > TEMPLATE_EXTENSION_LENGTH && fname.endsWith(TEMPLATE_EXTENSION) ; + } + ) + .map(p -> templatesDir.relativize(p)) + .toList(); ArrayList out = new ArrayList<>(); - for (String template : templates) { - Path outpath = outputDir.resolve(outputFilenameFromTemplate(template)); - Template ftl = this.cfg.getTemplate(template); + for (Path template : templates) { + Path outpath = outputDir.resolve(outputFilenameFromTemplate(template.toString())); + Files.createDirectories(outpath.getParent()); + Template ftl = this.cfg.getTemplate(template.toString()); try (var outw = Files.newBufferedWriter(outpath, StandardCharsets.UTF_8)) { ftl.process(this.data, outw); out.add(outpath);