Files
agent-cost-estimator/bootstrap.ps1
T
nvaldez c38579fb1e Add --local and --no-env options to bootstrap scripts
- Add --no-env flag to skip virtual environment creation
- Add --local flag to install with --user (user-local installation)
- Update both bash and PowerShell scripts

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
2026-10-21 23:55:53 +02:00

323 lines
9.8 KiB
PowerShell

# Generated by AI/LLM: Bootstrap for agent-cost-estimator on Windows PowerShell
#
# 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 ace_env
# . .\bootstrap.ps1 --env ace_env --python 3.11.8 -c
# . .\bootstrap.ps1 --no-env --local
#
# 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.
#
# Options:
# --pyenv Ignored on Windows (pyenv not supported)
# -c, --clean Delete existing environment before creating a new one
# --env NAME Use a custom environment name (default: venv)
# --python VER Use a specific Python version (default: current)
# --no-env Skip virtual environment creation, install to current Python
# --local Install package with --user flag (user-local installation)
#Requires -Version 5.1
function Write-AceStatus {
param([string]$Message)
Write-Host "[+] $Message" -ForegroundColor Green
}
function Write-AceWarning {
param([string]$Message)
Write-Host "[!] $Message" -ForegroundColor Yellow
}
function Write-AceError {
param([string]$Message)
Write-Host "[x] $Message" -ForegroundColor Red
}
function Remove-AceBootstrapFunctions {
if (Get-Variable -Name AcePrevErrorAction -Scope 1 -ErrorAction SilentlyContinue) {
Set-Variable -Name ErrorActionPreference -Value $AcePrevErrorAction -Scope 1
Remove-Variable -Name AcePrevErrorAction -Scope 1 -ErrorAction SilentlyContinue
}
foreach ($name in @(
"Write-AceStatus",
"Write-AceWarning",
"Write-AceError",
"Resolve-AcePython",
"Remove-AceBootstrapFunctions"
)) {
Remove-Item -Path "Function:\$name" -ErrorAction SilentlyContinue
}
}
function Resolve-AcePython {
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
}
$AcePrevErrorAction = $ErrorActionPreference
$ErrorActionPreference = "Stop"
if (Get-Variable -Name PSNativeCommandUseErrorActionPreference -ErrorAction SilentlyContinue) {
$PSNativeCommandUseErrorActionPreference = $false
}
$Clean = $false
$EnvName = "venv"
$PythonVersion = ""
$Pyenv = $false
$NoEnv = $false
$LocalInstall = $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-AceError "--env requires an environment name argument"
Remove-AceBootstrapFunctions
return
}
$i++
$EnvName = [string]$bootstrapArgs[$i]
}
elseif ($token -eq "--python") {
if (($i + 1) -ge $bootstrapArgs.Count) {
Write-AceError "--python requires a version number argument"
Remove-AceBootstrapFunctions
return
}
$i++
$PythonVersion = [string]$bootstrapArgs[$i]
}
elseif ($token -eq "--no-env") {
$NoEnv = $true
}
elseif ($token -eq "--local") {
$LocalInstall = $true
}
else {
Write-AceError "Unknown option: $token"
Write-AceError "Usage: . .\bootstrap.ps1 [--pyenv] [-c|--clean] [--env custom_name] [--python 3.x.x] [--no-env] [--local]"
Remove-AceBootstrapFunctions
return
}
}
if ($MyInvocation.InvocationName -ne ".") {
Write-AceError "Dot-source this script so the virtual environment stays active in this session."
Write-AceError " . .\bootstrap.ps1"
Remove-AceBootstrapFunctions
return
}
if ($Pyenv) {
Write-AceWarning "Windows bootstrap uses a local venv under .venvs\. It does not install pyenv."
}
if ($NoEnv) {
Write-AceStatus "Running in --no-env mode (no virtual environment will be created)"
}
if ($LocalInstall) {
Write-AceStatus "Running in --local mode (will install with --user flag)"
}
$repoRoot = $PSScriptRoot
if (-not $repoRoot) {
$repoRoot = (Get-Location).Path
}
Write-AceStatus "Starting agent-cost-estimator bootstrap with environment: $EnvName"
try {
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass -Force -ErrorAction Stop | Out-Null
}
catch {
# A GPO can refuse the process override. RemoteSigned still runs local scripts.
}
if (-not $NoEnv) {
$pythonExe = Resolve-AcePython -Version $PythonVersion
if (-not $pythonExe) {
Write-AceError "Python is not installed, or version '$PythonVersion' is not available to the py launcher."
Write-AceError "Install Python from https://www.python.org/downloads/ and enable the py launcher."
if ($PythonVersion) {
Write-AceError "Then re-run: . .\bootstrap.ps1 --python $PythonVersion"
}
Remove-AceBootstrapFunctions
return
}
$venvRoot = Join-Path $repoRoot ".venvs"
$venvPath = Join-Path $venvRoot $EnvName
if ($Clean -and (Test-Path -LiteralPath $venvPath)) {
Write-AceStatus "Deleting existing virtual environment $EnvName..."
try {
Remove-Item -LiteralPath $venvPath -Recurse -Force
}
catch {
Write-AceError "Failed to delete virtual environment $EnvName"
Write-AceError $_.Exception.Message
Remove-AceBootstrapFunctions
return
}
}
$activate = Join-Path $venvPath "Scripts\Activate.ps1"
if (-not (Test-Path -LiteralPath $activate)) {
Write-AceStatus "Creating virtual environment $EnvName with $pythonExe..."
New-Item -ItemType Directory -Path $venvRoot -Force | Out-Null
& $pythonExe -m venv $venvPath
if ($LASTEXITCODE -ne 0) {
Write-AceError "Failed to create virtual environment $EnvName"
Remove-AceBootstrapFunctions
return
}
}
else {
Write-AceStatus "Virtual environment $EnvName already exists"
}
Write-AceStatus "Activating $EnvName..."
. $activate
if (-not $env:VIRTUAL_ENV) {
Write-AceError "Failed to activate $EnvName"
Remove-AceBootstrapFunctions
return
}
}
else {
Write-AceStatus "Using current Python environment (no venv)"
}
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-AceWarning "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-AceWarning "Unsetting FORCE_COLOR so pytest output can be parsed"
Remove-Item Env:FORCE_COLOR
}
if ($env:PY_COLORS) {
Write-AceWarning "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-AceError "No pyproject.toml found at $pyproject"
Remove-AceBootstrapFunctions
return
}
Write-AceStatus "Upgrading pip..."
& python -m pip install --upgrade pip
if ($LASTEXITCODE -ne 0) {
Write-AceWarning "Failed to upgrade pip. Continuing with package installation..."
}
Write-AceStatus "Installing package and dependencies from pyproject.toml..."
Push-Location $repoRoot
try {
$pipArgs = "-e ."
if (-not $NoEnv) {
$pipArgs += "[dev]"
}
if ($LocalInstall) {
$pipArgs += " --user"
}
& python -m pip install $pipArgs
if ($LASTEXITCODE -ne 0) {
Write-AceError "Failed to install package and dependencies from pyproject.toml"
Remove-AceBootstrapFunctions
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-AceStatus "Bootstrap completed successfully!"
if (-not $NoEnv) {
Write-AceStatus "Virtual environment '$EnvName' is active"
}
else {
Write-AceStatus "Using current Python environment"
}
Write-AceStatus "Python: $pythonVersionText ($pythonWhere)"
Write-AceStatus "Pip: $pipVersionText"
Write-AceStatus ""
Write-AceStatus "To verify installation, try:"
Write-AceStatus " agent-cost-estimator --help"
if (-not $NoEnv) {
Write-AceStatus " python -m pytest tests/"
}
Write-AceStatus ""
Write-AceStatus "Happy coding!"
Remove-AceBootstrapFunctions