diff --git a/library/yaml_format.py b/library/yaml_format.py new file mode 100755 index 0000000..4e00117 --- /dev/null +++ b/library/yaml_format.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python3 + +import yaml +import sys + +# https://stackoverflow.com/questions/45004464/yaml-dump-adding-unwanted-newlines-in-multiline-strings +yaml.SafeDumper.org_represent_str = yaml.SafeDumper.represent_str + +# Represent strings as multiline within YAML file, if the string contains the newline character +def repr_str(dumper, data): + if '\n' in data: + return dumper.represent_scalar(u'tag:yaml.org,2002:str', data, style='|') + return dumper.org_represent_str(data) + + +yaml.add_representer(str, repr_str, Dumper=yaml.SafeDumper) + + +file_path = sys.argv[1] +with open(file_path, 'r') as file: + document = file.read() + +structure = yaml.safe_load(document) +with open(file_path, 'w') as file: + yaml.safe_dump(structure, file) diff --git a/tasks/migrations/run-step.yaml b/tasks/migrations/run-step.yaml index f7c1fdb..bed7fea 100644 --- a/tasks/migrations/run-step.yaml +++ b/tasks/migrations/run-step.yaml @@ -1,6 +1,6 @@ - name: "{{ migration_to_version }} applying migration" ansible.builtin.include_tasks: "tasks/migrations/{{ migration_filename }}" -- ansible.builtin.include_tasks: "tasks/migrations/update-repo-yaml-version.yaml" +- ansible.builtin.include_tasks: tasks/migrations/update-version.yaml vars: to_version: "{{ migration_to_version }}" diff --git a/tasks/migrations/update-repo-yaml-version.yaml b/tasks/migrations/update-version.yaml similarity index 53% rename from tasks/migrations/update-repo-yaml-version.yaml rename to tasks/migrations/update-version.yaml index 44a5794..89b96cd 100644 --- a/tasks/migrations/update-repo-yaml-version.yaml +++ b/tasks/migrations/update-version.yaml @@ -1,4 +1,7 @@ - name: "updating version in repo.yaml" copy: dest: "{{ repo_path }}/repo.yaml" - content: "{{ repo_yaml | combine({'version': to_version}) | to_nice_yaml(sort_keys=False, indent=2) }}" + content: "{{ repo_yaml | combine({'version': to_version}) | to_nice_yaml }}" + +- name: reformat repo.yaml file + ansible.builtin.command: "./library/yaml_format.py '{{ repo_path }}/repo.yaml'" diff --git a/tasks/version-migrations.yaml b/tasks/version-migrations.yaml index b4b43b3..9d43ea6 100644 --- a/tasks/version-migrations.yaml +++ b/tasks/version-migrations.yaml @@ -44,6 +44,6 @@ # XXX renamed to avoid warnings if included tasks include loops of their own loop_var: item_migration - - ansible.builtin.include_tasks: "tasks/migrations/update-repo-yaml-version.yaml" + - ansible.builtin.include_tasks: "tasks/migrations/update-version.yaml" vars: to_version: "{{ target_repo_ansible_version }}"