name: Package

env:
  LC_COLLATE: en_US.UTF-8
on:
  push:
    tags:
      - "[0-9]+.[0-9]+.[0-9]+*"
jobs:
  package:
    permissions:
      # IMPORTANT: this permission is mandatory for trusted publishing
      id-token: write
      contents: write
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        python-version: [3.9]
        os: [ubuntu-22.04, macos-13, windows-latest]

    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python-version }}

      - name: Install tox
        run: |
          python -m pip install --upgrade --upgrade-strategy eager tox

      - name: Install macos dependencies
        run: |
          brew upgrade icu4c pkg-config || brew install icu4c pkg-config
        if: runner.os == 'macOS'

      - name: Install linux dependencies
        run: |
          sudo apt-get update && sudo apt-get upgrade && sudo apt-get install pkg-config libicu-dev libqt5gui5 libfuse2 desktop-file-utils
        if: runner.os == 'Linux'

      - name: Build, Install and Test PyPi packages
        run: |
          export PKG_CONFIG_PATH="/usr/local/opt/icu4c/lib/pkgconfig:/opt/homebrew/opt/icu4c/lib/pkgconfig${PKG_CONFIG_PATH+:$PKG_CONFIG_PATH}";
          export PATH="/usr/local/opt/icu4c/bin:/usr/local/opt/icu4c/sbin${PATH+:$PATH}"
          python -m tox r
          python -m tox r -m release
        shell: bash

      - name: Get release name
        if: startsWith(github.ref, 'refs/tags/')
        shell: bash
        run: |
          git fetch --depth=1 origin +refs/tags/*:refs/tags/* # github is dumb
          echo "release_name=$(git tag -l --format "%(refname:strip=2): %(contents:lines=1)" ${{ github.ref_name }})" >> $GITHUB_ENV

      - name: Release
        uses: softprops/action-gh-release@v2
        if: startsWith(github.ref, 'refs/tags/')
        with:
          name: "${{ env.release_name }}"
          prerelease: "${{ contains(github.ref, '-') }}" # alpha-releases should be 1.3.0-alpha.x full releases should be 1.3.0
          draft: false
          # upload the single application zip file for each OS and include the wheel built on linux
          files: |
            dist/binary/*.zip
            dist/binary/*.tar.gz
            dist/binary/*.dmg
            dist/binary/*.AppImage
            dist/*${{ fromJSON('["never", ""]')[runner.os == 'Linux'] }}.whl
