on:
  workflow_call:

permissions: {}

env:
  CARGO_INCREMENTAL: 0
  CARGO_NET_RETRY: 10
  CARGO_TERM_COLOR: always
  RUSTUP_MAX_RETRIES: 10

jobs:
  # Verify windows crate version matches between workspaces
  windows-version-check:
    timeout-minutes: 5
    runs-on: ubuntu-latest
    name: "check windows crate version"
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          persist-credentials: false

      - uses: astral-sh/setup-uv@08807647e7069bb48b6ef5acd8ec9567f424441b # v8.1.0

      - name: "Check windows crate versions match"
        run: uv run --no-project scripts/check-trampoline-version-consistency.py

  # Verify committed trampolines match a reproducible Docker build
  windows-trampoline-reproducible:
    # uv version bumps (e.g., https://github.com/astral-sh/uv/pull/18805) cause the reproducible
    # check to fail and these are not regenerated as a part of the release process so we temporarily
    # disable this job
    if: false
    timeout-minutes: 30
    runs-on: ubuntu-latest
    name: "check reproducible build"
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          persist-credentials: false

      - name: "Set up Docker Buildx"
        uses: docker/setup-buildx-action@4d04d5d9486b7bd6fa91e7baf45bbb4f8b9deedd # v4.0.0

      - name: "Build trampolines in Docker"
        run: scripts/build-trampolines.sh --cache-from type=gha --cache-to type=gha

      - name: "Check committed trampolines match build"
        run: git diff --exit-code crates/uv-trampoline-builder/trampolines/

  # Separate jobs for the nightly crate
  windows-trampoline-check:
    timeout-minutes: 15
    runs-on: windows-latest
    name: "check on ${{ matrix.target-arch }}"
    strategy:
      fail-fast: false
      matrix:
        target-arch: ["x86_64", "i686", "aarch64"]
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          persist-credentials: false

      - name: Setup Dev Drive
        run: ${{ github.workspace }}/.github/workflows/setup-dev-drive.ps1

      # actions/checkout does not let us clone into anywhere outside ${{ github.workspace }}, so we have to copy the clone...
      - name: Copy Git Repo to Dev Drive
        run: |
          Copy-Item -Path "${{ github.workspace }}" -Destination "$Env:UV_WORKSPACE" -Recurse

      - uses: Swatinem/rust-cache@c19371144df3bb44fab255c43d04cbc2ab54d1c4 # v2.9.1
        with:
          workspaces: ${{ env.UV_WORKSPACE }}/crates/uv-trampoline

      - name: "Install Rust toolchain"
        working-directory: ${{ env.UV_WORKSPACE }}/crates/uv-trampoline
        run: |
          rustup target add ${{ matrix.target-arch }}-pc-windows-msvc
          rustup component add rust-src --target ${{ matrix.target-arch }}-pc-windows-msvc

      - name: "Install cargo-bloat"
        uses: taiki-e/install-action@85b24a67ef0c632dfefad70b9d5ce8fddb040754 # v2.75.10
        with:
          tool: cargo-bloat

      - name: "rustfmt"
        working-directory: ${{ env.UV_WORKSPACE }}/crates/uv-trampoline
        run: cargo fmt --check

      - name: "Clippy"
        working-directory: ${{ env.UV_WORKSPACE }}/crates/uv-trampoline
        run: cargo clippy --all-features --locked --target x86_64-pc-windows-msvc --tests -- -D warnings

      - name: "Bloat Check"
        working-directory: ${{ env.UV_WORKSPACE }}/crates/uv-trampoline
        run: |
          $output = cargo bloat --release --target x86_64-pc-windows-msvc
          $filteredOutput = $output | Select-String -Pattern 'core::fmt::write|core::fmt::getcount' -NotMatch
          $containsPatterns = $filteredOutput | Select-String -Pattern 'core::fmt|std::panicking|std::backtrace_rs'

          if ($containsPatterns) {
              Exit 1
          } else {
              Exit 0
          }

  # Separate jobs for the nightly crate
  windows-trampoline-test:
    timeout-minutes: 10
    runs-on: ${{ matrix.runner }}
    name: "test on ${{ matrix.target-arch }}"
    strategy:
      fail-fast: false
      matrix:
        include:
          - { runner: windows-latest, target-arch: "x86_64" }
          - { runner: windows-latest, target-arch: "i686" }
          - { runner: windows-11-arm, target-arch: "aarch64" }
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          persist-credentials: false
      - name: Setup Dev Drive
        run: ${{ github.workspace }}/.github/workflows/setup-dev-drive.ps1
      # actions/checkout does not let us clone into anywhere outside ${{ github.workspace }}, so we have to copy the clone...
      - name: Copy Git Repo to Dev Drive
        run: |
          Copy-Item -Path "${{ github.workspace }}" -Destination "$Env:UV_WORKSPACE" -Recurse
      - uses: Swatinem/rust-cache@c19371144df3bb44fab255c43d04cbc2ab54d1c4 # v2.9.1
        with:
          workspaces: ${{ env.UV_WORKSPACE }}/crates/uv-trampoline
      - name: "Install Rust toolchain"
        working-directory: ${{ env.UV_WORKSPACE }}/crates/uv-trampoline
        run: |
          rustup target add ${{ matrix.target-arch }}-pc-windows-msvc
          rustup component add rust-src --target ${{ matrix.target-arch }}-pc-windows-msvc
      - name: "Test committed binaries"
        working-directory: ${{ env.UV_WORKSPACE }}
        run: |
          rustup target add ${{ matrix.target-arch }}-pc-windows-msvc
          cargo test -p uv-trampoline-builder --target ${{ matrix.target-arch }}-pc-windows-msvc
      # Build and copy the new binaries
      - name: "Build"
        working-directory: ${{ env.UV_WORKSPACE }}/crates/uv-trampoline
        run: |
          cargo build --target ${{ matrix.target-arch }}-pc-windows-msvc
          cp target/${{ matrix.target-arch }}-pc-windows-msvc/debug/uv-trampoline-console.exe ../uv-trampoline-builder/trampolines/uv-trampoline-${{ matrix.target-arch }}-console.exe
          cp target/${{ matrix.target-arch }}-pc-windows-msvc/debug/uv-trampoline-gui.exe ../uv-trampoline-builder/trampolines/uv-trampoline-${{ matrix.target-arch }}-gui.exe
      - name: "Test new binaries"
        working-directory: ${{ env.UV_WORKSPACE }}
        run: |
          # We turn off the default "production" test feature since these are debug binaries
          cargo test -p uv-trampoline-builder --target ${{ matrix.target-arch }}-pc-windows-msvc --no-default-features
