Tried with JTE but it requires access to the classpath to author templates.

This commit is contained in:
Johan Maasing 2025-03-30 21:01:42 +02:00
parent 66434a6bc1
commit 8057928548
4 changed files with 66 additions and 10 deletions

View file

@ -23,6 +23,11 @@
<artifactId>picocli</artifactId>
<version>4.7.6</version>
</dependency>
<dependency>
<groupId>gg.jte</groupId>
<artifactId>jte</artifactId>
<version>3.2.0</version>
</dependency>
</dependencies>
<build>
<plugins>

View file

@ -14,7 +14,7 @@ public class ParserWrapper {
var ins = CharStreams.fromPath(sourcePath, StandardCharsets.UTF_8);
EndpointsLexer lexer = new EndpointsLexer(ins);
EndpointsParser parser = new EndpointsParser(new CommonTokenStream(lexer));
var document= parser.document() ;
var document = parser.document();
var astTransformer = new EndpointsVisitorTransformer();
astTransformer.visit(document);
return new DocumentNode(astTransformer.getConfig(), astTransformer.getDataTypes(), astTransformer.getEndpoints());

View file

@ -2,6 +2,7 @@ package nu.zoom.dsl.cli;
import nu.zoom.dsl.ast.DocumentNode;
import nu.zoom.dsl.ast.ParserWrapper;
import nu.zoom.dsl.jte.Generator;
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
@ -11,6 +12,7 @@ import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.concurrent.Callable;
@Command(
@ -47,7 +49,11 @@ public class EndpointsCLI implements Callable<Integer> {
validateInputFile();
validateOutputDirectory();
DocumentNode rootNode = ParserWrapper.parse(file);
System.out.println(rootNode);
Generator generator = new Generator(templateDir, rootNode, outputDir);
List<Path> generatedPaths = generator.generate();
if (generatedPaths.isEmpty()) {
System.out.println("No generated paths found.");
}
return 0;
} catch (Exception e) {
System.err.println(e.getMessage());

View file

@ -0,0 +1,45 @@
package nu.zoom.dsl.jte;
import gg.jte.CodeResolver;
import gg.jte.ContentType;
import gg.jte.TemplateEngine;
import gg.jte.TemplateOutput;
import gg.jte.output.FileOutput;
import gg.jte.resolve.DirectoryCodeResolver;
import nu.zoom.dsl.ast.DocumentNode;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
public class Generator {
private final Path templatesDir ;
private final DocumentNode data;
private final Path outputDir ;
private final TemplateEngine templateEngine;
public Generator(Path templatesDir, DocumentNode data, Path outputDir) {
this.templatesDir = Objects.requireNonNull(templatesDir);
this.data = Objects.requireNonNull(data);
this.outputDir = Objects.requireNonNull(outputDir);
this.templateEngine = TemplateEngine.create(
new DirectoryCodeResolver(templatesDir),
ContentType.Plain
);
}
public List<Path> generate() throws IOException {
List<Path> templates = Files.list(templatesDir).filter(p -> p.toString().endsWith(".jte")).toList() ;
ArrayList<Path> out = new ArrayList<>();
for (Path template : templates) {
// TODO file ending
Path outpath = outputDir.resolve(template.getFileName());
this.templateEngine.render(template.getFileName().toString(), this.data, new FileOutput(outpath));
out.add(outpath);
}
return out ;
}
}