---
version: '3'

vars:
  DOCKER_BUILDING:
    sh: |
      if [ -f /.dockerenv ] || [ "$container" == 'docker' ]; then
        if [ -n "$BUILD_DATE" ]; then
          echo "true"
        fi
      fi
  DOCKER_ENVIRONMENT:
    sh: |
      if [ -f /.dockerenv ] || [ "$container" == 'docker' ]; then
        echo "true"
      fi
  ENVIRONMENT_TYPE:
    sh: |
      if [ -f /.dockerenv ] || [ "$container" == 'docker' ]; then
        if [ -n "$BUILD_DATE" ]; then
          echo "docker-build"
        else
          echo "docker"
        fi
      else
        echo "unknown"
      fi

tasks:
  clean:
    deps:
      - :install:software:rsync
    vars:
      RANDOM_STRING:
        sh: openssl rand -hex 14
    log:
      error: Error removing `{{.CLEAN_TARGETS}}`
      start: Removing `{{.CLEAN_TARGETS}}`
      success: Removed `{{.CLEAN_TARGETS}}`
    cmds:
      - mkdir -p '/tmp/{{.RANDOM_STRING}}'
      - mkdir -p '/tmp/{{.RANDOM_STRING}}-empty'
      - |
        for TMP_FILE in {{.CLEAN_TARGETS}}; do
          if [ -d "$TMP_FILE" ]; then
            mv "$TMP_FILE" "/tmp/{{.RANDOM_STRING}}/$TMP_FILE" 2> /dev/null
            (rsync -a --delete '/tmp/{{.RANDOM_STRING}}-empty' "/tmp/{{.RANDOM_STRING}}/$TMP_FILE" && rm -rf "/tmp/{{.RANDOM_STRING}}-$TMP_FILE") &
          fi
        done
        wait

  commit:
    deps:
      - :install:modules:local
      - :install:npm:commitizen
      - :install:npm:commitlint
    log:
      start: Initiating commit dialog
    cmds:
      - . ./.config/husky/pre-commit
      - exec < /dev/tty && git cz --hook || true

  husky:
    deps:
      - :install:modules:local
      - :install:npm:commitizen
      - :install:npm:commitlint
      - :install:npm:husky
    cmds:
      - task: husky:install
      - task: husky:permissions
    status:
      - '[ "{{.DOCKER_ENVIRONMENT}}" == "true" ] || [ "$SEMANTIC_RELEASE" == "true" ]'

  husky:ci:
    cmds:
      - task: husky:permissions
    status:
      - '[ "{{.DOCKER_ENVIRONMENT}}" != "true" ] || [ "$SEMANTIC_RELEASE" == "true" ]'

  husky:install:
    deps:
      - :install:npm:husky
    log:
      error: Error installing Husky git hooks
      start: Installing Husky git hooks
    cmds:
      - |
        if [ -d .git ] && [ "${container:=}" != 'docker' ]; then
          husky install .config/husky > /dev/null
          .config/log success 'Installed Husky git hooks'
        else
          .config/log warn 'Cannot run `husky install` because there is no `.git/` folder (or this is a Docker container)'
        fi
    status:
      - '[ ! -d .git ] || [ "{{.DOCKER_ENVIRONMENT}}" == "true" ]'

  husky:permissions:
    log:
      error: Encountered error while ensuring git hook scripts have the appropriate permissions
      start: Ensuring git hook scripts are executable
      success: Ensured git hook scripts are executable
    cmds:
      - |
        chmod +x .config/log
        if [ -f .config/log ]; then chmod +x .config/log; fi
        while read PATHH; do
          chmod +x "$PATHH"
        done < <(find ./.config/husky/* -maxdepth 0 -type f)
        .config/log success 'Ensured git hook scripts are executable'

  prepare-release:
    deps:
      - :install:npm:standard-version
    summary: |
      # Prepare a new release

      > NOTE: This is no longer used, in favor of semantic-release which handles publishing during CI/CD

      This task performs the following tasks in order:

      1. Ensures the project is up-to-date with the latest upstream changes
      2. Lints the project with all available linters
      3. Updates the version of the project in the `package.json` file and other relevant files
      4. Add the appropriate details to the CHANGELOG.md file
    cmds:
      - task: update
      - task: :lint:all
      - standard-version --no-verify

  repair:
    vars:
      MODEL_TMP:
        sh: mktemp
    cmds:
      - |
        curl -sSL '{{.MODEL_TASKFILE}}' > '{{.MODEL_TMP}}.yml'
        export ZXXXY="$(yq e '.' {{.MODEL_TMP}}.yml)"
        yq e -i '. | .vars = (env(ZXXXY) | .vars) | .env = (env(ZXXXY) | .env)' Taskfile.yml
        yq eval-all -i 'select(fileIndex == 0) * select(fileIndex == 1)' '{{.MODEL_TMP}}.yml' Taskfile.yml

  requirements:
    deps:
      - :install:modules:local
      - :install:python:requirements
    summary: |
      # Ensure local dependencies are installed

      This task ensures local dependencies are installed. These include dependencies defined in `package.json`
      and `pyproject.toml` (if the project is a Python project).
    run: once

  reset:
    cmds:
      - task: clean
      - task: update

  reset:force:
    deps:
      - software:git
    log:
      error: Failed to reset project back to its HEAD
      start: Forcing project to reset aggressively
      success: Successfully reset project back to its HEAD
    cmds:
      - task: clean
      - git reset --hard HEAD
      - git clean -fxd :/
      - git checkout master
      - git pull origin master --no-rebase

  shell:
    deps:
      - :install:software:docker
    cmds:
      - task: shell:{{if .CLI_ARGS}}cli{{else}}prompt{{end}}

  shell:cli:
    vars:
      DC: '{{.CLI_ARGS}}'
      WORKDIR:
        sh: basename $PWD
    log:
      start: Starting a Docker container for `{{.DOCKER_CONTAINER}}` and attaching to `/bin/bash`
    cmds:
      - .config/log info 'Running `docker run -it -v "$PWD:/{{.WORKDIR}}" -w /{{.WORKDIR}} --rm megabytelabs/ansible-molecule-{{.DC}}:latest /bin/bash`'
      - cmd: docker run -it -v "$PWD:/{{.WORKDIR}}" -w /{{.WORKDIR}} --rm megabytelabs/ansible-molecule-{{.DC}}:latest /bin/bash
        ignore_error: true

  shell:prompt:
    vars:
      MARKDOWN: |
        # Launch a Docker Shell Environment

        Open a shell session quickly, safely, and easily using Docker. Select an option from
        the prompt below to download and shell into a Docker environment. The environment
        will be automatically deleted after you exit the terminal session.
    cmds:
      - task: :log:markdown
        vars:
          MARKDOWN: '{{.MARKDOWN}}'
      - task: shell:prompt:question

  shell:prompt:question:
    interactive: true
    prompt:
      type: select
      message: Which operating system would you like to open up a terminal session with?
      options:
        - Archlinux
        - CentOS 7
        - CentOS 8
        - Debian 9
        - Debian 10
        - Fedora 33
        - Fedora 34
        - Ubuntu 18.04
        - Ubuntu 20.04
        - Ubuntu 21.04
      answer:
        cmds:
          - task: shell:cli
            env:
              DC:
                sh: echo '{{.ANSWER}}' | sed 's/ /-/g' | tr '[:upper:]' '[:lower:]'

  start:
    run: once
    cmds:
      - task: :common:start:docker
      - task: :common:start:ci
      - task: :common:start:dev

  update:
    deps:
      - :common:update:update
