#15 Output Configurations
Dec 11, 2012 |
8 minutes |
Beginners
In this episode I demonstrate, how to create multiple output configurations (also called outlets) with different default settings and use it within an IGenerator implementation.
- Download:
- source code Project Files in Zip (92.7 KB)
- mp4 Full Size H.264 Video (21.5 MB)
- m4v Smaller H.264 Video (12.3 MB)
- webm Full Size VP8 Video (12.8 MB)
- ogv Full Size Theora Video (25.4 MB)
Defining multiple output configurations
MyOutputConfigurationProvider.java
public class MyOutputConfigurationProvider implements IOutputConfigurationProvider {
public static final String GEN_ONCE_OUTPUT = "gen-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 readonlyOutput = new OutputConfiguration(GEN_ONCE_OUTPUT);
readonlyOutput.setDescription("Read-only Output Folder");
readonlyOutput.setOutputDirectory("./src");
readonlyOutput.setOverrideExistingResources(false);
readonlyOutput.setCreateOutputDirectory(true);
readonlyOutput.setCleanUpDerivedResources(false);
readonlyOutput.setSetDerivedProperty(false);
return newHashSet(defaultOutput, readonlyOutput);
}
}
public class MyOutputConfigurationProvider implements IOutputConfigurationProvider { public static final String GEN_ONCE_OUTPUT = "gen-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 readonlyOutput = new OutputConfiguration(GEN_ONCE_OUTPUT); readonlyOutput.setDescription("Read-only Output Folder"); readonlyOutput.setOutputDirectory("./src"); readonlyOutput.setOverrideExistingResources(false); readonlyOutput.setCreateOutputDirectory(true); readonlyOutput.setCleanUpDerivedResources(false); readonlyOutput.setSetDerivedProperty(false); return newHashSet(defaultOutput, readonlyOutput); } }
MyDslRuntimeModule.java
public class MyDslRuntimeModule extends org.xtext.example.mydsl.AbstractMyDslRuntimeModule {
@Override
public void configure(Binder binder) {
super.configure(binder);
binder.bind(IOutputConfigurationProvider.class)
.to(MyOutputConfigurationProvider.class)
.in(Singleton.class);
}
}
public class MyDslRuntimeModule extends org.xtext.example.mydsl.AbstractMyDslRuntimeModule { @Override public void configure(Binder binder) { super.configure(binder); binder.bind(IOutputConfigurationProvider.class) .to(MyOutputConfigurationProvider.class) .in(Singleton.class); } }
Selecting output configuration in the generator
MyDslGenerator.java
class MyDslGenerator implements IGenerator {
override void doGenerate(Resource resource, IFileSystemAccess fsa) {
for (model : resource.contents.filter(typeof(Model))) {
fsa.generateFile("test/Greetings.java", model.generate);
fsa.generateFile("test/GreetingsImpl.java",
MyOutputConfigurationProvider::GEN_ONCE_OUTPUT, model.generateImpl);
}
}
...
}
class MyDslGenerator implements IGenerator { override void doGenerate(Resource resource, IFileSystemAccess fsa) { for (model : resource.contents.filter(typeof(Model))) { fsa.generateFile("test/Greetings.java", model.generate); fsa.generateFile("test/GreetingsImpl.java", MyOutputConfigurationProvider::GEN_ONCE_OUTPUT, model.generateImpl); } } ... }