name: CI

on:
  pull_request:
  push:
    branches:
      - master
  schedule:
    - cron: "0 3 * * wed"
  workflow_dispatch:

concurrency:
  group: ci-${{ github.ref }}
  cancel-in-progress: true

env:
  CARGO_TERM_COLOR: always

jobs:
  test:
    strategy:
      fail-fast: false
      matrix:
        environment: [ubuntu-latest, macos-latest, windows-latest]

    runs-on: ${{ matrix.environment }}

    defaults:
      run:
        shell: bash

    steps:
      - uses: actions/checkout@v6

      - uses: actions/cache@v5
        with:
          path: |
            ~/.cargo/bin/
            ~/.cargo/registry/index/
            ~/.cargo/registry/cache/
            ~/.cargo/git/db/
            ~/.dylint_drivers/
            ~/.rustup/toolchains/
            target/dylint/
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}

      - name: Update and install toolchains
        run: |
          rustup toolchain update beta --profile minimal
          rustup install stable
          rustup default stable
          rustc -vV

      - name: Install tools
        run: |
          rustup update
          rustup install nightly
          rustup component add clippy --toolchain nightly
          cargo install cargo-dylint dylint-link         || true
          cargo install cargo-msrv --no-default-features || true
          cargo install cargo-supply-chain               || true
          cargo install unset                            || true

      - name: Build
        run: cargo build --workspace --all-targets

      - name: Test all but example
        run: cargo test --workspace --exclude nested_workspace_example --config "target.'cfg(all())'.runner = 'unset'" -- --nocapture

      - name: Test example
        run: cargo test -p nested_workspace_example -- --nocapture

  all-checks:
    needs: [test]

    if: ${{ always() }}

    runs-on: ubuntu-latest

    steps:
      - name: Check results
        if: ${{ contains(needs.*.result, 'failure') || contains(needs.*.result, 'cancelled') }}
        run: exit 1
