name: CI

on:
  push:
    tags: ['v*']

env:
  CARGO_TERM_COLOR: always

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

jobs:
  check:
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu-latest
            target: x86_64-unknown-linux-gnu
          - os: macos-14
            target: aarch64-apple-darwin
          - os: windows-latest
            target: x86_64-pc-windows-msvc

    runs-on: ${{ matrix.os }}
    timeout-minutes: 20

    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.target }}
          components: clippy

      - name: Cache Rust dependencies
        uses: Swatinem/rust-cache@v2
        with:
          key: ${{ matrix.target }}

      - name: Install dependencies (Linux)
        if: runner.os == 'Linux'
        run: |
          sudo apt-get update
          sudo apt-get install -y build-essential cmake pkg-config libasound2-dev libclang-dev libjack-dev \
            libxcb-render0-dev libxcb-shape0-dev libxcb-xfixes0-dev libxkbcommon-dev libssl-dev libgl1-mesa-dev

      - name: Install dependencies (macOS)
        if: runner.os == 'macOS'
        run: brew list cmake &>/dev/null || brew install cmake

      - name: Install dependencies (Windows)
        if: runner.os == 'Windows'
        run: |
          choco install cmake --installargs 'ADD_CMAKE_TO_PATH=System'
          echo "C:\Program Files\CMake\bin" >> $env:GITHUB_PATH

      - name: Build
        run: cargo build --release --target ${{ matrix.target }}

      - name: Build desktop
        run: cargo build --release --features desktop --bin cagire-desktop --target ${{ matrix.target }}

      - name: Test
        run: cargo test --target ${{ matrix.target }}

      - name: Clippy
        run: cargo clippy --target ${{ matrix.target }} -- -D warnings
