# 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-latest
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          fetch-depth: 1

      - name: Setup Xcode
        uses: maxim-lobanov/setup-xcode@60606e260d2fc5762a71e64e74b2174e8ea3c8bd # v1.6.0
        with:
          xcode-version: latest-stable
      
      - name: Show Swift version
        run: swift --version
      
      - name: Set up Python ${{ inputs.PYTHON_VERSION }}
        uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.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@cdf6c1fa76f9f475f3d7449005a359c84ca0f306 # v5.0.3
        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@cdf6c1fa76f9f475f3d7449005a359c84ca0f306 # v5.0.3
        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-latest
    steps:
      - name: Restore cached Craft directories containing the built client
        uses: actions/cache/restore@cdf6c1fa76f9f475f3d7449005a359c84ca0f306 # v5.0.3
        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
