Create and publish your own project templates in SAP Business Application Studio
How many times have you thought “I wish I could save this application as a template”, or “I wish I could easily generate this code and share it with others”?
Well, we’ve thought about this too! With SAP Business Applications Studio (aka BAS), you can create a template using the open-source Yeoman for project generation framework and share it with others. You can’t imagine how simple it is.
If you are thinking of creating a template in BAS, this blog is for you.
In this blog I will show you how to convert your yeoman generator into a BAS template. But don’t worry, it’s worth the effort because most BAS users are using the ‘Template wizard’ to create their new projects. The wizard contains a step-by-step graphical user interface, and it also allows users to explore and select the project template that best fits their needs.
Note that when customers use your template, they will become the owners of the code they write.
Before you start, keep in mind that the Yeoman generator should be short (we recommend up to 6 or 7 questions/decisions), and it should generate a complete set of resources that the developer can build/run/deploy. It is OK not to provide an answer for all the questions (e.g., it may not be bound to a service). The generator can create placeholders that can be replaced by the user or another generator at a later stage. Each generator should create a markdown file noting what was generated and how to proceed.
The flexibility acquired from this type of template fits the ‘Grow-as-you-go’ concept we believe in, where the developers can run generators whenever they need them.
Let’s dive to understand how this is done!
Yeoman Generator Creation
To create your template, you must first create a yeoman generator on which it will be based.
To do so, please follow the steps in the Yeoman documentation.
Making your generator accessible as a template
Once you have created a template based on your yeoman generator, you want to make it accessible from BAS so that your users can search for it in the BAS Template wizard.
The steps for making this happen are different if you are an extension developer for BAS or not.
If you already have a VS Code extension
- Declare your Yeoman generator as being of type `project` in your generator’s ‘package.json‘ file.
- Use the following example in your generator’s ‘package.json‘ file https://github.com/SAP/yeoman-ui/blob/master/packages/generator-foodq/package.json
If you don’t have a VS Code extension
- Upload your generator to the Yeoman generator registry, and then install it in BAS. To do so, in the command palette, enter ‘Template Wizard’, then click Explore and Install Generators from the upper-right corner of the Template wizard.
- The Explore and Install Generators tool opens in a new tab showing the generators from the Yeoman generator registry. Click Install. The generator is added to your machine and can now be selected from the Template Wizard. This will make your template visible in the Template Wizard.
Best practices for writing a Yeoman Generator for BAS
Provide a prompt(s) and the questions (within each prompt) used in the wizard, including the default answers.
Prompts can be added to the metadata so that the developers can have an indication of which steps they have left. If there are no prompts in the metadata, or the generator adds prompts dynamically, then they are added dynamically to the UI.
The Yeoman Player receives prompts and renders them to steps and questions according to the question type (input, list, confirmation, etc.). The player is an extensible component that can support more question and custom types in the future.
Each template contains the following metadata:
- List of the prompts to show in the UI
- More information
To render your generator well in the wizard, follow these guidelines:
- Group several questions into a single prompt – all questions in the same prompt will render as a single wizard step.
- Guide the user by showing the prompts in advance.
You can further enhance your generator by adding subgenerators, adding hints and questions, adding a logger, and more, see this example.
I hope this helps you create tons of useful templates for your users 🙂 . Feel free to reach out to me if you have any questions.