If you are new to MPS and know that you want to use it to generate certain code and integrate the generated code eventually into your system but don’t know how to approach this task, below is an idea to get you started.

As a prerequisite, you need to have an example of the code you want to generate from MPS. If you are not sure yet, first write the necessary code by hand and come back with a small working example.

First, create a new project in MPS. Choose Language project in the wizard, and check the Create Sandbox Solution checkbox.

Add a single concept to the language that was created for you. A good name for the concept would be the artifact that you want to generate from it: Microservice, Program, TaxCalculation, Example. Don’t sweat too much, you can always change the name later without much work.

Do not add any properties nor children to the concept, just set the “can be root” flag to true.

Next, add generator rules for the concept. Since you know what files you need to generate, add a root mapping rule for each file you need. If you need to generate Java or XML files you can use the languages bundled with MPS. If you need to generate something else, install the plaintextgen plugin and use that (later you should switch to project libraries but don’t bother for now).

Each root mapping rule is associated with a root template. This is a root node that serves as a template, to be decorated with generator macros to modify it at run time according to the input. For now, ignore everything about macros and just modify the root templates directly to exactly match the code that you want to generate.

Don’t bother arranging the output files into proper directory structure. MPS will put all the files it generates into the source_gen subdirectory of the solution directory. Java files will be put into further subdirectories according to their model name, other files will be placed directly under source_gen. The directory structure can be adjusted later.

Build the language. Fix any errors.

Next, go to the sandbox model in the sandbox solution that MPS created for you (or create one yourself if you didn’t check the checkbox when creating the project). Add a single new root node of the concept that you have just created.

Build the model. Check the source_gen directory of the sandbox solution for the generated files. If you cannot find where MPS put the generated files, the directory is listed in the properties of the solution as Generator output path:

At this point, you should have the generated files in the ouptut directory. Congratulate yourself, for this means that you have successfully implemented the end-to-end journey from a concept in MPS to the generated code.

The generated code probably contains lots of repetition and hardcoded strings that should depend on the data in the model. Your next job is to look for this repetition in the generated code and extract it into the model. A good first step in this direction is enhancing the single concept we created with a name by implementing INamedConcept and then using the name in the names and contents of the generated files.