Skip to content
Snippets Groups Projects
Commit f58aef9d authored by René Schöne's avatar René Schöne
Browse files

Add versioning, allow multiple grammars as input.

parent 244d6631
No related branches found
No related tags found
No related merge requests found
Pipeline #7378 passed
......@@ -27,6 +27,25 @@ dependencies {
testImplementation group: 'org.assertj', name: 'assertj-core', version: '3.12.1'
}
def versionFile = 'src/main/resources/relast2umlVersion.properties'
def oldProps = new Properties()
try {
file(versionFile).withInputStream { stream -> oldProps.load(stream) }
version = oldProps['version']
} catch (e) {
// this happens, if either the properties file is not present, or cannot be read from
throw new GradleException("File ${versionFile} not found or unreadable. Aborting.")
}
task newVersion() {
doFirst {
def props = new Properties()
props['version'] = value
props.store(file(versionFile).newWriter(), null)
}
}
sourceSets {
main {
java.srcDir "src/gen/java"
......
......@@ -18,14 +18,15 @@ import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
public class Compiler {
private StringOption optionOutputFile;
private StringOption optionInputGrammar;
private StringOption optionInputRelast2Uml;
private FlagOption optionHelp;
private FlagOption optionVersion;
private FlagOption optionVerbose;
private ArrayList<Option<?>> options;
......@@ -49,6 +50,11 @@ public class Compiler {
return;
}
if (optionVersion.isSet()) {
System.out.println(readVersion());
return;
}
if (optionVerbose.isSet()) {
try {
run();
......@@ -62,6 +68,8 @@ public class Compiler {
}
private void run() throws CompilerException {
printMessage("Running relast2uml " + readVersion());
String output;
if (optionOutputFile.isSet()) {
output = optionOutputFile.getValue();
......@@ -75,17 +83,11 @@ public class Compiler {
throw new CompilerException("Error creating output dir " + Paths.get(output).getParent().toAbsolutePath(), e);
}
printMessage("Running Relast2Uml Preprocessor");
if (anyRequiredOptionIsUnset()) {
throw new CompilerException("Aborting due to missing values for required options.");
}
List<String> otherArgs = commandLine.getArguments();
if (!otherArgs.isEmpty()) {
printMessage("Superfluous arguments will be ignored: " + otherArgs);
}
Relast2Uml relast2uml = parseProgram(optionInputGrammar.getValue(), optionInputRelast2Uml.getValue());
Relast2Uml relast2uml = parseProgram();
if (!relast2uml.errors().isEmpty()) {
System.err.println("Errors:");
......@@ -95,10 +97,30 @@ public class Compiler {
System.exit(1);
}
printMessage("Writing output files");
printMessage("Writing output file " + output);
writeToFile(output, relast2uml.generateAspect());
}
/**
* Reads the version string.
*
* The version string is read from the property file
* src/main/resources/Version.properties. This
* file should be generated during the build process. If it is missing
* then there is some problem in the build script.
*
* @author Jesper Öqvist <jesper.oqvist@cs.lth.se>
* @return the read version string, or <code>version ?</code>
*/
private String readVersion() {
try {
ResourceBundle resources = ResourceBundle.getBundle("relast2umlVersion");
return resources.getString("version");
} catch (MissingResourceException e) {
return "version ?";
}
}
private boolean anyRequiredOptionIsUnset() {
boolean foundError = false;
for (Option<?> option : options) {
......@@ -137,9 +159,9 @@ public class Compiler {
private void addOptions() {
optionOutputFile = addOption(new StringOption("output", "target file to be generated."));
optionInputGrammar = addOption(new StringOption("inputGrammar", "base grammar."));
optionInputRelast2Uml = addOption(new StringOption("inputRelast2Uml", "relast2uml definition file."));
optionHelp = addOption(new FlagOption("help", "Print usage and exit."));
optionVersion = addOption(new FlagOption("help", "Print version and exit."));
optionVerbose = addOption(new FlagOption("verbose", "Print more messages while compiling."));
}
......@@ -148,11 +170,12 @@ public class Compiler {
return option;
}
private Relast2Uml parseProgram(String inputGrammarFileName, String inputRelast2UmlFileName) throws CompilerException {
private Relast2Uml parseProgram() throws CompilerException {
Program program = new Program();
Relast2Uml relast2Uml;
GrammarFile inputGrammar;
for (String inputGrammarFileName : commandLine.getArguments()) {
GrammarFile inputGrammar;
try (BufferedReader reader = Files.newBufferedReader(Paths.get(inputGrammarFileName))) {
Relast2UmlScanner scanner = new Relast2UmlScanner(reader);
Relast2UmlParser parser = new Relast2UmlParser();
......@@ -166,16 +189,24 @@ public class Compiler {
} catch (IOException | Parser.Exception e) {
throw new CompilerException("Could not parse grammar file " + inputGrammarFileName, e);
}
}
if (optionInputRelast2Uml.isSet()) {
String inputRelast2UmlFileName = optionInputRelast2Uml.getValue();
try (BufferedReader reader = Files.newBufferedReader(Paths.get(inputRelast2UmlFileName))) {
Relast2UmlScanner scanner = new Relast2UmlScanner(reader);
Relast2UmlParser parser = new Relast2UmlParser();
relast2Uml = (Relast2Uml) parser.parse(scanner, Relast2UmlParser.AltGoals.relast2uml);
relast2Uml.setProgram(program);
relast2Uml.setFileName(inputRelast2UmlFileName);
} catch (IOException | Parser.Exception e) {
throw new CompilerException("Could not parse relast2uml file " + inputRelast2UmlFileName, e);
}
} else {
// no special setting given
relast2Uml = new Relast2Uml();
relast2Uml.setFileName("<none>");
}
relast2Uml.setProgram(program);
relast2Uml.treeResolveAll();
return relast2Uml;
}
......
#Fri Jul 17 15:33:27 CEST 2020
version=0.1.0
......@@ -34,8 +34,8 @@ public class CompilerTest {
String[] args = {
"--output=" + output,
"--inputGrammar=" + inputGrammar,
"--inputRelast2Uml=" + inputRelast2Uml
"--inputRelast2Uml=" + inputRelast2Uml,
inputGrammar
};
new Compiler().run(args);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment