Skip to content

RBirdwatcher/rosetta-code-generators

 
 

Repository files navigation

Rosetta Code Generators

Continuous Integration: Codefresh build status
License: Apache 2.0

JavaDoc: Coming soon

Did you want to leverage the power of the Rosetta DSL for your project but in a language other than the default (Java) implementation?

You can use this guide and write your own code generator in the language of your choosing.

What are the Rosetta Code Generators

Rosetta is an open source DSL comprising a grammar and a set of default code generators (see documentation). The open source Rosetta DSL repository has 1 built-in code generator:

The Rosetta Code Generators repository allows the community to contribute code generators in other languages: e.g. Go, Python etc. This repository currently provides 1 additional code generator:

Code generation works by allowing API hooks to get access to the Ecore model, which represents the model elements in the Rosetta DSL and allows to perform a model transformation. The API expects a set Rosetta files (with extension .rosetta) as input. The files are parsed and an Ecore model instance is produced. This Ecore model is then accessible via an API hook in this repo.

A comprehensive guide on how to write Rosetta files will be available soon.

Here is an illustration of how code generation works:

Quick start guide

You will need Maven and Git installed and configured in your environment.

Fork and clone the project in your own workspace. Then run the first build:

/path/to/workspace/rosetta-code-generators > mvn clean install

This project follows the Maven multi-module format, to make it easier to provide your own generator in its own separate module.

Simply come up with a sensible name for your module (it should relate to the progamming language that you want to generate code in) and run the following command:

> mvn archetype:generate -DgroupId=com.regnosys.rosetta.code-generators  -DartifactId=my-language

This will create a module named after your artifactId with the appropriate maven structure and also update the parent pom.xml.

Writing a generator

There is already an example module named sample to help you get going: we have written a rudimentary code generator (that generates some valid Groovy code):

sample/src/main/java/com/regnosys/rosetta/generators/sample/SampleCodeGenerator.java

Within your just created module, create your own package under com/regnosys/rosetta/generators and add your source file(s). Your generator must subclass the AbstractExternalGenerator class and provide a concrete implementation of its generate method.

public abstract Map<String, ? extends CharSequence> generate(RosettaJavaPackages packages, List<RosettaRootElement> elements, String version);

Testing your generator

You can then test your code with a JUnit test, like in

sample/src/test/java/com/regnosys/rosetta/generators/sample/SampleCodeGeneratorTest.java

In folder sample/src/test/resources/rosetta you can see the file: sample.rosetta. It contains a simple Rosetta text file with a few attributes of type string & int.

Finally, the file sample/src/test/resources/sample/Foo.groovy.sample contains the correct source code, against which we will compare our results.

The test-helper module contains some infrastructure code that is used to drive the tests in the other modules. It weaves together, using the Google Guice dependency injection mechanism, all the necessary elements to run a Rosetta-enabled application and parses a .rosetta file into the corresponding root Ecore object.

In folder test-helper/src/main/resources/rosetta you can see the file: types.rosetta. It contains the basic types contained in the Rosetta DSL, like string, int, time etc.

These types are used to bootstrap the Rosetta-enabled application.

How to contribute

Please read the detailed guide.

About

Write code generators for any language, based on the rosetta DSL

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Xtend 64.8%
  • Java 35.2%