name: CI

on:
  push:
    branches:
      - main
    paths:
      - "Cargo.lock"
      - "**.rs"
      - "**.sh"
      - "**.ps1"
      - "**.yml"
      - "**.toml"
      - "!**.md"
      - "!LICENSE-APACHE"
      - "!LICENSE-MIT"
  pull_request:
    types: [opened, synchronize]
    paths:
      - "Cargo.lock"
      - "**.rs"
      - "**.sh"
      - "**.ps1"
      - "**.yml"
      - "**.toml"
      - "!**.md"
      - "!LICENSE-APACHE"
      - "!LICENSE-MIT"

env:
  # Note: It is not possible to define top level env vars and pass them to composite actions.
  # To work around this issue we use inputs and define all the env vars here.

  RUST_PREVIOUS_VERSION: 1.92.0

  # Dependency versioning
  # from wgpu repo: https://github.com/gfx-rs/wgpu/blob/trunk/.github/workflows/ci.yml

  # Mozilla Grcov
  GRCOV_LINK: "https://github.com/mozilla/grcov/releases/download"
  GRCOV_VERSION: "0.8.19"

  # Test in release mode (make it an empty string to test in debug mode)
  TEST_RELEASE_FLAG: "--release"

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

jobs:
  prepare-checks:
    runs-on: ubuntu-latest
    outputs:
      rust-prev-version: ${{ env.RUST_PREVIOUS_VERSION }}
    steps:
      - name: Do Nothing
        if: false
        run: echo

  code-quality:
    runs-on: ubuntu-22.04
    needs: prepare-checks
    strategy:
      matrix:
        rust: [stable]
        include:
          - rust: stable
            toolchain: stable
    steps:
      - name: checkout
        uses: actions/checkout@v6
      # --------------------------------------------------------------------------------
      - name: Setup Rust
        uses: tracel-ai/github-actions/install-rust@v9
        with:
          rust-toolchain: ${{ matrix.toolchain }}
          cache-key: ${{ matrix.rust }}-linux
      # --------------------------------------------------------------------------------
      - name: Audit
        run: cargo xtask check audit
      # --------------------------------------------------------------------------------
      - name: Format
        shell: bash
        env:
          # work around for colors
          # see: https://github.com/rust-lang/rustfmt/issues/3385
          TERM: xterm-256color
        run: cargo xtask check format
      # --------------------------------------------------------------------------------
      - name: Lint
        run: cargo xtask check lint
      # --------------------------------------------------------------------------------
      - name: Typos
        uses: tracel-ai/github-actions/check-typos@v9

  documentation:
    runs-on: ubuntu-22.04
    needs: prepare-checks
    strategy:
      matrix:
        rust: [stable]
        include:
          - rust: stable
            toolchain: stable
    steps:
      - name: checkout
        uses: actions/checkout@v6
      # --------------------------------------------------------------------------------
      - name: Setup Rust
        uses: tracel-ai/github-actions/install-rust@v9
        with:
          rust-toolchain: ${{ matrix.toolchain }}
          cache-key: ${{ matrix.rust }}-linux
      # --------------------------------------------------------------------------------
      - name: Documentation Build
        run: cargo xtask doc build
      # --------------------------------------------------------------------------------
      - name: Documentation Tests
        run: cargo xtask doc tests

  linux-std-tests:
    runs-on: ubuntu-22.04
    needs: [prepare-checks, code-quality]
    env:
      DISABLE_WGPU_SPIRV: "1"
      # disable incremental compilation (reduces artifact size)
      CARGO_PROFILE_TEST_INCREMENTAL: "false"
    strategy:
      matrix:
        rust: [stable, prev]
        include:
          - rust: stable
            toolchain: stable
            coverage: --enable-coverage
          - rust: prev
            toolchain: ${{ needs.prepare-checks.outputs.rust-prev-version }}
    steps:
      - name: checkout
        uses: actions/checkout@v6
      # --------------------------------------------------------------------------------
      - name: Setup Rust
        uses: tracel-ai/github-actions/install-rust@v9
        with:
          rust-toolchain: ${{ matrix.toolchain }}
          cache-key: ${{ matrix.rust }}-linux
          # Disable cache on linux-std (stable) runner which currently always runs out of disk space with tests + coverage
          enable-cache: ${{ matrix.rust != 'stable' }}
      # # --------------------------------------------------------------------------------
      - name: Install grcov
        if: matrix.rust == 'stable'
        shell: bash
        run: |
          curl -L "$GRCOV_LINK/v$GRCOV_VERSION/grcov-x86_64-unknown-linux-musl.tar.bz2" |
          tar xj -C $HOME/.cargo/bin
          cargo xtask coverage install
      # --------------------------------------------------------------------------------
      - name: Tests
        run: cargo xtask ${{ matrix.coverage }} test ${{ env.TEST_RELEASE_FLAG }} --ci github-runner
      # --------------------------------------------------------------------------------
      - name: Generate lcov.info
        if: matrix.rust == 'stable'
        # /* is to exclude std library code coverage from analysis
        run: cargo xtask coverage generate --ignore "/*,xtask/*,examples/*" --profile release
      # --------------------------------------------------------------------------------
      - name: Codecov upload lcov.info
        if: matrix.rust == 'stable'
        uses: codecov/codecov-action@v6
        with:
          files: lcov.info
          token: ${{ secrets.CODECOV_TOKEN }}

  linux-no-std-tests:
    runs-on: ubuntu-22.04
    needs: [prepare-checks, code-quality]
    strategy:
      matrix:
        rust: [stable, prev]
        include:
          - rust: stable
            toolchain: stable
          - rust: prev
            toolchain: ${{ needs.prepare-checks.outputs.rust-prev-version }}
    steps:
      - name: checkout
        uses: actions/checkout@v6
      # --------------------------------------------------------------------------------
      - name: Setup Rust
        uses: tracel-ai/github-actions/install-rust@v9
        with:
          rust-toolchain: ${{ matrix.toolchain }}
          cache-key: ${{ matrix.rust }}-linux-no-std
      # --------------------------------------------------------------------------------
      - name: Crates Build
        run: cargo xtask --context no-std build --ci
      # --------------------------------------------------------------------------------
      - name: Crates Tests
        run: cargo xtask --context no-std test ${{ env.TEST_RELEASE_FLAG }} --ci github-runner

  windows-std-tests:
    runs-on: windows-2022
    needs: [prepare-checks, code-quality]
    # Keep the stragegy to be able to easily add new rust versions if required
    strategy:
      matrix:
        rust: [stable]
        include:
          - rust: stable
            toolchain: stable
    steps:
      - name: checkout
        uses: actions/checkout@v6
      # --------------------------------------------------------------------------------
      - name: Setup Rust
        uses: tracel-ai/github-actions/install-rust@v9
        with:
          rust-toolchain: ${{ matrix.toolchain }}
          cache-key: ${{ matrix.rust }}-windows
      # --------------------------------------------------------------------------------
      - name: Tests
        run: cargo xtask test ${{ env.TEST_RELEASE_FLAG }} --ci github-runner

  macos-std-tests:
    runs-on: blaze/macos-15
    needs: [prepare-checks, code-quality]
    timeout-minutes: 60
    # Keep the stragegy to be able to easily add new rust versions if required
    strategy:
      matrix:
        rust: [stable]
        include:
          - rust: stable
            toolchain: stable
    steps:
      - name: checkout
        uses: actions/checkout@v6
      # --------------------------------------------------------------------------------
      - name: Setup Rust
        uses: tracel-ai/github-actions/install-rust@v9
        with:
          rust-toolchain: ${{ matrix.toolchain }}
          cache-key: ${{ matrix.rust }}-macos
      # --------------------------------------------------------------------------------
      - name: Device check
        run: system_profiler SPHardwareDataType
      # --------------------------------------------------------------------------------
      - name: Tests
        run: cargo xtask test ${{ env.TEST_RELEASE_FLAG }} --ci github-mac-runner
