name: Rust

on:
  merge_group:
  push:
    branches: [master]
  pull_request:
    branches: [master]
  schedule:
    - cron: "10 6 * * *"

env:
  CARGO_TERM_COLOR: always
  # RUSTUP_TOOLCHAIN: ${{ !github.event.schedule && 'nightly-2023-12-01' || 'nightly' }}
  RUSTUP_TOOLCHAIN: "nightly"

jobs:
  build:
    name: Just CI
    runs-on: ubuntu-22.04
    steps:
      - name: Set CARGO_TARGET_DIR
        run: echo "CARGO_TARGET_DIR=$HOME/target" >> $GITHUB_ENV
      - name: Install build tools
        run: sudo apt-get update && sudo apt-get install build-essential libelf-dev zip libasound-dev -y
      - name: Install Miri
        run: |
          rustup component add miri clippy rustfmt
          cargo miri setup
      - uses: actions/checkout@v4
      - name: Cache
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            ~/target
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
      - name: install mgba-test-runner
        run: cargo install --path emulator/test-runner --verbose
      - uses: extractions/setup-just@v2
      - name: Setup mdBook
        uses: peaceiris/actions-mdbook@v2
        with:
          mdbook-version: "0.4.13"
      - name: Build and test all crates
        run: just ci
      - name: Upload build artifacts
        uses: actions/upload-artifact@v4
        with:
          name: example-games
          path: ~/target/*.gba