Repair scratch setup preflight for repo-local host processes

This commit is contained in:
master
2026-03-11 21:19:25 +02:00
parent 4a84f901ab
commit 08006100a5
7 changed files with 221 additions and 6 deletions

View File

@@ -21,6 +21,7 @@
[CmdletBinding()]
param(
[switch]$Test,
[switch]$StopRepoHostProcesses,
[ValidateSet('Debug', 'Release')]
[string]$Configuration = 'Debug'
)
@@ -49,6 +50,103 @@ function Get-RepoRelativePath {
return $normalizedPath
}
function Test-RepoOwnedText {
param(
[Parameter(Mandatory = $true)]
[string]$Root,
[AllowNull()]
[string]$Value
)
if ([string]::IsNullOrWhiteSpace($Value)) {
return $false
}
$normalizedRoot = [System.IO.Path]::GetFullPath($Root).TrimEnd('\', '/')
$normalizedValue = $Value.Replace('/', '\')
return $normalizedValue.IndexOf($normalizedRoot, [System.StringComparison]::OrdinalIgnoreCase) -ge 0
}
function Test-IsWindowsPlatform {
return [System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform(
[System.Runtime.InteropServices.OSPlatform]::Windows)
}
function Stop-RepoHostProcesses {
param(
[Parameter(Mandatory = $true)]
[string]$Root
)
if (-not (Test-IsWindowsPlatform)) {
return
}
$candidates = @(Get-CimInstance Win32_Process -Filter "Name = 'dotnet.exe' OR Name LIKE 'StellaOps.%'")
$staleProcesses = @()
foreach ($candidate in $candidates) {
if ($candidate.ProcessId -eq $PID) {
continue
}
$executablePath = "$($candidate.ExecutablePath)"
$commandLine = "$($candidate.CommandLine)"
$name = "$($candidate.Name)"
$repoOwned = (Test-RepoOwnedText -Root $Root -Value $executablePath) -or
(Test-RepoOwnedText -Root $Root -Value $commandLine)
if (-not $repoOwned) {
continue
}
$looksLikeService = $name -like 'StellaOps.*' -or $commandLine -match 'StellaOps\.[A-Za-z0-9_.-]+'
if (-not $looksLikeService) {
continue
}
$staleProcesses += [pscustomobject]@{
ProcessId = $candidate.ProcessId
Name = $name
ExecutablePath = $executablePath
CommandLine = $commandLine
}
}
$staleProcesses = @($staleProcesses | Sort-Object ProcessId -Unique)
if ($staleProcesses.Count -eq 0) {
Write-Host 'No repo-local Stella host processes detected.' -ForegroundColor DarkGray
return
}
Write-Host "Stopping $($staleProcesses.Count) repo-local Stella host process(es) before build." -ForegroundColor Yellow
foreach ($stale in $staleProcesses) {
$location = if (-not [string]::IsNullOrWhiteSpace($stale.ExecutablePath)) {
Get-RepoRelativePath -Root $Root -Path $stale.ExecutablePath
} else {
$stale.CommandLine
}
Write-Host " - [$($stale.ProcessId)] $($stale.Name) :: $location" -ForegroundColor DarkYellow
Stop-Process -Id $stale.ProcessId -Force -ErrorAction Stop
}
Start-Sleep -Seconds 1
$remaining = @($staleProcesses | Where-Object { Get-Process -Id $_.ProcessId -ErrorAction SilentlyContinue })
if ($remaining.Count -gt 0) {
$remainingIds = ($remaining | ForEach-Object { $_.ProcessId }) -join ', '
throw "Failed to stop repo-local Stella host processes: $remainingIds"
}
Write-Host 'Repo-local Stella host processes stopped.' -ForegroundColor Green
}
if ($StopRepoHostProcesses) {
Stop-RepoHostProcesses -Root $repoRoot
}
$solutions = Get-ChildItem -Path $srcDir -Filter '*.sln' -Recurse |
Where-Object {
$_.Name -ne 'StellaOps.sln' -and

View File

@@ -14,17 +14,60 @@ SRC_DIR="$REPO_ROOT/src"
RUN_TESTS=false
CONFIGURATION="Debug"
STOP_REPO_HOST_PROCESSES=false
while [[ $# -gt 0 ]]; do
case "$1" in
--test|-t) RUN_TESTS=true; shift ;;
--stop-repo-host-processes) STOP_REPO_HOST_PROCESSES=true; shift ;;
--configuration|-c) CONFIGURATION="$2"; shift 2 ;;
*) echo "Unknown option: $1" >&2; exit 1 ;;
esac
done
stop_repo_host_processes() {
local found=0
while IFS= read -r line; do
[[ -z "$line" ]] && continue
local pid="${line%% *}"
local cmd="${line#* }"
[[ "$cmd" != *"$REPO_ROOT"* ]] && continue
[[ "$cmd" != *"StellaOps."* ]] && continue
[[ "$pid" == "$$" ]] && continue
if (( found == 0 )); then
echo "Stopping repo-local Stella host processes before build."
fi
echo " - [$pid] $cmd"
kill "$pid" 2>/dev/null || true
sleep 1
if kill -0 "$pid" 2>/dev/null; then
kill -9 "$pid" 2>/dev/null || true
fi
found=1
done < <(ps -eo pid=,args=)
if (( found == 0 )); then
echo "No repo-local Stella host processes detected."
else
echo "Repo-local Stella host processes stopped."
fi
}
if $STOP_REPO_HOST_PROCESSES; then
stop_repo_host_processes
fi
# Discover solutions (exclude root StellaOps.sln)
mapfile -t SOLUTIONS < <(find "$SRC_DIR" -name '*.sln' ! -name 'StellaOps.sln' | sort)
mapfile -t SOLUTIONS < <(
find "$SRC_DIR" \
\( -path '*/node_modules/*' -o -path '*/bin/*' -o -path '*/obj/*' \) -prune -o \
-name '*.sln' ! -name 'StellaOps.sln' -print | sort
)
if [[ ${#SOLUTIONS[@]} -eq 0 ]]; then
echo "ERROR: No solution files found under src/." >&2

View File

@@ -415,7 +415,7 @@ function Build-Solutions {
Write-Step 'Building all .NET solutions'
$buildScript = Join-Path $Root 'scripts/build-all-solutions.ps1'
if (Test-Path $buildScript) {
& $buildScript
& $buildScript -StopRepoHostProcesses
if ($LASTEXITCODE -ne 0) {
Write-Fail '.NET solution build failed.'
exit 1

View File

@@ -294,10 +294,10 @@ build_solutions() {
step 'Building all .NET solutions'
local script="${ROOT}/scripts/build-all-solutions.sh"
if [[ -x "$script" ]]; then
"$script"
"$script" --stop-repo-host-processes
ok '.NET solutions built successfully'
elif [[ -f "$script" ]]; then
bash "$script"
bash "$script" --stop-repo-host-processes
ok '.NET solutions built successfully'
else
warn "Build script not found at $script. Skipping .NET build."