name: Rust

on: [ workflow_dispatch, push, pull_request ]

jobs:
  check-format:
    runs-on: ubuntu-20.04
    defaults:
      run:
        shell: bash
    steps:
      - uses: actions/checkout@v2
        with:
          submodules: 'recursive'
      - name: Setup
        run: |
          rustup component add rustfmt
      - name: Format
        run: cargo fmt --all -- --check

  build-x86:
    runs-on: ubuntu-20.04
    defaults:
      run:
        shell: bash
    steps:
      - uses: actions/checkout@v2
        with:
          submodules: 'recursive'
      - uses: actions/cache@v2
        with:
          path: |
            ~/.cargo/bin/
            ~/.cargo/registry/index/
            ~/.cargo/registry/cache/
            ~/.cargo/git/db/
            ~/.cargo/.crates*
            target/
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
      - name: Install Dependencies
        run: sudo apt-get install -y libwayland-dev libxkbcommon-dev # Required for winit
      - name: Build
        run: cargo build --example desktop
      - name: Clippy
        run: |
          rustup component add clippy
          cargo clippy --all-targets --all-features
      - name: API Documentation
        run: cargo doc --no-deps --workspace --lib --all-features --document-private-items
      - name: Deploy API Documentation
        env:
          SSH_KEY: ${{ secrets.SSH_KEY_MAXAMMANN_ORG }}
        run: |
          echo "$SSH_KEY" > id_rsa
          chmod 600 id_rsa
          ssh -o StrictHostKeyChecking=no -i id_rsa max@maxammann.org 'mkdir -p ~/public_html/mapr/api-docs && find ~/public_html/mapr/api-docs -type f -not -name ".htaccess" -delete'
          rsync -e "ssh -o StrictHostKeyChecking=no -i id_rsa" -r "target/x86_64-unknown-linux-gnu/doc/." max@maxammann.org:~/public_html/mapr/api-docs/
      - uses: actions/upload-artifact@v2
        with:
          name: mapr
          path: target/debug/mapr

  build-aarch64-android:
    runs-on: ubuntu-20.04
    defaults:
      run:
        shell: bash
    steps:
      - uses: actions/checkout@v2
        with:
          submodules: 'recursive'
      - uses: actions/cache@v2
        with:
          path: |
            ~/.cargo/bin/
            ~/.cargo/registry/index/
            ~/.cargo/registry/cache/
            ~/.cargo/git/db/
            ~/.cargo/.crates*
            target/
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
      - name: Prepare Rust Toolchain # This is required because we are recompiling the stdlib
        run: |
          rustup install nightly-2022-04-04-x86_64-unknown-linux-gnu
          rustup component add rust-src --toolchain nightly-2022-04-04-x86_64-unknown-linux-gnu
      - name: Build
        run: tools/build-android
      - uses: actions/upload-artifact@v2
        with:
          name: mapr.apk
          path: target/debug/apk/mapr-demo.apk

  build-wasm-webgl:
    runs-on: ubuntu-20.04
    defaults:
      run:
        shell: bash
        working-directory: web
    steps:
      - uses: actions/checkout@v2
        with:
          submodules: 'recursive'
      - uses: actions/cache@v2
        with:
          path: |
            ~/.cargo/bin/
            ~/.cargo/registry/index/
            ~/.cargo/registry/cache/
            ~/.cargo/git/db/
            ~/.cargo/.crates*
            target/
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
      - name: Prepare Rust Toolchain # This is required because we are recompiling the stdlib
        run: |
          rustup install nightly-2022-04-04-x86_64-unknown-linux-gnu
          rustup component add rust-src --toolchain nightly-2022-04-04-x86_64-unknown-linux-gnu
      - name: NPM Install
        run: npm install
      - name: Build
        run: npm run webgl-production-build
      - name: Deploy to maxammann.org
        env:
          SSH_KEY: ${{ secrets.SSH_KEY_MAXAMMANN_ORG }}
        run: |
          echo "$SSH_KEY" > id_rsa
          chmod 600 id_rsa
          ssh -o StrictHostKeyChecking=no -i id_rsa max@maxammann.org 'mkdir -p ~/public_html/mapr/webgl && find ~/public_html/mapr/webgl -type f -not -name ".htaccess" -delete'
          rsync -e "ssh -o StrictHostKeyChecking=no -i id_rsa" -r "dist/demo/." max@maxammann.org:~/public_html/mapr/webgl/

  build-wasm-webgpu:
    runs-on: ubuntu-20.04
    defaults:
      run:
        shell: bash
        working-directory: web
    steps:
      - uses: actions/checkout@v2
        with:
          submodules: 'recursive'
      - uses: actions/cache@v2
        with:
          path: |
            ~/.cargo/bin/
            ~/.cargo/registry/index/
            ~/.cargo/registry/cache/
            ~/.cargo/git/db/
            ~/.cargo/.crates*
            target/
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
      - name: Prepare Rust Toolchain # This is required because we are recompiling the stdlib
        run: |
          rustup install nightly-2022-04-04-x86_64-unknown-linux-gnu
          rustup component add rust-src --toolchain nightly-2022-04-04-x86_64-unknown-linux-gnu
      - name: NPM Install
        run: npm install
      - name: Build
        run: npm run production-build
      - name: Deploy to maxammann.org
        env:
          SSH_KEY: ${{ secrets.SSH_KEY_MAXAMMANN_ORG }}
        run: |
          echo "$SSH_KEY" > id_rsa
          chmod 600 id_rsa
          ssh -o StrictHostKeyChecking=no -i id_rsa max@maxammann.org 'mkdir -p ~/public_html/mapr/webgpu && find ~/public_html/mapr/webgpu -type f -not -name ".htaccess" -delete'
          rsync -e "ssh -o StrictHostKeyChecking=no -i id_rsa" -r "dist/demo/." max@maxammann.org:~/public_html/mapr/webgpu/

  book:
    runs-on: ubuntu-20.04
    defaults:
      run:
        shell: bash
        working-directory: docs
    steps:
      - uses: actions/checkout@v2
        with:
          submodules: 'recursive'
      - uses: actions/cache@v2
        with:
          path: |
            ~/.cargo/bin/
            ~/.cargo/registry/index/
            ~/.cargo/registry/cache/
            ~/.cargo/git/db/
            ~/.cargo/.crates*
            target/
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}-1
      - name: Install mdbook
        run: |
          cargo install mdbook --no-default-features --features output --vers "^0.1.0"
      - name: Build
        run: mdbook build
      - name: Deploy to maxammann.org
        env:
          SSH_KEY: ${{ secrets.SSH_KEY_MAXAMMANN_ORG }}
        run: |
          echo "$SSH_KEY" > id_rsa
          chmod 600 id_rsa
          ssh -o StrictHostKeyChecking=no -i id_rsa max@maxammann.org 'mkdir -p ~/public_html/mapr/docs && find ~/public_html/mapr/docs -type f -not -name ".htaccess" -delete'
          rsync -e "ssh -o StrictHostKeyChecking=no -i id_rsa" -r "book/." max@maxammann.org:~/public_html/mapr/docs/
