Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Adding another file into the generated src-gen folder with Xtext-xtend compiler
Adding another file into the generated src-gen folder with Xtext-xtend compiler [message #1239890] Wed, 05 February 2014 09:18 Go to next message
mcse mcse is currently offline mcse mcseFriend
Messages: 53
Registered: August 2012
Member
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 #1239902 is a reply to message #1239890] Wed, 05 February 2014 09:48 Go to previous messageGo to next message
Uli Merkel is currently offline Uli MerkelFriend
Messages: 248
Registered: June 2013
Senior Member
for reading includefiles, I use the following:


import static extension com.google.common.io.CharStreams.*

def addIncludeFile(String filename) '''
«val File incfile = new File("..\\!includefiles\\" + filename)»
«IF incfile.exists()»
«FOR String line : new FileReader(incfile).readLines»
«line»
«ENDFOR»
«ENDIF»
'''

and its used in the generator as:

def addMergeFile(Mergefile mf) '''«addIncludeFile(mf.name + ".txt")»'''
Re: Adding another file into the generated src-gen folder with Xtext-xtend compiler [message #1239922 is a reply to message #1239902] Wed, 05 February 2014 10:27 Go to previous messageGo to next message
mcse mcse is currently offline mcse mcseFriend
Messages: 53
Registered: August 2012
Member
Hi,
Thanks a lot for your suggestion.

However, I want this read-write operation to take place when the standalone jar file is used.
So, when one runs the jar file, the compiler should create a copy of the XcD_PACKAGE file (located in the same package as Main.java) within the generated src-gen folder.

Does anyone else have some ideas ? Thanks a lot.
Re: Adding another file into the generated src-gen folder with Xtext-xtend compiler [message #1239934 is a reply to message #1239922] Wed, 05 February 2014 10:44 Go to previous messageGo to next message
Uli Merkel is currently offline Uli MerkelFriend
Messages: 248
Registered: June 2013
Senior Member
it's exactly the same thing:

Think all you have to do is to to call the routine in your

fileAccess.generateFile()

statement.


The key issue: you have opened a file as a stream, but you are not reading it.
Re: Adding another file into the generated src-gen folder with Xtext-xtend compiler [message #1239945 is a reply to message #1239934] Wed, 05 February 2014 11:09 Go to previous messageGo to next message
mcse mcse is currently offline mcse mcseFriend
Messages: 53
Registered: August 2012
Member
Hi,
I used the following function to read from the file.
	def addIncludeFile() '''
	«val File incfile = new File("..\\!includefiles\\" + "XcD_PACKAGE.h")»
	«IF incfile.exists()»
	«FOR String line : (new FileReader(incfile)).readLines»
	«line»
	«ENDFOR»
	«ENDIF»
	'''	


Then, I used the following command to create the new file during the code generation process. The file is created succesfully, but has no content. So, the function above does not work. Can you please let me know what is wrong with the above function ?


fsa.generateFile("XcD_PACKAGE.h",addIncludeFile());

[Updated on: Wed, 05 February 2014 14:44]

Report message to a moderator

Re: Adding another file into the generated src-gen folder with Xtext-xtend compiler [message #1240037 is a reply to message #1239890] Wed, 05 February 2014 15:23 Go to previous messageGo to next message
mcse mcse is currently offline mcse mcseFriend
Messages: 53
Registered: August 2012
Member
Hi

Can anyone please help me on the issue I explained above?

Thanks a lot.
Re: Adding another file into the generated src-gen folder with Xtext-xtend compiler [message #1240084 is a reply to message #1240037] Wed, 05 February 2014 17:04 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13493
Registered: July 2009
Senior Member
Hi I still not understand the problem why not calling

FSA.fenerateFile("path/to/my.file", content)

--
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext at itemis dot de


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Adding another file into the generated src-gen folder with Xtext-xtend compiler [message #1240086 is a reply to message #1240084] Wed, 05 February 2014 17:09 Go to previous messageGo to next message
mcse mcse is currently offline mcse mcseFriend
Messages: 53
Registered: August 2012
Member
Hi
THanks for the reply.

That is what I am doing essentially.
So, the code fsa.generateFile("XcD_PACKAGE.h",addIncludeFile()) does what you suggested, creating a file called XcD_PACKAGE.h and include the string resulted from the function addIncludeFile().

The problem is that the addIncludeFile function does not work properly, because the created XcD_PACKAGE file is empty.
Re: Adding another file into the generated src-gen folder with Xtext-xtend compiler [message #1240092 is a reply to message #1240086] Wed, 05 February 2014 17:25 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13493
Registered: July 2009
Senior Member
Hi,

can you please share a complete reproducable example (Junit Test / Project / ....)

and why dont you do this different e.g.

def addIncludeFile() {
val File incfile = new File("..\\!includefiles\\" + "XcD_PACKAGE.h")
if (ncfile.exists())
'''
«FOR String line : (new FileReader(incfile)).readLines»
«line»
«ENDFOR»
''' else '''File does not exist'''

}


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Adding another file into the generated src-gen folder with Xtext-xtend compiler [message #1240098 is a reply to message #1240092] Wed, 05 February 2014 17:42 Go to previous messageGo to next message
mcse mcse is currently offline mcse mcseFriend
Messages: 53
Registered: August 2012
Member
Thanks a lot for the reply.
I have just tried your code snippet. This time the output file include "File does not exist".
So, it seems that the file instance created by the code cannot let me reach the file.
Do you think that the initialisation parameter argument I passed to the File is correct ?
I think that it cannot find the file although I simply put it in the same package as Main.java.
Re: Adding another file into the generated src-gen folder with Xtext-xtend compiler [message #1240104 is a reply to message #1240098] Wed, 05 February 2014 18:03 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13493
Registered: July 2009
Senior Member
Hi,

i do not know where the file is?!?
maybe loading it with Main.class.getResourceAsStream or something like that works better...


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Adding another file into the generated src-gen folder with Xtext-xtend compiler [message #1240112 is a reply to message #1240104] Wed, 05 February 2014 18:21 Go to previous messageGo to next message
mcse mcse is currently offline mcse mcseFriend
Messages: 53
Registered: August 2012
Member
Hi,
I followed your advices and tried something like the below.
But this time I got an NullPointerException.
def String addIncludeFile() {
var ClassLoader cl = this.getClass().getClassLoader();
var InputStream in = cl.getResourceAsStream("/XcD_PACKAGE.h");
//val File incfile = new File(IOUtils.toString(in, "UTF-8"));
var InputStreamReader is = new InputStreamReader(in);
var BufferedReader br = new BufferedReader(is);
var String read = br.readLine();

while(read != null) {
    //System.out.println(read);
        read = read + br.readLine();

}
return read;
}



Below is the thrown exception details:

Exception in thread "main" java.lang.NullPointerException
at java.io.Reader.<init>(Reader.java:78)
at java.io.InputStreamReader.<init>(InputStreamReader.java:72)
at org.xtext.example.mydsl.generator.XcdGenerator.addIncludeFile(XcdGenerator.java:120)
at org.xtext.example.mydsl.generator.XcdGenerator.doGenerate(XcdGenerator.java:157)
at org.xtext.example.mydsl.generator.Main.runGenerator(Main.java:76)
at org.xtext.example.mydsl.generator.Main.main(Main.java:28)
Re: Adding another file into the generated src-gen folder with Xtext-xtend compiler [message #1240117 is a reply to message #1240112] Wed, 05 February 2014 18:35 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13493
Registered: July 2009
Senior Member
Hi,

i don't see where this is Xtext/Xtend specific. if you are not sure how todo this by hand have a look at google guava api e.g.

import com.google.common.base.Charsets
import com.google.common.io.Resources

class SomeClass {

	def static void main(String[] args) {
		println(Resources.toString(SomeClass.getResource("xxx.h"), Charsets.UTF_8))
	}

}


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Adding another file into the generated src-gen folder with Xtext-xtend compiler [message #1240149 is a reply to message #1240117] Wed, 05 February 2014 20:33 Go to previous messageGo to next message
mcse mcse is currently offline mcse mcseFriend
Messages: 53
Registered: August 2012
Member
Thanks a lot for the reply.
Can you please let me know what I am expected to write for "SomeClass" in your code snippet ?

I essentially added this code:
fsa.generateFile("XcD_PACKAGE.h",Resources.toString(Main.getResource("/XcD_PACKAGE.h"), Charsets.UTF_8));

When I then created a runnable jar file from my xtext project, this raises a java.lang.NullPointerException.

[Updated on: Wed, 05 February 2014 20:34]

Report message to a moderator

Re: Adding another file into the generated src-gen folder with Xtext-xtend compiler [message #1240151 is a reply to message #1240149] Wed, 05 February 2014 20:36 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13493
Registered: July 2009
Senior Member
sure,

someclass is the class the file to read is next.
are you sure the file is in the jar?
how does the url look like (the thing that comes out from SOmeClass.getResource
are you sure the resource name is correct ....
does it work if you call the main from eclipse?

can you please share a complete reproducable sample
(code that i can taske, build and run)


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Adding another file into the generated src-gen folder with Xtext-xtend compiler [message #1240152 is a reply to message #1240151] Wed, 05 February 2014 20:38 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13493
Registered: July 2009
Senior Member
btw did you try to get this running with a project/jar without any xtext things contained?

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Adding another file into the generated src-gen folder with Xtext-xtend compiler [message #1240154 is a reply to message #1240152] Wed, 05 February 2014 20:43 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13493
Registered: July 2009
Senior Member
btw why the /thing
slashes are bad


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
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 20:45 Go to previous messageGo to next message
mcse mcse is currently offline mcse mcseFriend
Messages: 53
Registered: August 2012
Member
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.
Re: Adding another file into the generated src-gen folder with Xtext-xtend compiler [message #1240158 is a reply to message #1240156] Wed, 05 February 2014 20:54 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13493
Registered: July 2009
Senior Member
HI,

still the question,
how does the folder structure inside the jar look like?


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Adding another file into the generated src-gen folder with Xtext-xtend compiler [message #1240159 is a reply to message #1240158] Wed, 05 February 2014 20:55 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13493
Registered: July 2009
Senior Member
P.S:

/xxxx are still a bad idea use xxxx


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Wed, 05 February 2014 20:55]

Report message to a moderator

Re: Adding another file into the generated src-gen folder with Xtext-xtend compiler [message #1240162 is a reply to message #1240159] Wed, 05 February 2014 21:04 Go to previous message
mcse mcse is currently offline mcse mcseFriend
Messages: 53
Registered: August 2012
Member
Hi Christian,

Thanks a lot for your all comments. When I removed from /xxxx the slash(/), the jar file produces the expected result, i.e., a folder which also includes a copy of the file.

Thanks again; problem resolved.
Previous Topic:EmbeddedEditor has missing key bindings
Next Topic:javadoc Problem with code compiled by Xtend 2.5.1
Goto Forum:
  


Current Time: Sun Jul 12 07:39:25 GMT 2020

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

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

Back to the top