Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Specify output directory of generated classes via configuration
Specify output directory of generated classes via configuration [message #1850965] Wed, 23 March 2022 15:23 Go to next message
Mandy Neumann is currently offline Mandy NeumannFriend
Messages: 6
Registered: March 2022
Junior Member
I want to specify the target directory for generated classes on a per-project basis.

To achieve this, I intend to require a config file inside the project that specifies the target path.

myProject
|-src
    |- foo.dsl
| targetProject
    |- src
        |-main
            |-generated
| config
    |-generator.properties


So in generator.properties (might as well just be a text file, whatever), I want to specify that the output directory for classes generated from foo.dsl will be ../targetProject/src/main/generated. Ex:
targetPath=../targetProject/src/main/generated


Here's my initial naive approach using IOutputConfigurationProvider:

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

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
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 {

	/**
	 * @return a set of {@link OutputConfiguration} available for the generator
	 */
	public Set<OutputConfiguration> getOutputConfigurations() {
		String outputDirectoryPath = getOutputDirectoryPathFromConfig();

		OutputConfiguration defaultOutput = new OutputConfiguration(IFileSystemAccess.DEFAULT_OUTPUT);
		defaultOutput.setDescription("Output Folder");
		defaultOutput.setOutputDirectory(outputDirectoryPath);
		defaultOutput.setOverrideExistingResources(true);
		defaultOutput.setCreateOutputDirectory(true);
		defaultOutput.setCleanUpDerivedResources(true);
		defaultOutput.setSetDerivedProperty(true);

		return newHashSet(defaultOutput);
	}

	private String getOutputDirectoryPathFromConfig() {
		try (InputStream inputStream = getFileFromResourceAsStream("/config/generator.properties")) {
			Properties config = new Properties();
			config.load(inputStream);
			String targetPath = config.getProperty("targetPath");
			if (targetPath == null) {
				throw new IllegalArgumentException("TargetPath is not defined in config file");
			}
			return targetPath;
		} catch (IOException e) {
			throw new IllegalArgumentException("Error reading config file");
		}
	}

	private InputStream getFileFromResourceAsStream(String fileName) throws IOException {
		ClassLoader classLoader = getClass().getClassLoader();
		InputStream inputStream = classLoader.getResourceAsStream(fileName);

		if (inputStream == null) {
			throw new IOException("Config file not found: " + fileName);
		} else {
			return inputStream;
		}
	}
}


I get an Exception because the config file cannot be found, apparently it is not a resource accessible by the class loader in action.

Am I even on the right track, or is there another way to achieve what I want?
Please note that I do not want to set the target directory in the preferences dialogue, because I need to have multiple projects with *.dsl files in the workspace and they will need to have individual output directories.
Re: Specify output directory of generated classes via configuration [message #1850966 is a reply to message #1850965] Wed, 23 March 2022 15:38 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
am not sure if i get it.
is this standalone or in eclipse?
getResourceAsStream never is about the project but about the code the where OutputConfigProvider is

maybe you can use
org.eclipse.xtext.generator.IContextualOutputConfigurationProvider.getOutputConfigurations(Resource)
repectively
org.eclipse.xtext.builder.EclipseOutputConfigurationProvider.getOutputConfigurations(Resource)
org.eclipse.xtext.builder.EclipseOutputConfigurationProvider.getOutputConfigurations(IProject)

as customization point


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Specify output directory of generated classes via configuration [message #1850967 is a reply to message #1850965] Wed, 23 March 2022 15:43 Go to previous messageGo to next message
Mandy Neumann is currently offline Mandy NeumannFriend
Messages: 6
Registered: March 2022
Junior Member
Nevermind, I think I just found the right solution. I figured I can use project-specific settings, which creates a file .settings/<mydsl>.prefs where I can specify outlet.DEFAULT_OUTPUT.directory. I can check this in with version control so other users will just get the right configuration and I don't need to mess with additional config files.
Re: Specify output directory of generated classes via configuration [message #1850993 is a reply to message #1850967] Thu, 24 March 2022 07:56 Go to previous message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

If by "generated classes" you mean the many src-gen files such as

src-gen/org/eclipse/ocl/xtext/XXX/parser/antlr/internal/InternalXXXParser.java

this is already supported. You just need to specify appropriate paths in your *.mwe2 script. The GIT\org.eclipse.ocl\examples\org.eclipse.ocl.examples.build plugin demonstrates how the MWE2 scripts and support for multiple editors may be isolated from the plugins that are generated. This avoids delivering build-time only support in the editor plugins.

Regards

Ed Willink
Previous Topic:Xtext LSP - Validation - @Check annotations
Next Topic:General Xtext question
Goto Forum:
  


Current Time: Thu Mar 28 21:22:47 GMT 2024

Powered by FUDForum. Page generated in 0.02876 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top