359 lines
11 KiB
PowerShell
359 lines
11 KiB
PowerShell
# Generated by AI/LLM: 2026-09-29 - Windows PowerShell bootstrap for Fiona development
|
|
# Modified by AI/LLM: 2026-09-30 - --local and --no-env install options.
|
|
#
|
|
# Project bootstrap for Windows. Dot-source a virtual-environment install so
|
|
# the 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
|
|
# . .\bootstrap.ps1 --local
|
|
# powershell -ExecutionPolicy Bypass -File .\bootstrap.ps1 --local --no-env
|
|
#
|
|
# 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.
|
|
#
|
|
# --local installs this repository as a normal package (not editable).
|
|
# --no-env skips the virtual environment and installs with pip --user,
|
|
# so fiona-assistant can run without activating an environment.
|
|
# --no-env may be executed with -File. A venv install must be dot-sourced.
|
|
|
|
#Requires -Version 5.1
|
|
|
|
# AI/LLM: 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 {
|
|
# AI/LLM: 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"
|
|
# AI/LLM: 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
|
|
$Local = $false
|
|
$NoEnv = $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 "--local") {
|
|
$Local = $true
|
|
}
|
|
elseif ($token -eq "--no-env") {
|
|
$NoEnv = $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] [--local] [--no-env]"
|
|
Remove-FionaBootstrapFunctions
|
|
return
|
|
}
|
|
}
|
|
|
|
if (-not $NoEnv -and $MyInvocation.InvocationName -ne ".") {
|
|
Write-FionaError "Dot-source this script so the virtual environment stays active in this session."
|
|
Write-FionaError " . .\bootstrap.ps1"
|
|
Write-FionaError "To install without a virtual environment, run:"
|
|
Write-FionaError " powershell -ExecutionPolicy Bypass -File .\bootstrap.ps1 --local --no-env"
|
|
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
|
|
}
|
|
|
|
if ($NoEnv) {
|
|
Write-FionaStatus "Starting project bootstrap (no virtual environment)"
|
|
}
|
|
else {
|
|
Write-FionaStatus "Starting project bootstrap with environment: $EnvName"
|
|
}
|
|
if ($Local) {
|
|
Write-FionaStatus "Local install: this repository, not editable"
|
|
}
|
|
if ($NoEnv) {
|
|
Write-FionaStatus "No virtual environment: installing with --user"
|
|
}
|
|
if ($NoEnv -and $Clean) {
|
|
Write-FionaWarning "--clean has no effect with --no-env"
|
|
}
|
|
|
|
try {
|
|
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass -Force -ErrorAction Stop | Out-Null
|
|
}
|
|
catch {
|
|
# AI/LLM: 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
|
|
}
|
|
|
|
$pipPython = $pythonExe
|
|
if (-not $NoEnv) {
|
|
$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
|
|
}
|
|
|
|
$pipPython = Join-Path $venvPath "Scripts\python.exe"
|
|
}
|
|
|
|
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..."
|
|
if ($NoEnv) {
|
|
& $pipPython -m pip install --upgrade pip --user
|
|
}
|
|
else {
|
|
& $pipPython -m pip install --upgrade pip
|
|
}
|
|
if ($LASTEXITCODE -ne 0) {
|
|
Write-FionaWarning "Failed to upgrade pip. Continuing with package installation..."
|
|
}
|
|
|
|
$helper = Join-Path $repoRoot "scripts\bootstrap_install.py"
|
|
if (-not (Test-Path -LiteralPath $helper)) {
|
|
Write-FionaError "Missing $helper"
|
|
Remove-FionaBootstrapFunctions
|
|
return
|
|
}
|
|
$helperArgs = @($helper)
|
|
if ($Local) {
|
|
$helperArgs += "--local"
|
|
}
|
|
if ($NoEnv) {
|
|
$helperArgs += "--no-env"
|
|
}
|
|
$pipWords = @(& $pipPython @helperArgs | Where-Object { $_ -and "$_".Trim() -ne "" })
|
|
if ($LASTEXITCODE -ne 0 -or $pipWords.Count -eq 0) {
|
|
Write-FionaError "Failed to resolve pip install arguments"
|
|
Remove-FionaBootstrapFunctions
|
|
return
|
|
}
|
|
|
|
if ($Local) {
|
|
Write-FionaStatus "Installing this repository as a normal package..."
|
|
}
|
|
else {
|
|
Write-FionaStatus "Installing package and dependencies from pyproject.toml..."
|
|
}
|
|
Push-Location $repoRoot
|
|
try {
|
|
& $pipPython -m pip @pipWords
|
|
if ($LASTEXITCODE -ne 0) {
|
|
Write-FionaError "Failed to install package and dependencies from pyproject.toml"
|
|
Remove-FionaBootstrapFunctions
|
|
return
|
|
}
|
|
}
|
|
finally {
|
|
Pop-Location
|
|
}
|
|
|
|
$pythonVersionText = (& $pipPython --version 2>&1 | Out-String).Trim()
|
|
$pipVersionText = (& $pipPython -m pip --version 2>&1 | Out-String).Trim()
|
|
|
|
Write-FionaStatus "Bootstrap completed successfully!"
|
|
if ($NoEnv) {
|
|
$scriptsDir = (& $pipPython -c "import sysconfig; print(sysconfig.get_path('scripts', 'nt_user'))" | Out-String).Trim()
|
|
Write-FionaStatus "Installed for the current user (no virtual environment)"
|
|
Write-FionaStatus "Command directory: $scriptsDir"
|
|
Write-FionaStatus "If fiona-assistant is not found, add that directory to PATH."
|
|
}
|
|
else {
|
|
Write-FionaStatus "Virtual environment '$EnvName' is active"
|
|
}
|
|
Write-FionaStatus "Python: $pythonVersionText ($pipPython)"
|
|
Write-FionaStatus "Pip: $pipVersionText"
|
|
Write-FionaStatus ""
|
|
Write-FionaStatus "To verify installation, try:"
|
|
if (-not $NoEnv) {
|
|
Write-FionaStatus " python scripts/run_tests.py"
|
|
}
|
|
Write-FionaStatus " fiona-assistant --help"
|
|
Write-FionaStatus ""
|
|
Write-FionaStatus "Happy coding!"
|
|
|
|
Remove-FionaBootstrapFunctions
|