Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Generating multiple files with xtend
Generating multiple files with xtend [message #717873] Mon, 22 August 2011 15:38 Go to next message
darkdigitaldream is currently offline darkdigitaldreamFriend
Messages: 7
Registered: August 2011
Junior Member
I've been learning about code generation with xtend, and I've run out of documentation to help me along.

I'm attempting to generate multiple files with xtend, specifically one file for each of a list of model elements. To use the fowlerdsl example as a reference, a java function is generated for each command in the state machine via the call to declareCommand(Command):

«FOR c : sm.commands»
	«c.declareCommand»
«ENDFOR»
//...more code generation...
def declareCommand(Command command){
}

When the fowlerDSL example generates the StateMachine, it uses a resource labelled "IFileSystemAccess fsa" who's entry point into the .xtext file is at the declaration of the doGenerate() function

My question, then, is how do I bring the IFileSystemAccess class from the doGenerate function into the declareCommand function, allowing me to generate a .java file for each command in the DSL? I've tried the following, with no luck:

«FOR c : sm.commands»
	«c.declareCommand, fsa»
«ENDFOR»
//...more code generation...
def declareCommand(Command command, IFileSystemAccess fsa){
   //I hope to call fsa.generateFile() here
}


Edit: code block formatting

[Updated on: Mon, 22 August 2011 15:38]

Report message to a moderator

Re: Generating multiple files with xtend [message #717880 is a reply to message #717873] Mon, 22 August 2011 15:52 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

what about calling fsa.generateFile() for/with each stuff you want to generate a file with?
e.g

fsa.generateFile("a.txt",'''a''')
fsa.generateFile("b.txt",'''b''')
fsa.generateFile("c.txt",'''c''')


~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Generating multiple files with xtend [message #717889 is a reply to message #717880] Mon, 22 August 2011 16:20 Go to previous messageGo to next message
darkdigitaldream is currently offline darkdigitaldreamFriend
Messages: 7
Registered: August 2011
Junior Member
If I understand your post correctly, your recommendation is relatively static compared to what I need. To illustrate:

«FOR c : sm.commands»
	«c.declareCommand»
«ENDFOR»

def declareCommand(Command command){
   fsa.generateFile("com"+command.Name+".java",myCommandGenFunction(command))
}


due to the looping nature of the FOR block, I cannot statically assign the file names and file contents as these values are dependent on the contents of each 'c' passed to declareCommand().

The above code fails because fsa is out of scope in the declareCommand function, but my attempts to either bring it in to the declareCommand function or allow it to be global within the entire .xtend IGenerator implementation class have all failed.
Re: Generating multiple files with xtend [message #717895 is a reply to message #717889] Mon, 22 August 2011 16:43 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
hi,

why not passing fsa as you would do it in java???

«FOR c : sm.commands»
	«c.declareCommand(fsa)»
«ENDFOR»

def declareCommand(Command command, IFileSystemAccess fsa){
   fsa.generateFile("com"+command.Name+".java",myCommandGenFunction(command))
}




~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
(no subject) [message #717912 is a reply to message #717889] Mon, 22 August 2011 16:43 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
hi,

why not passing fsa as you would do it in java???


«FOR c : sm.commands»
«c.declareCommand(fsa)»
«ENDFOR»

def declareCommand(Command command, IFileSystemAccess fsa){
fsa.generateFile("com"+command.Name+".java",myCommandGenFunction(command))
}




~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: (no subject) [message #717917 is a reply to message #717912] Mon, 22 August 2011 17:20 Go to previous message
darkdigitaldream is currently offline darkdigitaldreamFriend
Messages: 7
Registered: August 2011
Junior Member
This is exactly what I was looking for; The '« »' syntax is unfamiliar, and I was unaware of what was capable within them.

Thanks a million!
Previous Topic:[xtend] Ecore post processing
Next Topic:When the Xtextmydsl editor plugin is installed
Goto Forum:
  


Current Time: Thu Apr 25 02:30:30 GMT 2024

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

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

Back to the top