Files
codex-console/scripts/make_windows_test_bundle.ps1
T

246 lines
6.6 KiB
PowerShell

param(
[string]$TargetDir = "",
[string]$ListenHost = "127.0.0.1",
[int]$Port = 1455,
[string]$AccessPassword = "admin123"
)
$ErrorActionPreference = "Stop"
function Write-AsciiCrlfFile {
param(
[Parameter(Mandatory = $true)][string]$Path,
[Parameter(Mandatory = $true)][string]$Content
)
$normalized = [regex]::Replace($Content, "`r?`n", "`r`n")
[System.IO.File]::WriteAllText($Path, $normalized, [System.Text.Encoding]::ASCII)
}
$RepoRoot = Split-Path -Parent $PSScriptRoot
if (-not $TargetDir) {
$TargetDir = Join-Path (Split-Path -Parent $RepoRoot) "codex-console2-win-test"
}
Write-Host "[bundle] repo: $RepoRoot"
Write-Host "[bundle] target: $TargetDir"
New-Item -ItemType Directory -Path $TargetDir -Force | Out-Null
$distDir = Join-Path $RepoRoot "dist"
$exeCandidates = @()
if (Test-Path $distDir) {
$exeCandidates += Get-ChildItem -Path $distDir -Filter "codex-console-windows-*.exe" -File -ErrorAction SilentlyContinue
$exeCandidates += Get-ChildItem -Path $distDir -Filter "codex-console.exe" -File -ErrorAction SilentlyContinue
}
$mode = "source"
if ($exeCandidates.Count -gt 0) {
$latestExe = $exeCandidates | Sort-Object LastWriteTime -Descending | Select-Object -First 1
Copy-Item $latestExe.FullName (Join-Path $TargetDir "codex-console.exe") -Force
$mode = "exe"
Write-Host "[bundle] use exe: $($latestExe.Name)"
}
else {
Write-Host "[bundle] exe not found, fallback to source mode."
$copyItems = @(
"webui.py",
"requirements.txt",
"pyproject.toml",
".env.example",
"templates",
"static",
"src"
)
foreach ($item in $copyItems) {
$srcPath = Join-Path $RepoRoot $item
if (Test-Path $srcPath) {
$dstPath = Join-Path $TargetDir $item
if (Test-Path $dstPath) {
Remove-Item -Path $dstPath -Recurse -Force
}
Copy-Item $srcPath $dstPath -Recurse -Force
}
}
}
$envFile = Join-Path $TargetDir ".env"
if (-not (Test-Path $envFile)) {
$envLines = @(
"# Generated by scripts/make_windows_test_bundle.ps1",
"WEBUI_HOST=$ListenHost",
"WEBUI_PORT=$Port",
"WEBUI_ACCESS_PASSWORD=$AccessPassword",
"DEBUG=false",
"LOG_LEVEL=INFO"
)
Set-Content -Path $envFile -Value $envLines -Encoding UTF8
}
$startScript = @"
@echo off
setlocal
cd /d "%~dp0"
set "WEBUI_HOST=$ListenHost"
set "WEBUI_PORT=$Port"
set "WEBUI_ACCESS_PASSWORD=$AccessPassword"
set "LAUNCHER_LOG=%~dp0launcher-debug.log"
set "EXIT_CODE=0"
set "PY_EXE="
set "PY_ARGS="
> "%LAUNCHER_LOG%" echo launcher start: %DATE% %TIME%
>> "%LAUNCHER_LOG%" echo cwd: %CD%
echo ===================================================
echo codex-console2 Windows test launcher
echo Host: %WEBUI_HOST% Port: %WEBUI_PORT%
echo ===================================================
if exist "codex-console.exe" goto RUN_EXE
goto RUN_SOURCE
:RUN_EXE
echo [mode] exe
>> "%LAUNCHER_LOG%" echo [mode] exe
start "" "http://%WEBUI_HOST%:%WEBUI_PORT%"
codex-console.exe --host %WEBUI_HOST% --port %WEBUI_PORT% --access-password %WEBUI_ACCESS_PASSWORD% >> "%LAUNCHER_LOG%" 2>&1
set "EXIT_CODE=%ERRORLEVEL%"
if not "%EXIT_CODE%"=="0" (
echo.
echo codex-console.exe exited with code %EXIT_CODE%
goto END
)
echo.
echo codex-console.exe exited normally.
goto END
:RUN_SOURCE
echo [mode] source
>> "%LAUNCHER_LOG%" echo [mode] source
if not exist "requirements.txt" (
echo requirements.txt not found in %CD%
set "EXIT_CODE=1"
goto END
)
where python >nul 2>nul
if not errorlevel 1 (
python -c "import sys" >nul 2>nul
if not errorlevel 1 (
set "PY_EXE=python"
set "PY_ARGS="
)
)
if not defined PY_EXE (
where py >nul 2>nul
if not errorlevel 1 (
py -3 -c "import sys" >nul 2>nul
if not errorlevel 1 (
set "PY_EXE=py"
set "PY_ARGS=-3"
)
)
)
if not defined PY_EXE (
echo Valid Python 3 launcher not found in PATH. Install Python 3.10+ first.
echo Download: https://www.python.org/downloads/windows/
set "EXIT_CODE=1"
goto END
)
echo python launcher: %PY_EXE% %PY_ARGS%
>> "%LAUNCHER_LOG%" echo python launcher: %PY_EXE% %PY_ARGS%
if not exist ".venv\Scripts\python.exe" (
echo creating venv...
"%PY_EXE%" %PY_ARGS% -m venv .venv >> "%LAUNCHER_LOG%" 2>&1
if errorlevel 1 (
echo failed to create venv
set "EXIT_CODE=1"
goto END
)
)
.venv\Scripts\python.exe -c "import fastapi,uvicorn,sqlalchemy,jinja2" >> "%LAUNCHER_LOG%" 2>&1
if errorlevel 1 (
echo installing dependencies...
.venv\Scripts\python.exe -m pip install --upgrade pip >> "%LAUNCHER_LOG%" 2>&1
.venv\Scripts\python.exe -m pip install -r requirements.txt >> "%LAUNCHER_LOG%" 2>&1
if errorlevel 1 (
echo failed to install dependencies
set "EXIT_CODE=1"
goto END
)
)
if not exist ".venv\.playwright_chromium_ok" (
.venv\Scripts\python.exe -c "import playwright" >nul 2>nul
if errorlevel 1 (
echo playwright package missing, installing...
.venv\Scripts\python.exe -m pip install playwright >> "%LAUNCHER_LOG%" 2>&1
)
echo installing playwright chromium ^(first run may take a while^)...
.venv\Scripts\python.exe -m playwright install chromium >> "%LAUNCHER_LOG%" 2>&1
if not errorlevel 1 (
type nul > ".venv\.playwright_chromium_ok"
)
)
start "" "http://%WEBUI_HOST%:%WEBUI_PORT%"
echo starting webui...
.venv\Scripts\python.exe webui.py --host %WEBUI_HOST% --port %WEBUI_PORT% --access-password %WEBUI_ACCESS_PASSWORD% >> "%LAUNCHER_LOG%" 2>&1
set "EXIT_CODE=%ERRORLEVEL%"
if not "%EXIT_CODE%"=="0" (
echo.
echo webui exited with code %EXIT_CODE%
echo check logs under: %CD%\logs
)
goto END
:END
echo.
echo launcher exit code: %EXIT_CODE%
echo debug log: %LAUNCHER_LOG%
if not "%EXIT_CODE%"=="0" (
echo 启动失败,请把 launcher-debug.log 发我。
)
pause
exit /b %EXIT_CODE%
"@
Write-AsciiCrlfFile -Path (Join-Path $TargetDir "start-webui.bat") -Content $startScript
$stopScript = @"
@echo off
setlocal
echo stopping codex-console.exe ...
taskkill /IM codex-console.exe /F >nul 2>nul
echo done.
"@
Write-AsciiCrlfFile -Path (Join-Path $TargetDir "stop-webui.bat") -Content $stopScript
$readme = @"
# codex-console2 Windows test bundle
Generated from: $RepoRoot
Mode: $mode
## Quick start
1. Double click `start-webui.bat`
2. Open: `http://${ListenHost}:$Port`
3. Password: $AccessPassword
## Notes
- If `codex-console.exe` exists, launcher runs exe mode.
- If exe is missing, launcher runs source mode and auto-creates `.venv`.
"@
Set-Content -Path (Join-Path $TargetDir "README-WIN-TEST.md") -Value $readme -Encoding UTF8
Write-Host ""
Write-Host "[bundle] done."
Write-Host "[bundle] mode: $mode"
Write-Host "[bundle] run: $TargetDir\start-webui.bat"