---
version: '3'

vars:
  MAIN_TASKS_PATH: tasks/main.yml
  META_PATH: meta/main.yml
  MOLECULE_RESULTS_PATH: molecule/.results/logs
  REQUIREMENTS_PATH: requirements.yml
  VARIABLES_PATH: .variables.json

tasks:
  ansibler:
    deps:
      - :install:pipx:ansibler
    cmds:
      - task: compatibility-chart
      - task: populate-platforms
      - task: tasks:{{.REPOSITORY_SUBTYPE}}

  compatibility-chart:
    deps:
      - :install:software:jq
    log:
      error: Failed to generate operating system compatibility chart
      start: Generating operating system compatibility chart
      success: Successfully generated operating system compatibility chart
    cmds:
      - mkdir -p .cache
      - if [ ! -f .cache/compatibility-chart.json ]; then echo "{}" > .cache/compatibility-chart.json; fi
      - task: compatibility-chart:generate
      - |
        TMP="$(mktemp)"
        jq -s -S '.[0] + .[1]' '{{.VARIABLES_PATH}}' .cache/compatibility-chart.json > "$TMP"
        mv "$TMP" '{{.VARIABLES_PATH}}'

  compatibility-chart:ansifilter:
    deps:
      - :install:software:ansifilter
    cmds:
      - |
        for LOG in {{.MOLECULE_RESULTS_PATH}}; do
          if [ "$LOG" != '{{.MOLECULE_RESULTS_PATH}}' ]; then
            TMP="$(mktemp)" && cat "$LOG" | ansifilter > "$TMP" && mv "$TMP" "$LOG"
          fi
        done
    sources:
      - '{{.MOLECULE_RESULTS_PATH}}/*'

  compatibility-chart:generate:
    deps:
      - :install:pipx:ansibler
    cmds:
      - task: compatibility-chart:ansifilter
      - >
        PATH="$PATH:$HOME/.local/bin"

        {{.PYTHON_HANDLE}}ansibler --generate-compatibility-chart --molecule-results-dir '{{.MOLECULE_RESULTS_PATH}}'
        --json-file .cache/compatibility-chart.json
    sources:
      - '{{.MOLECULE_RESULTS_PATH}}/*'
    generates:
      - .cache/compatibility-chart.json

  populate-platforms:
    deps:
      - :install:pipx:ansibler
    log:
      error: Failed to populate platforms in `meta/main.yml
      start: Populating the supported platforms listed in `meta/main.yml` based on the compatibility chart data
      success: Successfully populated `meta/main.yml` platforms
    cmds:
      - cmd: |
          PATH="$PATH:$HOME/.local/bin"
          {{.PYTHON_HANDLE}}ansibler --populate-platforms --json-file .cache/compatibility-chart.json
        ignore_error: true
    sources:
      - .cache/compatibility-chart.json
      - meta/main.yml

  role-dependencies:
    deps:
      - :install:software:jq
    log:
      error: Failed to acquire role dependency information
      start: Gathering information about role dependencies
      success: Acquired role dependency information
    cmds:
      - mkdir -p .cache
      - if [ ! -f .cache/role-dependencies.json ]; then echo "{}" > .cache/role-dependencies.json; fi
      - task: role-dependencies:generate
      - if [ -f role-dependencies.json ]; then mv role-dependencies.json .cache/role-dependencies.json; fi
      - |
        TMP="$(mktemp)"
        jq -s -S '.[0] + .[1]' '{{.VARIABLES_PATH}}' .cache/role-dependencies.json > "$TMP"
        mv "$TMP" '{{.VARIABLES_PATH}}'

  role-dependencies:generate:
    deps:
      - :install:pipx:ansibler
    cmds:
      - cmd: |
          PATH="$PATH:$HOME/.local/bin"
          {{.PYTHON_HANDLE}}ansibler --role-dependencies --json-file .cache/role-dependencies.json
        ignore_error: true
    sources:
      - '{{.REQUIREMENTS_PATH}}'
    generates:
      - .cache/role-dependencies.json

  tasks:playbook:
    deps:
      - :ansible:collection-dependencies
      - role-dependencies

  tasks:role:
    deps:
      - :ansible:collection-dependencies
      - populate-platforms
      - role-dependencies
