name: Build

on:
  push:
    branches:
      - main
      - next-major

  pull_request:
    branches:
      - '*'

jobs:
  test:
    timeout-minutes: 20

    strategy:
      matrix:
        os:
          - ubuntu-latest
          - windows-latest
        rust:
          - stable
          - nightly

    runs-on: ${{ matrix.os }}
    env:
      RUSTFLAGS: "-D warnings"

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

      - name: Global cache
        uses: actions/cache@v2.1.4
        with:
          path: |
            ~/.cargo
            ~/.rustup
          key: cargo-${{ matrix.os }}-${{ matrix.rust }}-${{ hashFiles('**/Cargo.toml') }}
          restore-keys: cargo-${{ matrix.os }}-${{ matrix.rust }}

      - name: Target Cache
        uses: actions/cache@v2.1.4
        with:
          path: ./target
          key: target-${{ matrix.os }}-${{ matrix.rust }}-${{ hashFiles('**/Cargo.toml') }}-${{ hashFiles('**/*.rs') }}
          restore-keys: |
            target-${{ matrix.os }}-${{ matrix.rust }}-${{ hashFiles('**/Cargo.toml') }}
            target-${{ matrix.os }}-${{ matrix.rust }}

      - name: Install rust toolchain
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: ${{ matrix.rust }}
          override: true

      - name: Install linux dependencies
        if: matrix.os == 'ubuntu-latest'
        run: sudo apt-get update; sudo apt-get install --no-install-recommends libudev-dev libasound2-dev

      - name: Format check
        if: matrix.os == 'ubuntu-latest' && matrix.rust == 'stable'
        run: cargo fmt --all -- --check

      - name: Static analysis
        if: matrix.os == 'ubuntu-latest' && matrix.rust == 'stable'
        run: cargo clippy -- -Dwarnings

      - name: Run tests with default features
        run: cargo test --workspace

  test-features:
    runs-on: ubuntu-latest
    needs: test
    timeout-minutes: 20
    env:
      RUSTFLAGS: "-D warnings"

    strategy:
      matrix:
        feature-set:
          - "2d"
          - "3d"
          - "2d, debug"
          - "3d, debug"

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

      - name: Global cache
        uses: actions/cache@v2.1.4
        with:
          path: |
            ~/.cargo
            ~/.rustup
          key: cargo-ubuntu-latest-stable-${{ hashFiles('**/Cargo.toml') }}
          restore-keys: cargo-ubuntu-latest-stable

      - name: Target Cache
        uses: actions/cache@v2.1.4
        with:
          path: ./target
          key: target-ubuntu-latest-stable-${{ hashFiles('**/Cargo.toml') }}-${{ hashFiles('**/*.rs') }}
          restore-keys: |
            target-ubuntu-latest-stable-${{ hashFiles('**/Cargo.toml') }}
            target-ubuntu-latest-stable

      - name: Install rust toolchain
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true

      - name: Install dependencies
        run: sudo apt-get update; sudo apt-get install --no-install-recommends libudev-dev libasound2-dev

      - name: Run tests
        run: cargo test --workspace --no-default-features --features "${{ matrix.feature-set }}"

  wasm-check:
    needs: test
    timeout-minutes: 20
    runs-on: ubuntu-latest
    env:
      RUSTFLAGS: "-D warnings"

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

      - name: Global cache
        uses: actions/cache@v2.1.4
        with:
          path: |
            ~/.cargo
            ~/.rustup
          key: cargo-ubuntu-latest-stable-wasm-${{ hashFiles('**/Cargo.toml') }}
          restore-keys: cargo-ubuntu-latest-stable-wasm

      - name: Target Cache
        uses: actions/cache@v2.1.4
        with:
          path: ./target
          key: target-ubuntu-latest-stable-wasm-${{ hashFiles('**/Cargo.toml') }}-${{ hashFiles('**/*.rs') }}
          restore-keys: |
            target-ubuntu-latest-stable-wasm-${{ hashFiles('**/Cargo.toml') }}
            target-ubuntu-latest-stable-wasm

      - name: Install rust toolchain
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          target: wasm32-unknown-unknown
          override: true

      - name: Check WASM
        run: cargo check --workspace --target wasm32-unknown-unknown

  ignored-tests:
    needs: test
    runs-on: ubuntu-latest
    timeout-minutes: 20
    env:
      RUSTFLAGS: "-D warnings"

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

      - name: Global cache
        uses: actions/cache@v2.1.4
        with:
          path: |
            ~/.cargo
            ~/.rustup
          key: cargo-ubuntu-latest-stable-${{ hashFiles('**/Cargo.toml') }}
          restore-keys: cargo-ubuntu-latest-stable

      - name: Target Cache
        uses: actions/cache@v2.1.4
        with:
          path: ./target
          key: target-ubuntu-latest-stable-${{ hashFiles('**/Cargo.toml') }}-${{ hashFiles('**/*.rs') }}
          restore-keys: |
            target-ubuntu-latest-stable-${{ hashFiles('**/Cargo.toml') }}
            target-ubuntu-latest-stable

      - name: Install rust toolchain
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true

      - name: Install dependencies
        run: sudo apt-get update; sudo apt-get install --no-install-recommends libudev-dev libasound2-dev

      - name: Run ignored tests
        run: cargo test --workspace --features "debug" -- --ignored
