name: node-hub

on:
  workflow_dispatch:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main
  release:
    types: [published]

jobs:
  find-jobs:
    runs-on: ubuntu-24.04
    name: Find Jobs
    outputs:
      folders: ${{ steps.jobs.outputs.folders }}
    steps:
      - uses: actions/checkout@v1

      - id: jobs
        uses: kmanimaran/list-folder-action@v4
        with:
          path: ./node-hub

  ci:
    runs-on: ${{ matrix.platform }}
    needs: [find-jobs]
    defaults:
      run:
        working-directory: node-hub/${{ matrix.folder }}
    strategy:
      fail-fast: ${{ github.event_name != 'workflow_dispatch' && !(github.event_name == 'release' && startsWith(github.ref, 'refs/tags/')) }}
      matrix:
        platform: [ubuntu-24.04, macos-14]
        folder: ${{ fromJson(needs.find-jobs.outputs.folders )}}
    steps:
      - name: Checkout repository
        if: runner.os == 'Linux' || github.event_name == 'workflow_dispatch' || (github.event_name == 'release' && startsWith(github.ref, 'refs/tags/'))
        uses: actions/checkout@v4
        with:
          submodules: true # Make sure to check out the sub-module

      - name: Update submodule
        if: runner.os == 'Linux'
        run: |
          git submodule update --init --recursive
          git submodule update --remote --recursive

      - name: Install system-level dependencies
        if: runner.os == 'Linux'
        run: |
          sudo apt update
          sudo apt-get install portaudio19-dev
          sudo apt-get install libdav1d-dev nasm libudev-dev
          mkdir -p $HOME/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib
          ln -s /lib/x86_64-linux-gnu/libdav1d.so $HOME/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libdav1d.so

          # Install mingw-w64 cross-compilers
          sudo apt install g++-mingw-w64-x86-64 gcc-mingw-w64-x86-64

      - name: Install system-level dependencies for MacOS
        if: runner.os == 'MacOS' && (github.event_name == 'workflow_dispatch' || (github.event_name == 'release' && startsWith(github.ref, 'refs/tags/')))
        run: |
          brew install portaudio
          brew install dav1d nasm

      - name: Set up Python
        if: runner.os == 'Linux' || github.event_name == 'workflow_dispatch' || (github.event_name == 'release' && startsWith(github.ref, 'refs/tags/'))
        uses: actions/setup-python@v2
        with:
          python-version: "3.10"

      - name: Install the latest version of uv
        uses: astral-sh/setup-uv@v5

      - name: Set up Rust
        if: runner.os == 'Linux' || github.event_name == 'workflow_dispatch' || (github.event_name == 'release' && startsWith(github.ref, 'refs/tags/'))
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          override: true

      - name: Run Linting and Tests
        ## Run Linting and testing only on Mac for release workflows.
        if: runner.os == 'Linux' || github.event_name == 'workflow_dispatch' || (github.event_name == 'release' && startsWith(github.ref, 'refs/tags/'))
        env:
          GITHUB_EVENT_NAME: ${{ github.event_name }}
          MATURIN_PYPI_TOKEN: ${{ secrets.PYPI_PASS }}
          UV_PUBLISH_TOKEN: ${{ secrets.PYPI_PASS }}
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
        run: |
          chmod +x ../../.github/workflows/node_hub_test.sh
          ../../.github/workflows/node_hub_test.sh
