taskvisor 0.0.8

Event-driven task orchestration with restart, backoff, and user-defined subscribers
version: '3'

vars:
  git_root:
    sh: git rev-parse --show-toplevel

  rust_version: "1.90"
  cache_dir: ".cache"

tasks:
  default:
    desc: Default task.
    cmds:
      - echo "Please enter a task or use '-l' or '--list-all' to list all available tasks"
    silent: true

  # ================================================#
  # ---------------------INTERNAL-------------------#
  # ================================================#

  _docker/run:
    desc: Internal wrapper to run task in containers.
    internal: true
    silent: true
    requires:
      vars: [ IMAGE, CMD, MOUNT_DIR ]
    dir: "{{.git_root}}"
    cmd: |
      set -euo pipefail
      
      if ! docker image inspect "{{.IMAGE}}" >/dev/null 2>&1; then
        echo "pulling: {{.IMAGE}}"
      
        docker pull -q "{{.IMAGE}}" >/dev/null 2>&1 || {
          echo "Failed to pull image: {{.IMAGE}}"
          exit 1
        }
      fi
      docker run --rm --init --pull=never \
        --security-opt no-new-privileges \
        --user $(id -u):$(id -g) \
        --cap-drop=ALL \
        \
        --volume "{{.git_root}}/{{.MOUNT_DIR}}:/workspace:rw" \
        {{if .VOLUMES}}{{range $vol := .VOLUMES}}--volume {{$vol}} {{end}}{{end}} \
        {{if .ENVS}}{{range $env := .ENVS}}--env {{$env}} {{end}}{{end}} \
        \
        --workdir /workspace \
        {{.IMAGE}} \
        {{.CMD}}

  _cargo/tool:
    desc: Internal wrapper for running cargo tools.
    internal: true
    silent: true
    requires:
      vars: [ CMD ]
    cmds:
      - cmd: mkdir -p "{{.git_root}}/{{.cache_dir}}"
      - task: _docker/run
        vars:
          IMAGE: "rust:{{.rust_version}}-slim"
          CMD: "{{.CMD}}"
          MOUNT_DIR: "."
          VOLUMES:
            - "{{.git_root}}/{{.cache_dir}}:/tmp/{{.cache_dir}}"
          ENVS:
            - "CARGO_TARGET_DIR=/tmp/{{.cache_dir}}/target"
            - "CARGO_HOME=/tmp/{{.cache_dir}}/cargo"
            - "CARGO_INCREMENTAL=0"

  # ================================================#
  # ----------------------PUBLIC--------------------#
  # ================================================#

  cargo/fmt:
    desc: Run 'cargo fmt'.
    cmds:
      - task: _cargo/tool
        vars:
          CMD: "rustup component add rustfmt && cargo fmt --check --verbose"

  cargo/clippy:
    desc: Run 'cargo clippy'.
    cmds:
      - task: _cargo/tool
        vars:
          CMD: "rustup component add clippy && cargo clippy --all --all-features -- -D warnings"

  cargo/test:
    desc: Run 'cargo test'.
    cmds:
      - task: _cargo/tool
        vars:
          CMD: "cargo test --all --all-features"