Adding another file into the generated src-gen folder with Xtext-xtend compiler [message #1239890] |
Wed, 05 February 2014 04:18  |
Eclipse User |
|
|
|
Hi,
I create a runnable jar file from my xtext-xtend project so that my compiler can be used as a standalone jar.
When the jar file is run, the output is a folder (src-gen) including the generated code files.
However, I also want to include another separate file in the src-gen. I have added this file(XcD_PACKAGE.h) winthin the same package as the Main.java of my project.
Then, I use the following code snippet in the Main.java so as to create a copy of the XcD.PACKAGE.h file in the generated src-gen folder.
However, this does not work unfortunately.
Can anyone please help me on this ?
Thanks.
protected void runGenerator(String string) {
// load the resource
ResourceSet set = resourceSetProvider.get();
Resource resource = set.getResource(URI.createURI(string), true);
// validate the resource
List<Issue> list = validator.validate(resource, CheckMode.ALL, CancelIndicator.NullImpl);
if (!list.isEmpty()) {
for (Issue issue : list) {
System.err.println(issue);
}
return;
}
// configure and start the generator
fileAccess.setOutputPath("src-gen/");
generator.doGenerate(resource, fileAccess);
ClassLoader cl = this.getClass().getClassLoader();
java.io.InputStream is = cl.getResourceAsStream("org.xtext.example.mydsl.generator/XcD_PACKAGE.h");
fileAccess.generateFile("XcD_PACKAGE.h",is);
System.out.println("Code generation finished.");
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Re: Adding another file into the generated src-gen folder with Xtext-xtend compiler [message #1240156 is a reply to message #1239890] |
Wed, 05 February 2014 15:45   |
Eclipse User |
|
|
|
Thanks a lot again.
I have placed the file, which needs to be copied inside the generated src-gen folder,
into the same package as the Main.java and my xtend file.
So, the file do exist in the same package.
My xtend file content is like the following:
override void doGenerate(Resource resource, IFileSystemAccess fsa) {
var String exceptions_mtype = "";
root = resource.contents.head as compilationUnits;
fsa.generateFile("XcD_PACKAGE.h",Resources.toString(Main.getResource("/XcD_PACKAGE.h"), Charsets.UTF_8));
..........
..........
The Main.java that executes the xtend file has the following content.
public class Main {
public static void main(String[] args) {
if (args.length==0) {
System.err.println("Aborting: no path to EMF resource provided!");
return;
}
Injector injector = new XcdStandaloneSetupGenerated().createInjectorAndDoEMFRegistration();
Main main = injector.getInstance(Main.class);
main.runGenerator(args[0]);
}
@Inject
private Provider<ResourceSet> resourceSetProvider;
@Inject
private IResourceValidator validator;
@Inject
private IGenerator generator;
@Inject
private JavaIoFileSystemAccess fileAccess;
protected void runGenerator(String string) {
// load the resource
ResourceSet set = resourceSetProvider.get();
Resource resource = set.getResource(URI.createURI(string), true);
// validate the resource
List<Issue> list = validator.validate(resource, CheckMode.ALL, CancelIndicator.NullImpl);
if (!list.isEmpty()) {
for (Issue issue : list) {
System.err.println(issue);
}
return;
}
// configure and start the generator
fileAccess.setOutputPath("src-gen/");
generator.doGenerate(resource, fileAccess);
System.out.println("Code generation finished.");
}
}
So, the compiler (that I deploy as runnable jar) generates a folder with some files of generated codes. However, I also want to create a copy of a file in the folder. Indeed, it is this file that is located in the same package as Main.java.
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.09185 seconds