name: Publish (PR)

on:
    pull_request:
        branches:
            - development

jobs:
    publish:
        runs-on: ${{ matrix.os }}

        strategy:
            matrix:
                os: [macos-latest, ubuntu-latest, windows-latest]

        steps:
            - name: Checkout git repo
              uses: actions/checkout@v3

            - name: Install Node and PNPM
              uses: pnpm/action-setup@v4
              with:
                  version: 9

            - name: Install dependencies
              run: pnpm install

            - name: Build for Windows
              if: ${{ matrix.os == 'windows-latest' }}
              uses: nick-invision/retry@v2.8.2
              with:
                  timeout_minutes: 30
                  max_attempts: 3
                  retry_on: error
                  command: |
                      pnpm run package:win:pr

            - name: Build for Linux
              if: ${{ matrix.os == 'ubuntu-latest' }}
              uses: nick-invision/retry@v2.8.2
              with:
                  timeout_minutes: 30
                  max_attempts: 3
                  retry_on: error
                  command: |
                      pnpm run package:linux:pr

            - name: Build for MacOS
              if: ${{ matrix.os == 'macos-latest' }}
              uses: nick-invision/retry@v2.8.2
              with:
                  timeout_minutes: 30
                  max_attempts: 3
                  retry_on: error
                  command: |
                      pnpm run package:mac:pr

            - name: Zip Windows Binaries
              if: ${{ matrix.os == 'windows-latest' }}
              shell: pwsh
              run: |
                  Compress-Archive -Path "dist/*.exe" -DestinationPath "dist/windows-binaries.zip" -Force

            - name: Zip Linux Binaries
              if: ${{ matrix.os == 'ubuntu-latest' }}
              run: |
                  zip -r dist/linux-binaries.zip dist/*.{AppImage,deb,rpm}

            - name: Zip MacOS Binaries
              if: ${{ matrix.os == 'macos-latest' }}
              run: |
                  zip -r dist/macos-binaries.zip dist/*.dmg

            - name: Upload Windows Binaries
              if: ${{ matrix.os == 'windows-latest' }}
              uses: actions/upload-artifact@v4
              with:
                  name: windows-binaries
                  path: dist/windows-binaries.zip

            - name: Upload Linux Binaries
              if: ${{ matrix.os == 'ubuntu-latest' }}
              uses: actions/upload-artifact@v4
              with:
                  name: linux-binaries
                  path: dist/linux-binaries.zip

            - name: Upload MacOS Binaries
              if: ${{ matrix.os == 'macos-latest' }}
              uses: actions/upload-artifact@v4
              with:
                  name: macos-binaries
                  path: dist/macos-binaries.zip
