---
on:
  pull_request: null
name: pr checks

concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: true

jobs:
  check-default:
    name: Check default
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3
      - name: Install stable toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          toolchain: stable
          target: wasm32-unknown-unknown
      - name: Update
        run: sudo apt-get update
      - name: Deps
        run: sudo apt-get install g++ pkg-config libx11-dev libasound2-dev libudev-dev
      - name: Cache
        uses: Swatinem/rust-cache@v2
      - name: Check
        run: cargo check --all-targets
  check-wasm:
    name: Check wasm
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3
      - name: Install stable toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          toolchain: stable
          target: wasm32-unknown-unknown
      - name: Cache
        uses: Swatinem/rust-cache@v2
      - name: Check
        run: RUSTFLAGS=--cfg=web_sys_unstable_apis cargo check --target wasm32-unknown-unknown --all-targets
  lints:
    name: Lints
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3
      - name: Install stable toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          toolchain: stable
          components: rustfmt, clippy
      - name: Deps
        run: sudo apt-get install g++ pkg-config libx11-dev libasound2-dev libudev-dev
      - name: Cache
        uses: Swatinem/rust-cache@v2
      - name: Fmt
        run: cargo fmt --all -- --check
      - name: Clippy
        run: cargo clippy -- -A clippy::type_complexity -A clippy::too_many_arguments -D warnings
  test-cosmic-edit:
    name: Test cosmic-edit
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Install stable toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          toolchain: stable

      - name: Cache
        uses: Swatinem/rust-cache@v2
 
      - name: Test bevy-markdown
        run: cargo test -p bevy_cosmic_edit --lib
  coverage:
    runs-on: ubuntu-latest
    env:
      CARGO_TERM_COLOR: always
    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Install stable toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          toolchain: stable

      - name: Cache
        uses: Swatinem/rust-cache@v2

      - name: Install cargo-llvm-cov
        uses: taiki-e/install-action@cargo-llvm-cov
    
      - name: Generate code coverage
        run: cargo llvm-cov --all-features --workspace --lcov --output-path lcov.info
        
      - name: Upload coverage to Codecov
        uses: codecov/codecov-action@v3
        with:
          token: ${{ secrets.CODECOV_TOKEN }} # not required for public repos
          files: lcov.info
          fail_ci_if_error: true