init.ps1 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. [CmdletBinding()]
  2. param(
  3. [switch]$CheckOnly,
  4. [switch]$Offline
  5. )
  6. $ErrorActionPreference = 'Stop'
  7. # Native failures are checked explicitly on both Windows PowerShell and PowerShell 7.
  8. $PSNativeCommandUseErrorActionPreference = $false
  9. $previousEnvironment = $env:UV_PROJECT_ENVIRONMENT
  10. $exitCode = 0
  11. function Invoke-UvStep {
  12. param([string[]]$UvArguments)
  13. & $script:uvPath @UvArguments
  14. if ($LASTEXITCODE -ne 0) {
  15. throw "uv $($UvArguments[0]) failed (exit $LASTEXITCODE). Check the error above; no environment reset was attempted."
  16. }
  17. }
  18. Push-Location $PSScriptRoot
  19. try {
  20. $harnessPath = Join-Path $PSScriptRoot 'scripts/check-harness.ps1'
  21. if (Test-Path -LiteralPath $harnessPath -PathType Leaf) {
  22. & $harnessPath
  23. } elseif ($CheckOnly) {
  24. throw 'Local Harness files are not included in this checkout. Run init.ps1 without -CheckOnly to initialize the environment.'
  25. } else {
  26. Write-Output 'Local Harness not included; proceeding with environment initialization.'
  27. }
  28. if ($CheckOnly) {
  29. Write-Output 'Harness check only passed. Python and dependencies were NOT verified.'
  30. } else {
  31. $uvCommand = Get-Command uv -CommandType Application -ErrorAction SilentlyContinue
  32. if (-not $uvCommand) { throw 'uv is required. Install uv and reopen the terminal, or use -CheckOnly for documentation checks.' }
  33. $script:uvPath = $uvCommand.Source
  34. Invoke-UvStep -UvArguments @('--version')
  35. # Use this repository even when the caller activated another virtual environment.
  36. $env:UV_PROJECT_ENVIRONMENT = Join-Path $PSScriptRoot '.venv'
  37. $common = @('--project', $PSScriptRoot, '--cache-dir', (Join-Path $PSScriptRoot '.uv-cache'), '--no-python-downloads')
  38. if ($Offline) { $common += '--offline' }
  39. $pythonPath = Join-Path $env:UV_PROJECT_ENVIRONMENT 'Scripts/python.exe'
  40. if ([Environment]::OSVersion.Platform -ne [PlatformID]::Win32NT) {
  41. $pythonPath = Join-Path $env:UV_PROJECT_ENVIRONMENT 'bin/python'
  42. }
  43. if (Test-Path -LiteralPath $env:UV_PROJECT_ENVIRONMENT) {
  44. if (-not (Test-Path -LiteralPath $pythonPath -PathType Leaf)) {
  45. throw 'Existing .venv has no Python executable. Inspect it before repairing; this script will not delete it.'
  46. }
  47. & $pythonPath (Join-Path $PSScriptRoot 'scripts/check-runtime.py')
  48. if ($LASTEXITCODE -ne 0) { throw "Existing Python preflight failed (exit $LASTEXITCODE). Check execution permissions and interpreter availability." }
  49. }
  50. if (Test-Path -LiteralPath (Join-Path $PSScriptRoot 'uv.lock')) {
  51. Invoke-UvStep -UvArguments (@('lock', '--check') + $common)
  52. } else {
  53. Write-Output 'Creating the initial uv.lock from pyproject.toml.'
  54. Invoke-UvStep -UvArguments (@('lock') + $common)
  55. }
  56. # Retain unrelated packages already installed in the user's local environment.
  57. Invoke-UvStep -UvArguments (@('sync', '--frozen', '--inexact') + $common)
  58. & $pythonPath (Join-Path $PSScriptRoot 'scripts/check-runtime.py')
  59. if ($LASTEXITCODE -ne 0) { throw "Python smoke check failed (exit $LASTEXITCODE)." }
  60. Write-Output 'Environment initialization passed. Business functionality and external services were NOT verified.'
  61. }
  62. } catch {
  63. Write-Error $_ -ErrorAction Continue
  64. $exitCode = 1
  65. } finally {
  66. $env:UV_PROJECT_ENVIRONMENT = $previousEnvironment
  67. Pop-Location
  68. }
  69. exit $exitCode