Commit 4ccb348a authored by 0xJMR's avatar 0xJMR
Browse files

Played around with generation process.

parent 82a25073
Pipeline #55843 passed with stage
in 29 seconds
......@@ -17,6 +17,7 @@
<junit.version>4.11</junit.version>
<logback.version>1.1.2</logback.version>
<montimath.version>0.0.13</montimath.version>
<commons.cli.version>1.4</commons.cli.version>
<!-- .. Plugins ....................................................... -->
......@@ -111,6 +112,13 @@
<version>${jsr305.version}</version>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>${commons.cli.version}</version>
</dependency>
<!-- .. MontiCore .................................................... -->
<!-- .. MontiMath .................................................... -->
......
package de.rwth.se.mpp.codegen;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Options;
public class Application {
protected final Options options;
protected final Generator generator;
public Application() {
this.options = new Options();
this.generator = new Generator();
this.addOptions();
}
protected Options getOptions() {
return this.options;
}
protected void addOptions() {
this.options.addOption("modelPath", true,
"The absolute path to the root of the models.");
this.options.addOption("outputDirectory", true,
"The absolute path to the output directory.");
}
protected void start(String[] args) throws Exception {
CommandLineParser parser = new DefaultParser();
CommandLine commandLine = parser.parse(this.options, args);
this.generator.execute(commandLine);
}
public static void main(String[] args) {
Application application = new Application();
try {
application.start(args);
} catch (Exception exception) {
System.out.println(application.getOptions());
}
}
}
package de.rwth.se.mpp.codegen;
import de.monticore.generating.GeneratorEngine;
import de.monticore.generating.GeneratorSetup;
import de.monticore.generating.templateengine.GlobalExtensionManagement;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.ParseException;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
public class Generator {
protected Path parseModelPath(CommandLine commandLine) throws ParseException {
String modelPath = (String)commandLine.getParsedOptionValue("modelPath");
return Paths.get(modelPath);
}
protected Path parseOutputDirectory(CommandLine commandLine) throws ParseException {
String outputDirectory = (String)commandLine.getParsedOptionValue("outputDirectory");
return Paths.get(outputDirectory);
}
public void execute(CommandLine commandLine) throws Exception {
Path modelPath = this.parseModelPath(commandLine);
Path outputDirectory = this.parseOutputDirectory(commandLine);
ModelPathVisitor visitor = new ModelPathVisitor(modelPath, ".m");
List<File> mathFiles = visitor.searchFiles();
System.out.println(mathFiles);
this.generateIndex(mathFiles, outputDirectory);
//this.generateScripts(mathFiles);
}
protected void generateIndex(List<File> mathFiles, Path outputDirectory) {
GeneratorSetup setup = new GeneratorSetup();
GeneratorEngine engine = new GeneratorEngine(setup);
GlobalExtensionManagement glex = new GlobalExtensionManagement();
GeneratorHelper helper = new GeneratorHelper();
helper.setMathFiles(mathFiles);
glex.setGlobalValue("helper", helper);
setup.setGlex(glex);
setup.setOutputDirectory(outputDirectory.toFile());
setup.setTracing(false);
Path output = Paths.get("index.html");
engine.generateNoA("templates/index.ftl", output);
}
protected void generateScripts(List<File> mathFiles, Path outputDirectory) {
}
}
package de.rwth.se.mpp.codegen;
import java.io.File;
import java.util.List;
public class GeneratorHelper {
protected List<File> mathFiles;
public void setMathFiles(List<File> mathFiles) {
this.mathFiles = mathFiles;
}
public List<File> getMathFiles() {
return this.mathFiles;
}
}
package de.rwth.se.mpp.codegen;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;
public class ModelPathVisitor implements FileVisitor<Path> {
protected final Path modelPath;
protected final String extension;
protected List<File> searchedFiles;
public ModelPathVisitor(Path modelPath, String extension) {
this.modelPath = modelPath;
this.extension = extension;
}
public List<File> searchFiles() throws IOException {
this.searchedFiles = new ArrayList<>();
Files.walkFileTree(this.modelPath, this);
return this.searchedFiles;
}
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
if (file.getFileName().toString().endsWith(this.extension)) {
this.searchedFiles.add(file.toFile());
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
if (exc != null) throw exc;
return FileVisitResult.TERMINATE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
if (exc != null) throw exc;
return FileVisitResult.CONTINUE;
}
}
package de.rwth.se.mpp.codegen;
import org.junit.Test;
import java.nio.file.Paths;
public class ApplicationTest {
@Test
public void testStart() throws Exception {
String[] args = {
"-modelPath", Paths.get("src/test/resources/models").toAbsolutePath().toString(),
"-outputDirectory", Paths.get("target/generated-sources/application").toAbsolutePath().toString(),
};
Application application = new Application();
application.start(args);
}
}
<#assign helper = glex.getGlobalVar("helper")>
<!DOCTYPE HTML>
<html>
<head>
<title>Math Views Examples</title>
<style>
<!--
@import url("https://fonts.googleapis.com/css?family=Roboto");
body {
background-color:#303130;
color:#cecece;
margin:0;
padding:0;
}
h1 {
text-align:center;
font-family:"Roboto", sans-serif;
}
ul {
list-style:none;
display:inline;
margin:0;
padding:0;
}
a {
text-decoration:none;
}
li {
float:left;
text-align:center;
padding:10px;
font-family:"Roboto", sans-serif;
background-color:#252525;
margin:10px;
border-radius:20px;
}
li:hover {
background-color:#303130;
color:#252525;
transition:background-color ease-in 0.3s;
cursor:pointer;
font-weight:bold;
}
-->
</style>
</head>
<body>
<h1>Math Views Examples</h1>
<ul>
<#list helper.getMathFiles() as mathFile>
<li>
<a href="">${mathFile.getName()}</a>
</li>
</#list>
</ul>
</body>
</html>
\ No newline at end of file
<#assign genHelper = glex.getGlobalVar("genHelper")>
<!DOCTYPE HTML>
<html>
<head>
<title>${genHelper.getTitle()}</title>
<style type="text/css">
<!--
ul {
list-style:none;
display:inline;
}
li {
float:left;
}
-->
</style>
</head>
<body>
<ul>
<li>
${}
</li>
<li>
${}
</li>
</ul>
</body>
</html>
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment