# SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
# SPDX-License-Identifier: GPL-2.0-or-later

name: macOS Build and Test

on:
  workflow_call:
    inputs:
      CRAFT_TARGET:
        type: string
        required: true
      CRAFT_MASTER_LOCATION:
        type: string
        required: true
      CRAFT_MASTER_CONFIG:
        type: string
        required: true
      PYTHON_VERSION:
        type: string
        required: true
      buildFileProviderModule:
        type: string
        required: true

jobs:
  build:
    name: Build nextcloud-client
    timeout-minutes: 60
    runs-on: macos-15
    steps:
      - uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
        with:
          fetch-depth: 1

      - name: List Xcode installations
        run: sudo ls -1 /Applications | grep "Xcode"

      - name: Select Xcode 16.2
        run: sudo xcode-select -s /Applications/Xcode_16.2.app/Contents/Developer

      - name: Set up Python ${{ inputs.PYTHON_VERSION }}
        uses: actions/setup-python@e797f83bcb11b83ae66e0230d6156d7c80228e7c # v6.0.0
        with:
          python-version: ${{ inputs.PYTHON_VERSION }}

      - name: Install Homebrew dependencies
        run: |
          brew install homebrew/cask/inkscape

      - name: Restore cached Craft directories
        uses: actions/cache/restore@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0
        with:
          path: |
            ${{ inputs.CRAFT_MASTER_LOCATION }}
            ${{ github.workspace }}/craft-clone
            ${{ github.workspace }}/${{ inputs.CRAFT_TARGET }}
          key: ${{ runner.os }}-craft-${{ hashFiles(inputs.CRAFT_MASTER_CONFIG) }}

      - name: Build nextcloud-client
        run: |
          python "${{ inputs.CRAFT_MASTER_LOCATION }}/CraftMaster.py" --config "${{ inputs.CRAFT_MASTER_CONFIG }}" --target ${{ inputs.CRAFT_TARGET }} -c --options nextcloud-client.srcDir=${{ github.workspace }} --options nextcloud-client.buildFileProviderModule=${{ inputs.buildFileProviderModule }} nextcloud-client

      - name: Cache Craft directories containing the built client
        uses: actions/cache/save@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0
        with:
          path: |
            ${{ inputs.CRAFT_MASTER_LOCATION }}
            ${{ github.workspace }}/craft-clone
            ${{ github.workspace }}/${{ inputs.CRAFT_TARGET }}
          key: ${{ runner.os }}-craft-build-${{ github.sha }}-${{ inputs.buildFileProviderModule }}


  test:
    name: Run tests
    needs: build
    timeout-minutes: 60
    runs-on: macos-15
    steps:
      - name: Restore cached Craft directories containing the built client
        uses: actions/cache/restore@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0
        with:
          path: |
            ${{ inputs.CRAFT_MASTER_LOCATION }}
            ${{ github.workspace }}/craft-clone
            ${{ github.workspace }}/${{ inputs.CRAFT_TARGET }}
          key: ${{ runner.os }}-craft-build-${{ github.sha }}-${{ inputs.buildFileProviderModule }}

      - name: Run tests
        run: |
          cd ${{ github.workspace }}/${{ inputs.CRAFT_TARGET }}/build/nextcloud-client/work/build
          ctest --output-on-failure --output-junit testResult.xml
