Modules/businessdev.ALbuild.Containers/Private/Remove-BcCaptureBrowserVersion.ps1
|
function Remove-BcCaptureBrowserVersion { <# .SYNOPSIS Removes capture browser versions other than the one in use, best-effort. .DESCRIPTION The capture browser is installed per Playwright version so that a pin bump lands BESIDE the old one rather than over it - which is what keeps a build that is still running against the previous version working. The cost of that isolation is a few hundred megabytes per bump, and nothing reclaims it on its own. So this removes the other version folders under the capture root, with two rules that decide whether it is safe rather than fast: * The folder to keep is never touched, and neither is anything outside the capture root. * A folder whose files are LOCKED - a capture running against the older version right now - is reported and left whole. A half-deleted browser install is worse than a stale one: the version check would still find its package.json and report ready. Best-effort throughout. Reclaiming disk is a convenience, and a failure here must never take down the install it was meant to tidy up after. .PARAMETER KeepPath The version folder to keep - normally the pinned one that was just verified. .PARAMETER CaptureRoot The folder holding the per-version installs. Default: <BaseFolder>\capture. .OUTPUTS PSCustomObject per folder considered, with Path, Removed and Reason. #> [CmdletBinding(SupportsShouldProcess)] [OutputType([PSCustomObject])] param( [Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [string] $KeepPath, [string] $CaptureRoot ) if (-not $CaptureRoot) { $CaptureRoot = Join-Path -Path (Get-ALbuildConfig -Name BaseFolder) -ChildPath 'capture' } if (-not (Test-Path -LiteralPath $CaptureRoot)) { return } $keep = [System.IO.Path]::GetFullPath($KeepPath).TrimEnd('\', '/') foreach ($candidate in @(Get-ChildItem -LiteralPath $CaptureRoot -Directory -ErrorAction SilentlyContinue)) { $full = [System.IO.Path]::GetFullPath($candidate.FullName).TrimEnd('\', '/') if ($full -eq $keep) { continue } if (-not $PSCmdlet.ShouldProcess($full, 'Remove a superseded capture browser')) { [PSCustomObject]@{ Path = $full; Removed = $false; Reason = 'skipped' } continue } try { Remove-Item -LiteralPath $full -Recurse -Force -ErrorAction Stop Write-ALbuildLog "Removed the superseded capture browser in '$full'." [PSCustomObject]@{ Path = $full; Removed = $true; Reason = 'removed' } } catch { # Almost always a running Chromium holding its own executable. Saying so is more useful # than a stack trace, and the folder is tried again on the next update. Write-ALbuildLog -Level Warning ("Left the capture browser in '$full' in place: " + "$($_.Exception.Message). It is most likely in use by a capture that is still running.") [PSCustomObject]@{ Path = $full; Removed = $false; Reason = "in use: $($_.Exception.Message)" } } } } |