param([Parameter(Mandatory = $true)][string]$KeepVersion) $ErrorActionPreference = 'Stop' if ($KeepVersion -notmatch '^\d+\.\d+\.\d+$') { throw 'Invalid version' } $root = (Resolve-Path (Join-Path $PSScriptRoot '..')).Path $dist = [IO.Path]::GetFullPath((Join-Path $root 'dist')) $keep = [IO.Path]::GetFullPath((Join-Path $dist "releases/v$KeepVersion")) foreach ($name in @("TTSTDConnect-$KeepVersion-windows-x64-setup.exe", "TTSTDConnect-Receiver-$KeepVersion-neutral.apk", "TTSTDConnect-Receiver-$KeepVersion-aosp.apk", "TTSTDConnect-Receiver-$KeepVersion-zhanxun.apk", 'SHA256SUMS')) { if (-not (Test-Path -LiteralPath (Join-Path $keep $name))) { throw "Latest release is incomplete: $name" } } function Remove-OwnedArtifact([string]$Path) { if (-not (Test-Path -LiteralPath $Path)) { return } $resolved = (Resolve-Path -LiteralPath $Path).Path if (-not $resolved.StartsWith($dist + '\', [StringComparison]::OrdinalIgnoreCase) -or $resolved -eq $keep -or $resolved.StartsWith($keep + '\', [StringComparison]::OrdinalIgnoreCase)) { throw "Unsafe cleanup target: $resolved" } $item = Get-Item -LiteralPath $resolved if ($item.PSIsContainer) { $contents = @(Get-ChildItem -LiteralPath $resolved -Recurse -Force) if ($item.Attributes -band [IO.FileAttributes]::ReparsePoint) { throw "Refusing a linked cleanup root: $resolved" } foreach ($link in ($contents | Where-Object { $_.Attributes -band [IO.FileAttributes]::ReparsePoint })) { $linkPath = [IO.Path]::GetFullPath($link.FullName) if (-not $linkPath.StartsWith($resolved + '\', [StringComparison]::OrdinalIgnoreCase)) { throw "Unsafe link: $linkPath" } # Unlink the entry itself without recursion; never traverse its target. Remove-Item -LiteralPath $linkPath -Force } # Preserve diagnostics and local app settings that accidentally landed in a bundle. foreach ($file in ($contents | Where-Object { -not $_.PSIsContainer -and -not ($_.Attributes -band [IO.FileAttributes]::ReparsePoint) -and ($_.Extension -eq '.log' -or $_.Name -like 'ttstd_*.json') })) { $relative = $file.FullName.Substring($dist.Length + 1) $saved = Join-Path $dist "validation/retained-artifact-data/$relative" New-Item -ItemType Directory -Force -Path (Split-Path $saved) | Out-Null Copy-Item -LiteralPath $file.FullName -Destination $saved -Force } } Write-Output "Removing old artifact: $resolved" Remove-Item -LiteralPath $resolved -Recurse -Force } Get-ChildItem -LiteralPath (Join-Path $dist 'releases') -Directory | Where-Object { $_.FullName -ne $keep } | ForEach-Object { Remove-OwnedArtifact $_.FullName } $windows = Join-Path $dist 'windows' if (Test-Path -LiteralPath $windows) { Get-ChildItem -LiteralPath $windows -Directory -Filter 'stage-*' | ForEach-Object { Remove-OwnedArtifact $_.FullName } Get-ChildItem -LiteralPath $windows -File -Filter 'TTSTDConnect-*-setup.exe' | Where-Object { $_.Name -ne "TTSTDConnect-$KeepVersion-windows-x64-setup.exe" } | ForEach-Object { Remove-OwnedArtifact $_.FullName } } foreach ($name in @('TTSTDConnect-Windows', 'TTSTDConnect-Android', 'linux', 'linux-signaling')) { Remove-OwnedArtifact (Join-Path $dist $name) } Get-ChildItem -LiteralPath $dist -File | Where-Object { $_.Name -match '^(ttstdconnect[-_].*\.(deb|rpm)|TTSTDConnect-.*\.(AppImage|apk|exe|zip)|SHA256SUMS)$' } | ForEach-Object { Remove-OwnedArtifact $_.FullName }