name: main

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

jobs:
  # skip ci if the last commit contains the appropriate tag
  skip-commit:
    name: Conditionally skip ci
    runs-on: ubuntu-latest
    steps:
      - if: "contains(github.event.head_commit.message, '[skip-ci]')
          || contains(github.event.head_commit.message, '[skip ci]')
          || contains(github.event.head_commit.message, '[ci-skip]')
          || contains(github.event.head_commit.message, '[ci skip]')"
        run: exit 78

  # verify that Cargo.lock passes audit
  cargo-audit:
    name: Run cargo audit
    needs: [skip-commit]
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Fetch latest release version of cargo-audit
      run: |
        mkdir -p .github/caching
        cargo search cargo-audit | grep '^cargo-audit' | awk '{gsub(/"/,"",$3); print $3}' > .github/caching/cargo-audit.lock
    - name: Cache cargo-audit/bin
      id: cache-cargo-audit
      uses: actions/cache@v1
      with:
        path: ${{ runner.tool_cache }}/cargo-audit/bin
        key: cargo-audit-bin-${{ hashFiles('.github/caching/cargo-audit.lock') }}
    - name: Install cargo-audit
      if: "steps.cache-cargo-audit.outputs.cache-hit != 'true'"
      uses: actions-rs/cargo@v1
      with:
        command: install
        args: --root ${{ runner.tool_cache }}/cargo-audit --force cargo-audit
    - run: echo "${{ runner.tool_cache }}/cargo-audit/bin" >> $GITHUB_PATH
    - run: cargo audit

  # verify that project passes clippy lints
  cargo-clippy:
    name: Run cargo clippy
    needs: [skip-commit]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Install Rust toolchain
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          components: clippy
      - name: Run cargo clippy
        uses: actions-rs/clippy-check@v1
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          args: --all-targets --workspace -- -D warnings

  # build the documentation
  cargo-docs:
    name: Run cargo docs
    needs: [skip-commit]
    runs-on: ubuntu-latest
    env:
      RUST_TOOLCHAIN: nightly
    steps:
      - uses: actions/checkout@v2
      - name: Install Rust toolchain
        uses: actions-rs/toolchain@v1
        with:
          toolchain: ${{ env.RUST_TOOLCHAIN }}
          profile: minimal
          override: true
      - name: Run cargo xtask doc
        uses: actions-rs/cargo@v1
        with:
          command: xtask
          args: doc -- --no-deps --package tree-sitter-facade
      - uses: peaceiris/actions-gh-pages@v3
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_dir: ./target/doc
        if: github.event_name == 'push' && github.ref == 'refs/heads/main'

  # verify that code is formatted
  cargo-fmt:
    name: Run cargo fmt
    needs: [skip-commit]
    runs-on: ubuntu-latest
    env:
      RUST_TOOLCHAIN: nightly
    steps:
      - uses: actions/checkout@v2
      - name: Install Rust toolchain
        uses: actions-rs/toolchain@v1
        with:
          toolchain: ${{ env.RUST_TOOLCHAIN }}
          profile: minimal
          override: true
          components: rustfmt
      - name: Run cargo fmt
        uses: actions-rs/cargo@v1
        with:
          toolchain: ${{ env.RUST_TOOLCHAIN }}
          command: fmt
          args: --all -- --check

  # verify that tests pass for native target
  cargo-test-native:
    name: Run cargo test (native)
    needs: [skip-commit]
    strategy:
      matrix:
        os: [macos-latest, ubuntu-latest, windows-latest]
        runtime: [smol, tokio]
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v2
      - name: Install Rust toolchain
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
      - name: Run cargo xtask test
        uses: actions-rs/cargo@v1
        with:
          command: xtask
          args: test

  # verify that tests pass for wasm target
  cargo-test-wasm:
    name: Run cargo test (wasm)
    needs: [skip-commit]
    runs-on: ubuntu-latest
    env:
      WASM_BINDGEN_CLI_VERSION: "0.2.70"
    steps:
      - uses: actions/checkout@v2
      - name: Install Rust toolchain
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
      - run: rustup target add wasm32-unknown-unknown
      - name: Cache wasm-bindgen-cli/bin
        id: cache-wasm-bindgen-cli
        uses: actions/cache@v1
        with:
          path: ${{ runner.tool_cache }}/wasm-bindgen-cli/bin
          key: wasm-bindgen-cli-bin-${{ hashFiles('.github/caching/wasm-bindgen-cli.lock') }}
      - name: Install wasm-bindgen-cli
        if: "steps.cache-wasm-bindgen-cli.outputs.cache-hit != 'true'"
        uses: actions-rs/cargo@v1
        with:
          command: install
          args: --root ${{ runner.tool_cache }}/wasm-bindgen-cli --force wasm-bindgen-cli --version ${{ env.WASM_BINDGEN_CLI_VERSION }}
      - name: Configure $PATH for wasm-bindgen-cli
        run: echo "${{ runner.tool_cache }}/wasm-bindgen-cli/bin" >> $GITHUB_PATH
      - name: Run cargo xtask test
        uses: actions-rs/cargo@v1
        with:
          command: xtask
          args: test -- --target wasm32-unknown-unknown
