# Copyright © SixtyFPS GmbH <info@slint.dev>
# SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0

name: Upload component to Python Package Index

on:
    workflow_dispatch:
      inputs:
        release:
            type: boolean
            default: false
            required: false
            description: "Release? If false, publish to test.pypi.org, if true, publish to pypi.org"

jobs:
    build_binaries:
        env:
            MACOSX_DEPLOYMENT_TARGET: "11.0"
        strategy:
            matrix:
                platform:
                    - runner: windows-latest
                      target: x64
                      container: auto
# Pending https://github.com/actions/partner-runner-images/issues/85
#                    - runner: windows-11-arm
#                      target: aarch64
#                      container: auto
                    - runner: macos-14
                      target: aarch64
                      container: auto
                    - runner: ubuntu-22.04
                      target: x86_64
                      container: auto
                    - runner: ubuntu-22.04
                      target: aarch64
                      container: "ghcr.io/slint-ui/slint/aarch64-unknown-linux-gnu"
                    - runner: ubuntu-22.04
                      target: armv7
                      container: "ghcr.io/slint-ui/slint/armv7-unknown-linux-gnueabihf"
        runs-on: ${{ matrix.platform.runner }}
        steps:
            - uses: actions/checkout@v6
            - uses: ./.github/actions/install-linux-dependencies
              if: runner.os == 'Linux'
            - uses: actions/setup-python@v6
              with:
                python-version: '3.12'
            - uses: ./.github/actions/setup-rust
            - uses: baptiste0928/cargo-install@v3
              with:
                  crate: taplo-cli
            - name: Prepare feature config for binaries
              working-directory: api/python/slint
              shell: bash
              run: |
                cat Cargo.toml | taplo format --option column_width=100000 --stdin-filepath=Cargo.toml - | \
                  perl -p -e 's,^\s*default\s*=.*,,' | \
                  perl -p -e 's,# binaries:\s?,,' > Cargo.toml.new
                cat Cargo.toml.new | taplo format --stdin-filepath=Cargo.toml - > Cargo.toml
                rm Cargo.toml.new
                taplo get -f Cargo.toml features.default

            - name: Build a binary wheel
              uses: PyO3/maturin-action@v1
              with:
                  working-directory: api/python/slint
                  target: ${{ matrix.platform.target }}
                  args: --release --out wheelhouse --find-interpreter
                  container: ${{ matrix.platform.container }}
            - name: Store the distribution packages
              uses: actions/upload-artifact@v7
              with:
                name: python-package-distributions-${{ matrix.platform.runner }}-${{ strategy.job-index }}
                path: api/python/slint/wheelhouse/*.whl

    ios_binaries:
        runs-on: macos-14
        steps:
            - uses: actions/checkout@v6
            - uses: ./.github/actions/setup-rust
              with:
                  target: "aarch64-apple-ios aarch64-apple-ios-sim"
            - uses: baptiste0928/cargo-install@v3
              with:
                  crate: taplo-cli
            - name: Prepare feature config for binaries
              working-directory: api/python/slint
              shell: bash
              run: |
                cat Cargo.toml | taplo format --option column_width=100000 --stdin-filepath=Cargo.toml - | \
                  perl -p -e 's,^\s*default\s*=.*,,' | \
                  perl -p -e 's,# binaries:\s?,,' > Cargo.toml.new
                cat Cargo.toml.new | taplo format --stdin-filepath=Cargo.toml - > Cargo.toml
                rm Cargo.toml.new
                taplo get -f Cargo.toml features.default

            - name: Build iOS wheels
              uses: pypa/cibuildwheel@v3.4.1
              with:
                  package-dir: api/python/slint
                  output-dir: wheelhouse
              env:
                  CIBW_PLATFORM: ios
                  CIBW_ARCHS: arm64_iphoneos arm64_iphonesimulator

            - name: Store the distribution packages
              uses: actions/upload-artifact@v7
              with:
                name: python-package-distributions-ios
                path: wheelhouse/*.whl

    build_source_package:
        name: Build source package
        runs-on: ubuntu-latest
        steps:
            - uses: actions/checkout@v6
            - uses: baptiste0928/cargo-install@v3
              with:
                  crate: taplo-cli
            - name: Prepare feature config for binaries
              working-directory: api/python/slint
              shell: bash
              run: |
                cat Cargo.toml | taplo format --option column_width=100000 --stdin-filepath=Cargo.toml - | \
                  perl -p -e 's,^\s*default\s*=.*,,' | \
                  perl -p -e 's,# binaries:\s?,,' > Cargo.toml.new
                cat Cargo.toml.new | taplo format --stdin-filepath=Cargo.toml - > Cargo.toml
                rm Cargo.toml.new
                taplo get -f Cargo.toml features.default
            - name: Build source package
              uses: PyO3/maturin-action@v1
              with:
                  working-directory: api/python/slint
                  command: sdist
                  args: --out dist
            - uses: actions/upload-artifact@v7
              with:
                name: python-package-distributions-source
                path: api/python/slint/dist/*.tar.gz

    publish-to-test-pypi:
        if: ${{ github.event.inputs.release != 'true' }}
        name: >-
            Publish Python 🐍 distribution 📦 to Test PyPI
        needs: [build_binaries, ios_binaries, build_source_package]
        runs-on: ubuntu-latest
        environment:
            name: testpypi
            url: https://test.pypi.org/p/slint
        permissions:
            id-token: write  # IMPORTANT: mandatory for trusted publishing
        steps:
            - uses: actions/download-artifact@v8
              with:
                pattern: python-package-distributions-*
                path: dist
                merge-multiple: true
            - name: Publish distribution 📦 to Test PyPI
              uses: pypa/gh-action-pypi-publish@release/v1
              with:
                repository-url: https://test.pypi.org/legacy/

    publish-to-pypi:
      if: ${{ github.event.inputs.release == 'true' }}
      name: >-
          Publish Python 🐍 distribution 📦 to PyPI
      needs: [build_binaries, ios_binaries, build_source_package]
      runs-on: ubuntu-latest
      environment:
          name: pypi
          url: https://test.pypi.org/p/slint
      permissions:
          id-token: write  # IMPORTANT: mandatory for trusted publishing
      steps:
          - uses: actions/download-artifact@v8
            with:
              pattern: python-package-distributions-*
              path: dist
              merge-multiple: true
          - name: Publish distribution 📦 to PyPI
            uses: pypa/gh-action-pypi-publish@release/v1
            with:
              verbose: true

