---
version: '3'

vars:
  MAP_FILE: .cache/task-map/Taskfile.json

tasks:
  task:map:generate:
    deps:
      - :install:software:jq
      - :install:software:yq
    vars:
      MAP_FILE_QUERY: >-
        [.includes | with_entries(select(.value.optional == true)) | to_entries | .[] |
        { "key": .key, "value": .value.taskfile }] + (.includes | with_entries(select(.value.optional == null)) |
        to_entries) + { "key": "", "value": "Taskfile.yml" }
    log:
      error: Encountered error while generating Taskfile task map array
      start: Generating Taskfile task map array
      success: Successfully generated Taskfile task map array at `{{.MAP_FILE}}`
    cmds:
      - |
        COMBINED="$(mktemp)"
        echo '[]' > "$COMBINED"
        TMP_COMBINED="$(mktemp)"
        TMP_JQ="$(mktemp)"
        TMP_YQ="$(mktemp)"
        mkdir -p "$(dirname '{{.MAP_FILE}}')"
        yq e -o=j '{{.MAP_FILE_QUERY}}' Taskfile.yml > "{{.MAP_FILE}}"
        jq -c '.[]' "{{.MAP_FILE}}" | while read i; do
          PREFIX="$(echo "$i" | jq -r '.key')"
          FILE="$(echo "$i" | jq -r '.value')"
          if [ -f "$FILE" ]; then
            yq e -o=j '.tasks' "$FILE" > "$TMP_YQ"
            jq --arg prefix "$PREFIX:" '[to_entries | .[] | .key as $key | .value | ._task? = $prefix + $key]' "$TMP_YQ" > "$TMP_JQ"
            jq -s '.[0] + .[1]' "$COMBINED" "$TMP_JQ" > "$TMP_COMBINED"
            cp "$TMP_COMBINED" "$COMBINED"
          fi
        done
        mv "$COMBINED" '{{.MAP_FILE}}'
    sources:
      - .config/taskfiles/**/*.yml
      - Taskfile.yml

  task:tag:command:
    deps:
      - task:map:generate
    vars:
      TAG: '{{if .TAG}}{{.TAG}}{{else}}{{.CLI_ARGS}}{{end}}'
      TEMPLATE: 'task $i && '
    log:
      error: Encountered error while building Taskfile task list from tag named `{{.TAG}}`
      start: Selecting by Taskfile task tag named `{{.TAG}}`
      success: Wrote task tag command to `tag-command.txt`
    cmds:
      - |
        RESULT=""
        jq -cr '(.. | select(.tags?[]? == "{{.TAG}}")) | ._task' {{.MAP_FILE}} | while read i; do
          RESULT="$RESULT{{.TEMPLATE}}"
        done
        if [ '{{.TEMPLATE}}' == 'task $i && ' ]; then
          RESULT="$(echo $RESULT | sed 's/ && $//')"
        fi
        echo "$RESULT" > tag-command.txt

  task:tag:deps:
    deps:
      - task:map:generate
    vars:
      TAG: '{{if .TAG}}{{.TAG}}{{else}}{{.CLI_ARGS}}{{end}}'
      TEMPLATE: '- task: $i\n'
    log:
      error: Encountered error while building Taskfile task deps file from tag named `{{.TAG}}`
      start: Selecting by Taskfile task tag named `{{.TAG}}` for deps file
      success: Completed `Taskfile.yml` update logic for deps task named `deps:{{.TAG}}`
    cmds:
      - |
        RESULT=""
        COUNT="0"
        jq -r '(.. | select(.tags?[]? == "{{.TAG}}")) | ._task' {{.MAP_FILE}} | while read i; do
          COUNT="$(("$COUNT" + 1))"
          RESULT="$RESULT{{.TEMPLATE}}"
        done
        if [ "$COUNT" -gt 0 ]; then
          ARR="$(echo -e "$RESULT")" yq e -i '.tasks["deps:run:parallel"].cmds = env(ARR)' Taskfile.yml
        else
          .config/log warn 'There were no results so nothing was added to the `Taskfile.yml`'
        fi
