# See: https://taskfile.dev/#/usage
version: "3"

tasks:
  # Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-dependencies-task/Taskfile.yml
  general:cache-dep-licenses:
    desc: Cache dependency license metadata
    cmds:
      - |
        if ! which licensed &>/dev/null; then
          if [[ {{OS}} == "windows" ]]; then
            echo "Licensed does not have Windows support."
            echo "Please use Linux/macOS or download the dependencies cache from the GitHub Actions workflow artifact."
          else
            echo "licensed not found or not in PATH. Please install: https://github.com/github/licensed#as-an-executable"
          fi
          exit 1
        fi
      - licensed cache

  # Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-dependencies-task/Taskfile.yml
  general:check-dep-licenses:
    desc: Check for unapproved dependency licenses
    deps:
      - task: general:cache-dep-licenses
    cmds:
      - licensed status

  docs:generate:
    desc: Create all generated documentation content
    # This is an "umbrella" task used to call any documentation generation processes the project has.
    # It can be left empty if there are none.

  # Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-markdown-task/Taskfile.yml
  markdown:check-links:
    desc: Check for broken links
    deps:
      - task: docs:generate
      - task: npm:install-deps
    cmds:
      - |
        if [[ "{{.OS}}" == "Windows_NT" ]]; then
          # npx --call uses the native shell, which makes it too difficult to use npx for this application on Windows,
          # so the Windows user is required to have markdown-link-check installed and in PATH.
          if ! which markdown-link-check &>/dev/null; then
            echo "markdown-link-check not found or not in PATH. Please install: https://github.com/tcort/markdown-link-check#readme"
            exit 1
          fi
          # Default behavior of the task on Windows is to exit the task when the first broken link causes a non-zero
          # exit status, but it's better to check all links before exiting.
          set +o errexit
          STATUS=0
          # Using -regex instead of -name to avoid Task's behavior of globbing even when quoted on Windows
          # The odd method for escaping . in the regex is required for windows compatibility because mvdan.cc/sh gives
          # \ characters special treatment on Windows in an attempt to support them as path separators.
          for file in $(
            find . \
              -type d -name '.git' -prune -o \
              -type d -name '.licenses' -prune -o \
              -type d -name '__pycache__' -prune -o \
              -type d -name 'node_modules' -prune -o \
              -regex ".*[.]md" -print
          ); do
            markdown-link-check \
              --quiet \
              --config "./.markdown-link-check.json" \
              "$file"
            STATUS=$(( $STATUS + $? ))
          done
          exit $STATUS
        else
          npx --package=markdown-link-check --call='
            STATUS=0
            for file in $(
              find . \
                -type d -name '.git' -prune -o \
                -type d -name '.licenses' -prune -o \
                -type d -name '__pycache__' -prune -o \
                -type d -name 'node_modules' -prune -o \
                -regex ".*[.]md" -print
            ); do
              markdown-link-check \
                --quiet \
                --config "./.markdown-link-check.json" \
                "$file"
              STATUS=$(( $STATUS + $? ))
            done
            exit $STATUS
          '
        fi

  # Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-markdown-task/Taskfile.yml
  markdown:fix:
    desc: Automatically correct linting violations in Markdown files where possible
    deps:
      - task: npm:install-deps
    cmds:
      - npx markdownlint-cli --fix "**/*.md"

  # Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-markdown-task/Taskfile.yml
  markdown:lint:
    desc: Check for problems in Markdown files
    deps:
      - task: npm:install-deps
    cmds:
      - npx markdownlint-cli "**/*.md"
  
  # Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/npm-task/Taskfile.yml
  npm:install-deps:
    desc: Install dependencies managed by npm
    cmds:
      - npm install
