name: upload pyrogis

on:
  release:
    types: [ created ]

jobs:
  manylinux-build:
    runs-on: ubuntu-latest
    container: quay.io/pypa/manylinux2014_x86_64
    steps:
      - uses: actions/checkout@v1
      - name: build wheels
        run: 'sh build_wheels_manylinux.sh'
        env:
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          AWS_DEFAULT_REGION: us-east-1
          DIST_DIR: ${{ github.sha }}

  macos-arm-build:
    runs-on: [ "self-hosted", "macos" ]
    continue-on-error: true
    timeout-minutes: 10
    steps:
      - uses: actions/checkout@v1
      - name: build wheels
        run: 'arch -arm64 sh build_wheels_mac_arm.sh'
        env:
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          AWS_DEFAULT_REGION: us-east-1
          DIST_DIR: ${{ github.sha }}

  macos-windows-build:
    runs-on: ${{ matrix.platform.os }}
    strategy:
      matrix:
        python-version: [ 3.6, 3.7, 3.8, 3.9 ]
        platform: [
          { os: "macos-10.15",    python-architecture: "x64", rust-target: "x86_64-apple-darwin" },
          { os: "windows-latest", python-architecture: "x64", rust-target: "x86_64-pc-windows-msvc" },
          { os: "windows-latest", python-architecture: "x86", rust-target: "i686-pc-windows-msvc" },
        ]
    steps:
      - uses: actions/checkout@v2

      - name: install python
        uses: actions/setup-python@v2
        with:
          python-version: ${{ matrix.python-version }}
          architecture: ${{ matrix.platform.python-architecture }}

      - name: install rust
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          target: ${{ matrix.platform.rust-target }}
          profile: minimal
          default: true

      - name: build wheel
        run: |
          pip install -U setuptools wheel setuptools-rust
          python setup.py bdist_wheel

      - name: upload to aws
        shell: bash
        env:
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          AWS_DEFAULT_REGION: us-east-1
          DIST_DIR: ${{ github.sha }}
        run: |
          pip install -U awscli
          python -m awscli s3 sync --exact-timestamps ./dist "s3://pierogis/dist/$DIST_DIR"

  publish:
    runs-on: ubuntu-latest
    needs: [ manylinux-build, macos-windows-build, macos-arm-build ]
    steps:
      - uses: actions/checkout@v2

      - name: install python
        uses: actions/setup-python@v2

      - name: retrieve all wheels
        shell: bash
        env:
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          AWS_DEFAULT_REGION: us-east-1
          DIST_DIR: ${{ github.sha }}
        run: |
          pip install awscli
          aws s3 sync "s3://pierogis/dist/$DIST_DIR" ./dist
      - name: Install dependencies
        run: |
          pip install setuptools wheel setuptools-rust
      - name: create source distribution
        run: |
          python setup.py sdist

      - name: publish to pypi
        if: "startsWith(github.ref, 'refs/tags')"
        env:
          PYPI_TOKEN: ${{ secrets.PYPI_TOKEN }}
        run: |
          pip install twine
          twine upload dist/* -u __token__ -p "$PYPI_TOKEN"