name: CI

on: [push, pull_request]

jobs:
  test:
    name: Test
    runs-on: ubuntu-latest
    steps:
      - name: Cancel Previous Runs
        uses: styfle/cancel-workflow-action@0.12.1
      - name: Checkout
        uses: actions/checkout@v4
      - name: Get current date for caching
        id: get-date
        shell: bash
        run: |
          echo date=$(date -u +%Y-%m-%d) >> $GITHUB_OUTPUT
      - uses: actions/cache@v4
        id: cache
        with:
          path: |
            ~/.cargo/bin
            ~/.cargo/registry
          key: ${{ runner.os }}-cargo-${{ steps.get-date.outputs.date }}
      - name: Install Rust and Garden
        if: steps.cache.outputs.cache-hit != 'true'
        run: |
          rustup toolchain install stable --profile minimal --no-self-update
          cargo install garden-tools
      - name: Install system-provided test dependencies
        run: |
          sudo apt-get update
          sudo apt-get install \
              python3-pyqt5 \
              python3-pyqt5.qtopengl \
              python3-pyqt5.qtsvg \
              python3-pyqt5.qtwebengine \
              python3-qtpy
      - name: Install pip-provided test dependencies
        run: |
          set -x
          garden dev/virtualenv -vv -D pip_upgrade=true
          garden dev -vv
      - name: Configure tests
        run: |
          git config --global user.name "Git Cola"
          git config --global user.email git-cola@localhost
      - name: Run tests and checks
        run: garden check -vv
      - name: Code formatting
        run: garden fmt -vv -- --check
      - name: Test the garden installation method
        run: garden install -vv -D prefix=dist

  windows:
    name: Windows
    runs-on: windows-latest
    steps:
      - name: Cancel Previous Runs
        uses: styfle/cancel-workflow-action@0.12.1
      - name: Checkout
        uses: actions/checkout@v4
      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.x'
          architecture: x64
      - name: Pip Cache
        uses: actions/cache@v4
        with:
          path: ~/AppData/Local/pip/Cache
          key: ${{ runner.os }}-pip-${{ hashFiles('pyproject.toml') }}
      - name: Install Dependencies
        run: |
          pip install '.[build,docs,dev,extras]'
      # Based on: contrib/win32/run-pynsist.sh
      - name: Build Installer
        run: |
          make doc
          make htmldir="$PWD/share/doc/git-cola/html" install-doc
          pynsist pynsist.cfg
          rm -r share/doc/git-cola/html

          # Rename executable output
          cd build/nsis
          mv git-cola*.exe git-cola-latest-${{github.run_number}}.exe
      - name: Upload Artifact
        uses: actions/upload-artifact@v4
        with:
          name: git-cola-latest-${{github.run_number}}-${{runner.os}}
          path: build/nsis/git-cola*.exe

  macos:
    name: macOS
    runs-on: macos-latest
    steps:
      - name: Cancel Previous Runs
        uses: styfle/cancel-workflow-action@0.12.1
      - name: Checkout
        uses: actions/checkout@v4
      - name: Get current date for caching
        id: get-date
        shell: bash
        run: |
          echo date=$(date -u +%Y-%m-%d) >> $GITHUB_OUTPUT
      - uses: actions/cache@v4
        id: cache
        with:
          path: |
            ~/.cargo/bin
            ~/.cargo/registry
          key: ${{ runner.os }}-cargo-${{ steps.get-date.outputs.date }}
      - name: Install Rust and Garden
        if: steps.cache.outputs.cache-hit != 'true'
        run: |
          rustup toolchain install stable --profile minimal --no-self-update
          cargo install garden-tools
      - name: Install Dependencies
        run: |
          brew install git-cola
      - name: Build Bundle
        run: |
          garden macos/app -vv
          mv git-cola.app build/
      - name: Zip Artifact
        run: zip -r git-cola.app.zip build/*
      - name: Upload Artifact
        uses: actions/upload-artifact@v4
        with:
          name: git-cola-latest-${{github.run_number}}-${{runner.os}}
          path: git-cola.app.zip
