name: Python Build

"on":
  push:
    branches: ["main"]
    tags:
      - "v[0-9]+.[0-9]+.[0-9]+"
      - "v[0-9]+.[0-9]+.[0-9]+rc[0-9]+"
  pull_request:

jobs:
  development-mode:
    name: pip development mode on ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [macos-latest]
        python-version: [3.8]

    steps:
      - uses: actions/checkout@v2
      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v2
        with:
          python-version: ${{ matrix.python-version }}

      - name: Install build packages and pytest
        run: |
          python -m pip install --upgrade pip wheel
          python -m pip install scikit-build

      - name: Install python so3
        run: pip install -e .[dev]

      - name: run pytest
        run: pytest tests/test_so3.py

  from-sdist:
    name: python source distribution
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Set up Python 3.8
        uses: actions/setup-python@v2
        with:
          python-version: 3.8

      - name: Install build packages and pytest
        run: |
          python -m pip install --upgrade pip wheel setuptools
          python -m pip install conan scikit-build pytest cython numpy

      - name: Create sdist
        run: python setup.py sdist

      - name: Install python so3
        run: "pip install dist/so3-*.tar.gz"

      - name: run pytest
        run: pytest tests/test_so3.py

      - uses: actions/upload-artifact@v2
        if: ${{ startsWith(github.ref, 'refs/tags') }}
        with:
          path: ./dist/*.tar.gz
          name: source-distribution


  build_wheels:
    name: Build wheels on ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [macos-latest]
        python-version: [3.8]

    steps:
      - uses: actions/checkout@v2

      - uses: actions/setup-python@v2
        name: Install Python
        with:
          python-version: ${{ matrix.python-version }}

      - name: Setup environment
        run: |
          python -m pip install --upgrade pip wheel
          python -m pip install conan pytest
          conan profile new default --detect

      - name: Build wheels
        run: pip wheel . --use-pep517 --no-deps -w dist

      - name: install wheel
        run: "pip install dist/*.whl"

      - name: run pytests
        run: pytest tests

      - uses: actions/upload-artifact@v2
        if: ${{ startsWith(github.ref, 'refs/tags') }}
        with:
          path: ./dist/*.whl
          name: wheel-${{matrix.os}}-${{matrix.python-version}}

  publication:
    name: publish to pypi
    if: ${{ startsWith(github.ref, 'refs/tags') }}
    runs-on: ubuntu-latest
    needs: [build_wheels, from-sdist]
    steps:
      - name: Download wheels and sdist
        uses: actions/download-artifact@v2

      - name: Move wheels and source distribution to dist/
        run: |
            mkdir -p dist
            mv source-distribution/*.tar.gz  wheel-*/*.whl dist

      - name: Publish distribution 📦 to Test PyPI
        if: ${{ github.ref != 'refs/tags/v1.3.0' }}
        uses: pypa/gh-action-pypi-publish@master
        with:
          password: ${{ secrets.TEST_PYPI_TOKEN }}
          repository_url: https://test.pypi.org/legacy/

      - name: Publish distribution 📦 to PyPI
        if: ${{ github.ref == 'refs/tags/v1.3.0' }}
        uses: pypa/gh-action-pypi-publish@master
        with:
          password: ${{ secrets.PYPI_TOKEN }}
