# Main CI workflow to validate PRs and branches are correctly formatted
# and pass tests.
#
# CI workflow was based on a lot of work from other people:
# - https://github.com/heim-rs/heim/blob/master/.github/workflows/ci.yml
# - https://github.com/BurntSushi/ripgrep/blob/master/.github/workflows/ci.yml
# - https://www.reillywood.com/blog/rust-faster-ci/
# - https://matklad.github.io/2021/09/04/fast-rust-builds.html
#
# Supported platforms run the following tasks:
# - cargo fmt
# - cargo test (built/test in separate steps)
# - cargo clippy (apparently faster to do it after the build/test)
#
# Unsupported platforms run the following tasks:
# - cargo check

name: ci

on:
  workflow_dispatch:
  pull_request:
  push:
    branches:
      - master

env:
  RUST_BACKTRACE: 1
  CARGO_INCREMENTAL: 0
  CARGO_PROFILE_DEV_DEBUG: 0
  CARGO_HUSKY_DONT_INSTALL_HOOKS: true
  COMPLETION_DIR: "target/tmp/bottom/completion/"
  MANPAGE_DIR: "target/tmp/bottom/manpage/"

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: ${{ github.event_name == 'pull_request' || github.repository != 'ClementTsang/bottom' }}

jobs:
  # Check if things should be skipped.
  pre-job:
    runs-on: ubuntu-latest
    outputs:
      should_skip: ${{ steps.skip_check.outputs.should_skip }}
    steps:
      - name: Check if this action should be skipped
        id: skip_check
        uses: fkirc/skip-duplicate-actions@f11521568414503656a5af807dc3018c012552c4 # v5.2.0
        with:
          skip_after_successful_duplicate: "true"
          paths: '[".cargo/**", ".github/workflows/ci.yml", "sample_configs/**", "src/**", "tests/**", "build.rs", "Cargo.lock", "Cargo.toml", "clippy.toml", "rustfmt.toml", "Cross.toml"]'
          do_not_skip: '["workflow_dispatch", "push"]'

  # Runs rustfmt + tests + clippy on the main supported platforms.
  supported:
    needs: pre-job
    runs-on: ${{ matrix.info.os }}
    if: ${{ needs.pre-job.outputs.should_skip != 'true' }}
    timeout-minutes: 30
    strategy:
      fail-fast: false
      matrix:
        info:
          - {
              os: "ubuntu-latest",
              target: "x86_64-unknown-linux-gnu",
              cross: false,
            }
          - {
              os: "ubuntu-latest",
              target: "aarch64-unknown-linux-gnu",
              cross: true,
            }
          - { os: "macos-12", target: "x86_64-apple-darwin", cross: false }
          - {
              os: "windows-2019",
              target: "x86_64-pc-windows-msvc",
              cross: false,
            }
        features: ["--all-features", "--no-default-features"]
    steps:
      - name: Checkout repository
        uses: actions/checkout@v3

      - name: Set up Rust toolchain
        uses: dtolnay/rust-toolchain@ba37adf8f94a7d9affce79bd3baff1b9e3189c33 # https://github.com/dtolnay/rust-toolchain/commit/ba37adf8f94a7d9affce79bd3baff1b9e3189c33
        with:
          toolchain: stable
          components: rustfmt, clippy
          target: ${{ matrix.info.target }}

      - name: Enable Rust cache
        uses: Swatinem/rust-cache@359a70e43a0bb8a13953b04a90f76428b4959bb6 # 2.2.0
        if: ${{ github.event_name != 'pull_request' || ! github.event.pull_request.head.repo.fork }} # If it is a PR, only if not a fork
        with:
          shared-key: build-cache-${{ matrix.info.target }}

      - name: Check cargo fmt
        run: cargo fmt --all -- --check

      - name: Build tests
        uses: ClementTsang/cargo-action@v0.0.3
        with:
          command: test
          args: --no-run --locked ${{ matrix.features }} --target=${{ matrix.info.target }}
          use-cross: ${{ matrix.info.cross }}
          cross-version: 0.2.4
        env:
          RUST_BACKTRACE: full

      - name: Run tests
        uses: ClementTsang/cargo-action@v0.0.3
        with:
          command: test
          args: --no-fail-fast ${{ matrix.features }} --target=${{ matrix.info.target }} -- --nocapture --quiet
          use-cross: ${{ matrix.info.cross }}
          cross-version: 0.2.4
        env:
          RUST_BACKTRACE: full

      - name: Run clippy
        uses: ClementTsang/cargo-action@v0.0.3
        with:
          command: clippy
          args: ${{ matrix.features }} --all-targets --workspace --target=${{ matrix.info.target }} -- -D warnings
          use-cross: ${{ matrix.info.cross }}
          cross-version: 0.2.4
        env:
          RUST_BACKTRACE: full

  # Run cargo check on all other platforms
  other-check:
    needs: pre-job
    runs-on: ${{ matrix.info.os }}
    if: ${{ needs.pre-job.outputs.should_skip != 'true' }}
    continue-on-error: true
    timeout-minutes: 30
    strategy:
      fail-fast: false
      matrix:
        info:
          # x86 or x64
          - {
              os: "ubuntu-latest",
              target: "i686-unknown-linux-gnu",
              cross: true,
              rust: stable,
            }
          - {
              os: "ubuntu-latest",
              target: "x86_64-unknown-linux-musl",
              cross: false,
              rust: stable,
            }
          - {
              os: "ubuntu-latest",
              target: "i686-unknown-linux-musl",
              cross: true,
              rust: stable,
            }
          - {
              os: "windows-2019",
              target: "i686-pc-windows-msvc",
              cross: false,
              rust: stable,
            }
          - {
              os: "windows-2019",
              target: "x86_64-pc-windows-gnu",
              cross: false,
              rust: stable,
            }

          # Beta; should be allowed to fail.
          - {
              os: "ubuntu-latest",
              target: "x86_64-unknown-linux-gnu",
              cross: false,
              rust: beta,
            }
          - {
              os: "macos-12",
              target: "x86_64-apple-darwin",
              cross: false,
              rust: beta,
            }
          - {
              os: "windows-2019",
              target: "x86_64-pc-windows-msvc",
              cross: false,
              rust: beta,
            }

          # armv7
          - {
              os: "ubuntu-latest",
              target: "armv7-unknown-linux-gnueabihf",
              cross: true,
              rust: stable,
            }

          # armv6
          - {
              os: "ubuntu-latest",
              target: "arm-unknown-linux-gnueabihf",
              cross: true,
              rust: stable,
            }

          # PowerPC 64 LE
          - {
              os: "ubuntu-latest",
              target: "powerpc64le-unknown-linux-gnu",
              cross: true,
              rust: stable,
            }

          # Risc-V 64gc
          - {
              os: "ubuntu-latest",
              target: "riscv64gc-unknown-linux-gnu",
              cross: true,
              rust: stable,
            }

    steps:
      - name: Checkout repository
        uses: actions/checkout@v3

      - name: Set up Rust toolchain
        uses: dtolnay/rust-toolchain@ba37adf8f94a7d9affce79bd3baff1b9e3189c33 # https://github.com/dtolnay/rust-toolchain/commit/ba37adf8f94a7d9affce79bd3baff1b9e3189c33
        with:
          toolchain: ${{ matrix.info.rust }}
          target: ${{ matrix.info.target }}

      - name: Enable Rust cache
        uses: Swatinem/rust-cache@359a70e43a0bb8a13953b04a90f76428b4959bb6 # 2.2.0
        if: ${{ github.event_name != 'pull_request' || ! github.event.pull_request.head.repo.fork }} # If it is a PR, only if not a fork
        with:
          shared-key: build-cache-${{ matrix.info.target }}

      - name: Check
        uses: ClementTsang/cargo-action@v0.0.3
        with:
          command: check
          args: --all-targets --verbose --target=${{ matrix.info.target }} --locked
          use-cross: ${{ matrix.info.cross }}
          cross-version: 0.2.4

  completion:
    name: "CI Pass Check"
    needs: [supported, other-check]
    runs-on: "ubuntu-latest"
    steps:
      - name: CI Passed
        run: |
          echo "CI workflow completed."
