Tried with JTE but it requires access to the classpath to author templates.
This commit is contained in:
parent
66434a6bc1
commit
8057928548
4 changed files with 66 additions and 10 deletions
|
@ -23,6 +23,11 @@
|
||||||
<artifactId>picocli</artifactId>
|
<artifactId>picocli</artifactId>
|
||||||
<version>4.7.6</version>
|
<version>4.7.6</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>gg.jte</groupId>
|
||||||
|
<artifactId>jte</artifactId>
|
||||||
|
<version>3.2.0</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
|
|
|
@ -10,13 +10,13 @@ import java.nio.charset.StandardCharsets;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
|
||||||
public class ParserWrapper {
|
public class ParserWrapper {
|
||||||
public static DocumentNode parse(Path sourcePath) throws IOException {
|
public static DocumentNode parse(Path sourcePath) throws IOException {
|
||||||
var ins = CharStreams.fromPath(sourcePath, StandardCharsets.UTF_8);
|
var ins = CharStreams.fromPath(sourcePath, StandardCharsets.UTF_8);
|
||||||
EndpointsLexer lexer = new EndpointsLexer(ins);
|
EndpointsLexer lexer = new EndpointsLexer(ins);
|
||||||
EndpointsParser parser = new EndpointsParser(new CommonTokenStream(lexer));
|
EndpointsParser parser = new EndpointsParser(new CommonTokenStream(lexer));
|
||||||
var document= parser.document() ;
|
var document = parser.document();
|
||||||
var astTransformer = new EndpointsVisitorTransformer();
|
var astTransformer = new EndpointsVisitorTransformer();
|
||||||
astTransformer.visit(document);
|
astTransformer.visit(document);
|
||||||
return new DocumentNode(astTransformer.getConfig(), astTransformer.getDataTypes(), astTransformer.getEndpoints());
|
return new DocumentNode(astTransformer.getConfig(), astTransformer.getDataTypes(), astTransformer.getEndpoints());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package nu.zoom.dsl.cli;
|
||||||
|
|
||||||
import nu.zoom.dsl.ast.DocumentNode;
|
import nu.zoom.dsl.ast.DocumentNode;
|
||||||
import nu.zoom.dsl.ast.ParserWrapper;
|
import nu.zoom.dsl.ast.ParserWrapper;
|
||||||
|
import nu.zoom.dsl.jte.Generator;
|
||||||
import picocli.CommandLine;
|
import picocli.CommandLine;
|
||||||
import picocli.CommandLine.Command;
|
import picocli.CommandLine.Command;
|
||||||
import picocli.CommandLine.Option;
|
import picocli.CommandLine.Option;
|
||||||
|
@ -11,6 +12,7 @@ import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
|
import java.util.List;
|
||||||
import java.util.concurrent.Callable;
|
import java.util.concurrent.Callable;
|
||||||
|
|
||||||
@Command(
|
@Command(
|
||||||
|
@ -47,7 +49,11 @@ public class EndpointsCLI implements Callable<Integer> {
|
||||||
validateInputFile();
|
validateInputFile();
|
||||||
validateOutputDirectory();
|
validateOutputDirectory();
|
||||||
DocumentNode rootNode = ParserWrapper.parse(file);
|
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;
|
return 0;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
System.err.println(e.getMessage());
|
System.err.println(e.getMessage());
|
||||||
|
|
45
parser/src/main/java/nu/zoom/dsl/jte/Generator.java
Normal file
45
parser/src/main/java/nu/zoom/dsl/jte/Generator.java
Normal 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 ;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue