简体   繁体   中英

How to add multiple outlets for generated XText DSL

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?

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);
}

}

实现自定义IOutputConfigurationProvider应该做的伎俩

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM