recipes to build code generators for non-xtext models with xtend

67
Karsten Thoms & Holger Schill Recipes to build Code Generators for Non-Xtext Models with Xtend _____ ________

Upload: karsten-thoms

Post on 12-Apr-2017

333 views

Category:

Engineering


4 download

TRANSCRIPT

Page 1: Recipes to build Code Generators for Non-Xtext Models with Xtend

Karsten Thoms & Holger Schill

Recipes to build Code Generators for Non-Xtext Models with Xtend_____

________

Page 2: 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€

Page 3: Recipes to build Code Generators for Non-Xtext Models with Xtend
Page 4: Recipes to build Code Generators for Non-Xtext Models with Xtend

Template Expressions

(aka String Concatenation)

Page 5: Recipes to build Code Generators for Non-Xtext Models with Xtend

GreyspaceShows what makes it in the generated code

Makes readable templates possible

Page 6: Recipes to build Code Generators for Non-Xtext Models with Xtend

GreyspaceIndentation of call is taken into account

Page 7: Recipes to build Code Generators for Non-Xtext Models with Xtend

Model Navigation

Page 8: Recipes to build Code Generators for Non-Xtext Models with Xtend

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

Page 9: Recipes to build Code Generators for Non-Xtext Models with Xtend

filter

map

fold

sortBy

head

reduce

Build your own! It’s just library!Higher-Order Functions

Page 10: Recipes to build Code Generators for Non-Xtext Models with Xtend

ExtensionsAdd new API where you need it

Page 11: Recipes to build Code Generators for Non-Xtext Models with Xtend

Extensions

Page 12: Recipes to build Code Generators for Non-Xtext Models with Xtend

Extensions

Page 13: Recipes to build Code Generators for Non-Xtext Models with Xtend

Dynamic Dispatch

Page 14: Recipes to build Code Generators for Non-Xtext Models with Xtend

Dynamic Dispatch

Page 15: Recipes to build Code Generators for Non-Xtext Models with Xtend

Dynamic DispatchGenerated Java

Page 16: Recipes to build Code Generators for Non-Xtext Models with Xtend

Dynamic Dispatch

Page 17: Recipes to build Code Generators for Non-Xtext Models with Xtend

IDE Support

Extract methodRenameRefactoring

Content AssistQuick Assist

Formatting

Type Hierarchy

Call Hierarchy

Syntax Coloring

Outline

etc…

Debugging

Page 18: Recipes to build Code Generators for Non-Xtext Models with Xtend

Modularization

Page 19: Recipes to build Code Generators for Non-Xtext Models with Xtend

Feature Models?

Page 20: Recipes to build Code Generators for Non-Xtext Models with Xtend

AOP?

Page 21: Recipes to build Code Generators for Non-Xtext Models with Xtend

Dependency Injection

Page 22: Recipes to build Code Generators for Non-Xtext Models with Xtend

’s easy usage

No Engine needed

No ContextObject needed

Page 23: Recipes to build Code Generators for Non-Xtext Models with Xtend

’s easy usage

No Engine needed

No ContextObject needed

At the end it’s feels like using Java

but better…

Page 24: Recipes to build Code Generators for Non-Xtext Models with Xtend

Write readable and understandable codeShould be self-evident but …

Page 25: Recipes to build Code Generators for Non-Xtext Models with Xtend

Write readable and understandable codeShould be self-evident but …

Page 26: Recipes to build Code Generators for Non-Xtext Models with Xtend

Make use of dependency injection

Guice

Page 27: Recipes to build Code Generators for Non-Xtext Models with Xtend

Use incremental code generators

Page 28: Recipes to build Code Generators for Non-Xtext Models with Xtend

Use incremental code generatorsFast turnarounds

Page 29: Recipes to build Code Generators for Non-Xtext Models with Xtend

Use incremental code generatorsFast turnarounds

Just having it run on a server is no option

Page 30: Recipes to build Code Generators for Non-Xtext Models with Xtend

The m - 1 Problem

BeanBeanObject

Reso

urce

Configuration File

BeanBeanObject

