name: Produce Arbor release artifacts

on:
  push:
    branches: 
      - ciwheel
    tags:
      - v*
  schedule:
    - cron: '0 3 * * 0'

jobs:
  get_timestamp:
    name: Prep pyproject.toml
    runs-on: ubuntu-latest
    steps:
      - name: Get Arbor
        uses: actions/checkout@v4
        with:
          submodules: recursive
      - name: Create unique version in pyproject.toml
        if: startsWith(github.ref, 'refs/tags/v') == false
        run: python3 -c 'import time;f=open("pyproject.toml","r+");c = f.readlines();d=[i.split("#")[0].strip()[:-1]+time.strftime("%Y%m%d%H%I%S")+"\"\n" if i.startswith("version") else i for i in c];f.seek(0);f.writelines(d);f.truncate()'
      - uses: actions/upload-artifact@v4
        with:
            name: arbver
            path: ${{ github.workspace }}/pyproject.toml

  build_binary_wheels:
    name: Build wheels on ${{ matrix.os }}
    needs: get_timestamp
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest]

    steps:
      - name: Get Arbor
        uses: actions/checkout@v4
        with:
          submodules: recursive
      - name: Delete pyproject.toml
        run: rm -f pyproject.toml
      - name: Get pyproject.toml
        uses: actions/download-artifact@v4
        with:
          name: arbver
      - name: Check pyproject.toml
        run: cat pyproject.toml
      - name: Move fast and...
        run: python3 -m pip config set global.break-system-packages true
      - name: Install cibuildwheel
        run: python3 -m pip install cibuildwheel
      - name: Build wheels
        run: python3 -m cibuildwheel --output-dir dist
      - uses: actions/upload-artifact@v4
        with:
          name: dist-${{ matrix.os }}
          path: dist/*.whl

  build_sdist:
    name: Build sdist
    needs: get_timestamp
    runs-on: ubuntu-latest

    steps:
      - name: Update pip and setup venv
        run: python -m pip install --upgrade pip && python -m venv ~/env && . ~/env/bin/activate && echo PATH=$PATH >> $GITHUB_ENV
      - name: Get packages
        run: python3 -m pip install build
      - name: Get Arbor
        uses: actions/checkout@v4
        with:
          submodules: recursive
      - name: Delete pyproject.toml
        run: rm -f pyproject.toml
      - name: Get pyproject.toml
        uses: actions/download-artifact@v4
        with:
          name: arbver
      - name: Check pyproject.toml
        run: cat pyproject.toml
      - name: Make sdist
        run: python3 -m build -s
      - name: Install sdist
        run:  python3 -m pip install dist/arbor*.tar.gz
      - name: Run Python tests
        run: python3 -m unittest discover -v -s python
      - name: Run Python examples
        run: scripts/run_python_examples.sh
      - name: Test executables
        run: scripts/test_executables.sh
      - uses: actions/upload-artifact@v4
        with:
          name: dist
          path: dist/*.tar.gz

  upload_test_pypi:
    name: upload to test pypi
    runs-on: ubuntu-latest
    needs: [build_binary_wheels, build_sdist]
    steps:
      - uses: actions/download-artifact@v4
        with:
          name: dist
      - name: Publish distribution 📦 to Test PyPI
        run: |
          pip install -U packaging
          pip install twine
          twine upload -r testpypi ./*
        env:
          TWINE_USERNAME: __token__
          TWINE_PASSWORD: ${{ secrets.TESTPYPI_SECRET }}

  make_release:
    name: draft new GitHub release
    if: startsWith(github.ref, 'refs/tags/v')
    runs-on: ubuntu-latest
    needs: [build_binary_wheels, build_sdist]
    steps:
      - name: "Clone w/ submodules"
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
          submodules: recursive
          path: arbor
      - name: Make full tarball
        run: |
          the_ref=${{ github.ref }}
          the_tag="${the_ref/refs\/tags\//}"
          $GITHUB_WORKSPACE/arbor/scripts/create_tarball $GITHUB_WORKSPACE/arbor $the_tag $GITHUB_WORKSPACE/arbor-$the_tag-full.tar.gz
      - name: "Make Release"
        uses: ncipollo/release-action@v1
        with:
          omitBody: false
          draft: true
          prerelease: false
          generateReleaseNotes: true
          artifacts: '*.whl,*full.tar.gz'
          token: ${{ secrets.GITHUB_TOKEN }}
