recipes to build code generators for non-xtext models with xtend
TRANSCRIPT
Karsten Thoms & Holger Schill
Recipes to build Code Generators for Non-Xtext Models with Xtend_____
________
Templates a la Xtend
Minimal Generator with JSON Model
Generator with Xtext Framework on EMF UML Model
3,50€
8,70€
16,20€
Template Expressions
(aka String Concatenation)
GreyspaceShows what makes it in the generated code
Makes readable templates possible
GreyspaceIndentation of call is taken into account
Model Navigation
Model Navigation
val eClasses = myEPackage.EClassifiers.filter(EClass)
val abstractEClasses = eClasses.filter[isAbstract]
val namesOfAbstractClasses = abstractEClasses.map[name]
val commaSeparatedNamesOfAbstractClasses = namesOfAbstractClasses.join(',')
CommaSeparatedNamesOfAbstractClasses
Model Navigation
filter
map
fold
sortBy
head
reduce
Build your own! It’s just library!Higher-Order Functions
ExtensionsAdd new API where you need it
Extensions
Extensions
Dynamic Dispatch
Dynamic Dispatch
Dynamic DispatchGenerated Java
Dynamic Dispatch
IDE Support
Extract methodRenameRefactoring
Content AssistQuick Assist
Formatting
Type Hierarchy
Call Hierarchy
Syntax Coloring
Outline
etc…
Debugging
Modularization
Feature Models?
AOP?
Dependency Injection
’s easy usage
No Engine needed
No ContextObject needed
’s easy usage
No Engine needed
No ContextObject needed
At the end it’s feels like using Java
but better…
Write readable and understandable codeShould be self-evident but …
Write readable and understandable codeShould be self-evident but …
Make use of dependency injection
Guice
Use incremental code generators
Use incremental code generatorsFast turnarounds
Use incremental code generatorsFast turnarounds
Just having it run on a server is no option
The m - 1 Problem
BeanBeanObject
Reso
urce
Configuration File
BeanBeanObject
Reso
urce
BeanBeanObject
Reso
urce
ResourceSet
ResourceSet
The m - 1 Problem
BeanBeanObject
Reso
urce
Configuration File
BeanBeanObject
Reso
urce
BeanBeanObject
Reso
urce
It’s worth to think about a merging strategy
Incremental code generators in Xtext// generator APIfragment = generator.GeneratorFragment auto-inject {}
Incremental code generators in Xtext// generator APIfragment = generator.GeneratorFragment auto-inject {}
class MyDslGenerator implements IGenerator {
override void doGenerate(Resource resource, IFileSystemAccess fsa) {// fsa.generateFile('greetings.txt', 'People to greet: ' + // resource.allContents// .filter(typeof(Greeting))// .map[name]// .join(', '))
}}
Incremental code generators in Xtext// generator APIfragment = generator.GeneratorFragment auto-inject {}
class MyDslGenerator implements IGenerator {
override void doGenerate(Resource resource, IFileSystemAccess fsa) {// fsa.generateFile('greetings.txt', 'People to greet: ' + // resource.allContents// .filter(typeof(Greeting))// .map[name]// .join(', '))
}}
Customisable mechanism is taking care to recompile affected resources
Incremental code generators in Xtext// generator APIfragment = generator.GeneratorFragment auto-inject {}
class MyDslGenerator implements IGenerator {
override void doGenerate(Resource resource, IFileSystemAccess fsa) {// fsa.generateFile('greetings.txt', 'People to greet: ' + // resource.allContents// .filter(typeof(Greeting))// .map[name]// .join(', '))
}}
http://www.eclipse.org/Xtext/documentation/350_continuous_integration.html
Get Maven integration for free
Why „for Non-Xtext Models“?
• Xtext provides an infrastructure to generate code from Xtext DSL files
• Proper setup of Guide required
• Default Xtext Guice modules are only suitable for Xtext DSLs
• Not much code generators for non-Xtext models known in open source projects
Generator Process
1.Read Models
2.Validation
3.Execute Templates
4.Write to File-System
2,1 MB Guava Library 370 kB Xbase/Xtend Library 226 kB JSON Library (Gson) 60 kB CLI Library (JCommander) 4 Metamodel Classes 1 Xtend Code Template
Minimal Generator with JSON Model
JSON Input
Meta Classes
Xtend Code Template
Main CLI Class
1 Xtext Framework 14 MB of Libraries 1 (EMF) UML Model 1 MWE2 Workflow 1 Guice Configuration 1 Standalone Setup 1 Output Configuration some Xtend Templates
Generator with Xtext Framework on EMF UML Model
Xtext’s Generator Support• IGenerator Interface
• MWE components for reading models and executing the generator
• Dependency Injection support
• Eclipse build integration, Incremental build
• Different file access for plain Java and EFS
• Outlets
Generic Resource Service• Package org.eclipse.xtext.resource.generic contains
support for generic resources
• Requires EMF based models
• Makes resources for non-Xtext based EMF models available for the Xtext infrastructure
• Index
• compute IResourceDescription
• create IEObjectDescription
Xtend Code Template
Guice Runtime Module
Guice Runtime Module
Guice Runtime Module
Guice Runtime Module
!!
Guice Runtime Module
Guice Runtime Module
Standalone Setup
Generator Workflow
Generator Workflow
UI Integration
plugin.xml
Extension Factory
UI Builder Integration
Generate on file save
Build Integration
Import Organizing
Bon Appetit!
KEEP INFORMED
itemis.com / Blogs / Xtext
@kthoms
@holgerschill
@itemis
@chrdietrich
@franzbecker_io
Find the examples here: https://github.com/kthoms/xtend-codegen-examples