---
name: CI
on:
  push:
    branches:
      - main
    tags:
      - '*'
  pull_request:

concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: true

jobs:
  test:
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: macos-latest
          - os: windows-latest
    steps:
      - uses: actions/checkout@v3
        with:
          submodules: recursive
      - uses: actions/setup-python@v4
        with:
          python-version: '3.10.7'

      - name: Install Poetry Action
        uses: snok/install-poetry@v1.3.1
        with:
          virtualenvs-create: true
          virtualenvs-in-project: true

      - name: Load cached venv
        id: cached-poetry-dependencies
        uses: actions/cache@v3
        with:
          path: .venv
          key: venv-${{ runner.os }}-${{ hashFiles('**/poetry.lock') }}-2

      - name: Load cached Whisper models
        id: cached-whisper-models
        uses: actions/cache@v3
        with:
          path: |
            ~/Library/Caches/Buzz
            ~/.cache/whisper
          key: whisper-models-${{ runner.os }}

      - uses: FedericoCarboni/setup-ffmpeg@v1
        id: setup-ffmpeg
        with:
          token: ${{ secrets.GITHUB_TOKEN }}

      - name: Install dependencies
        run: poetry config experimental.new-installer false && poetry install

      - name: Test
        run: poetry run make test

  build:
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: macos-latest
          - os: windows-latest
    steps:
      - uses: actions/checkout@v3
        with:
          submodules: recursive
      - uses: actions/setup-python@v4
        with:
          python-version: '3.10.7'

      - name: Install Poetry Action
        uses: snok/install-poetry@v1.3.1
        with:
          virtualenvs-create: true
          virtualenvs-in-project: true

      - name: Load cached venv
        id: cached-poetry-dependencies
        uses: actions/cache@v3
        with:
          path: .venv
          key: venv-${{ runner.os }}-${{ hashFiles('**/poetry.lock') }}-2

      - uses: FedericoCarboni/setup-ffmpeg@v1
        id: setup-ffmpeg
        with:
          token: ${{ secrets.GITHUB_TOKEN }}

      - name: Install dependencies
        run: poetry config experimental.new-installer false && poetry install

      - name: Bundle
        run: |
          if [ "$RUNNER_OS" == "macOS" ]; then
            poetry run make bundle_mac
          elif [ "$RUNNER_OS" == "Windows" ]; then
            poetry run make dist/Buzz.exe dist/Buzz-windows.exe
          elif [ "$RUNNER_OS" == "Linux" ]; then
            poetry run make bundle_linux
          fi
        env:
          BUZZ_VERSION: ${{ github.ref_name }}
        shell: bash

      - uses: actions/upload-artifact@v3
        with:
          name: Buzz-${{ runner.os }}
          path: |
            dist/Buzz*.tar.gz
            dist/Buzz*.zip
            dist/Buzz*.exe

  release:
    runs-on: ubuntu-latest
    needs: [build, test]
    if: startsWith(github.ref, 'refs/tags/')
    steps:
      - uses: actions/download-artifact@v3
        with:
          name: Buzz-Linux
      - uses: actions/download-artifact@v3
        with:
          name: Buzz-Windows
      - name: Release
        uses: softprops/action-gh-release@v1
        with:
          files: |
            Buzz*.tar.gz
            Buzz*.dmg
            Buzz.exe
