  bundle-windows-x86_64:
    name: bundle windows-x86_64
    runs-on: windows-latest
    # No `defaults: run: shell: bash` here, on purpose: zef -- both the one
    # installing ariza below and the one ariza's own site-install runs
    # inside `ariza bundle` -- resolves `tar` off whatever shell launched
    # it. Under bash that is Git Bash's MSYS tar, which treats a leading
    # `C:` in a path as a remote hostname and aborts extracting into any
    # mixed-separator temp path zef hands it. The runner's default pwsh
    # instead finds Windows' own bsdtar, which has no such reading of `C:`.
    # zef's child processes inherit whatever shell ran the step, so pwsh
    # has to be load-bearing for every step that might invoke it, not just
    # the ones below that already needed PowerShell for path handling.
    steps:
      - uses: actions/checkout@v6
        with:
          ref: {% raw %}${{ inputs.ref || github.ref }}{% endraw %}

      # This Raku runs ariza. It is NOT the runtime that ends up in the
      # bundle -- ariza downloads the pinned one ({{ rakudo_tag }}) from
      # rakudo.org itself.
      - uses: Raku/setup-raku@v1
        with:
          raku-version: 'latest'

      - name: Install ariza
        run: {{ ariza_install }}
{%- for line in ariza_install_note %}
        # {{ line }}
{%- endfor %}

      # Windows has no package manager ariza drives, so it is told where
      # the library is: SQLCIPHER_LIB_DIR names the directory holding the
      # SQLCipher DLL and beats every other source. (ariza probes MSYS2's
      # prefixes and a VCPKG_ROOT tree by itself, but naming the directory
      # outright is one less thing to be wrong about.)
      #
      # MSYS2's UCRT package rather than vcpkg's, and the difference is
      # not a preference:
      #
      #   * vcpkg builds with MSVC, so its sqlcipher.dll imports
      #     vcruntime140.dll -- the Visual C++ Redistributable, which is
      #     NOT part of Windows. Every CI runner has it installed, so a
      #     bundle built that way passes every check here and fails to
      #     load on a clean machine, which is the machine a bundle is for.
      #     (ariza's PE audit now refuses that bundle outright.) MSYS2's
      #     mingw-w64-ucrt-x86_64-* packages import ucrtbase.dll instead,
      #     which Windows has shipped in System32 since Windows 10.
      #   * The MSYS2 package is prebuilt. The vcpkg port was a fifteen-
      #     minute source build, which is why this lane used to carry a
      #     restore/save cache pair around it; both are gone.
      #   * Its whole runtime closure -- OpenSSL, the mingw runtime --
      #     lands in ucrt64\bin beside the DLL, which is the search space
      #     contract App::Ariza::Native relies on to copy dependencies in.
      #
      # MSYS2 is preinstalled at C:\msys64 on windows runners, so this is
      # a package install and nothing else. `-Sy` refreshes the package
      # databases first: the image's copy is as old as the image, and
      # MSYS2's mirrors keep only current versions, so a stale database
      # sends pacman after a file that is no longer there.
      - name: Install SQLCipher
        shell: pwsh
        run: |
          $ErrorActionPreference = 'Stop'
          & C:\msys64\usr\bin\pacman.exe -Sy --noconfirm --needed mingw-w64-ucrt-x86_64-sqlcipher
          if ($LASTEXITCODE -ne 0) { throw "pacman failed (exit $LASTEXITCODE)" }

      # The name is a wildcard because MSYS2's is libsqlcipher-0.dll,
      # where vcpkg's was sqlcipher.dll: ariza accepts either and stages
      # whichever it finds under the canonical name. Asserted rather than
      # assumed -- a pacman that exits 0 having installed nothing would
      # otherwise surface as a confusing failure inside `ariza bundle`.
      - name: Locate SQLCipher
        shell: pwsh
        run: |
          $ErrorActionPreference = 'Stop'
          $lib = 'C:\msys64\ucrt64\bin'
          if (-not (Test-Path (Join-Path $lib 'libsqlcipher*.dll'))) {
              throw "pacman installed no libsqlcipher*.dll in $lib"
          }
          "SQLCIPHER_LIB_DIR=$lib" | Out-File -FilePath $env:GITHUB_ENV -Append -Encoding utf8

      - name: Build the bundle
        run: |
          $ErrorActionPreference = 'Stop'
          ariza bundle --app=. --platform=windows-x86_64 --out-dir=dist-out
          if ($LASTEXITCODE -ne 0) { throw "ariza bundle failed (exit $LASTEXITCODE)" }

      # Unpacks the archive somewhere new, with a replaced environment,
      # and runs {{ app_name }}'s own bundle.smoke commands against it.
      - name: Smoke the bundle
        run: |
          $ErrorActionPreference = 'Stop'
          $archive = (Get-ChildItem dist-out\*.tar.gz | Select-Object -First 1).FullName
          ariza smoke --archive="$archive"
          if ($LASTEXITCODE -ne 0) { throw "ariza smoke failed (exit $LASTEXITCODE)" }

      - name: Upload
        uses: actions/upload-artifact@v6
        with:
          name: windows-x86_64
          path: |
            dist-out/*.tar.gz
            dist-out/*.tar.gz.sha256
          if-no-files-found: error
