name: Test Emscripten/Pyodide build

on:
  pull_request:
    branches:
      - main
      - maintenance/**
  # Note: this workflow gets triggered on the same schedule as the
  # wheels.yml workflow to upload WASM wheels to Anaconda.org.
  schedule:
    #        ┌───────────── minute (0 - 59)
    #        │  ┌───────────── hour (0 - 23)
    #        │  │ ┌───────────── day of the month (1 - 31)
    #        │  │ │ ┌───────────── month (1 - 12 or JAN-DEC)
    #        │  │ │ │ ┌───────────── day of the week (0 - 6 or SUN-SAT)
    #        │  │ │ │ │
    - cron: "42 2 * * SUN,WED"
  workflow_dispatch:
    inputs:
      push_wheels:
        # Can be 'true' or 'false'. Default is 'false'.
        # Warning: this will overwrite existing wheels.
        description: >
          Push wheels to Anaconda.org if the build succeeds
        required: false
        default: 'false'

env:
  FORCE_COLOR: 3

concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
  cancel-in-progress: true


jobs:
  build-wasm-emscripten:
    permissions:
      contents: read  # to fetch code (actions/checkout)
    name: Build NumPy distribution for Pyodide
    runs-on: ubuntu-22.04
    # To enable this workflow on a fork, comment out:
    if: github.repository == 'numpy/numpy'
    steps:
      - name: Checkout NumPy
        uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11  # v4.1.1
        with:
          submodules: recursive
          fetch-tags: true

      - uses: pypa/cibuildwheel@7940a4c0e76eb2030e473a5f864f291f63ee879b  # v2.21.3
        env:
          CIBW_PLATFORM: pyodide

      - name: Upload wheel artifact(s)
        uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882  # v4.4.3
        with:
          name: cp312-pyodide_wasm32
          path: ./wheelhouse/*.whl
          if-no-files-found: error

  # Push to https://anaconda.org/scientific-python-nightly-wheels/numpy
  # WARNING: this job will overwrite any existing WASM wheels.
  upload-wheels:
    name: Upload NumPy WASM wheels to Anaconda.org
    runs-on: ubuntu-22.04
    permissions: {}
    needs: [build-wasm-emscripten]
    if: >-
      (github.repository == 'numpy/numpy') &&
      (github.event_name == 'workflow_dispatch' && github.event.inputs.push_wheels == 'true') ||
      (github.event_name == 'schedule')
    steps:
      - name: Download wheel artifact(s)
        uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16  # v4.1.8
        with:
          path: wheelhouse/
          merge-multiple: true

      - name: Push to Anaconda PyPI index
        uses: scientific-python/upload-nightly-action@82396a2ed4269ba06c6b2988bb4fd568ef3c3d6b  # v0.6.1
        with:
          artifacts_path: wheelhouse/
          anaconda_nightly_upload_token: ${{ secrets.NUMPY_NIGHTLY_UPLOAD_TOKEN }}
