Skip to main content



      Home
Home » Modeling » TMF (Xtext) » Generating multiple files with xtend
Generating multiple files with xtend [message #717873] Mon, 22 August 2011 11:38 Go to next message
Eclipse UserFriend
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 11:38] by Moderator

Re: Generating multiple files with xtend [message #717880 is a reply to message #717873] Mon, 22 August 2011 11:52 Go to previous messageGo to next message
Eclipse UserFriend
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
Re: Generating multiple files with xtend [message #717889 is a reply to message #717880] Mon, 22 August 2011 12:20 Go to previous messageGo to next message
Eclipse UserFriend
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 12:43 Go to previous messageGo to next message
Eclipse UserFriend
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
(no subject) [message #717912 is a reply to message #717889] Mon, 22 August 2011 12:43 Go to previous messageGo to next message
Eclipse UserFriend
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
Re: (no subject) [message #717917 is a reply to message #717912] Mon, 22 August 2011 13:20 Go to previous message
Eclipse UserFriend
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 Jul 17 17:52:46 EDT 2025

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

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

Back to the top