diff --git a/relast2uml.base/build.gradle b/relast2uml.base/build.gradle index 4a14b3e1c953676155efb255fcc4b51b858e4f60..7c6be0213b75876f976bce2167129fa0960afbe1 100644 --- a/relast2uml.base/build.gradle +++ b/relast2uml.base/build.gradle @@ -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" diff --git a/relast2uml.base/src/main/java/org/jastadd/relast2uml/compiler/Compiler.java b/relast2uml.base/src/main/java/org/jastadd/relast2uml/compiler/Compiler.java index 4352f14608922aa2f7d4927a616bff0e5138c30b..a216bd7e4bbafc8a0f831ea3ecd577a3f2783181 100644 --- a/relast2uml.base/src/main/java/org/jastadd/relast2uml/compiler/Compiler.java +++ b/relast2uml.base/src/main/java/org/jastadd/relast2uml/compiler/Compiler.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,34 +170,43 @@ 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; - - try (BufferedReader reader = Files.newBufferedReader(Paths.get(inputGrammarFileName))) { - Relast2UmlScanner scanner = new Relast2UmlScanner(reader); - Relast2UmlParser parser = new Relast2UmlParser(); - inputGrammar = (GrammarFile) parser.parse(scanner); - if (optionVerbose.isSet()) { - inputGrammar.dumpTree(System.out); + + for (String inputGrammarFileName : commandLine.getArguments()) { + GrammarFile inputGrammar; + try (BufferedReader reader = Files.newBufferedReader(Paths.get(inputGrammarFileName))) { + Relast2UmlScanner scanner = new Relast2UmlScanner(reader); + Relast2UmlParser parser = new Relast2UmlParser(); + inputGrammar = (GrammarFile) parser.parse(scanner); + if (optionVerbose.isSet()) { + inputGrammar.dumpTree(System.out); + } + program.addGrammarFile(inputGrammar); + inputGrammar.treeResolveAll(); + inputGrammar.setFileName(inputGrammarFileName); + } catch (IOException | Parser.Exception e) { + throw new CompilerException("Could not parse grammar file " + inputGrammarFileName, e); } - program.addGrammarFile(inputGrammar); - inputGrammar.treeResolveAll(); - inputGrammar.setFileName(inputGrammarFileName); - } catch (IOException | Parser.Exception e) { - throw new CompilerException("Could not parse grammar file " + inputGrammarFileName, e); } - 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); + 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.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; } diff --git a/relast2uml.base/src/main/resources/relast2umlVersion.properties b/relast2uml.base/src/main/resources/relast2umlVersion.properties new file mode 100644 index 0000000000000000000000000000000000000000..6af1a4098cd81977ed6e6ea0ad547cf5aee5ffda --- /dev/null +++ b/relast2uml.base/src/main/resources/relast2umlVersion.properties @@ -0,0 +1,2 @@ +#Fri Jul 17 15:33:27 CEST 2020 +version=0.1.0 diff --git a/relast2uml.base/src/test/java/org/jastadd/relast2uml/test/CompilerTest.java b/relast2uml.base/src/test/java/org/jastadd/relast2uml/test/CompilerTest.java index a19f8476accc2e03de91a5455497d409259b1e8a..871500ba6f059b4926fb6b4eb655b79362f16f5d 100644 --- a/relast2uml.base/src/test/java/org/jastadd/relast2uml/test/CompilerTest.java +++ b/relast2uml.base/src/test/java/org/jastadd/relast2uml/test/CompilerTest.java @@ -34,8 +34,8 @@ public class CompilerTest { String[] args = { "--output=" + output, - "--inputGrammar=" + inputGrammar, - "--inputRelast2Uml=" + inputRelast2Uml + "--inputRelast2Uml=" + inputRelast2Uml, + inputGrammar }; new Compiler().run(args);