1

I am currently building a framework which would benefit from having a DSL for creating a configuration file, so I created one using Xtext.

Now I want to add a dependency to the classes that I have created so that I can generate configurations at runtime, but on Xtext's site it looks like the only two cases for integration are:

  • When I want CI for the language itself;
  • When I want to include a plugin that would generate code at build time.

How can I use the generator that I wrote in Xtext at runtime in my Maven project?

Toastrackenigma
  • 7,604
  • 4
  • 45
  • 55
Hristo Vrigazov
  • 1,357
  • 2
  • 12
  • 20

1 Answers1

1

For CI for Xtext itself simpy use the new project wizard and select Maven as build system on the second page of the project. To build your model files have a look that the xtext-maven-plugin e.g. as used here https://github.com/xtext/maven-xtext-example/blob/master/example-project/pom.xml or here https://github.com/cdietrich/xtext-maven-example/blob/master/org.xtext.example.mydsl.model/pom.xml

If you simply want to read a model file and call the generator

package org.eclipse.xtext.example.domainmodel;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.xtext.generator.GeneratorContext;
import org.eclipse.xtext.generator.GeneratorDelegate;
import org.eclipse.xtext.generator.IGeneratorContext;
import org.eclipse.xtext.generator.JavaIoFileSystemAccess;
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 com.google.common.collect.Lists;
import com.google.inject.Injector;

/**
 * @author dietrich - Initial contribution and API
 */
public class Main {

    public static void main(String[] args) {
        // TODO traverse directory
        List<String> files = Lists.newArrayList("model/a.dmodel", "model/b.dmodel");
        Injector injector = new DomainmodelStandaloneSetup().createInjectorAndDoEMFRegistration();
        ResourceSet rs = injector.getInstance(ResourceSet.class);
        ArrayList<Resource> resources = Lists.newArrayList();
        for (String file : files) {
            Resource r = rs.getResource(URI.createFileURI(file), true);
            resources.add(r);
        }

        IResourceValidator validator = injector.getInstance(IResourceValidator.class);
        for (Resource r : resources) {
            List<Issue> issues = validator.validate(r, CheckMode.ALL, CancelIndicator.NullImpl);
            for (Issue i : issues) {
                System.out.println(i);
            }
        }

        GeneratorDelegate generator = injector.getInstance(GeneratorDelegate.class);
        JavaIoFileSystemAccess fsa = injector.getInstance(JavaIoFileSystemAccess.class);
        fsa.setOutputPath("src-gen-code/");
        GeneratorContext context = new GeneratorContext();
        context.setCancelIndicator(CancelIndicator.NullImpl);

        for (Resource r : resources) {
            generator.generate(r, fsa, context);
        }
    }

}
Christian Dietrich
  • 11,778
  • 4
  • 24
  • 32
  • This does not look like my use case I think. I want to embed the generator so that I can use it inside another java class in the Maven project. This looks like build-time generation. – Hristo Vrigazov Oct 11 '17 at 09:12
  • can you be more explicit on your usecase? – Christian Dietrich Oct 11 '17 at 09:13
  • I don't know how to explain it more clearly. I want to have a packaged maven dependency that I can add to my project and from the code itself to use the generator. The example shown is adding a maven plugin that would generate code during build. – Hristo Vrigazov Oct 11 '17 at 09:15
  • so you ask: how to i read 1 or multiple dsl files from java code and then call the code generator on these model files? – Christian Dietrich Oct 11 '17 at 09:17
  • Exactly, how do I use the whole pipeline - I just give as input the text file with the program in my DSL and have a class which generates files based on this, but to do it imperatively (from my code), not in the build – Hristo Vrigazov Oct 11 '17 at 09:18
  • Fantastic, thanks! I thought I should use Guice in some way, but was not sure which classes exactly I should instantiate. Awesome! – Hristo Vrigazov Oct 11 '17 at 09:28