name: Build macOS

on:
  workflow_call:
    inputs:
      run-tests:
        type: boolean
        default: false
      run-clippy:
        type: boolean
        default: false
      build-packages:
        type: boolean
        default: false
      matrix:
        type: string
        default: '[{"os":"macos-14","target":"aarch64-apple-darwin","artifact":"cagire-macos-aarch64"}]'
  workflow_dispatch:
    inputs:
      run-tests:
        type: boolean
        default: true
      run-clippy:
        type: boolean
        default: true
      build-packages:
        type: boolean
        default: false
      matrix:
        type: string
        default: '[{"os":"macos-14","target":"aarch64-apple-darwin","artifact":"cagire-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 }}
          components: clippy

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

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

      - name: Build
        run: cargo build --release --target ${{ matrix.target }}

      - name: Build desktop
        run: cargo build --release --features desktop --bin cagire-desktop --target ${{ matrix.target }}

      - name: Test
        if: inputs.run-tests
        run: cargo test --target ${{ matrix.target }}

      - name: Clippy
        if: inputs.run-clippy
        run: cargo clippy --target ${{ matrix.target }} -- -D warnings

      - name: Bundle desktop app
        if: inputs.build-packages
        run: scripts/make-app-bundle.sh ${{ matrix.target }}

      - name: Bundle CLAP plugin
        if: inputs.build-packages
        run: cargo xtask bundle cagire-plugins --release --target ${{ matrix.target }}

      - name: Zip macOS app bundle
        if: inputs.build-packages
        run: |
          cd target/${{ matrix.target }}/release/bundle/osx
          zip -r Cagire.app.zip Cagire.app

      - name: Upload CLI artifact
        if: inputs.build-packages
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.artifact }}
          path: target/${{ matrix.target }}/release/cagire

      - name: Upload desktop artifact
        if: inputs.build-packages
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.artifact }}-desktop
          path: target/${{ matrix.target }}/release/bundle/osx/Cagire.app.zip

      - name: Prepare plugin artifacts
        if: inputs.build-packages
        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
        if: inputs.build-packages
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.artifact }}-clap
          path: staging/clap/

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