name: starsky
options:
  bundleIdPrefix: nl.qdraw
  deploymentTarget:
    macOS: "13.0"
  xcodeVersion: "15.0"
  generateEmptyDirectories: true

packages:
  Sparkle:
    url: https://github.com/sparkle-project/Sparkle
    from: 2.9.6

fileGroups:
  - ../history.md

settings:
  base:
    SWIFT_VERSION: "5.10"
    MACOSX_DEPLOYMENT_TARGET: "13.0"
    SWIFT_STRICT_CONCURRENCY: minimal
  configs:
    Release:
      ARCHS: "arm64 x86_64"
      ONLY_ACTIVE_ARCH: NO
      ENABLE_HARDENED_RUNTIME: YES
      OTHER_CODE_SIGN_FLAGS: "--options runtime"
    Debug:
      ONLY_ACTIVE_ARCH: YES
      ENABLE_HARDENED_RUNTIME: NO

targets:
  starsky:
    type: application
    platform: macOS
    sources:
      - path: starsky
    dependencies:
      - package: Sparkle
        product: Sparkle
    settings:
      base:
        PRODUCT_BUNDLE_IDENTIFIER: nl.qdraw.starsky
        PRODUCT_NAME: Starsky
        PRODUCT_MODULE_NAME: Starsky
        INFOPLIST_FILE: starsky/Info.plist
        CODE_SIGN_ENTITLEMENTS: starsky/starsky.entitlements
        CODE_SIGN_IDENTITY: "Developer ID Application"
        DEVELOPMENT_TEAM: "LHZ4426ABB"
        ASSETCATALOG_COMPILER_APPICON_NAME: AppIcon
        LD_RUNPATH_SEARCH_PATHS: "$(inherited) @executable_path/../Frameworks"
        SPARKLE_PUBLIC_ED_KEY: ""
      configs:
        Debug:
          CODE_SIGN_IDENTITY: "-"
          DEVELOPMENT_TEAM: ""
          CODE_SIGN_ENTITLEMENTS: starsky/starsky.debug.entitlements
    preBuildScripts:
      - script: |
          for ARCH in arm64 x64; do
            RUNTIME_SRC="$SRCROOT/../starsky/osx-${ARCH}"
            RUNTIME_DST="$BUILT_PRODUCTS_DIR/$PRODUCT_NAME.app/Contents/MacOS/runtime-starsky-osx-${ARCH}"
            if [ -d "$RUNTIME_SRC" ]; then
              rm -rf "$RUNTIME_DST"
              mkdir -p "$RUNTIME_DST"
              cp -R "$RUNTIME_SRC/." "$RUNTIME_DST/"
              find "$RUNTIME_DST" -name ".DS_Store" -delete
              find "$RUNTIME_DST" -type f | while read -r f; do
                codesign_output=$(codesign --force --options runtime --entitlements "$SRCROOT/starsky/runtime.entitlements" --sign "$EXPANDED_CODE_SIGN_IDENTITY" "$f" 2>&1)
                if [ $? -ne 0 ]; then
                  echo "warning: codesign failed for $(basename "$f"): $codesign_output"
                fi
              done
            else
              echo "warning: Starsky runtime not found at $RUNTIME_SRC — Local mode will not work"
            fi
          done
        name: Copy Starsky Runtime
        basedOnDependencyAnalysis: true
        inputFiles:
          - $(SRCROOT)/../starsky/osx-arm64
          - $(SRCROOT)/../starsky/osx-x64
        outputFiles:
          - $(BUILT_PRODUCTS_DIR)/$(PRODUCT_NAME).app/Contents/MacOS/runtime-starsky-osx-arm64
          - $(BUILT_PRODUCTS_DIR)/$(PRODUCT_NAME).app/Contents/MacOS/runtime-starsky-osx-x64

  starskyTests:
    type: bundle.unit-test
    platform: macOS
    sources:
      - path: starskyTests
      - path: starsky
        excludes:
          - App/main.swift
    dependencies:
      - package: Sparkle
        product: Sparkle
    settings:
      base:
        PRODUCT_BUNDLE_IDENTIFIER: nl.qdraw.starskyTests
        INFOPLIST_FILE: starskyTests/Info.plist
        LD_RUNPATH_SEARCH_PATHS: "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks"

schemes:
  starsky:
    build:
      targets:
        starskyTests: [test]
        starsky: all
    run:
      config: Debug
    test:
      config: Debug
      targets:
        - starskyTests
      coverageTargets:
        - starsky
    archive:
      config: Release
