name: Build Plugins macOS

on:
  workflow_call:
    inputs:
      matrix:
        type: string
        default: '[{"os":"macos-14","target":"aarch64-apple-darwin","artifact":"plugins-macos-aarch64"},{"os":"macos-15-intel","target":"x86_64-apple-darwin","artifact":"plugins-macos-x86_64"}]'
  workflow_dispatch:
    inputs:
      matrix:
        type: string
        default: '[{"os":"macos-14","target":"aarch64-apple-darwin","artifact":"plugins-macos-aarch64"}]'

env:
  CARGO_TERM_COLOR: always
  MACOSX_DEPLOYMENT_TARGET: "12.0"

jobs:
  build:
    strategy:
      fail-fast: false
      matrix:
        include: ${{ fromJSON(inputs.matrix) }}

    runs-on: ${{ matrix.os }}
    timeout-minutes: 60

    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.target }}

      - name: Cache Rust dependencies
        uses: Swatinem/rust-cache@v2
        with:
          key: ${{ matrix.target }}-plugins

      - name: Install dependencies
        run: brew list cmake &>/dev/null || brew install cmake

      - name: Build plugins
        run: cargo xtask bundle cagire-plugins --release --target ${{ matrix.target }}

      - name: Prepare plugin artifacts
        run: |
          mkdir -p staging/clap staging/vst3
          cp -R target/bundled/cagire-plugins.clap staging/clap/
          cp -R target/bundled/cagire-plugins.vst3 staging/vst3/

      - name: Upload CLAP artifact
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.artifact }}-clap
          path: staging/clap/

      - name: Upload VST3 artifact
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.artifact }}-vst3
          path: staging/vst3/
