SSPC-Tester/run.ps1

126 lines
3.4 KiB
PowerShell
Raw Permalink Normal View History

[CmdletBinding()]
param(
[ValidateSet('Debug', 'Release')]
[string]$Configuration = 'Release',
[string]$Runtime = 'win-x64',
[bool]$SelfContained = $true,
[bool]$SingleFile = $false,
[switch]$NoBuild,
[switch]$Test,
[switch]$Seed,
[switch]$CleanSeed,
[switch]$NoLaunch,
[switch]$Wait,
[switch]$Clean
)
$ErrorActionPreference = 'Stop'
function Initialize-ConsoleUtf8 {
try {
if ($IsWindows) {
& chcp.com 65001 > $null
}
} catch {
Write-Warning "无法切换到 UTF-8 代码页,将继续使用当前代码页。"
}
$utf8NoBom = New-Object System.Text.UTF8Encoding($false)
[Console]::InputEncoding = $utf8NoBom
[Console]::OutputEncoding = $utf8NoBom
Set-Variable -Name OutputEncoding -Value $utf8NoBom -Scope Global
if ([string]::IsNullOrWhiteSpace($env:DOTNET_CLI_UI_LANGUAGE)) {
$env:DOTNET_CLI_UI_LANGUAGE = 'zh-Hans'
}
}
Initialize-ConsoleUtf8
$root = $PSScriptRoot
Set-Location $root
$sln = Join-Path $root 'SSPCTester.sln'
$uiProj = Join-Path $root 'SSPCTester.UI\SSPCTester.UI.csproj'
$testProj = Join-Path $root 'SSPCTester.Tests\SSPCTester.Tests.csproj'
$seedProj = Join-Path $root 'SSPCTester.Seed\SSPCTester.Seed.csproj'
$projectsRoot = Join-Path $env:LOCALAPPDATA 'SSPCTester\Projects'
$publishDir = Join-Path $root "publish\$Runtime"
$exe = Join-Path $publishDir 'SSPCTester.UI.exe'
function Write-Step([string]$msg) {
Write-Host ""
Write-Host "==> $msg" -ForegroundColor Cyan
}
if ($Clean -and (Test-Path $publishDir)) {
Write-Step "Clean publish dir: $publishDir"
Remove-Item -Path $publishDir -Recurse -Force
}
if (-not $NoBuild) {
Write-Step "Publish ($Configuration, $Runtime, SelfContained=$SelfContained, SingleFile=$SingleFile)"
$publishArgs = @(
'publish', $uiProj,
'-c', $Configuration,
'-r', $Runtime,
"-p:SelfContained=$($SelfContained.ToString().ToLower())",
"-p:PublishSingleFile=$($SingleFile.ToString().ToLower())",
'-p:IncludeNativeLibrariesForSelfExtract=true',
'-p:DebugType=embedded',
'-o', $publishDir,
'--nologo'
)
& dotnet @publishArgs
if ($LASTEXITCODE -ne 0) { throw "Publish failed (exit $LASTEXITCODE)." }
}
if ($Test) {
Write-Step "Test"
& dotnet test $testProj -c $Configuration --no-build --nologo
if ($LASTEXITCODE -ne 0) { throw "Tests failed (exit $LASTEXITCODE)." }
}
if ($Seed) {
if ($CleanSeed) {
Write-Step "Clean ProjectsRoot: $projectsRoot"
if (Test-Path $projectsRoot) {
Remove-Item -Path "$projectsRoot\*" -Recurse -Force
}
}
Write-Step "Seed virtual projects"
& dotnet run --project $seedProj -c $Configuration -- $projectsRoot
if ($LASTEXITCODE -ne 0) { throw "Seed failed (exit $LASTEXITCODE)." }
}
if ($NoLaunch) { return }
if (-not (Test-Path $exe)) {
throw "Executable not found: $exe"
}
Write-Step "Launch $exe"
$proc = Start-Process -FilePath $exe -WorkingDirectory $publishDir -PassThru
Start-Sleep -Seconds 2
if ($proc.HasExited) {
throw "Program started but exited immediately. ExitCode=$($proc.ExitCode)."
}
Write-Host "Started PID=$($proc.Id)" -ForegroundColor Green
if ($Wait) {
Write-Host "Waiting for process to exit (Ctrl+C to abort)..."
$proc.WaitForExit()
Write-Host "Exited with code $($proc.ExitCode)" -ForegroundColor Yellow
}