aboutsummaryrefslogtreecommitdiffstats
blob: 3307fb106f390878b614d42b23562272dc1ae6f5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
 * generated by Xtext
 */
package org.eclipse.emf.ecore.xcore.generator

import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.xtext.generator.IGenerator
import org.eclipse.xtext.generator.IFileSystemAccess
import org.eclipse.emf.ecore.xcore.XPackage
import com.google.inject.Inject
import org.eclipse.emf.ecore.xcore.util.MappingFacade
import org.eclipse.emf.ecore.xcore.XOperation
import static extension org.eclipse.xtext.xtend2.lib.EObjectExtensions.*
import org.eclipse.xtext.xbase.compiler.XbaseCompiler
import org.eclipse.xtext.xbase.compiler.IAppendable
import org.eclipse.xtext.xbase.compiler.StringBuilderBasedAppendable
import org.eclipse.emf.ecore.EcoreFactory
import org.eclipse.emf.codegen.ecore.genmodel.GenModelPackage
import org.eclipse.emf.codegen.ecore.genmodel.GenModel
import org.eclipse.emf.codegen.ecore.generator.Generator
import org.eclipse.emf.codegen.ecore.genmodel.generator.GenBaseGeneratorAdapter
import org.eclipse.emf.common.util.BasicMonitor
import org.eclipse.emf.ecore.xcore.mappings.XcoreMapper

class XcoreGenerator implements IGenerator {
	
	@Inject
	extension XcoreMapper mappings
	
	@Inject
	XbaseCompiler compiler
	
	override void doGenerate(Resource resource, IFileSystemAccess fsa) {
		val pack = resource.contents.head as XPackage
		// install operation bodies
		for (op : pack.allContentsIterable.filter(typeof(XOperation))) {
			val eOperation = op.mapping.EOperation
			val appendable = new StringBuilderBasedAppendable()
//			val expectedType = op.jvmOperation.returnType
			compiler.compile(op.body, appendable, null)
			eOperation.EAnnotations.add(createGenModelAnnotation("body", appendable.toString))
		}
		
		generateGenModel(resource.contents.filter(typeof(GenModel)).head)
	}
	
	def generateGenModel(GenModel genModel) {
		genModel.canGenerate = true
		val generator = new Generator()
		generator.input = genModel
		generator.generate(genModel, GenBaseGeneratorAdapter::MODEL_PROJECT_TYPE,
				new BasicMonitor());
	}
	
	def createGenModelAnnotation(String key, String value) {
		val result = EcoreFactory::eINSTANCE.createEAnnotation
		result.source = GenModelPackage::eNS_URI
		result.details.put(key, value)
		return result
	}
}