[CmdletBinding()] param( [switch]$CheckOnly, [switch]$Offline ) $ErrorActionPreference = 'Stop' # Native failures are checked explicitly on both Windows PowerShell and PowerShell 7. $PSNativeCommandUseErrorActionPreference = $false $previousEnvironment = $env:UV_PROJECT_ENVIRONMENT $exitCode = 0 function Invoke-UvStep { param([string[]]$UvArguments) & $script:uvPath @UvArguments if ($LASTEXITCODE -ne 0) { throw "uv $($UvArguments[0]) failed (exit $LASTEXITCODE). Check the error above; no environment reset was attempted." } } Push-Location $PSScriptRoot try { $harnessPath = Join-Path $PSScriptRoot 'scripts/check-harness.ps1' if (Test-Path -LiteralPath $harnessPath -PathType Leaf) { & $harnessPath } elseif ($CheckOnly) { throw 'Local Harness files are not included in this checkout. Run init.ps1 without -CheckOnly to initialize the environment.' } else { Write-Output 'Local Harness not included; proceeding with environment initialization.' } if ($CheckOnly) { Write-Output 'Harness check only passed. Python and dependencies were NOT verified.' } else { $uvCommand = Get-Command uv -CommandType Application -ErrorAction SilentlyContinue if (-not $uvCommand) { throw 'uv is required. Install uv and reopen the terminal, or use -CheckOnly for documentation checks.' } $script:uvPath = $uvCommand.Source Invoke-UvStep -UvArguments @('--version') # Use this repository even when the caller activated another virtual environment. $env:UV_PROJECT_ENVIRONMENT = Join-Path $PSScriptRoot '.venv' $common = @('--project', $PSScriptRoot, '--cache-dir', (Join-Path $PSScriptRoot '.uv-cache'), '--no-python-downloads') if ($Offline) { $common += '--offline' } $pythonPath = Join-Path $env:UV_PROJECT_ENVIRONMENT 'Scripts/python.exe' if ([Environment]::OSVersion.Platform -ne [PlatformID]::Win32NT) { $pythonPath = Join-Path $env:UV_PROJECT_ENVIRONMENT 'bin/python' } if (Test-Path -LiteralPath $env:UV_PROJECT_ENVIRONMENT) { if (-not (Test-Path -LiteralPath $pythonPath -PathType Leaf)) { throw 'Existing .venv has no Python executable. Inspect it before repairing; this script will not delete it.' } & $pythonPath (Join-Path $PSScriptRoot 'scripts/check-runtime.py') if ($LASTEXITCODE -ne 0) { throw "Existing Python preflight failed (exit $LASTEXITCODE). Check execution permissions and interpreter availability." } } if (Test-Path -LiteralPath (Join-Path $PSScriptRoot 'uv.lock')) { Invoke-UvStep -UvArguments (@('lock', '--check') + $common) } else { Write-Output 'Creating the initial uv.lock from pyproject.toml.' Invoke-UvStep -UvArguments (@('lock') + $common) } # Retain unrelated packages already installed in the user's local environment. Invoke-UvStep -UvArguments (@('sync', '--frozen', '--inexact') + $common) & $pythonPath (Join-Path $PSScriptRoot 'scripts/check-runtime.py') if ($LASTEXITCODE -ne 0) { throw "Python smoke check failed (exit $LASTEXITCODE)." } Write-Output 'Environment initialization passed. Business functionality and external services were NOT verified.' } } catch { Write-Error $_ -ErrorAction Continue $exitCode = 1 } finally { $env:UV_PROJECT_ENVIRONMENT = $previousEnvironment Pop-Location } exit $exitCode