Docs
Declarations
Examples
Generate file on build

Generating Files Dynamically

AIOP allows generating files dynamically during the build phase. This can be useful for generating configuration files, data files, specific code files based on your declaration.

The principle is to create a template with predefined tags as well as custom tags that will be replaced by values defined in the resource declaration. To do this, you must use the FreeMarker template format (opens in a new tab). AIOP relies on the tool FMPP or Jinja to generate files dynamically.

Conditions

To use fmpp, you must install the dependency fmpp on your machine (see installation). No specific installation is required for jinja. To generate the file dynamically, you must specify the template engine fmpp or jinja in the template field.

Declaration

For example, you can declare a file template that will generate a different readme for each system and client.

    • .aiop
      • .aml
      • readme.md
  • resources/.aml
    - source: readme.md
      destination: readme.md
      template: fmpp
      compatibility:
        - archi1/model1:
            apps: [app]
            clients: [client1, client2]
          template_data:
            version: 1.0
        - archi1/model2:
            apps: [app]
            clients: [client1, client2]
          template_data:
            version: 2.0

    Here, the file readme.md is a template that will be used to generate the file readme.md. We have declared the resource for two specific system models. They both contain additional data to use in the FreeMarker template with the additional field template_data. This field is optional. Let's take an example content of the readme.md file:

    readme.md
    # README
    Version: ${version}
    Target: ${system_target.name}
    <#if system_target.args.clients=="client1">
    <#assign client=system_target.args.clients>
    
    This message is for ${client?cap_first}... keep it secret...
    </#if>
    
    Here's a completely normal readme...

    You'll notice that there are FreeMarker tags ${version} and ${system_target...}. The ${version} tag is a custom tag specific to a given system. However, the ${system_target...} tag is a predefined tag by AIOP. It allows retrieving values from the resource declaration (the list of predefined values is available here).

    In this case, if we request to generate a package for the model archi1/model1 with the client client1, the generated readme.md file will be:

    readme.md
    # README
    Version: 1.0
    Target: archi1/model1
     
    This message is for Client1... keep it secret...
     
    Here's a completely normal readme...

    However, if we request to generate a package for the model archi1/model2 with the client client1, the generated readme.md file will be:

    readme.md
    # README
    Version: 2.0
    Target: archi1/model2
     
    This message is for Client1... keep it secret...
     
    Here's a completely normal readme...

    Continuing with the comparison, if we request to generate a package for the model archi1/model2 with the client client2, the generated readme.md file will be:

    readme.md
    # README
    Version: 2.0
    Target: archi1/model2
     
    Here's a completely normal readme...

    Going Further

    To learn more about the template system, please refer to the FMPP Template doc or Jinja Template doc.