[CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Medium')] param( [switch]$Apply ) $repoRoot = [System.IO.Path]::GetFullPath((Join-Path $PSScriptRoot '..\..\..')) $outputRoot = [System.IO.Path]::GetFullPath((Join-Path $PSScriptRoot '..\out')) $allowedBuildDirectories = @('build-nmake', 'build-track-b') if (-not (Test-Path -LiteralPath $outputRoot -PathType Container)) { throw "Expected generated output directory was not found: $outputRoot" } $outputRootInfo = Get-Item -LiteralPath $outputRoot -Force if (($outputRootInfo.Attributes -band [System.IO.FileAttributes]::ReparsePoint) -ne 0) { throw "Refusing to use a reparse-point output directory: $outputRoot" } $repoPrefix = $repoRoot.TrimEnd('\') + '\' if (-not $outputRoot.StartsWith($repoPrefix, [System.StringComparison]::OrdinalIgnoreCase)) { throw "Output directory is outside the repository: $outputRoot" } $outputPrefix = $outputRoot.TrimEnd('\') + '\' $targets = [System.Collections.Generic.List[System.IO.DirectoryInfo]]::new() foreach ($directoryName in $allowedBuildDirectories) { $targetPath = [System.IO.Path]::GetFullPath((Join-Path $outputRoot $directoryName)) if (-not $targetPath.StartsWith($outputPrefix, [System.StringComparison]::OrdinalIgnoreCase)) { throw "Refusing to process a path outside the generated output directory: $targetPath" } if (-not (Test-Path -LiteralPath $targetPath -PathType Container)) { Write-Output "Skip (not present): $targetPath" continue } $targetInfo = Get-Item -LiteralPath $targetPath -Force if (($targetInfo.Attributes -band [System.IO.FileAttributes]::ReparsePoint) -ne 0) { throw "Refusing to remove a reparse-point build directory: $targetPath" } if (-not (Test-Path -LiteralPath (Join-Path $targetPath 'CMakeCache.txt') -PathType Leaf)) { throw "Refusing to remove a directory without a CMake cache: $targetPath" } $targets.Add($targetInfo) } foreach ($target in $targets) { if (-not $Apply) { Write-Output "Dry run; would remove generated CMake build directory: $($target.FullName)" continue } if ($PSCmdlet.ShouldProcess($target.FullName, 'Remove generated CMake build directory')) { Remove-Item -LiteralPath $target.FullName -Recurse -Force Write-Output "Removed: $($target.FullName)" } }