Documentation
on:
  push:
  pull_request:
  workflow_dispatch:

jobs:
  test:
    name: Test (native)
    runs-on: ${{ matrix.platform.runs-on }}
    strategy:
      matrix:
        platform:
          - runs-on: ubuntu-latest
            channel: linux
          - runs-on: macos-latest
            channel: mac
          - runs-on: windows-latest
            channel: windows
    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - name: Install things
        if: matrix.platform.channel == 'linux'
        run: sudo apt-get update && sudo apt-get install -y libasound2-dev libudev-dev

      - name: Install Rust toolchain
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          profile: minimal

      - name: Test
        run: cargo test --workspace

  test-web:
    name: Test (web)
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2

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

      - name: Test
        run: cargo check --workspace --target wasm32-unknown-unknown

  pages:
    needs:
      - test
      - test-web
    name: Pages
    permissions:
      contents: write
    if: github.ref == 'refs/heads/main' && github.repository_owner == 'geng-engine'
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2

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

      - name: Install cargo-geng
        run: |
          cargo install cargo-geng --git https://github.com/geng-engine/cargo-geng

      - name: Build examples
        run: |
          mkdir -p website/static/examples
          examples_list="["
          for example in examples/*; do
            example=$(basename $example)
            example=${example%.rs}
            cargo geng build --release --web --package geng --example $example
            mv target/geng website/static/examples/$example
            examples_list="$examples_list\"$example\","
          done
          examples_list="$examples_list]"
          echo "$examples_list"
          sed -i "s|\\[\"this-will-be-populated-in-ci\"\\]|$examples_list|g" website/content/examples.md

      - name: Deploy GitHub Pages
        uses: shalzz/zola-deploy-action@v0.16.1-1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          BUILD_DIR: website