| modify the signature of "doGenerate" function [message #1769774] |
Thu, 03 August 2017 06:07  |
Eclipse User |
|
|
|
Hi, here I am again :)
I have a setup where my resource constitutes of many imported files. Among which only 1 is the main file for which the code should be generated. My idea was then to let the user enter the name of this file as command line argument. and then in the Generator, loop through all the files to check if the name matches.
To achieve this, I have to modify the signature of :
generate(Resource input, IFileSystemAccess2 fsa, IGeneratorContext context) in the GeneratorDelegate.class
and also the
doGenerate(Resource input, IFileSystemAccess2 fsa, IGeneratorContext context)
My question now is to know if this is doable if not, what is a better approach for this: Below is a snippet of what I wish to achieve.
In Main.xtend:
generator.generate(resource, fileAccess, context, systemName)
In DslGenerator.xtend:
class DznGenerator extends AbstractGenerator {
private CharSequence systemToGenerate
override void doGenerate(Resource resource, IFileSystemAccess2 fsa, IGeneratorContext context, CharSequence systemName) {
systemToGenerate = systemName
val root = resource.allContents.head as ModelDeclarationList ;
if (root != null)
{
for (model : root.models)if (model.name.equals(systemToGenerate) ) //Do something
}
}
}
I tried to alter their declaration in their base classes/interfaces but the files seem to be read-only.
Thanks in advance
|
|
|
| Re: modify the signature of "doGenerate" function [message #1769775 is a reply to message #1769774] |
Thu, 03 August 2017 06:17  |
Eclipse User |
|
|
|
I would rather implement it that the generator will only get those resources that should code be generated for. You will need to read all resources into one ResourceSet in order to resolve references, but then select a subset of those for which the generator is called for.
Xtext's MWE workflow components do it like that, and you could just use them in your generator. If you don't want to write a .mwe2 file and execute that just instantiate the required context (a Workflow, WorkflowContext, put and configure components) in the generator code.
The components you are looking for are org.eclipse.xtext.mwe.Reader and org.eclipse.xtext.generator.GeneratorComponent. They communicate through the workflow and a 'Slot'. The SlotEntry could be added to the Reader with a filter. GeneratorComponent will pick a collection of resources through a WorklowContext's slot.
|
|
|
Powered by
FUDForum. Page generated in 0.03572 seconds