name: Release to pypi.org

on:
  release:
    types: [created]

jobs:
  python-publish-nix:
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        python-version: [3.6, 3.7, 3.8, 3.9]
        os: [ubuntu-latest, macos-latest]
    steps:
      - uses: actions/checkout@v1

      - uses: actions/setup-python@v1
        with:
          python-version: ${{ matrix.python-version }}

      - name: Install latest stable
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          override: true

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install poetry
          poetry install

      - name: Patch Cargo.toml file (no win)
        run: sed -i.bak 's/"rillrate-py"/"rillrate"/' Cargo.toml

      - name: Build Python package (no win)
        run: poetry run maturin build --release --no-sdist --strip --interpreter python${{matrix.python-version}}

      - name: List wheels (no win)
        run: find ./target/wheels/

      - name: Install wheels (no win)
        run: pip install target/wheels/rillrate*.whl

      - name: Release
        uses: softprops/action-gh-release@v1
        with:
          files: target/wheels/rillrate*.whl
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: PyPi publish (no win)
        env:
          MATURIN_PASSWORD: ${{ secrets.PYPI_TOKEN }}
        run: poetry run maturin publish --username __token__ --interpreter python${{matrix.python-version}}

  python-publish-win:
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        python-version: [3.6, 3.7, 3.8, 3.9]
        os: [windows-latest]
    steps:
      - uses: actions/checkout@v1

      - uses: actions/setup-python@v1
        with:
          python-version: ${{ matrix.python-version }}

      - name: Install latest stable
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          override: true

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install poetry
          poetry install

      - name: Patch Cargo.toml file (win)
        shell: pwsh
        run: (Get-Content Cargo.toml).replace('"rillrate-py"', '"rillrate"') | Set-Content Cargo.toml

      - name: Build Python package (win)
        run: poetry run maturin build --release --no-sdist --strip --interpreter python

      - name: List wheels (win)
        run: dir target\wheels\

      # Note: Windows doesn't support glob
      # https://stackoverflow.com/a/52481267/270334
      - name: Install wheels (win)
        run: pip install --find-links=target\wheels rillrate

      - name: Release
        uses: softprops/action-gh-release@v1
        with:
          files: target/wheels/rillrate*.whl
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: PyPi publish (win)
        env:
          MATURIN_PASSWORD: ${{ secrets.PYPI_TOKEN }}
        run: poetry run maturin publish --username __token__ --interpreter python
