name: PyPI

on:
  release:
    types: [published]
  workflow_dispatch:

permissions:
  contents: read
  id-token: write

jobs:
  build:
    strategy:
      fail-fast: false
      matrix:
        include:
          - target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
          - target: x86_64-apple-darwin
            os: macos-14
          - target: aarch64-apple-darwin
            os: macos-latest
          - target: x86_64-pc-windows-msvc
            os: windows-latest

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

    steps:
      - uses: actions/checkout@v4

      - name: Clone instruction-files (path dependency)
        run: git clone --depth 1 https://github.com/btakita/instruction-files.git ../instruction-files

      - uses: actions/setup-python@v5
        with:
          python-version: "3.12"

      - name: Build wheel
        uses: PyO3/maturin-action@v1
        with:
          target: ${{ matrix.target }}
          args: --release --out dist --features vendored-openssl
          manylinux: auto
          before-script-linux: |
            yum install -y perl-core || apk add --no-cache perl || true
            git clone --depth 1 https://github.com/btakita/instruction-files.git ../instruction-files

      - uses: actions/upload-artifact@v4
        with:
          name: wheel-${{ matrix.target }}
          path: dist

  sdist:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Clone instruction-files (path dependency)
        run: git clone --depth 1 https://github.com/btakita/instruction-files.git ../instruction-files

      - name: Build sdist
        uses: PyO3/maturin-action@v1
        with:
          command: sdist
          args: --out dist

      - uses: actions/upload-artifact@v4
        with:
          name: sdist
          path: dist

  publish:
    needs: [build, sdist]
    runs-on: ubuntu-latest
    environment: pypi

    steps:
      - uses: actions/download-artifact@v4
        with:
          path: dist
          merge-multiple: true

      - name: Publish to PyPI
        uses: pypa/gh-action-pypi-publish@release/v1
        with:
          packages-dir: dist
