name: CI

on:
  push:
    branches:
    - master
  pull_request:
    branches:
    - master

defaults:
  run:
    shell: bash

env:
  RUSTFLAGS: --deny warnings
  # keep in sync with `Cargo.toml`
  WASM_BINDGEN_VERSION: 0.2.83

jobs:
  test:
    name: Test

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2

    - name: Install Rust Toolchain Components
      uses: actions-rs/toolchain@v1
      with:
        target: wasm32-unknown-unknown
        toolchain: stable

    - uses: Swatinem/rust-cache@v1

    - name: Install `wasm-bindgen`
      uses: jetli/wasm-bindgen-action@v0.1.0
      with:
        version: ${{ env.WASM_BINDGEN_VERSION }}

    - name: Install browsers
      working-directory: tests
      run: |
        npm clean-install
        npx playwright install chromium
        npx playwright install webkit

    - name: Run Playwright tests
      working-directory: tests
      run: npx playwright test

    - uses: actions/upload-artifact@v2
      with:
        name: playwright-report
        path: tests/playwright-report/
        retention-days: 365

  check:
    name: Check

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2

    - name: Install Rust Toolchain Components
      uses: actions-rs/toolchain@v1
      with:
        components: clippy, rustfmt
        target: wasm32-unknown-unknown
        toolchain: stable

    - uses: Swatinem/rust-cache@v1
    - name: Check Lockfile
      run: cargo update --locked --package degenerate

    - name: Install `wasm-bindgen`
      uses: jetli/wasm-bindgen-action@v0.1.0
      with:
        version: ${{ env.WASM_BINDGEN_VERSION }}

    - name: Format
      run: |
        npx prettier --check tests features www/{index.js,interpreter.js,loader.js}
        cargo fmt --all -- --check

    - name: Clippy
      run: |
        cargo clippy --target wasm32-unknown-unknown

    - name: Check for Forbidden Words
      run: |
        sudo apt-get update
        sudo apt-get install ripgrep
        ./bin/forbid

  pages:
    name: Pages

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2

    - name: Install Rust Toolchain Components
      uses: actions-rs/toolchain@v1
      with:
        target: wasm32-unknown-unknown
        toolchain: stable

    - name: Install `wasm-bindgen`
      uses: jetli/wasm-bindgen-action@v0.1.0
      with:
        version: ${{ env.WASM_BINDGEN_VERSION }}

    - name: Install `mdbook`
      uses: peaceiris/actions-mdbook@v1
      with:
        mdbook-version: latest

    - name: Build Book
      run: mdbook build man

    - name: Build WASM
      run: |
        cargo \
          build \
          --release \
          --target wasm32-unknown-unknown
        wasm-bindgen \
          --target web \
          --no-typescript \
          --out-dir www \
          target/wasm32-unknown-unknown/release/degenerate.wasm
        cargo \
          build \
          --release \
          --target wasm32-unknown-unknown \
          --package program
        wasm-bindgen \
          --target web \
          --no-typescript \
          --out-dir www \
          target/wasm32-unknown-unknown/release/program.wasm

    - name: Record Git Revision
      run: git rev-parse --verify HEAD > www/commit.txt

    - name: Deploy Pages
      uses: peaceiris/actions-gh-pages@v3
      if: github.ref == 'refs/heads/master'
      with:
        github_token: ${{secrets.GITHUB_TOKEN}}
        publish_branch: gh-pages
        publish_dir: www
