Calculated Variables

A calculated variable is a variable that is deduced from a user-provided value.

They do not appear in the template questionaire and therefore cannot be modified by the user.

Calculated template variables are managed in the context file within the template:

{# Example: remote_url = git@git01.iis.fhg.de:ks-ip-lib/software/sample_project.git #}
{# |----------------|---------|-----------------------| #}
{# domain group pages_path #}
{# |---------------------------------| #}
{# path #}
{# remote_url_https = https://git01.iis.fhg.de/ks-ip-lib/software/sample_project/ #}
{# remote_url_pages = https://ks-ip-lib.git01.iis.fhg.de/software/sample_project/ #}

{% set domain = (remote_url | replace("git@", "")).split(":")[0] %}
{% set path = remote_url.split(":")[1].replace(".git", "") %}
{% set group = path.split("/")[0] %}
{% set pages_path = "/".join(path.split("/")[1:]) %}

{% set remote_url_https = "https://" + domain + "/" + path %}

{% if remote == 'github' %}
{% set domain_pages = 'github.io' %}
{% set path_pipeline = '/actions?query=branch%3A' + default_branch %}
{% elif remote == 'gitlab-iis' %}
{% set domain_pages = domain %}
{% elif remote == 'gitlab-fhg' %}
{% set domain_pages = 'pages.fraunhofer.de' %}
{% endif %}

{% if remote.startswith('gitlab') %}
{% set path_pipeline = '/-/pipelines' %}
{% endif %}

{% set remote_url_pages = "https://" + group + "." + domain_pages + "/" + pages_path %}
{% set remote_url_pipeline = remote_url_https + path_pipeline %}

{% if remote == 'github' %}
{# coverage badge is provided by gitlab #}
## coverage badge is generated by github action and published on github pages
{% set remote_url_coverage_badge = remote_url_pages + '/badges/coverage.svg' %}
## pipeline badge is provided by github
{% set remote_url_pipeline_badge = remote_url_https + '/actions/workflows/ci.yaml/badge.svg' %}
{% else %}
## coverage and pipeline badges are provided by gitlab
{% set remote_url_coverage_badge = remote_url_https + '/badges/' + default_branch + '/coverage.svg' %}
{% set remote_url_pipeline_badge = remote_url_https + '/badges/' + default_branch + '/pipeline.svg' %}
{% endif %}

{% set cli_command = package_name | replace("_", "-") %}
template/context, lines 1-

This context can then be imported using the jinja import command.

As an example, this was used in the README template to reuse the remote_url_pages and remote_url_https URLs, which are calculated from the remote_url provided by the user:

# {{project_name}}

{%- import 'template/context' as ctx with context %}

[![badge_documentation][]][documentation] [![badge_pipeline][]][pipeline] [![badge_coverage][]][coverage] [![badge_maintainability][]]()

[documentation]: {{ctx.remote_url_pages}}
[badge_documentation]: https://img.shields.io/badge/Documentation-{{default_branch}}-blue
template/README.md.jinja, lines 1-8