name: Build and publish Python package

on:
  push:
    branches:
      - master
    tags:
      - 'v*'
  workflow_dispatch:

jobs:
  build:
    strategy:
      matrix:
        feature: [core, js]
        os: [macos-11.0, ubuntu-18.04, windows-2019]
        python: [3.7, 3.8, 3.9]
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v1
      - name: Set up Python
        uses: actions/setup-python@v1
        with:
          python-version: ${{ matrix.python }}
          architecture: 'x64'

      - name: Set up Rust
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          profile: minimal
          default: true

      - name: Set up GCC (Windows)
        if: runner.os == 'Windows'
        run: .\.github\workflows\gcc.ps1

      - name: Set up Go
        uses: actions/setup-go@v2
        with:
          go-version: '^1.14.0'

      - name: Run prebuild steps
        shell: bash
        run: bash ./prebuild.sh

      - name: Build native module
        working-directory: ./python
        run: |
          python prepare.py ${{ matrix.feature }}
          cargo build --release

      - name: Install Python build tools (macOS)
        if: runner.os == 'macOS'
        run: sudo pip install --upgrade maturin==0.9.4 setuptools wheel twine
      - name: Install Python build tools (Linux, Windows)
        if: runner.os != 'macOS'
        run: pip install --upgrade maturin==0.9.4 setuptools wheel twine

      - name: Pack and publish package
        shell: bash
        working-directory: ./python
        run: |
          cat << 'EOF' > "$HOME/.pypirc"
          [pypi]
          username = __token__
          password = ${{ secrets.PYPI_API_TOKEN }}
          EOF
          maturin build --release --strip -i "$(which python)"
          if [[ "$GITHUB_REF" == refs/tags/v* ]]; then
            twine upload target/wheels/*
          fi
