name: Windows

# Builds and tests the package on Windows, and checks the release tool and its package there.

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

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

jobs:
  windows:
    name: Build & test (Windows ${{ matrix.arch }})
    runs-on: ${{ matrix.runner }}
    # A run takes about 4 to 5 minutes. The limit stops a hang, for example in the plugin server,
    # from holding a runner for the default 6 hours.
    timeout-minutes: 30
    strategy:
      fail-fast: false
      matrix:
        include:
          - arch: x64
            runner: windows-latest
            build_arch: amd64
          - arch: arm64
            runner: windows-11-arm
            build_arch: arm64
    steps:
      - name: Checkout
        uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5

      - name: Install Swift
        uses: compnerd/gha-setup-swift@bbae8ce86bac5a3449f3992b3d88bc4ca25f2057 # v0.5.0
        with:
          swift-version: swift-6.4.0-release
          swift-build: 6.4.0-RELEASE
          build_arch: ${{ matrix.build_arch }}

      - name: Setup Node
        uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
        with:
          node-version: 24

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

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

      - name: Build
        run: swift build

      - name: Test
        run: swift test

      - name: Build release tool
        # Runs `pnpm run build` (scripts/build.js): builds the release plugin binary for this
        # architecture as platforms/win32-<arch>/ExpoModulesMacros.exe.
        run: pnpm run build

      - name: Verify the release tool
        # Runs it with only the Windows directories on PATH, so it must not need the Swift runtime DLLs.
        # With no arguments it is the plugin server, which exits when stdin closes; with arguments it
        # is the scanner CLI.
        shell: pwsh
        run: |
          $exe = (Resolve-Path "platforms/win32-${{ matrix.arch }}/ExpoModulesMacros.exe").Path
          $env:PATH = "$env:SystemRoot\System32;$env:SystemRoot"
          cmd /c "`"$exe`" < NUL"
          if ($LASTEXITCODE -ne 0) { throw "The plugin server exited with $LASTEXITCODE" }
          & $exe --help | Out-Null
          if ($LASTEXITCODE -ne 0) { throw "The scanner CLI exited with $LASTEXITCODE" }

      - name: Pack the platform package
        # Checks what `npm pack` would publish from `platforms/win32-<arch>`, where the release build
        # put the tool: the executable, the README, the license and the manifest, with the `os` and
        # `cpu` fields for this machine.
        shell: pwsh
        run: |
          # The `./` matters: npm reads `platforms/win32-<arch>` as a GitHub repository.
          $package = "./platforms/win32-${{ matrix.arch }}"
          $pack = npm pack $package --pack-destination $env:RUNNER_TEMP --json | ConvertFrom-Json
          if ($LASTEXITCODE -ne 0) { throw "npm pack failed" }
          $files = ($pack[0].files | ForEach-Object { $_.path } | Sort-Object) -join ","
          Write-Host "$($pack[0].filename): $files ($($pack[0].size) bytes packed)"
          if ($files -ne "ExpoModulesMacros.exe,LICENSE,package.json,README.md") { throw "Unexpected files: $files" }
          $manifest = Get-Content "$package/package.json" -Raw | ConvertFrom-Json
          if (($manifest.os -join ",") -ne "win32" -or ($manifest.cpu -join ",") -ne "${{ matrix.arch }}") {
            throw "Unexpected os/cpu: $($manifest.os) / $($manifest.cpu)"
          }
          "PLATFORM_PACKAGE_TARBALL=$(Join-Path $env:RUNNER_TEMP $pack[0].filename)" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append

      - name: Scan through the TypeScript wrapper
        # Installs the packed platform package. The wrapper must find the executable in
        # `@expo/modules-macros-win32-<arch>`, as it would in a project, not the local build.
        shell: pwsh
        run: |
          pnpm install
          if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
          pnpm add --save-optional $env:PLATFORM_PACKAGE_TARBALL
          if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
          pnpm run build:ts
          if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
          node scripts/check-scan.js .
          if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
