image: ubuntu:18.04
default: check
tasks:
  install_packages:
    command: |
      set -euo pipefail
      apt-get update
      apt-get install --yes build-essential curl shellcheck

  install_tagref:
    dependencies:
      - install_packages
    command: |
      set -euo pipefail
      curl https://raw.githubusercontent.com/stepchowfun/tagref/master/install.sh -LSfs |
        VERSION=1.2.1 sh

  create_user:
    command: |
      set -euo pipefail
      adduser --disabled-password --gecos '' user

  install_rust:
    dependencies:
      - install_packages
      - create_user
    user: user
    command: |
      set -euo pipefail
      curl https://sh.rustup.rs -sSf |
        sh -s -- -y --default-toolchain 1.39.0
      . $HOME/.cargo/env
      rustup component add clippy
      rustup component add rustfmt
      rm -rf "$(dirname "$(rustup which rustc)")/../share"

  install_tools:
    dependencies:
      - install_rust
      - install_tagref

  fetch_crates:
    dependencies:
      - install_tools
    input_paths:
      - Cargo.lock
      - Cargo.toml
    user: user
    command: |
      set -euo pipefail
      . $HOME/.cargo/env
      mv Cargo.lock Cargo.lock.og
      mv Cargo.toml Cargo.toml.og
      cargo init --vcs none
      mv Cargo.lock.og Cargo.lock
      mv Cargo.toml.og Cargo.toml
      cargo build
      cargo clean --package docuum
      cargo build --release
      cargo clean --release --package docuum
      cargo clippy
      rm -rf src

  build:
    dependencies:
      - fetch_crates
    input_paths:
      - src
    user: user
    command: |
      set -euo pipefail
      . $HOME/.cargo/env
      cargo build

  test:
    dependencies:
      - build
    user: user
    command: |
      set -euo pipefail
      . $HOME/.cargo/env
      cargo test

  lint:
    dependencies:
      - build
    input_paths:
      - .ignore # Used by `tagref`
      - install.sh # Linted by ShellCheck
      - release.sh   # Linted by ShellCheck
    user: user
    command: |
      set -euo pipefail
      . $HOME/.cargo/env
      cargo clippy --all-targets --all-features -- \
        --deny warnings --deny clippy::all --deny clippy::pedantic
      cargo fmt --all -- --check
      tagref
      shellcheck install.sh
      shellcheck release.sh

  run:
    dependencies:
      - build
    user: user
    command: |
      set -euo pipefail
      . $HOME/.cargo/env
      cargo run -- --help

  check:
    dependencies:
      - fetch_crates
    input_paths:
      - src
    user: user
    command: |
      set -euo pipefail
      . $HOME/.cargo/env
      cargo check

  format:
    dependencies:
      - fetch_crates
    input_paths:
      - src
    output_paths:
      - src
    user: user
    command: |
      set -euo pipefail
      . $HOME/.cargo/env
      cargo fmt --all

  release:
    dependencies:
      - fetch_crates
    input_paths:
      - src
    output_paths:
      - artifacts
    user: user
    command: |
      set -euo pipefail
      . $HOME/.cargo/env
      cargo build --release
      mkdir artifacts
      sha256sum --binary target/release/docuum
      cp target/release/docuum artifacts/docuum-x86_64-unknown-linux-gnu
