name: ci
on:
  pull_request:
  push:
    branches:
      - master
  schedule:
    - cron: "0 0 * * 1"

jobs:
  clippy:
    name: Run linter
    runs-on: ubuntu-latest
    env:
      RUSTFLAGS: -D warnings
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Checkout submodules
        shell: bash
        run: |
          auth_header="$(git config --local --get http.https://github.com/.extraheader)"
          git submodule sync --recursive
          git -c "http.extraheader=$auth_header" -c protocol.version=2 submodule update --init --force --recursive --depth=1
      - uses: hecrj/setup-rust-action@v1
        with:
          components: clippy
      - name: Cache cargo directories
        uses: actions/cache@v2
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.toml') }}
          restore-keys: |
            ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.toml') }}
            ${{ runner.os }}-cargo-
      - name: Cache cargo target dirs
        uses: actions/cache@v2
        with:
          path: target
          key: ${{ runner.os }}-target-lint-stable-${{ hashFiles('**/Cargo.toml') }}
          restore-keys: |
            ${{ runner.os }}-target-lint-stable-${{ hashFiles('**/Cargo.toml') }}
            ${{ runner.os }}-target-lint-stable-
            ${{ runner.os }}-target-lint-
      - run: cargo clippy --workspace --all-targets
      # supported winit versions
      - run: cargo clippy --manifest-path imgui-winit-support/Cargo.toml --all-features --all-targets
      - run: cargo clippy --manifest-path imgui-winit-support/Cargo.toml --no-default-features --features winit-19 --all-targets
      - run: cargo clippy --manifest-path imgui-winit-support/Cargo.toml --no-default-features --features winit-20 --all-targets
      - run: cargo clippy --manifest-path imgui-winit-support/Cargo.toml --no-default-features --features winit-22 --all-targets
      - run: cargo clippy --manifest-path imgui-winit-support/Cargo.toml --no-default-features --features winit-23 --all-targets
      - run: cargo clippy --manifest-path imgui-winit-support/Cargo.toml --no-default-features --features winit-24 --all-targets
  rustfmt:
    name: Check rustfmt
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      # TODO: why not `with: {submodules: true}`
      - name: Checkout submodules
        shell: bash
        run: |
          auth_header="$(git config --local --get http.https://github.com/.extraheader)"
          git submodule sync --recursive
          git -c "http.extraheader=$auth_header" -c protocol.version=2 submodule update --init --force --recursive --depth=1
      - uses: hecrj/setup-rust-action@v1
        with:
          components: rustfmt
      - run: cargo fmt --all -- --check

  test:
    name: Run tests
    runs-on: ${{ matrix.os }}
    env:
      RUSTFLAGS: -D warnings
      RUST_BACKTRACE: 1
    strategy:
      fail-fast: false
      matrix:
        rust:
          - stable
          - beta
        os:
          - ubuntu-latest
          - macos-latest
          - windows-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Checkout submodules
        shell: bash
        run: |
          auth_header="$(git config --local --get http.https://github.com/.extraheader)"
          git submodule sync --recursive
          git -c "http.extraheader=$auth_header" -c protocol.version=2 submodule update --init --force --recursive --depth=1
      - name: Install toolchain
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: ${{ matrix.rust }}
          override: true
      - run: sudo apt install libxcb-shape0-dev libxcb-xfixes0-dev
        if: runner.os == 'Linux'
      # workaround for https://github.com/actions/cache/issues/403
      - name: Install GNU tar
        if: matrix.os == 'macos-latest'
        run: |
          brew install gnu-tar
          echo PATH="/usr/local/opt/gnu-tar/libexec/gnubin:$PATH" >> $GITHUB_ENV
      - name: Cache cargo directories
        uses: actions/cache@v2
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.toml') }}
          restore-keys: |
            ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.toml') }}
            ${{ runner.os }}-cargo-
      - name: Cache cargo target dirs
        uses: actions/cache@v2
        with:
          path: target
          # note `cargo test` and `cargo clippy` don't use the same build
          # artifacts, so this has a different key
          key: ${{ runner.os }}-target-test-${{ matrix.rust }}-${{ hashFiles('**/Cargo.toml') }}
          restore-keys: |
            ${{ runner.os }}-target-test-${{ matrix.rust }}-${{ hashFiles('**/Cargo.toml') }}
            ${{ runner.os }}-target-test-${{ matrix.rust }}-
      - run: cargo test --workspace --all-targets
      # - run: cargo test --manifest-path imgui-gfx-examples/Cargo.toml --no-default-features --features directx
      #   if: runner.os == 'Windows'
      - run: cargo test --manifest-path imgui-winit-support/Cargo.toml --no-default-features --features winit-19
      - run: cargo test --manifest-path imgui-winit-support/Cargo.toml --no-default-features --features winit-20
      - run: cargo test --manifest-path imgui-winit-support/Cargo.toml --no-default-features --features winit-22
      - run: cargo test --manifest-path imgui-winit-support/Cargo.toml --no-default-features --features winit-23
      - run: cargo test --manifest-path imgui-winit-support/Cargo.toml --no-default-features --features winit-24
      # Run unreasonably slow tests under release, but only the crates that have
      # them, and don't bother doing this on most platforms.
      - run: cargo test -p imgui --release -- --ignored
        if: matrix.os == 'ubuntu-latest' && matrix.rust == 'stable'
