Search in sources :

Example 1 with MainContext

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

the class Main method runGenerator.

/**
 * Load the resource, validate it, and, invoke the code generator.
 */
private void runGenerator(List<Path> files, Injector injector) {
    Properties properties = this.getProps(cmd);
    String pathOption = CLIOption.OUTPUT_PATH.option.getOpt();
    Path root = null;
    if (cmd.hasOption(pathOption)) {
        root = Paths.get(cmd.getOptionValue(pathOption)).normalize();
        if (!Files.exists(root)) {
            // FIXME: Create it instead?
            reporter.printFatalErrorAndExit("Output location '" + root + "' does not exist.");
        }
        if (!Files.isDirectory(root)) {
            reporter.printFatalErrorAndExit("Output location '" + root + "' is not a directory.");
        }
    }
    for (Path path : files) {
        if (!Files.exists(path)) {
            reporter.printFatalErrorAndExit(path + ": No such file or directory");
        }
    }
    for (Path path : files) {
        path = path.toAbsolutePath();
        Path pkgRoot = FileConfig.findPackageRoot(path, reporter::printWarning);
        String resolved;
        if (root != null) {
            resolved = root.resolve("src-gen").toString();
        } else {
            resolved = pkgRoot.resolve("src-gen").toString();
        }
        this.fileAccess.setOutputPath(resolved);
        final Resource resource = getValidatedResource(path);
        exitIfCollectedErrors();
        LFGeneratorContext context = new MainContext(LFGeneratorContext.Mode.STANDALONE, CancelIndicator.NullImpl, (m, p) -> {
        }, properties, false, fileConfig -> injector.getInstance(ErrorReporter.class));
        this.generator.generate(resource, this.fileAccess, context);
        exitIfCollectedErrors();
        // print all other issues (not errors)
        issueCollector.getAllIssues().forEach(reporter::printIssue);
        System.out.println("Code generation finished.");
    }
}
Also used : Path(java.nio.file.Path) ErrorReporter(org.lflang.ErrorReporter) LFGeneratorContext(org.lflang.generator.LFGeneratorContext) MainContext(org.lflang.generator.MainContext) Resource(org.eclipse.emf.ecore.resource.Resource) Properties(java.util.Properties)

Example 2 with MainContext

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

the class TestBase method configure.

/**
 * Configure a test by applying the given configurator and return a
 * generator context. Also, if the given level is less than
 * `TestLevel.BUILD`, add a `no-compile` flag to the generator context. If
 * the configurator was not applied successfully, throw an AssertionError.
 *
 * @param test the test to configure.
 * @param configurator The configurator to apply to the test.
 * @param level The level of testing in which the generator context will be
 * used.
 * @return a generator context with a fresh resource, unaffected by any AST
 * transformation that may have occured in other tests.
 * @throws IOException if there is any file access problem
 */
private LFGeneratorContext configure(LFTest test, Configurator configurator, TestLevel level) throws IOException {
    var context = new MainContext(LFGeneratorContext.Mode.STANDALONE, CancelIndicator.NullImpl, (m, p) -> {
    }, new Properties(), true, fileConfig -> new DefaultErrorReporter());
    var r = resourceSetProvider.get().getResource(URI.createFileURI(test.srcFile.toFile().getAbsolutePath()), true);
    if (r.getErrors().size() > 0) {
        test.result = Result.PARSE_FAIL;
        throw new AssertionError("Test did not parse correctly.");
    }
    fileAccess.setOutputPath(FileConfig.findPackageRoot(test.srcFile, s -> {
    }).resolve(FileConfig.DEFAULT_SRC_GEN_DIR).toString());
    test.context = context;
    test.fileConfig = new FileConfig(r, FileConfig.getSrcGenRoot(fileAccess), context.useHierarchicalBin());
    // Set the no-compile flag the test is not supposed to reach the build stage.
    if (level.compareTo(TestLevel.BUILD) < 0) {
        context.getArgs().setProperty("no-compile", "");
    }
    addExtraLfcArgs(context.getArgs());
    // Update the test by applying the configuration. E.g., to carry out an AST transformation.
    if (configurator != null && !configurator.configure(test)) {
        test.result = Result.CONFIG_FAIL;
        throw new AssertionError("Test configuration unsuccessful.");
    }
    return context;
}
Also used : Arrays(java.util.Arrays) Inject(com.google.inject.Inject) LFGeneratorContext(org.lflang.generator.LFGeneratorContext) InjectionExtension(org.eclipse.xtext.testing.extensions.InjectionExtension) Result(org.lflang.tests.LFTest.Result) GeneratorResult(org.lflang.generator.GeneratorResult) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Map(java.util.Map) Path(java.nio.file.Path) PrintWriter(java.io.PrintWriter) Predicate(java.util.function.Predicate) TestCategory(org.lflang.tests.TestRegistry.TestCategory) Set(java.util.Set) LFCommand(org.lflang.util.LFCommand) Collectors(java.util.stream.Collectors) InvocationTargetException(java.lang.reflect.InvocationTargetException) Objects(java.util.Objects) List(java.util.List) Target(org.lflang.Target) LFStandaloneSetup(org.lflang.LFStandaloneSetup) FileConfig(org.lflang.FileConfig) IGeneratorContext(org.eclipse.xtext.generator.IGeneratorContext) CheckMode(org.eclipse.xtext.validation.CheckMode) URI(org.eclipse.emf.common.util.URI) HashMap(java.util.HashMap) Configurator(org.lflang.tests.Configurators.Configurator) Constructor(java.lang.reflect.Constructor) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) ArrayList(java.util.ArrayList) JavaIoFileSystemAccess(org.eclipse.xtext.generator.JavaIoFileSystemAccess) RuntimeIOException(org.eclipse.xtext.util.RuntimeIOException) CancelIndicator(org.eclipse.xtext.util.CancelIndicator) LFGenerator(org.lflang.generator.LFGenerator) PrintStream(java.io.PrintStream) Properties(java.util.Properties) LFRuntimeModule(org.lflang.LFRuntimeModule) BufferedWriter(java.io.BufferedWriter) Severity(org.eclipse.xtext.diagnostics.Severity) StringWriter(java.io.StringWriter) FileWriter(java.io.FileWriter) DefaultErrorReporter(org.lflang.DefaultErrorReporter) IResourceValidator(org.eclipse.xtext.validation.IResourceValidator) MainContext(org.lflang.generator.MainContext) IOException(java.io.IOException) File(java.io.File) Assertions.assertSame(org.junit.jupiter.api.Assertions.assertSame) TimeUnit(java.util.concurrent.TimeUnit) Injector(com.google.inject.Injector) Provider(com.google.inject.Provider) FileFilter(java.io.FileFilter) InjectWith(org.eclipse.xtext.testing.InjectWith) Collections(java.util.Collections) DefaultErrorReporter(org.lflang.DefaultErrorReporter) FileConfig(org.lflang.FileConfig) MainContext(org.lflang.generator.MainContext) Properties(java.util.Properties)

Aggregations

Path (java.nio.file.Path)2 Properties (java.util.Properties)2 Inject (com.google.inject.Inject)1 Injector (com.google.inject.Injector)1 Provider (com.google.inject.Provider)1 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileFilter (java.io.FileFilter)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 PrintStream (java.io.PrintStream)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 Constructor (java.lang.reflect.Constructor)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 List (java.util.List)1