# SPDX-FileCopyrightText: (c) TagStudio Contributors
# SPDX-License-Identifier: GPL-3.0-only
---
name: Release

on:
  push:
    tags:
      - v[0-9]+.[0-9]+.[0-9]+*

jobs:
  linux:
    strategy:
      matrix:
        build-type: ["", portable]
        include:
          - build-type: ""
            build-flag: ""
            suffix: ""
          - build-type: portable
            build-flag: --portable
            suffix: _portable
    runs-on: ubuntu-22.04
    steps:
      - name: Checkout repo
        uses: actions/checkout@v4

      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.12"
          cache: pip

      - name: Install Python dependencies
        run: |
          python -m pip install --upgrade uv
          uv pip install --system .[pyinstaller]

      - name: Execute PyInstaller
        run: |
          pyinstaller tagstudio.spec -- ${{ matrix.build-flag }}
          tar czfC dist/tagstudio_linux_x86_64${{ matrix.suffix }}.tar.gz dist tagstudio

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: tagstudio_linux_x86_64${{ matrix.suffix }}
          path: dist/tagstudio_linux_x86_64${{ matrix.suffix }}.tar.gz

  macos:
    strategy:
      matrix:
        os-version: ["14", "15"]
        include:
          - os-version: "14"
            arch: x86_64
          - os-version: "15"
            arch: aarch64

    runs-on: macos-${{ matrix.os-version }}

    env:
      # INFO: Even though we run on 14, target towards compatibility
      MACOSX_DEPLOYMENT_TARGET: "11.0"

    steps:
      - name: Checkout repo
        uses: actions/checkout@v4

      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.12"
          cache: pip

      - name: Install Python dependencies
        run: |
          python -m pip install --upgrade uv
          uv pip install --system .[pyinstaller]

      - name: Execute PyInstaller
        run: |
          pyinstaller tagstudio.spec
          tar czfC dist/tagstudio_macos_${{ matrix.arch }}.tar.gz dist TagStudio.app
      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: tagstudio_macos_${{ matrix.arch }}
          path: dist/tagstudio_macos_${{ matrix.arch }}.tar.gz

  windows:
    strategy:
      matrix:
        build-type: ["", portable]
        include:
          - build-type: ""
            build-flag: ""
            suffix: ""
            file-end: ""
          - build-type: portable
            build-flag: --portable
            suffix: _portable
            file-end: .exe

    runs-on: windows-2022

    steps:
      - name: Checkout repo
        uses: actions/checkout@v4

      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.12"
          cache: pip

      - name: Install Python dependencies
        run: |
          python -m pip install --upgrade uv
          uv pip install --system .[pyinstaller]

      - name: Execute PyInstaller
        run: |
          PyInstaller tagstudio.spec -- ${{ matrix.build-flag }}
          Compress-Archive -Path dist/TagStudio${{ matrix.file-end }} -DestinationPath dist/tagstudio_windows_x86_64${{ matrix.suffix }}.zip
      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: tagstudio_windows_x86_64${{ matrix.suffix }}
          path: dist/tagstudio_windows_x86_64${{ matrix.suffix }}.zip

  publish:
    needs: [linux, macos, windows]

    runs-on: ubuntu-latest

    permissions:
      contents: write

    steps:
      - name: Checkout repo
        uses: actions/checkout@v4

      - name: Fetch artifacts
        uses: actions/download-artifact@v4

      - name: Publish release
        uses: softprops/action-gh-release@v2
        with:
          files: |
            tagstudio_linux_x86_64/*
            tagstudio_linux_x86_64_portable/*
            tagstudio_macos_x86_64/*
            tagstudio_macos_aarch64/*
            tagstudio_windows_x86_64/*
            tagstudio_windows_x86_64_portable/*
