2017-09-19 18:08:17 +02:00
|
|
|
package com.minres.rdl.generator;
|
|
|
|
|
|
|
|
import com.google.inject.Inject;
|
|
|
|
import com.google.inject.Injector;
|
|
|
|
import com.google.inject.Provider;
|
|
|
|
import com.minres.rdl.RDLStandaloneSetup;
|
2017-09-20 10:37:50 +02:00
|
|
|
import com.minres.rdl.generator.Options;
|
2017-09-19 18:08:17 +02:00
|
|
|
import java.lang.reflect.MalformedParametersException;
|
|
|
|
import java.text.ParseException;
|
2017-09-20 10:37:50 +02:00
|
|
|
import java.util.Collections;
|
2017-09-19 18:08:17 +02:00
|
|
|
import java.util.List;
|
2017-09-20 10:37:50 +02:00
|
|
|
import java.util.function.BiConsumer;
|
2017-09-19 18:08:17 +02:00
|
|
|
import java.util.function.Consumer;
|
|
|
|
import org.eclipse.emf.common.util.URI;
|
|
|
|
import org.eclipse.emf.ecore.resource.Resource;
|
|
|
|
import org.eclipse.emf.ecore.resource.ResourceSet;
|
|
|
|
import org.eclipse.emf.mwe.utils.ProjectMapping;
|
|
|
|
import org.eclipse.emf.mwe.utils.StandaloneSetup;
|
|
|
|
import org.eclipse.xtext.generator.GeneratorContext;
|
|
|
|
import org.eclipse.xtext.generator.GeneratorDelegate;
|
|
|
|
import org.eclipse.xtext.generator.JavaIoFileSystemAccess;
|
|
|
|
import org.eclipse.xtext.generator.OutputConfiguration;
|
|
|
|
import org.eclipse.xtext.resource.XtextResource;
|
|
|
|
import org.eclipse.xtext.resource.XtextResourceSet;
|
|
|
|
import org.eclipse.xtext.util.CancelIndicator;
|
|
|
|
import org.eclipse.xtext.validation.CheckMode;
|
|
|
|
import org.eclipse.xtext.validation.IResourceValidator;
|
|
|
|
import org.eclipse.xtext.validation.Issue;
|
|
|
|
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
|
|
|
|
import org.eclipse.xtext.xbase.lib.Conversions;
|
|
|
|
import org.eclipse.xtext.xbase.lib.Exceptions;
|
|
|
|
import org.eclipse.xtext.xbase.lib.InputOutput;
|
|
|
|
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
|
|
|
|
import org.eclipse.xtext.xbase.lib.Pair;
|
|
|
|
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
|
|
|
|
|
|
|
|
@SuppressWarnings("all")
|
|
|
|
public class Main {
|
2017-09-20 10:44:11 +02:00
|
|
|
private final String USAGE_STR = "RDL2code [-h] [-v] [-I <RDL include dir] [-i <include output dir>] [-s <source output dir>] [-g <generated files output dir>] <input file> <input file>";
|
2017-09-19 18:08:17 +02:00
|
|
|
|
|
|
|
public static void main(final String[] args) {
|
|
|
|
boolean _isEmpty = ((List<String>)Conversions.doWrapArray(args)).isEmpty();
|
|
|
|
if (_isEmpty) {
|
2017-09-20 10:37:50 +02:00
|
|
|
System.err.println("Aborting: no path to RDL file provided!");
|
2017-09-19 18:08:17 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-09-20 10:37:50 +02:00
|
|
|
final Injector injector = new RDLStandaloneSetup().createInjectorAndDoEMFRegistration();
|
|
|
|
final Main main = injector.<Main>getInstance(Main.class);
|
2017-09-19 18:08:17 +02:00
|
|
|
try {
|
2017-09-20 10:37:50 +02:00
|
|
|
main.run(args);
|
2017-09-19 18:08:17 +02:00
|
|
|
} catch (final Throwable _t) {
|
|
|
|
if (_t instanceof MalformedParametersException) {
|
|
|
|
final MalformedParametersException e = (MalformedParametersException)_t;
|
|
|
|
String _message = e.getMessage();
|
|
|
|
String _plus = ("Command line error " + _message);
|
|
|
|
InputOutput.<String>print(_plus);
|
2017-09-20 10:37:50 +02:00
|
|
|
System.exit(1);
|
2017-09-19 18:08:17 +02:00
|
|
|
} else if (_t instanceof IllegalArgumentException) {
|
|
|
|
final IllegalArgumentException e_1 = (IllegalArgumentException)_t;
|
|
|
|
String _message_1 = e_1.getMessage();
|
|
|
|
String _plus_1 = ("generation error " + _message_1);
|
|
|
|
InputOutput.<String>print(_plus_1);
|
|
|
|
e_1.printStackTrace();
|
2017-09-20 10:37:50 +02:00
|
|
|
System.exit(2);
|
2017-09-19 18:08:17 +02:00
|
|
|
} else if (_t instanceof ParseException) {
|
|
|
|
final ParseException e_2 = (ParseException)_t;
|
|
|
|
String _message_2 = e_2.getMessage();
|
|
|
|
String _plus_2 = ("parse problem " + _message_2);
|
|
|
|
String _plus_3 = (_plus_2 + " (");
|
|
|
|
int _errorOffset = e_2.getErrorOffset();
|
|
|
|
String _plus_4 = (_plus_3 + Integer.valueOf(_errorOffset));
|
|
|
|
String _plus_5 = (_plus_4 + ")");
|
|
|
|
InputOutput.<String>print(_plus_5);
|
2017-09-20 10:37:50 +02:00
|
|
|
System.exit(3);
|
2017-09-19 18:08:17 +02:00
|
|
|
} else {
|
|
|
|
throw Exceptions.sneakyThrow(_t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
private Provider<ResourceSet> resourceSetProvider;
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
private IResourceValidator validator;
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
private GeneratorDelegate generator;
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
private JavaIoFileSystemAccess fileAccess;
|
|
|
|
|
|
|
|
public void run(final String[] args) {
|
2017-09-20 10:37:50 +02:00
|
|
|
final Options opt = new Options(args, 0, Integer.MAX_VALUE);
|
|
|
|
opt.getSet().addOption("h", Options.Multiplicity.ZERO_OR_ONE);
|
|
|
|
opt.getSet().addOption("v", Options.Multiplicity.ZERO_OR_ONE);
|
2017-09-20 10:44:11 +02:00
|
|
|
opt.getSet().addOption("i", Options.Separator.BLANK, Options.Multiplicity.ZERO_OR_ONE);
|
|
|
|
opt.getSet().addOption("s", Options.Separator.BLANK, Options.Multiplicity.ZERO_OR_ONE);
|
|
|
|
opt.getSet().addOption("g", Options.Separator.BLANK, Options.Multiplicity.ZERO_OR_ONE);
|
|
|
|
opt.getSet().addOption("I", Options.Separator.BLANK, Options.Multiplicity.ZERO_OR_ONE);
|
2017-09-20 10:37:50 +02:00
|
|
|
boolean _check = opt.check(false, false);
|
|
|
|
boolean _not = (!_check);
|
|
|
|
if (_not) {
|
2017-09-20 10:44:11 +02:00
|
|
|
System.err.println(("Usage is: " + this.USAGE_STR));
|
2017-09-20 10:37:50 +02:00
|
|
|
String _checkErrors = opt.getCheckErrors();
|
2017-09-20 10:44:11 +02:00
|
|
|
throw new MalformedParametersException(_checkErrors);
|
2017-09-20 10:37:50 +02:00
|
|
|
}
|
|
|
|
boolean _isSet = opt.getSet().isSet("h");
|
|
|
|
if (_isSet) {
|
2017-09-20 10:44:11 +02:00
|
|
|
InputOutput.<String>println(("Usage: " + this.USAGE_STR));
|
2017-09-20 10:37:50 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
boolean _xifexpression = false;
|
|
|
|
boolean _isSet_1 = opt.getSet().isSet("v");
|
|
|
|
if (_isSet_1) {
|
|
|
|
_xifexpression = true;
|
|
|
|
} else {
|
|
|
|
_xifexpression = false;
|
|
|
|
}
|
|
|
|
final boolean verbose = _xifexpression;
|
|
|
|
boolean _isSet_2 = opt.getSet().isSet("I");
|
|
|
|
if (_isSet_2) {
|
2017-09-19 18:08:17 +02:00
|
|
|
final ProjectMapping projectMapping = new ProjectMapping();
|
|
|
|
projectMapping.setProjectName("RDL Repository");
|
2017-09-20 10:37:50 +02:00
|
|
|
projectMapping.setPath(opt.getSet().getOption("I").getResultValue(0));
|
2017-09-19 18:08:17 +02:00
|
|
|
new StandaloneSetup().addProjectMapping(projectMapping);
|
|
|
|
}
|
2017-09-20 10:37:50 +02:00
|
|
|
this.fileAccess.setOutputPath("src-gen/");
|
|
|
|
Pair<String, Boolean> _mappedTo = Pair.<String, Boolean>of("incl-out", Boolean.valueOf(false));
|
|
|
|
Pair<String, Boolean> _mappedTo_1 = Pair.<String, Boolean>of("src-out", Boolean.valueOf(false));
|
|
|
|
Pair<String, Boolean> _mappedTo_2 = Pair.<String, Boolean>of("gen-out", Boolean.valueOf(true));
|
|
|
|
final BiConsumer<String, Boolean> _function = (String p1, Boolean p2) -> {
|
|
|
|
boolean _isSet_3 = opt.getSet().isSet(p1.substring(0, 1));
|
|
|
|
if (_isSet_3) {
|
|
|
|
String _resultValue = opt.getSet().getOption(p1.substring(0, 1)).getResultValue(0);
|
2017-09-20 10:44:11 +02:00
|
|
|
String _plus = (_resultValue + "/");
|
|
|
|
this.fileAccess.setOutputPath(p1, _plus);
|
2017-09-20 10:37:50 +02:00
|
|
|
} else {
|
|
|
|
this.fileAccess.setOutputPath(p1, "src-gen/");
|
2017-09-19 18:08:17 +02:00
|
|
|
}
|
2017-09-20 10:37:50 +02:00
|
|
|
OutputConfiguration _get = this.fileAccess.getOutputConfigurations().get(p1);
|
|
|
|
if (_get!=null) {
|
|
|
|
_get.setOverrideExistingResources((p2).booleanValue());
|
2017-09-19 18:08:17 +02:00
|
|
|
}
|
2017-09-20 10:37:50 +02:00
|
|
|
};
|
|
|
|
Collections.<String, Boolean>unmodifiableMap(CollectionLiterals.<String, Boolean>newHashMap(_mappedTo, _mappedTo_1, _mappedTo_2)).forEach(_function);
|
|
|
|
final Consumer<String> _function_1 = (String string) -> {
|
2017-09-19 18:08:17 +02:00
|
|
|
try {
|
2017-09-20 10:37:50 +02:00
|
|
|
if (verbose) {
|
|
|
|
InputOutput.<String>println(("Reading " + string));
|
2017-09-19 18:08:17 +02:00
|
|
|
}
|
2017-09-20 10:37:50 +02:00
|
|
|
ResourceSet _get = this.resourceSetProvider.get();
|
|
|
|
final XtextResourceSet resourceSet = ((XtextResourceSet) _get);
|
|
|
|
resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
|
|
|
|
final Resource resource = resourceSet.getResource(URI.createFileURI(string), true);
|
|
|
|
final List<Issue> issues = this.validator.validate(resource, CheckMode.ALL, CancelIndicator.NullImpl);
|
|
|
|
boolean _isEmpty = issues.isEmpty();
|
|
|
|
boolean _not_1 = (!_isEmpty);
|
|
|
|
if (_not_1) {
|
|
|
|
URI _uRI = resource.getURI();
|
2017-09-20 10:44:11 +02:00
|
|
|
String _plus = ("Error validating " + _uRI);
|
|
|
|
System.err.println(_plus);
|
2017-09-20 10:37:50 +02:00
|
|
|
final Consumer<Issue> _function_2 = (Issue it) -> {
|
|
|
|
System.err.println(it);
|
|
|
|
};
|
|
|
|
issues.forEach(_function_2);
|
|
|
|
URI _uRI_1 = resource.getURI();
|
2017-09-20 10:44:11 +02:00
|
|
|
String _plus_1 = ("error validating " + _uRI_1);
|
2017-09-20 10:37:50 +02:00
|
|
|
int _size = issues.size();
|
2017-09-20 10:44:11 +02:00
|
|
|
throw new ParseException(_plus_1, _size);
|
2017-09-20 10:37:50 +02:00
|
|
|
}
|
|
|
|
GeneratorContext _generatorContext = new GeneratorContext();
|
|
|
|
final Procedure1<GeneratorContext> _function_3 = (GeneratorContext it) -> {
|
|
|
|
it.setCancelIndicator(CancelIndicator.NullImpl);
|
|
|
|
};
|
|
|
|
final GeneratorContext context = ObjectExtensions.<GeneratorContext>operator_doubleArrow(_generatorContext, _function_3);
|
|
|
|
this.generator.generate(resource, this.fileAccess, context);
|
|
|
|
if (verbose) {
|
|
|
|
InputOutput.<String>print((("Code generation for " + string) + " finished, "));
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
if (verbose) {
|
|
|
|
URI _uRI_2 = this.fileAccess.getURI("", "incl-out");
|
2017-09-20 10:44:11 +02:00
|
|
|
String _plus_2 = ("includes are in " + _uRI_2);
|
|
|
|
String _plus_3 = (_plus_2 + ", ");
|
|
|
|
InputOutput.<String>print(_plus_3);
|
2017-09-20 10:37:50 +02:00
|
|
|
}
|
|
|
|
} catch (final Throwable _t) {
|
|
|
|
if (_t instanceof Exception) {
|
|
|
|
final Exception e = (Exception)_t;
|
|
|
|
URI _uRI_3 = this.fileAccess.getURI("");
|
2017-09-20 10:44:11 +02:00
|
|
|
String _plus_4 = ("includes are in " + _uRI_3);
|
|
|
|
String _plus_5 = (_plus_4 + ", ");
|
|
|
|
InputOutput.<String>print(_plus_5);
|
2017-09-20 10:37:50 +02:00
|
|
|
} else {
|
|
|
|
throw Exceptions.sneakyThrow(_t);
|
|
|
|
}
|
2017-09-19 18:08:17 +02:00
|
|
|
}
|
2017-09-20 10:37:50 +02:00
|
|
|
try {
|
|
|
|
if (verbose) {
|
|
|
|
URI _uRI_4 = this.fileAccess.getURI("", "src-out");
|
2017-09-20 10:44:11 +02:00
|
|
|
String _plus_6 = ("sources are in " + _uRI_4);
|
|
|
|
String _plus_7 = (_plus_6 + ", ");
|
|
|
|
InputOutput.<String>println(_plus_7);
|
2017-09-20 10:37:50 +02:00
|
|
|
}
|
|
|
|
} catch (final Throwable _t_1) {
|
|
|
|
if (_t_1 instanceof Exception) {
|
|
|
|
final Exception e_1 = (Exception)_t_1;
|
|
|
|
URI _uRI_5 = this.fileAccess.getURI("");
|
2017-09-20 10:44:11 +02:00
|
|
|
String _plus_8 = ("sources are in " + _uRI_5);
|
|
|
|
String _plus_9 = (_plus_8 + ", ");
|
|
|
|
InputOutput.<String>println(_plus_9);
|
2017-09-20 10:37:50 +02:00
|
|
|
} else {
|
|
|
|
throw Exceptions.sneakyThrow(_t_1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Throwable _e) {
|
|
|
|
throw Exceptions.sneakyThrow(_e);
|
2017-09-19 18:08:17 +02:00
|
|
|
}
|
2017-09-20 10:37:50 +02:00
|
|
|
};
|
|
|
|
opt.getSet().getData().forEach(_function_1);
|
2017-09-19 18:08:17 +02:00
|
|
|
}
|
|
|
|
}
|