Files
openswarm/run.ps1
T
Eric 4ce4dec33c [eric] windows polish + bump 1.0.25: publish-win.ps1 mirror of publish.sh + run.ps1 windows dev launcher, build-app-win.ps1 warns 8s if Mac latest-mac.yml missing in
v<version> release before -Publish, python-env cache skip via OPENSWARM_REBUILD_PYTHON, pip stderr wrapped to suppress PS5.1 NativeCommandError; analytics APP_VERSION
  auto-derived from electron/package.json (single source of truth); OnboardingModal renders Connected state for already-active providers; gitignore fix for backend/.venv.
  Bumps 1.0.25 - first version that ships Mac and Windows together.
2026-04-21 21:53:48 -07:00

193 lines
8.0 KiB
PowerShell

# Windows dev launcher - mirror of bash run.sh.
# Spins up backend (uvicorn --reload) + frontend (webpack dev server) + electron
# in one terminal. Hot-reload for Python and React. Ctrl+C to stop all three.
#
# Prereqs: Python 3.12+, Node 20+, npm. Everything else is installed on demand.
#
# Usage:
# powershell -ExecutionPolicy Bypass -File run.ps1
# (or) pwsh run.ps1 if PowerShell 7 is installed.
$ErrorActionPreference = 'Stop'
$ScriptDir = Split-Path -Parent $PSCommandPath
# --- Locate Python ---
$python = $null
foreach ($name in @('python', 'python3', 'py')) {
$cmd = Get-Command $name -ErrorAction SilentlyContinue
if ($cmd) { $python = $cmd.Source; break }
}
if (-not $python) {
throw "Python 3.12+ not found on PATH. Install from https://www.python.org/downloads/"
}
# --- Bundled uv/uvx (matches what build-app-win.ps1 does so MCP discovery works in dev) ---
$UvBinDir = Join-Path $ScriptDir 'backend\uv-bin'
if (-not (Test-Path (Join-Path $UvBinDir 'uvx.exe'))) {
Write-Host "[setup] Downloading uv/uvx for Windows..."
New-Item -ItemType Directory -Force -Path $UvBinDir | Out-Null
$UvUrl = 'https://github.com/astral-sh/uv/releases/latest/download/uv-x86_64-pc-windows-msvc.zip'
$TmpZip = Join-Path $env:TEMP "uv-win-$([guid]::NewGuid()).zip"
$TmpExtract = Join-Path $env:TEMP "uv-win-extract-$([guid]::NewGuid())"
try {
Invoke-WebRequest -Uri $UvUrl -OutFile $TmpZip -UseBasicParsing
Expand-Archive -Path $TmpZip -DestinationPath $TmpExtract -Force
Get-ChildItem $TmpExtract -Recurse -Filter 'uv.exe' | Select-Object -First 1 | ForEach-Object { Copy-Item $_.FullName (Join-Path $UvBinDir 'uv.exe') -Force }
Get-ChildItem $TmpExtract -Recurse -Filter 'uvx.exe' | Select-Object -First 1 | ForEach-Object { Copy-Item $_.FullName (Join-Path $UvBinDir 'uvx.exe') -Force }
} finally {
Remove-Item -Force $TmpZip -ErrorAction SilentlyContinue
Remove-Item -Recurse -Force $TmpExtract -ErrorAction SilentlyContinue
}
}
# --- Backend venv + deps ---
$Venv = Join-Path $ScriptDir 'backend\.venv'
$VenvPy = Join-Path $Venv 'Scripts\python.exe'
if (-not (Test-Path $VenvPy)) {
Write-Host "[setup] Creating Python venv at $Venv ..."
& $python -m venv $Venv
if ($LASTEXITCODE -ne 0) { throw "venv creation failed" }
}
Write-Host "[setup] Installing backend deps (idempotent, fast if already up to date)..."
# Suppress PowerShell 5.1's native-stderr-as-error wrapping for these idempotent
# pip calls (deprecation warnings on stderr would otherwise terminate the script
# under $ErrorActionPreference=Stop). We still check $LASTEXITCODE for real failures.
$prevEAP = $ErrorActionPreference
$ErrorActionPreference = 'Continue'
try {
& $VenvPy -m pip install --quiet --upgrade pip *>&1 | Out-Null
if ($LASTEXITCODE -ne 0) { throw "pip upgrade failed" }
& $VenvPy -m pip install --quiet -r (Join-Path $ScriptDir 'backend\requirements.txt')
if ($LASTEXITCODE -ne 0) { throw "pip install backend reqs failed" }
& $VenvPy -m pip install --quiet -e (Join-Path $ScriptDir 'debugger')
if ($LASTEXITCODE -ne 0) { throw "pip install debugger failed" }
} finally {
$ErrorActionPreference = $prevEAP
}
# --- Frontend deps ---
$FrontendDir = Join-Path $ScriptDir 'frontend'
if (-not (Test-Path (Join-Path $FrontendDir 'node_modules'))) {
Write-Host "[setup] Installing frontend deps..."
Push-Location $FrontendDir
try { & npm install } finally { Pop-Location }
}
# --- Electron deps ---
$ElectronDir = Join-Path $ScriptDir 'electron'
if (-not (Test-Path (Join-Path $ElectronDir 'node_modules'))) {
Write-Host "[setup] Installing electron deps..."
Push-Location $ElectronDir
try { & npm install } finally { Pop-Location }
}
# --- Process tracking + cleanup ---
$script:childPids = New-Object System.Collections.ArrayList
function Stop-Tree($processId, $label) {
if (-not $processId) { return }
try {
& taskkill /PID $processId /T /F 2>$null | Out-Null
Write-Host " killed $label (pid $processId)" -ForegroundColor DarkGray
} catch {}
}
function Cleanup-All {
Write-Host ""
Write-Host "Shutting down all services..." -ForegroundColor Yellow
foreach ($entry in $script:childPids) {
Stop-Tree $entry.Pid $entry.Label
}
Write-Host "All services stopped." -ForegroundColor Green
}
try {
# --- Start backend (NoNewWindow so logs interleave into this terminal) ---
Write-Host ""
Write-Host "[backend] Starting uvicorn --reload on http://localhost:8324 ..." -ForegroundColor Blue
$backend = Start-Process -PassThru -NoNewWindow `
-FilePath $VenvPy `
-WorkingDirectory $ScriptDir `
-ArgumentList @(
'-m', 'uvicorn', 'backend.main:app',
'--host', '0.0.0.0', '--port', '8324', '--reload',
'--reload-dir', (Join-Path $ScriptDir 'backend'),
'--reload-exclude', '*.pyc'
)
[void]$script:childPids.Add(@{ Pid = $backend.Id; Label = 'backend' })
Write-Host "Waiting for backend (max 90s)..." -ForegroundColor Yellow
$deadline = (Get-Date).AddSeconds(90)
$ready = $false
while ((Get-Date) -lt $deadline) {
if ($backend.HasExited) { throw "Backend exited prematurely (code $($backend.ExitCode))" }
try {
Invoke-WebRequest -Uri 'http://localhost:8324/api/health/check' -UseBasicParsing -TimeoutSec 1 -ErrorAction Stop | Out-Null
$ready = $true
break
} catch {}
Start-Sleep -Milliseconds 1500
}
if (-not $ready) { throw "Backend did not become ready within 90s" }
Write-Host "Backend ready." -ForegroundColor Green
# --- Start frontend ---
Write-Host ""
Write-Host "[frontend] Starting webpack dev server on http://localhost:3000 ..." -ForegroundColor Green
$frontend = Start-Process -PassThru -NoNewWindow `
-FilePath 'npm.cmd' `
-WorkingDirectory $FrontendDir `
-ArgumentList @('run', 'dev')
[void]$script:childPids.Add(@{ Pid = $frontend.Id; Label = 'frontend' })
Write-Host "Waiting for frontend (max 90s)..." -ForegroundColor Yellow
$deadline = (Get-Date).AddSeconds(90)
$ready = $false
while ((Get-Date) -lt $deadline) {
if ($frontend.HasExited) { throw "Frontend exited prematurely (code $($frontend.ExitCode))" }
try {
Invoke-WebRequest -Uri 'http://localhost:3000/' -UseBasicParsing -TimeoutSec 1 -ErrorAction Stop | Out-Null
$ready = $true
break
} catch {}
Start-Sleep -Milliseconds 1500
}
if (-not $ready) { throw "Frontend did not become ready within 90s" }
Write-Host "Frontend ready." -ForegroundColor Green
# --- Start electron in dev mode (npm run dev = cross-env ELECTRON_DEV=1 electron .) ---
Write-Host ""
Write-Host "[electron] Launching dev shell..." -ForegroundColor Magenta
$electron = Start-Process -PassThru -NoNewWindow `
-FilePath 'npm.cmd' `
-WorkingDirectory $ElectronDir `
-ArgumentList @('run', 'dev')
[void]$script:childPids.Add(@{ Pid = $electron.Id; Label = 'electron' })
Write-Host ""
Write-Host "All services running. Press Ctrl+C to stop." -ForegroundColor Cyan
Write-Host " Backend: http://localhost:8324" -ForegroundColor Blue
Write-Host " Frontend: http://localhost:3000" -ForegroundColor Green
Write-Host " Electron: dev shell (pid $($electron.Id))" -ForegroundColor Magenta
Write-Host ""
# --- Supervise: if any dies, tear down all ---
while ($true) {
Start-Sleep -Seconds 3
if ($backend.HasExited) {
Write-Host "[backend] exited unexpectedly (code $($backend.ExitCode)). Tearing down..." -ForegroundColor Red
break
}
if ($frontend.HasExited) {
Write-Host "[frontend] exited unexpectedly (code $($frontend.ExitCode)). Tearing down..." -ForegroundColor Red
break
}
if ($electron.HasExited) {
Write-Host "[electron] exited (normal close). Tearing down..." -ForegroundColor Yellow
break
}
}
} finally {
Cleanup-All
}