|
| 1 | +# Post-clone developer setup for AsyncFlow (Windows / PowerShell). |
| 2 | +# |
| 3 | +# What it does: |
| 4 | +# 1) Ensures Poetry is available (official installer if missing). |
| 5 | +# 2) Configures Poetry to create an in-project virtualenv (.venv). |
| 6 | +# 3) Removes poetry.lock (fresh dependency resolution by policy). |
| 7 | +# 4) Installs the project with dev extras. |
| 8 | +# 5) Runs ruff, mypy, and pytest (with coverage if available). |
| 9 | +# |
| 10 | +# Usage: |
| 11 | +# .\scripts\dev_setup.ps1 |
| 12 | +# |
| 13 | +# Notes: |
| 14 | +# - Run this from anywhere; it will cd to repo root. |
| 15 | +# - Requires Python >= 3.12 to be available (via 'py' launcher or python.exe). |
| 16 | +# - We do NOT delete an existing .venv; it will be reused if compatible. |
| 17 | + |
| 18 | +# Strict error handling |
| 19 | +$ErrorActionPreference = 'Stop' |
| 20 | +Set-StrictMode -Version Latest |
| 21 | + |
| 22 | +# --- helpers ------------------------------------------------------------------ |
| 23 | + |
| 24 | +function Write-Info { param([string]$Msg) Write-Host "==> $Msg" } |
| 25 | +function Write-Ok { param([string]$Msg) Write-Host "✅ $Msg" -ForegroundColor Green } |
| 26 | +function Fail { param([string]$Msg) Write-Error $Msg; exit 1 } |
| 27 | + |
| 28 | +# Resolve repo root (this script lives in scripts/) |
| 29 | +$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path |
| 30 | +$RepoRoot = Resolve-Path (Join-Path $ScriptDir '..') |
| 31 | + |
| 32 | +function Require-Pyproject { |
| 33 | + if (-not (Test-Path (Join-Path $RepoRoot 'pyproject.toml'))) { |
| 34 | + Fail "pyproject.toml not found at repo root ($RepoRoot)" |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +function Get-PythonPath-3_12Plus { |
| 39 | + <# |
| 40 | + Try common Windows launchers/executables and return the *actual* Python |
| 41 | + interpreter path (sys.executable) for a version >= 3.12. |
| 42 | + #> |
| 43 | + $candidates = @( |
| 44 | + @('py', '-3.13'), |
| 45 | + @('py', '-3.12'), |
| 46 | + @('py', '-3'), |
| 47 | + @('python3.13'), |
| 48 | + @('python3.12'), |
| 49 | + @('python') |
| 50 | + ) |
| 51 | + |
| 52 | + foreach ($cand in $candidates) { |
| 53 | + $exe = $cand[0] |
| 54 | + $args = @() |
| 55 | + if ($cand.Count -gt 1) { $args = $cand[1..($cand.Count-1)] } |
| 56 | + |
| 57 | + if (-not (Get-Command $exe -ErrorAction SilentlyContinue)) { continue } |
| 58 | + |
| 59 | + # Check version |
| 60 | + & $exe @args -c "import sys; import sys as s; raise SystemExit(0 if sys.version_info[:2] >= (3,12) else 1)" 2>$null |
| 61 | + if ($LASTEXITCODE -ne 0) { continue } |
| 62 | + |
| 63 | + # Obtain the real interpreter path |
| 64 | + $pyPath = & $exe @args -c "import sys; print(sys.executable)" 2>$null |
| 65 | + if ($LASTEXITCODE -eq 0 -and $pyPath) { |
| 66 | + return $pyPath.Trim() |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + return $null |
| 71 | +} |
| 72 | + |
| 73 | +function Ensure-Poetry { |
| 74 | + if (Get-Command poetry -ErrorAction SilentlyContinue) { |
| 75 | + poetry --version | Out-Null |
| 76 | + return |
| 77 | + } |
| 78 | + |
| 79 | + Write-Info "Poetry not found; attempting installation…" |
| 80 | + |
| 81 | + # Official installer (recommended by Poetry) |
| 82 | + $installer = (Invoke-WebRequest -Uri https://install.python-poetry.org -UseBasicParsing).Content |
| 83 | + # Pipe installer to Python (stdin) |
| 84 | + $pythonToUse = (Get-Command py -ErrorAction SilentlyContinue) ? 'py' : 'python' |
| 85 | + $installer | & $pythonToUse - |
| 86 | + |
| 87 | + # Common locations (make available for current session) |
| 88 | + $poetryCandidates = @( |
| 89 | + (Join-Path $env:APPDATA 'pypoetry\venv\Scripts'), |
| 90 | + (Join-Path $env:USERPROFILE '.local\bin') |
| 91 | + ) |
| 92 | + foreach ($p in $poetryCandidates) { |
| 93 | + if (Test-Path $p) { $env:Path = "$p;$env:Path" } |
| 94 | + } |
| 95 | + |
| 96 | + if (-not (Get-Command poetry -ErrorAction SilentlyContinue)) { |
| 97 | + Fail "Poetry installation failed (not on PATH). Close & reopen PowerShell or add the Poetry path to PATH." |
| 98 | + } |
| 99 | + |
| 100 | + poetry --version | Out-Null |
| 101 | +} |
| 102 | + |
| 103 | +function Run-Tests-WithOptionalCoverage { |
| 104 | + <# |
| 105 | + Try pytest with coverage first; if the plugin is missing, |
| 106 | + fall back to plain pytest. Propagate failure if tests fail. |
| 107 | + #> |
| 108 | + $cmd = { poetry run pytest --cov=src --cov-report=term-missing:skip-covered --cov-report=xml --disable-warnings -q } |
| 109 | + try { |
| 110 | + & $cmd |
| 111 | + if ($LASTEXITCODE -eq 0) { |
| 112 | + Write-Ok "Tests (with coverage) PASSED" |
| 113 | + return |
| 114 | + } |
| 115 | + } catch { |
| 116 | + # ignore; retry without coverage below |
| 117 | + } |
| 118 | + |
| 119 | + Write-Info "Coverage run failed (likely pytest-cov not installed). Falling back to plain pytest…" |
| 120 | + poetry run pytest --disable-warnings -q |
| 121 | + if ($LASTEXITCODE -ne 0) { |
| 122 | + Fail "Tests FAILED" |
| 123 | + } |
| 124 | + Write-Ok "Tests PASSED" |
| 125 | +} |
| 126 | + |
| 127 | +# --- main --------------------------------------------------------------------- |
| 128 | + |
| 129 | +Set-Location $RepoRoot |
| 130 | +Require-Pyproject |
| 131 | + |
| 132 | +$PythonExe = Get-PythonPath-3_12Plus |
| 133 | +if (-not $PythonExe) { |
| 134 | + Fail "Python >= 3.12 not found. Install Python 3.12+ and re-run." |
| 135 | +} |
| 136 | +Write-Info ("Using Python: " + (& $PythonExe -V)) |
| 137 | + |
| 138 | +Ensure-Poetry |
| 139 | + |
| 140 | +# Make sure Poetry venv lives inside the repo |
| 141 | +Write-Info "Configuring Poetry to use in-project virtualenv (.venv)…" |
| 142 | +poetry config virtualenvs.in-project true |
| 143 | +Write-Ok "Poetry configured to use .venv" |
| 144 | + |
| 145 | +# Bind Poetry to the chosen interpreter (creates .venv if needed) |
| 146 | +poetry env use "$PythonExe" | Out-Null |
| 147 | +Write-Ok "Virtualenv ready (.venv)" |
| 148 | + |
| 149 | +# Policy: always remove lock to avoid conflicts across environments |
| 150 | +$lockPath = Join-Path $RepoRoot 'poetry.lock' |
| 151 | +if (Test-Path $lockPath) { |
| 152 | + Write-Info "Removing poetry.lock for a clean resolution…" |
| 153 | + Remove-Item $lockPath -Force |
| 154 | + Write-Ok "poetry.lock removed" |
| 155 | +} |
| 156 | + |
| 157 | +# Faster installs and stable headless plotting |
| 158 | +$env:PIP_DISABLE_PIP_VERSION_CHECK = '1' |
| 159 | +$env:MPLBACKEND = 'Agg' |
| 160 | + |
| 161 | +Write-Info "Installing project with dev extras…" |
| 162 | +poetry install --with dev --no-interaction --no-ansi |
| 163 | +Write-Ok "Dependencies installed (dev)" |
| 164 | + |
| 165 | +Write-Info "Running Ruff (lint)…" |
| 166 | +poetry run ruff check src tests |
| 167 | +Write-Ok "Ruff PASSED" |
| 168 | + |
| 169 | +Write-Info "Running MyPy (type-check)…" |
| 170 | +poetry run mypy src tests |
| 171 | +Write-Ok "MyPy PASSED" |
| 172 | + |
| 173 | +Write-Info "Running tests (with coverage if available)…" |
| 174 | +Run-Tests-WithOptionalCoverage |
| 175 | + |
| 176 | +Write-Ok "All checks completed SUCCESSFULLY 🎉" |
0 commit comments