3

By default the generated XText artifacts generate code from my DSL to the default outlet (which defaults to src-gen folder). I know that you can explicitly pass the outlet configuration name in fsa.generateFile("myfile.txt", "MY_OUTLET_NAME", "Some file content").

I it because I want to generate code with my XText DSL and want to use the generation gap pattern and generate code in a folder called "src-once".

I'am using XText 2.2.1.

My questions:

1) Where and how do I define my outlets like "MY_OUTLET_NAME"?

2) Is there a way to prevent overwriting existing files in a specific outlet?

rehos
  • 168
  • 8

2 Answers2

10

The hint form Christian Dietrich pointed me in the right direction. Below is the code that I ended up with.

I have created a new class MyOutputConfigurationProvider that implements IOutputConfigurationProvider. The getOutputConfigurations method returns two output configurations, the default src-gen and a custom src-gen-once with the correct settings for generating sources only once.

package com.my.dsl;

import static com.google.common.collect.Sets.newHashSet;

import java.util.Set;

import org.eclipse.xtext.generator.IFileSystemAccess;
import org.eclipse.xtext.generator.IOutputConfigurationProvider;
import org.eclipse.xtext.generator.OutputConfiguration;

public class MyOutputConfigurationProvider implements
    IOutputConfigurationProvider {

public final static String DEFAULT_OUTPUT_ONCE = "DEFAULT_OUTPUT_ONCE";

/**
 * @return a set of {@link OutputConfiguration} available for the generator
 */
public Set<OutputConfiguration> getOutputConfigurations() {
    OutputConfiguration defaultOutput = new OutputConfiguration(IFileSystemAccess.DEFAULT_OUTPUT);
    defaultOutput.setDescription("Output Folder");
    defaultOutput.setOutputDirectory("./src-gen");
    defaultOutput.setOverrideExistingResources(true);
    defaultOutput.setCreateOutputDirectory(true);
    defaultOutput.setCleanUpDerivedResources(true);
    defaultOutput.setSetDerivedProperty(true);

    OutputConfiguration onceOutput = new OutputConfiguration(DEFAULT_OUTPUT_ONCE);
    onceOutput.setDescription("Output Folder (once)");
    onceOutput.setOutputDirectory("./src-gen-once");
    onceOutput.setOverrideExistingResources(false);
    onceOutput.setCreateOutputDirectory(true);
    onceOutput.setCleanUpDerivedResources(false);
    onceOutput.setSetDerivedProperty(true);
    return newHashSet(defaultOutput, onceOutput);
}

}

To use the MyOutputConfigurationProvider implementation add a configure method to your module class:

/**
 * Use this class to register components to be used within the IDE.
 */
public class MyDslUiModule extends com.my.dsl.ui.AbstractMyDslUiModule {
public MyDslUiModule(AbstractUIPlugin plugin) {
    super(plugin);
}

@Override
public void configure(Binder binder) {
    super.configure(binder);

    binder.bind(IOutputConfigurationProvider.class).to(MyOutputConfigurationProvider.class).in(Singleton.class);
}

}

rehos
  • 168
  • 8
  • This is very helpful! Once I get it to work within the editor (DefaultUiModule), I'll have to work it into the standalone generator (for the command-line build). If you've already done that, a pointer would be greatly appreciated. – John Aug 29 '12 at 16:59
  • 2
    I find that your illustrative example very useful. But it was missing something, some digging around led to this URL: http://xtextcasts.org/episodes/15-output-configurations – Mohamed El-Beltagy Jul 19 '14 at 02:25
  • Thanks for sharing – Chris Oct 05 '16 at 18:33
4

implement a custom IOutputConfigurationProvider should do the trick

Christian Dietrich
  • 11,778
  • 4
  • 24
  • 32