# This file has been originally generated by maturin v0.14.17
# To update, you can check
#
#    maturin generate-ci github --zig
#
# But note that some manual modification has been done.
# Check the diffs to make sure that you haven't broken anything.

name: pip-release

on:
  release:
    types:
      - "published"
  workflow_dispatch:
  push:
    branches:
      - main
  pull_request:
    paths:
      - "apis/python/node/**"
      - "binaries/cli/**"
      - "Cargo.toml"

permissions:
  contents: write

jobs:
  linux:
    runs-on: ${{ matrix.platform.runner }}
    strategy:
      fail-fast: false
      matrix:
        platform:
          - runner: ubuntu-22.04
            target: x86_64
          - runner: ubuntu-22.04
            target: x86
          - runner: ubuntu-22.04
            target: aarch64
          - runner: ubuntu-22.04
            target: armv7
          # - runner: ubuntu-22.04
          # target: s390x
          # - runner: ubuntu-22.04
          # target: ppc64le
        repository:
          - path: apis/python/node
            name: dora-node-api
          - path: binaries/cli
            name: dora-rs-cli
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: 3.8
      - uses: Swatinem/rust-cache@v2
        with:
          cache-provider: buildjet
          # only save caches for `main` branch
          save-if: ${{ github.ref == 'refs/heads/main' }}
      - name: Build wheels
        uses: PyO3/maturin-action@v1
        with:
          target: ${{ matrix.platform.target }}
          args: --release --out dist --zig
          manylinux: manylinux_2_28
          working-directory: ${{ matrix.repository.path }}
          before-script-linux: sudo apt-get install libatomic1-i386-cross libatomic1-armhf-cross && mkdir -p $HOME/.rustup/toolchains/1.84-x86_64-unknown-linux-gnu/lib/rustlib/i686-unknown-linux-gnu/lib/ && ln -s /usr/i686-linux-gnu/lib/libatomic.so.1 $HOME/.rustup/toolchains/1.84-x86_64-unknown-linux-gnu/lib/rustlib/i686-unknown-linux-gnu/lib/libatomic.so && ln -s /usr/i686-linux-gnu/lib/libatomic.so.1 $HOME/.rustup/toolchains/1.84-x86_64-unknown-linux-gnu/lib/rustlib/i686-unknown-linux-gnu/lib/libatomic.so.1 && ln -s /usr/i686-linux-gnu/lib/libatomic.so.1 /opt/hostedtoolcache/Python/3.8.18/x64/lib/libatomic.so.1 && mkdir -p $HOME/.rustup/toolchains/1.84-x86_64-unknown-linux-gnu/lib/rustlib/armv7-unknown-linux-gnueabihf/lib/ && ln -s /usr/arm-linux-gnueabihf/lib/libatomic.so.1 $HOME/.rustup/toolchains/1.84-x86_64-unknown-linux-gnu/lib/rustlib/armv7-unknown-linux-gnueabihf/lib/libatomic.so
      - name: Upload wheels
        if: github.event_name == 'release'
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.repository.name }}-linux-${{ matrix.platform.target }}
          path: ${{ matrix.repository.path }}/dist

  musllinux:
    runs-on: ${{ matrix.platform.runner }}
    strategy:
      fail-fast: false
      matrix:
        platform:
          - runner: ubuntu-22.04
            target: x86_64
          - runner: ubuntu-22.04
            target: x86
          - runner: ubuntu-22.04
            target: aarch64
        repository:
          - path: apis/python/node
            name: dora-node-api
          - path: binaries/cli
            name: dora-rs-cli
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: 3.8
      - name: Build wheels
        uses: PyO3/maturin-action@v1
        with:
          target: ${{ matrix.platform.target }}
          args: --release --out dist
          sccache: "false"
          manylinux: musllinux_1_2
          working-directory: ${{ matrix.repository.path }}

      - name: Upload wheels
        if: github.event_name == 'release'
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.repository.name }}-musllinux-${{ matrix.platform.target }}
          path: ${{ matrix.repository.path }}/dist

  musleabi:
    runs-on: ubuntu-22.04
    strategy:
      fail-fast: false
      matrix:
        platform:
          [
            {
              target: "armv7-unknown-linux-musleabihf",
              image_tag: "armv7-musleabihf",
            },
          ]
        repository:
          - path: apis/python/node
            name: dora-node-api
          - path: binaries/cli
            name: dora-rs-cli
    container:
      image: docker://messense/rust-musl-cross:${{ matrix.platform.image_tag }}
      env:
        CFLAGS_armv7_unknown_linux_musleabihf: "-mfpu=vfpv3-d16"
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: 3.8
      - name: Build Wheels
        uses: PyO3/maturin-action@v1
        with:
          target: ${{ matrix.platform.target }}
          manylinux: auto
          container: off
          args: --release -o dist
          working-directory: ${{ matrix.repository.path }}
      - name: Upload wheels
        if: github.event_name == 'release'
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.repository.name }}-musllinux-${{ matrix.platform.target }}
          path: ${{ matrix.repository.path }}/dist

  windows:
    runs-on: ${{ matrix.platform.runner }}
    strategy:
      fail-fast: false
      matrix:
        platform:
          - runner: windows-latest
            target: x64
        repository:
          - path: apis/python/node
            name: dora-node-api
          - path: binaries/cli
            name: dora-rs-cli
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: 3.8
          architecture: ${{ matrix.platform.target }}
      - name: Build wheels
        uses: PyO3/maturin-action@v1
        with:
          target: ${{ matrix.platform.target }}
          args: --release --out dist -i 3.8
          sccache: "true"
          working-directory: ${{ matrix.repository.path }}
      - name: Upload wheels
        if: github.event_name == 'release'
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.repository.name }}-windows-${{ matrix.platform.target }}
          path: ${{ matrix.repository.path }}/dist

  macos:
    runs-on: ${{ matrix.platform.runner }}
    strategy:
      fail-fast: false
      matrix:
        platform:
          - runner: macos-13
            target: aarch64
        repository:
          - path: apis/python/node
            name: dora-node-api
          - path: binaries/cli
            name: dora-rs-cli
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: 3.8
      - name: Build wheels
        uses: PyO3/maturin-action@v1
        with:
          target: ${{ matrix.platform.target }}
          args: --release --out dist -i 3.8
          working-directory: ${{ matrix.repository.path }}
      - name: Upload wheels
        if: github.event_name == 'release'
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.repository.name }}-macos-${{ matrix.platform.target }}
          path: ${{ matrix.repository.path }}/dist

  sdist:
    runs-on: ubuntu-22.04
    strategy:
      fail-fast: false
      matrix:
        repository:
          - path: apis/python/node
            name: dora-node-api
          - path: binaries/cli
            name: dora-rs-cli
    steps:
      - uses: actions/checkout@v3
      - name: Build sdist
        uses: PyO3/maturin-action@v1
        with:
          command: sdist
          args: --out dist
          working-directory: ${{ matrix.repository.path }}
      - name: Upload sdist
        if: github.event_name == 'release'
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.repository.name }}-sdist
          path: ${{ matrix.repository.path }}/dist

  release:
    name: Release
    runs-on: ubuntu-22.04
    if: github.event_name == 'workflow_dispatch' || github.event_name == 'release' && startsWith(github.ref, 'refs/tags/')
    needs: [linux, musllinux, musleabi, windows, macos, sdist]
    strategy:
      fail-fast: false
      matrix:
        repository:
          - path: apis/python/node
            name: dora-node-api
          - path: binaries/cli
            name: dora-rs-cli
    steps:
      - uses: actions/download-artifact@v4
      - name: Publish to PyPI
        uses: PyO3/maturin-action@v1
        env:
          MATURIN_PYPI_TOKEN: ${{ secrets.PYPI_PASS }}
        with:
          command: upload
          args: --non-interactive --skip-existing ${{ matrix.repository.name }}-*/*
