This module provides the SDK for implementing code generating targets
Other than normal Handlebars syntax, the codegen uses the #FILENAME:
syntax in template output to define the file path and file permissions for the generated file(s). This allows each template to determine the path and file permissions for the file(s) from within the template itself.
At the start of any template output, you can specify #FILENAME:
followed by the file path and optional parameters. This line controls the generation of single or multiple files.
An example could be:
#FILENAME:{{type data.name}}.ts:write-always:644
class {{type data.name}} {
}
The syntax of the filename line is:
#FILENAME:<file path>[:<write mode=write-always>[:<file permissions=644>]]
Where:
file path
: The path of the file.write mode
: Defaults towrite-always
. Options include:create-only
: Only writes the file if it doesn't exist. Ideal for generating boilerplate code that the user will need to modify to help speed things along.write-always
: Always overwrites the file, meaning any user changes will be overwritten. These files should not be adjusted by users.skip
: Does not generate the file.merge
: Attempts to merge generated code with any changes made by the user. Currently not supported.
file permissions
: The chmod value to apply to the file, useful if you need to generate executable files. Defaults to 644.
For generating a single file, the #FILENAME:
syntax must be the first line of the template. The rest of the template will be the content of the file.
Example:
#FILENAME:<file path>
...template content...
The #FILENAME:
syntax can also be used multiple times (or, for example, within loop constructs) to generate multiple files based on the loop's context.
Syntax within a loop:
{#each [context]}
#FILENAME:<file path based on context>
...template content...
{{/each}}
This enables the dynamic generation of file paths and contents based on the context of each iteration within the loop.
There is a list of built-in helpers which is documented below
Simply make string lowercase
Make opening or closing curl braces. Useful when handlebars gets confused
Iterates through a map and provides key of map as "propertyId"
Provides a switch structure to the template
Only renders if a block consumes / provides a certain kind of resource
Iterates through all consumers or providers of a given type.
metadata
contains the metadata of the consumer / provider
spec
contains the spec of the consumer / provider
Renders once for every type reference ( {$ref:'Type'}
) found in a data structure.
Useful when needing to import / require these references into the current file.
Default behaviour is to only include data structures / entities. If you want to include enum values as well simply add the "all=true" argument:
There are more formatters and you can add additional ones specific to the language target you're building. See the CodeFormatter class. Each method in the codeformatter is available in the template without the $
. E.g. CodeFormatter::$returnType
is available as
Template names are basically the kind
for both blocks and resources.
Templates for service blocks. Should contain core boilerplate for running, building and testing a service block for this language target - if applicable.
Templates for frontend blocks. Should contain core boilerplate for running, building and testing a frontend block for this language target - if applicable.
Templates for entities. Should handle both data structures and enums.
Templates for MongoDB. Should render setup for using a MongoDB Database.
Templates for Postgres. Should render setup for using a Postgres Database.
Templates for REST APIs. Should render code that provides a REST API for the block
Templates for REST Clients. Should render code that provides a REST Client for the block
NOTE: It is usually needed, for in particular resource types, to have some sort of Kapeta-specific SDK backing the code generation. Meaning the generated code uses a pre-built SDK.
This is because of how Kapeta automatically makes databases available Just-in-Time and to keep Kapeta in control of the traffic flowing between blocks - also locally.
This module provides a few different things to aid in code generation using handlebars templates.
The most important class in this library - which all language targets should extend.
It takes options
, baseDir
and a formatter
as arguments.
options
is simply passed on to the templatesbaseDir
is the basedir of the language target. It expects a folder namedtemplates
to be in the baseDir which contains all templates.
The formatter is either the CodeFormatter or a sub-class of that.
See below for more on CodeFormatters.
The main method for generating code using Target
is Target::generate(data, context)
. It will pass both data and context to the templates to be used.
It expects a kind
property to exist on the "data" argument - which is how it determines which template to use.
The template(s) is determined by baseDir and kind - e.g. for kind: "my.resource.type"
it will look for a folder named: {baseDir}/templates/my.resource.type
. It will then iterate all files in that folder and generate code based on each of them.
The code formatting base class contains a number of methods for formatting different types of common code structures
This is to make it easy to extend and override this formatting for different languages and conventions.
Contains a methods that sets up handlebars and otherwise helps with handlebar rendering.
The create method is called internally from Target and should usually not be used directly.
Template.create(data, context, codeFormatter);
The SafeString method is simply wrapping handlebars safestring - to allow for rendering strings that handlebars otherwise would escape.
Template.SafeString(someString);
This project is licensed under the MIT License - see the LICENSE file for details