name: Main workflows
on:
  push:
    branches:
      - main

jobs:
  benchmark:
    if: ${{ github.actor != 'dependabot[bot]' }}
    name: Upload docs and run benchmarks
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions-rs/toolchain@v1.0.7
        with:
          toolchain: stable
          override: true
          profile: minimal
      - name: Cache cargo
        uses: actions/cache@v3
        with:
          path: |
            target
            !target/doc_upload
            ~/.cargo/git
            ~/.cargo/registry
          key: ${{ runner.os }}-cargo-doc-bench-${{ hashFiles('**/Cargo.lock') }}
      - name: Generate documentation
        uses: actions-rs/cargo@v1
        with:
          command: doc
          args: -v --document-private-items --all-features --workspace --no-deps --exclude boa_examples
      - run: echo "<meta http-equiv=refresh content=0;url=boa_engine/index.html>" > target/doc/index.html
      - run: |
          if [ -d target/doc_upload ]; then rm -rf target/doc_upload; fi
          mkdir target/doc_upload && mv target/doc target/doc_upload/doc
      - name: Upload documentation
        uses: crazy-max/ghaction-github-pages@v3.1.0
        with:
          target_branch: gh-pages
          keep_history: true
          build_dir: target/doc_upload
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - name: Run benchmark
        run: cargo bench -p boa_engine -- --output-format bencher | tee output.txt
      - name: Store benchmark result
        uses: benchmark-action/github-action-benchmark@v1.15.0
        with:
          name: Boa Benchmarks
          tool: "cargo"
          output-file-path: output.txt
          auto-push: true
          github-token: ${{ secrets.GITHUB_TOKEN }}