Reso

urce

BeanBeanObject

Reso

urce

ResourceSet

Page 31: Recipes to build Code Generators for Non-Xtext Models with Xtend

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

Page 32: Recipes to build Code Generators for Non-Xtext Models with Xtend

Incremental code generators in Xtext// generator APIfragment = generator.GeneratorFragment auto-inject {}

Page 33: Recipes to build Code Generators for Non-Xtext Models with Xtend

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(', '))

}}

Page 34: Recipes to build Code Generators for Non-Xtext Models with Xtend

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

Page 35: Recipes to build Code Generators for Non-Xtext Models with Xtend

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

Page 36: Recipes to build Code Generators for Non-Xtext Models with Xtend

more than just a code-gen language…

http://www.xtend-lang.org/

Page 37: Recipes to build Code Generators for Non-Xtext Models with Xtend

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

Page 38: Recipes to build Code Generators for Non-Xtext Models with Xtend

Generator Process

1.Read Models

2.Validation

3.Execute Templates

4.Write to File-System

Page 39: Recipes to build Code Generators for Non-Xtext Models with Xtend

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

Page 40: Recipes to build Code Generators for Non-Xtext Models with Xtend

JSON Input

Page 41: Recipes to build Code Generators for Non-Xtext Models with Xtend

Meta Classes

Page 42: Recipes to build Code Generators for Non-Xtext Models with Xtend

Xtend Code Template

Page 43: Recipes to build Code Generators for Non-Xtext Models with Xtend

Main CLI Class

Page 44: Recipes to build Code Generators for Non-Xtext Models with Xtend
Page 45: Recipes to build Code Generators for Non-Xtext Models with Xtend

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

Page 46: Recipes to build Code Generators for Non-Xtext Models with Xtend

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

Page 47: Recipes to build Code Generators for Non-Xtext Models with Xtend

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

Page 48: Recipes to build Code Generators for Non-Xtext Models with Xtend

Xtend Code Template

Page 49: Recipes to build Code Generators for Non-Xtext Models with Xtend

Guice Runtime Module

Page 50: Recipes to build Code Generators for Non-Xtext Models with Xtend

Guice Runtime Module

Page 51: Recipes to build Code Generators for Non-Xtext Models with Xtend

Guice Runtime Module

Page 52: Recipes to build Code Generators for Non-Xtext Models with Xtend

Guice Runtime Module

!!

Page 53: Recipes to build Code Generators for Non-Xtext Models with Xtend

Guice Runtime Module

Page 54: Recipes to build Code Generators for Non-Xtext Models with Xtend

Guice Runtime Module

Page 55: Recipes to build Code Generators for Non-Xtext Models with Xtend

Standalone Setup

Page 56: Recipes to build Code Generators for Non-Xtext Models with Xtend

Generator Workflow

Page 57: Recipes to build Code Generators for Non-Xtext Models with Xtend

Generator Workflow

Page 58: Recipes to build Code Generators for Non-Xtext Models with Xtend

UI Integration

Page 59: Recipes to build Code Generators for Non-Xtext Models with Xtend

plugin.xml

Page 60: Recipes to build Code Generators for Non-Xtext Models with Xtend

Extension Factory

Page 61: Recipes to build Code Generators for Non-Xtext Models with Xtend
Page 62: Recipes to build Code Generators for Non-Xtext Models with Xtend

UI Builder Integration

Generate on file save

Page 63: Recipes to build Code Generators for Non-Xtext Models with Xtend
Page 64: Recipes to build Code Generators for Non-Xtext Models with Xtend

Build Integration

Page 65: Recipes to build Code Generators for Non-Xtext Models with Xtend

Import Organizing

Page 66: Recipes to build Code Generators for Non-Xtext Models with Xtend

Bon Appetit!

Page 67: Recipes to build Code Generators for Non-Xtext Models with Xtend

KEEP INFORMED

itemis.com / Blogs / Xtext

@kthoms

@holgerschill

@itemis

@chrdietrich

@franzbecker_io

Find the examples here: https://github.com/kthoms/xtend-codegen-examples