name: macOS

# Builds and tests the package on macOS, and checks the universal release tool there.

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]
  workflow_dispatch: {}

concurrency:
  group: ci-${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  macos:
    name: Build & test (macOS)
    # The package declares swift-tools-version 6.2, which requires Xcode 26+.
    runs-on: macos-26
    steps:
      - name: Checkout
        uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5

      - name: Setup pnpm
        uses: pnpm/action-setup@ea17c68df8912ef543352723c149a84f56e3d413 # v6.1.0

      - name: Select Xcode
        # Pin to an Xcode that ships Swift 6.2 (required by Package.swift).
        run: sudo xcode-select -switch /Applications/Xcode_26.4.1.app

      - name: Cache SwiftPM build
        uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
        with:
          path: .build
          key: spm-${{ github.repository }}-${{ runner.os }}-${{ hashFiles('Package.resolved') }}
          restore-keys: |
            spm-${{ github.repository }}-${{ runner.os }}-

      - name: Resolve dependencies
        run: swift package resolve

      - name: Test
        # Also builds the debug tool, which the next step uses.
        run: swift test -v

      - name: Compile the macro expansions
        # The expansion tests compare text. This compiles and runs the expansions of the fixtures in
        # `scripts/check-compile`, also with library evolution and with `-default-isolation MainActor`.
        # It runs in the background during the release build, with the debug tool, which the release
        # build doesn't change. `Wait for the compile check` fails the job when it fails.
        id: check-compile
        background: true
        run: node scripts/check-compile.js .build/debug/ExpoModulesMacrosTool

      - name: Build release tool
        # Runs `pnpm run build` (scripts/build.js): builds the universal
        # (arm64 + x86_64) release plugin binary, copies it to
        # apple/ExpoModulesMacros, and strips it.
        run: pnpm run build

      - name: Verify universal binary
        run: |
          archs="$(lipo -archs apple/ExpoModulesMacros)"
          echo "Architectures: $archs"
          case "$archs" in *arm64*) ;; *) echo "Missing arm64 slice"; exit 1;; esac
          case "$archs" in *x86_64*) ;; *) echo "Missing x86_64 slice"; exit 1;; esac
          arch -arm64 apple/ExpoModulesMacros < /dev/null
          arch -x86_64 apple/ExpoModulesMacros < /dev/null
          # With arguments the same binary runs the scanner CLI instead of the plugin server.
          arch -arm64 apple/ExpoModulesMacros --help > /dev/null
          arch -x86_64 apple/ExpoModulesMacros --help > /dev/null

      - name: Typecheck the TypeScript wrapper
        run: |
          pnpm install
          pnpm run typecheck

      - name: Scan through the TypeScript wrapper
        # The wrapper must find the release tool in `apple/`, and `scanExports` must report the
        # surface in `scripts/check-scan-exports.json`, which the Windows and Linux workflows also check.
        run: |
          pnpm run build:ts
          node scripts/check-scan.js .

      - name: Wait for the compile check
        wait: check-compile
