Pure ecore to Java by Xtend2 [message #701697] |
Mon, 25 July 2011 09:13  |
Eclipse User |
|
|
|
Hi,
a few days ago i started with Xtend2 to generate java code based on a pure ecore file.
I have created different Xtend2 files which are producing java code by Xtend2 richStrings. The Xtend2 files are dealing with the dyanmic emf api to access the required information.
So far everything was really easy. -> Thanks super cool Xtend2!
But right now all outputs are written to stdout console. And that is my problem!
I do not now how i can integrate a pure ecore file (no Xtext grammar exists!) into a mwe2 workflow and how to use the predefined Xtend2 files to generate code to different bundles. I'd like to invoke the generation manually (No auto compile by builders).
After browsing the web I do not have a solution yet!
I found an interesting blog post from Sebastian Zarnekow. His example seams to persist the generated output to a file using the injected extension JavaGeneratorUtils. But i could not find it. (toFileWith('''.....'''))
http://zarnekow.blogspot.com/2011/02/xtend2-whats-in-pipeline.html
Do i have to write my own Generator similar to org.eclipse.xtext.generator.Generator?
As i can remember, Xpand offered an expression called <<FILE ...>> which allowed to specify a target file that was generated inside a java package. Is there a similar access available?
Thanks a lot for your kind help,
Florian Pirchner
|
|
|
|
|
Re: Pure ecore to Java by Xtend2 [message #701969 is a reply to message #701956] |
Mon, 25 July 2011 15:54   |
Eclipse User |
|
|
|
Hi,
as i said before: this works out of the box
module test.Test
Workflow {
component = org.eclipse.xtext.ecore.EcoreSupport {}
component = org.eclipse.xtext.mwe.Reader {
// lookup all resources on the classpath
// useJavaClassPath = true
// or define search scope explicitly
path = "model"
// this class will be generated by the xtext generator
register = test.TestSetup {}
loadResource = {
slot = "model"
}
}
component = org.eclipse.xtext.generator.GeneratorComponent {
register = test.TestSetup {}
slot = 'model'
outlet = {
path = "src-gen"
}
}
}
package test;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.xtext.ISetup;
import org.eclipse.xtext.service.AbstractGenericModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
public class TestSetup implements ISetup {
@Override
public Injector createInjectorAndDoEMFRegistration() {
return Guice.createInjector(new AbstractGenericModule(){
public Class<? extends ResourceSet> bindResourceSet() {
return ResourceSetImpl.class;
}
public Class<? extends org.eclipse.xtext.generator.IGenerator> bindIGenerator() {
return test.TestGenerator.class;
}
});
}
}
package test
import org.eclipse.xtext.generator.IGenerator
import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.xtext.generator.IFileSystemAccess
import static extension org.eclipse.xtext.xtend2.lib.ResourceExtensions.*
import org.eclipse.emf.ecore.EPackage
import org.eclipse.emf.ecore.EClass
class TestGenerator implements IGenerator {
override void doGenerate(Resource resource, IFileSystemAccess fsa) {
for(p: resource.allContentsIterable.filter(typeof(EPackage))) {
fsa.generateFile(p.fileName, p.compile)
}
}
def fileName(EPackage p) {
p.name+".txt"
}
def compile(EPackage p) '''
«FOR c : p.EClassifiers.filter(typeof(EClass))»
class «c.name»
«ENDFOR»
'''
}
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: test.emf
Bundle-SymbolicName: test.emf
Bundle-Version: 0.1.0
Require-Bundle: org.eclipse.emf.ecore,
org.eclipse.xtext.xbase.lib;bundle-version="2.0.0",
org.eclipse.xtext.xtend2.lib;bundle-version="2.0.0",
org.eclipse.xtext;bundle-version="2.0.0",
org.eclipse.xtext.ecore;bundle-version="2.0.0",
org.eclipse.emf.mwe2.launch;bundle-version="2.0.0",
org.apache.log4j;bundle-version="1.2.15",
org.apache.commons.logging;bundle-version="1.0.4"
the folder "model" contains the -ecore files to process
~Christian
[Updated on: Mon, 25 July 2011 16:11] by Moderator
|
|
|
Re: Pure ecore to Java by Xtend2 [message #701976 is a reply to message #701969] |
Mon, 25 July 2011 16:12  |
Eclipse User |
|
|
|
Hi,
thanks a lot!
For hours I tried different approaches but all ended up in exceptions.
Now i got the missing link. You are using the ISetup to configure the component.
You do not have to write your own generatorComponent since you configure the xtext generator component with you TestGenerator.
Sooo many thanks for you help!
Florian Pirchner
|
|
|
Powered by
FUDForum. Page generated in 0.03040 seconds