---
version: '3'

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
      - :install:python:requirements
    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:
      - '[[ "${container:=}" == "docker" ]]'

  husky:ci:
    cmds:
      - task: husky:permissions
    status:
      - '[[ "${container:=}" != "docker" ]]'

  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
          {{.NPX_HANDLE}}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 ] || [[ "${container:=}" == "docker" ]]'

  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
      - '{{.NPX_HANDLE}}standard-version --no-verify'

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

      This task ensures that packages in various languages (Node.js, Python, etc.) are installed.
      At the very minimum, it will install the Node.js requirements by running `npm install`
      (or `pnpm install`, if configured to do so), install the NPM global dependencies, and
      then install the Python requirements by running `poetry update`. In some types of projects,
      additional types of requirements are installed (e.g. Ansible projects also install Ansible
      Galaxy requirements).
    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:]'

  update:
    deps:
      - :common:update:update
