Search in sources :

Example 1 with LFRuntimeModule

use of org.lflang.LFRuntimeModule in project lingua-franca by lf-lang.

the class Main method main.

/**
 * Main function of the stand-alone compiler.
 *
 * @param args CLI arguments
 */
public static void main(final String[] args) {
    final ReportingBackend reporter = new ReportingBackend(new Io());
    // Injector used to obtain Main instance.
    final Injector injector = new LFStandaloneSetup(new LFRuntimeModule(), new LFStandaloneModule(reporter)).createInjectorAndDoEMFRegistration();
    // Main instance.
    final Main main = injector.getInstance(Main.class);
    // Apache Commons Options object configured to according to available CLI arguments.
    Options options = CLIOption.getOptions();
    // CLI arguments parser.
    CommandLineParser parser = new DefaultParser();
    // Helper object for printing "help" menu.
    HelpFormatter formatter = new HelpFormatter();
    try {
        main.cmd = parser.parse(options, args, true);
        // If requested, print help and abort
        if (main.cmd.hasOption(CLIOption.HELP.option.getOpt())) {
            formatter.printHelp("lfc", options);
            System.exit(0);
        }
        // If requested, print version and abort
        if (main.cmd.hasOption(CLIOption.VERSION.option.getLongOpt())) {
            System.out.println("lfc " + VERSION);
            System.exit(0);
        }
        List<String> files = main.cmd.getArgList();
        if (files.size() < 1) {
            reporter.printFatalErrorAndExit("No input files.");
        }
        try {
            List<Path> paths = files.stream().map(Paths::get).collect(Collectors.toList());
            main.runGenerator(paths, injector);
        } catch (RuntimeException e) {
            reporter.printFatalErrorAndExit("An unexpected error occurred:", e);
        }
    } catch (ParseException e) {
        reporter.printFatalError("Unable to parse commandline arguments. Reason: " + e.getMessage());
        formatter.printHelp("lfc", options);
        System.exit(1);
    }
}
Also used : Path(java.nio.file.Path) Options(org.apache.commons.cli.Options) HelpFormatter(org.apache.commons.cli.HelpFormatter) Injector(com.google.inject.Injector) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) LFRuntimeModule(org.lflang.LFRuntimeModule) LFStandaloneSetup(org.lflang.LFStandaloneSetup) DefaultParser(org.apache.commons.cli.DefaultParser)

Example 2 with LFRuntimeModule

use of org.lflang.LFRuntimeModule in project lingua-franca by lf-lang.

the class TestBase method runSingleTestAndPrintResults.

/**
 * Run a test, print results on stderr.
 *
 * @param test      Test case.
 * @param testClass The test class that will execute the test. This is target-specific,
 *                  it may provide some target-specific configuration. We pass a class
 *                  and not a new instance because this method needs to ensure the object
 *                  is properly injected, and so, it needs to control its entire lifecycle.
 * @param level     Level to which to run the test.
 */
public static void runSingleTestAndPrintResults(LFTest test, Class<? extends TestBase> testClass, TestLevel level) {
    Injector injector = new LFStandaloneSetup(new LFRuntimeModule()).createInjectorAndDoEMFRegistration();
    TestBase runner;
    try {
        @SuppressWarnings("unchecked") Constructor<? extends TestBase> constructor = (Constructor<? extends TestBase>) testClass.getConstructors()[0];
        runner = constructor.newInstance();
    } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
        throw new IllegalStateException(e);
    }
    injector.injectMembers(runner);
    Set<LFTest> tests = Set.of(test);
    try {
        runner.validateAndRun(tests, t -> true, level);
    } catch (IOException e) {
        throw new RuntimeIOException(e);
    }
    checkAndReportFailures(tests);
}
Also used : RuntimeIOException(org.eclipse.xtext.util.RuntimeIOException) Constructor(java.lang.reflect.Constructor) RuntimeIOException(org.eclipse.xtext.util.RuntimeIOException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) Injector(com.google.inject.Injector) LFRuntimeModule(org.lflang.LFRuntimeModule) LFStandaloneSetup(org.lflang.LFStandaloneSetup)

Aggregations

Injector (com.google.inject.Injector)2 LFRuntimeModule (org.lflang.LFRuntimeModule)2 LFStandaloneSetup (org.lflang.LFStandaloneSetup)2 IOException (java.io.IOException)1 Constructor (java.lang.reflect.Constructor)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Path (java.nio.file.Path)1 CommandLineParser (org.apache.commons.cli.CommandLineParser)1 DefaultParser (org.apache.commons.cli.DefaultParser)1 HelpFormatter (org.apache.commons.cli.HelpFormatter)1 Options (org.apache.commons.cli.Options)1 ParseException (org.apache.commons.cli.ParseException)1 RuntimeIOException (org.eclipse.xtext.util.RuntimeIOException)1