Files
fiona-assistant/bootstrap.ps1
2026-09-29 10:04:57 +02:00

283 lines
9.0 KiB
PowerShell

# Generated by CURSOR: 2026-09-29 - Windows PowerShell bootstrap for Fiona development
#
# Project bootstrap for Windows. Dot-source it so the virtual environment
# stays active in the current session (same idea as `source bootstrap`):
#
# Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
# . .\bootstrap.ps1
# . .\bootstrap.ps1 --env fiona_env
# . .\bootstrap.ps1 --env fiona_env --python 3.13.8 -c
#
# Creates a repo-local venv at .venvs\<name> with the Python launcher (py)
# or python on PATH. Does not install pyenv. --pyenv is accepted so the
# Linux flag still parses; on Windows it is a no-op notice.
#Requires -Version 5.1
# CURSOR: flags are parsed by hand. Windows PowerShell 5.1 does not bind
# `--python` to a parameter alias, and the bash script uses those flags.
function Write-FionaStatus {
param([string]$Message)
Write-Host "[+] $Message" -ForegroundColor Green
}
function Write-FionaWarning {
param([string]$Message)
Write-Host "[!] $Message" -ForegroundColor Yellow
}
function Write-FionaError {
param([string]$Message)
Write-Host "[x] $Message" -ForegroundColor Red
}
function Remove-FionaBootstrapFunctions {
if (Get-Variable -Name FionaPrevErrorAction -Scope 1 -ErrorAction SilentlyContinue) {
Set-Variable -Name ErrorActionPreference -Value $FionaPrevErrorAction -Scope 1
Remove-Variable -Name FionaPrevErrorAction -Scope 1 -ErrorAction SilentlyContinue
}
foreach ($name in @(
"Write-FionaStatus",
"Write-FionaWarning",
"Write-FionaError",
"Resolve-FionaPython",
"Remove-FionaBootstrapFunctions"
)) {
Remove-Item -Path "Function:\$name" -ErrorAction SilentlyContinue
}
}
function Resolve-FionaPython {
# CURSOR: pick a real interpreter. Prefer the py launcher so a requested
# version (3.13.8, then 3.13) wins over whatever `python` happens to be.
param([string]$Version)
$tags = @()
if ($Version) {
$tags += $Version
if ($Version -match '^(\d+\.\d+)') {
$minor = $Matches[1]
if ($minor -ne $Version) {
$tags += $minor
}
}
}
$py = Get-Command py -ErrorAction SilentlyContinue
if ($py) {
foreach ($tag in $tags) {
$exe = & py "-$tag" -c "import sys; print(sys.executable)" 2>$null
if ($LASTEXITCODE -eq 0 -and $exe) {
return @($exe)[-1].ToString().Trim()
}
}
if (-not $Version) {
$exe = & py -3 -c "import sys; print(sys.executable)" 2>$null
if ($LASTEXITCODE -eq 0 -and $exe) {
return @($exe)[-1].ToString().Trim()
}
}
}
if (-not $Version) {
foreach ($name in @("python", "python3")) {
$cmd = Get-Command $name -ErrorAction SilentlyContinue
if ($cmd -and $cmd.Source -notmatch "WindowsApps") {
return $cmd.Source
}
}
}
return $null
}
$FionaPrevErrorAction = $ErrorActionPreference
$ErrorActionPreference = "Stop"
# CURSOR: a failed pip/py call must be checked via $LASTEXITCODE, not thrown.
if (Get-Variable -Name PSNativeCommandUseErrorActionPreference -ErrorAction SilentlyContinue) {
$PSNativeCommandUseErrorActionPreference = $false
}
$Clean = $false
$EnvName = "test_env"
$PythonVersion = ""
$Pyenv = $false
$bootstrapArgs = @($args)
for ($i = 0; $i -lt $bootstrapArgs.Count; $i++) {
$token = [string]$bootstrapArgs[$i]
if ($token -eq "--pyenv") {
$Pyenv = $true
}
elseif ($token -eq "-c" -or $token -eq "--clean") {
$Clean = $true
}
elseif ($token -eq "--env") {
if (($i + 1) -ge $bootstrapArgs.Count) {
Write-FionaError "--env requires an environment name argument"
Remove-FionaBootstrapFunctions
return
}
$i++
$EnvName = [string]$bootstrapArgs[$i]
}
elseif ($token -eq "--python") {
if (($i + 1) -ge $bootstrapArgs.Count) {
Write-FionaError "--python requires a version number argument"
Remove-FionaBootstrapFunctions
return
}
$i++
$PythonVersion = [string]$bootstrapArgs[$i]
}
else {
Write-FionaError "Unknown option: $token"
Write-FionaError "Usage: . .\bootstrap.ps1 [--pyenv] [-c|--clean] [--env custom_name] [--python 3.x.x]"
Remove-FionaBootstrapFunctions
return
}
}
if ($MyInvocation.InvocationName -ne ".") {
Write-FionaError "Dot-source this script so the virtual environment stays active in this session."
Write-FionaError " . .\bootstrap.ps1"
Remove-FionaBootstrapFunctions
return
}
if ($Pyenv) {
Write-FionaWarning "Windows bootstrap uses a local venv under .venvs\. It does not install pyenv."
}
$repoRoot = $PSScriptRoot
if (-not $repoRoot) {
$repoRoot = (Get-Location).Path
}
Write-FionaStatus "Starting project bootstrap with environment: $EnvName"
try {
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass -Force -ErrorAction Stop | Out-Null
}
catch {
# CURSOR: a GPO can refuse the process override. RemoteSigned still runs local scripts.
}
$pythonExe = Resolve-FionaPython -Version $PythonVersion
if (-not $pythonExe) {
Write-FionaError "Python is not installed, or version '$PythonVersion' is not available to the py launcher."
Write-FionaError "Install Python from https://www.python.org/downloads/ and enable the py launcher."
if ($PythonVersion) {
Write-FionaError "Then re-run: . .\bootstrap.ps1 --python $PythonVersion"
}
Remove-FionaBootstrapFunctions
return
}
$venvRoot = Join-Path $repoRoot ".venvs"
$venvPath = Join-Path $venvRoot $EnvName
if ($Clean -and (Test-Path -LiteralPath $venvPath)) {
Write-FionaStatus "Deleting existing virtual environment $EnvName..."
try {
Remove-Item -LiteralPath $venvPath -Recurse -Force
}
catch {
Write-FionaError "Failed to delete virtual environment $EnvName"
Write-FionaError $_.Exception.Message
Remove-FionaBootstrapFunctions
return
}
}
$activate = Join-Path $venvPath "Scripts\Activate.ps1"
if (-not (Test-Path -LiteralPath $activate)) {
Write-FionaStatus "Creating virtual environment $EnvName with $pythonExe..."
New-Item -ItemType Directory -Path $venvRoot -Force | Out-Null
& $pythonExe -m venv $venvPath
if ($LASTEXITCODE -ne 0) {
Write-FionaError "Failed to create virtual environment $EnvName"
Remove-FionaBootstrapFunctions
return
}
}
else {
Write-FionaStatus "Virtual environment $EnvName already exists"
}
Write-FionaStatus "Activating $EnvName..."
. $activate
if (-not $env:VIRTUAL_ENV) {
Write-FionaError "Failed to activate $EnvName"
Remove-FionaBootstrapFunctions
return
}
if ($env:PYTEST_ADDOPTS) {
$sanitized = $env:PYTEST_ADDOPTS -replace '--no-summary', '' -replace '(^|\s)-S(\s|$)', ' '
$sanitized = ($sanitized -replace '\s+', ' ').Trim()
if ($sanitized -ne $env:PYTEST_ADDOPTS.Trim()) {
Write-FionaWarning "Removing --no-summary from PYTEST_ADDOPTS so test output can be parsed"
if ($sanitized) {
$env:PYTEST_ADDOPTS = $sanitized
}
else {
Remove-Item Env:PYTEST_ADDOPTS
}
}
}
if ($env:FORCE_COLOR) {
Write-FionaWarning "Unsetting FORCE_COLOR so pytest output can be parsed"
Remove-Item Env:FORCE_COLOR
}
if ($env:PY_COLORS) {
Write-FionaWarning "Unsetting PY_COLORS so pytest output can be parsed"
Remove-Item Env:PY_COLORS
}
$pyproject = Join-Path $repoRoot "pyproject.toml"
if (-not (Test-Path -LiteralPath $pyproject)) {
Write-FionaError "No pyproject.toml found at $pyproject"
Remove-FionaBootstrapFunctions
return
}
Write-FionaStatus "Upgrading pip..."
& python -m pip install --upgrade pip
if ($LASTEXITCODE -ne 0) {
Write-FionaWarning "Failed to upgrade pip. Continuing with package installation..."
}
Write-FionaStatus "Installing package and dependencies from pyproject.toml..."
Push-Location $repoRoot
try {
& python -m pip install -e ".[dev]"
if ($LASTEXITCODE -ne 0) {
Write-FionaError "Failed to install package and dependencies from pyproject.toml"
Remove-FionaBootstrapFunctions
return
}
}
finally {
Pop-Location
}
$pythonCmd = Get-Command python -ErrorAction SilentlyContinue
$pythonWhere = if ($pythonCmd) { $pythonCmd.Source } else { "python" }
$pythonVersionText = (& python --version 2>&1 | Out-String).Trim()
$pipVersionText = (& python -m pip --version 2>&1 | Out-String).Trim()
Write-FionaStatus "Bootstrap completed successfully!"
Write-FionaStatus "Virtual environment '$EnvName' is active"
Write-FionaStatus "Python: $pythonVersionText ($pythonWhere)"
Write-FionaStatus "Pip: $pipVersionText"
Write-FionaStatus ""
Write-FionaStatus "To verify installation, try:"
Write-FionaStatus " python src/run_tests.py"
Write-FionaStatus " fiona-assistant --help"
Write-FionaStatus ""
Write-FionaStatus "Happy coding!"
Remove-FionaBootstrapFunctions