Commit 63380b3a authored by Pierre Smeyers's avatar Pierre Smeyers
Browse files

fixes

parent 8833c47a
Loading
Loading
Loading
Loading
+22 −9
Original line number Diff line number Diff line
@@ -208,20 +208,33 @@ def _migrate_tpl(tpl_desc, main_tpl_desc, project_dir: Path, var_prefix: str) ->
    # print(yaml.dump(tpl_spec, width=1000, default_flow_style=False, sort_keys=False))
    # print()
    first_non_comment_line: re.Match = next(re.finditer(r"^[^#]", tpl_content, flags=re.MULTILINE))
    print(f"first non-comment line: {first_non_comment_line}")
    tpl_content = tpl_content[0:first_non_comment_line.start(0)]+yaml.dump(tpl_spec, width=1000, default_flow_style=False, sort_keys=False)+"---\n"+tpl_content[first_non_comment_line.start(0):]

    # overwrite variables
    # for var in tpl["variables"]:
    #     tpl_content = re.sub(f"^  {var['name']}:.*$", yaml.dump(var, default_flow_style=False, sort_keys=False), tpl_content)
    new_vars = dict()
    for key, val in tpl["variables"].items():
        var_decl_match = next(re.finditer(f"^  {key}: *(.*)$", tpl_content, flags=re.MULTILINE), None)
        if var_decl_match:
            # replace existing declaration
            tpl_content = tpl_content[0:var_decl_match.start(1)]+val+tpl_content[var_decl_match.end(1):]
        else:
            # add declaration (end of variables section)
            new_vars[key] = val
    
    if new_vars:
        variables_block: re.Match = next(re.finditer("^variables:", tpl_content, flags=re.MULTILINE))
        first_block_after_variables: re.Match = next(re.compile(r"^[a-zA-Z#0-9\.]", flags=re.MULTILINE).finditer(tpl_content, pos=variables_block.end(0)))
        # insert new vars here
        tpl_content = tpl_content[0:first_block_after_variables.start(0)]+("\n".join([f"  {key}: {val}" for key, val in new_vars.items()]))+"\n\n"+tpl_content[first_block_after_variables.start(0):]

    # write
    with open(tpl_path, "w") as writer:
        writer.write(tpl_content)

    print(">> Variables sections:")
    print(yaml.dump({"variables": tpl["variables"]}, default_flow_style=False, sort_keys=False))
    print()
    # print(">> Variables sections:")
    # # print(yaml.dump({"variables": tpl["variables"]}, default_flow_style=False, sort_keys=False))
    # print(yaml.dump({"variables": new_vars}, default_flow_style=False, sort_keys=False))
    # print()

    return ret

@@ -233,7 +246,7 @@ def _migrate_readme(tpl_desc, project_dir: Path, var_input: list[tuple[TbcVar, O
        readme_content = reader.read()
    
    project_name = project_dir.name
    project_version = "TODO"
    project_version = "TODO.VERSION"

    # insert new usage chapter
    readme_content = readme_content.replace("## Usage", f"""## Usage
@@ -248,7 +261,7 @@ Add the following to your `gitlab-ci.yml`:
```yaml
include:
  # 1: include the component
  - component: gitlab.com/to-be-continuous/{project_name}/{tpl_desc['template_path'].split('/')[-1]}@{project_version}
  - component: gitlab.com/to-be-continuous/{project_name}/{tpl_desc['template_path'].split('/')[-1].split('.')[0]}@{project_version}
    # 2: set/override component inputs
    inputs:
      example-input: "TODO" # ⚠ this is only an example
@@ -263,7 +276,7 @@ include:
  # 1: include the template
  - project: 'to-be-continuous/{project_name}'
    ref: '{project_version}'
    file: '{tpl_desc['template_path']}'
    file: '/{tpl_desc['template_path']}'

variables:
  # 2: set/override template variables