name: Run Task

on:
  workflow_call:
    inputs:
      directory:
        required: true
        type: string
      cargo_make_task:
        required: true
        type: string
env:
  CARGO_TERM_COLOR: always
  CARGO_REGISTRIES_CRATES_IO_PROTOCOL: sparse

jobs:
  test:
    name: Run ${{ matrix.os }} (using rustc ${{ matrix.rust }})
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        rust:
          - nightly
        os:
          - ubuntu-latest

    steps:
      # Setup environment
      - name: Install playwright browser dependencies
        run: |
          sudo apt-get update
          sudo apt-get install libegl1 libvpx7 libevent-2.1-7 libopus0 libopengl0 libwoff1 libharfbuzz-icu0 libgstreamer-plugins-base1.0-0 libgstreamer-gl1.0-0 libhyphen0 libmanette-0.2-0 libgles2 gstreamer1.0-libav

      - uses: actions/checkout@v3

      - name: Setup Rust
        uses: actions-rs/toolchain@v1
        with:
          toolchain: ${{ matrix.rust }}
          override: true
          components: rustfmt

      - name: Add wasm32-unknown-unknown
        run: rustup target add wasm32-unknown-unknown

      - name: Setup cargo-make
        uses: davidB/rust-cargo-make@v1

      - name: Cargo generate-lockfile
        run: cargo generate-lockfile

      - uses: Swatinem/rust-cache@v2

      - name: Install Trunk
        uses: jetli/trunk-action@v0.4.0
        with:
          version: "latest"

      - name: Print Trunk Version
        run: trunk --version

      - name: Install Node.js
        uses: actions/setup-node@v3
        with:
          node-version: 18

      - uses: pnpm/action-setup@v2
        name: Install pnpm
        id: pnpm-install
        with:
          version: 8
          run_install: false

      - name: Get pnpm store directory
        id: pnpm-cache
        run: |
          echo "STORE_PATH=$(pnpm store path)" >> $GITHUB_OUTPUT

      - uses: actions/cache@v3
        name: Setup pnpm cache
        with:
          path: ${{ steps.pnpm-cache.outputs.STORE_PATH }}
          key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
          restore-keys: |
            ${{ runner.os }}-pnpm-store-

      - name: Install Chrome Webriver
        run: |
          sudo apt-get update
          sudo apt-get install chromium-chromedriver

      # Run Cargo Make Task
      - name: ${{ inputs.cargo_make_task }}
        run: |
          if [ "${{ inputs.directory }}" = "INTERNAL" ]; then
            echo No verification required
          else
            cd ${{ inputs.directory }}
            cargo make --profile=github-actions ${{ inputs.cargo_make_task }}
          fi
