<# .SYNOPSIS Finds regenerable build folders in all projects and (optionally) deletes them. .DESCRIPTION Default: only lists what it found (dry run). Nothing is deleted without -Apply. Tiers: Cache - always regenerable: node_modules, Unity Library/Temp, .NET bin/obj, Gradle/CMake/Flutter/PyInstaller build, .gradle, __pycache__ Release - finished builds: dist, builds (APKs, exe, zip). Rebuildable, but they are versions. Output - raw ComfyUI images in generator output\ folders (adopted assets are already in the project). Safety: - A folder is only a candidate when its parent proves the type (package.json, Assets+ProjectSettings, *.csproj/*.sln, build.gradle, CMakeLists.txt, pubspec.yaml, *.spec). - A folder that contains any git-tracked file is skipped. - _Tools is never touched. .EXAMPLE .\cleanup.ps1 # list Cache folders .\cleanup.ps1 -Tier Cache,Release # list Cache + Release .\cleanup.ps1 -Apply # delete Cache folders .\cleanup.ps1 -Project GallerySwiper -Apply .\cleanup.ps1 -Project GallerySwiper -Tier Cache,Output -Json # machine-readable (used by project-dashboard) .\cleanup.ps1 -Project X -Tier Cache -Only 'X\node_modules|X\build' -Apply -Json Lists (-Tier, -Project, -Only) may also be one string separated by ',' or '|', because powershell.exe -File cannot pass arrays. #> param( [string]$Root = 'D:\Oculusquest\Projekt', [string[]]$Tier = @('Cache'), [string[]]$Project, [string]$Only = '', # restrict -Apply to these paths (relative to Root, as listed) [switch]$Apply, [switch]$Json, [int]$MaxDepth = 8 ) $ErrorActionPreference = 'Stop' if ($Json) { [Console]::OutputEncoding = [Text.Encoding]::UTF8 } $Tier = @($Tier | ForEach-Object { $_ -split '[,|]' } | ForEach-Object { $_.Trim() } | Where-Object { $_ }) foreach ($t in $Tier) { if (@('Cache', 'Release', 'Output') -notcontains $t) { throw "Unknown tier '$t' (Cache, Release, Output)" } } $Project = @($Project | ForEach-Object { $_ -split '\|' } | ForEach-Object { $_.Trim() } | Where-Object { $_ }) $OnlyList = @($Only -split '\|' | ForEach-Object { $_.Trim() } | Where-Object { $_ }) function Test-Any($dir, [string[]]$patterns) { foreach ($p in $patterns) { if (Get-ChildItem -LiteralPath $dir -Filter $p -Force -ErrorAction SilentlyContinue | Select-Object -First 1) { return $true } } return $false } # Returns the tier for a folder, or $null when it is not a candidate. function Get-Tier([IO.DirectoryInfo]$d) { $p = $d.Parent.FullName switch -Regex ($d.Name) { '^node_modules$' { if (Test-Path -LiteralPath "$p\package.json") { return 'Cache' } } '^(Library|Temp)$' { if ((Test-Path -LiteralPath "$p\Assets") -and (Test-Path -LiteralPath "$p\ProjectSettings")) { return 'Cache' } } '^(bin|obj)$' { if (Test-Any $p @('*.csproj', '*.fsproj', '*.vbproj', '*.sln')) { return 'Cache' } } '^build$' { if (Test-Any $p @('build.gradle', 'build.gradle.kts', 'CMakeLists.txt', 'pubspec.yaml', '*.spec')) { return 'Cache' } if ((Test-Path -LiteralPath "$p\Assets") -and (Test-Path -LiteralPath "$p\ProjectSettings")) { return 'Release' } } '^Build$' { if ((Test-Path -LiteralPath "$p\Assets") -and (Test-Path -LiteralPath "$p\ProjectSettings")) { return 'Release' } } '^\.gradle$' { return 'Cache' } '^__pycache__$' { return 'Cache' } '^(dist|builds)$' { return 'Release' } '^output$' { if ((Test-Path -LiteralPath "$p\config.py") -or (Test-Path -LiteralPath "$p\gen.cmd") -or (Test-Any $p @('gen_*.py'))) { return 'Output' } } } return $null } function Get-RepoRoot([string]$path) { $cur = $path while ($cur -and $cur.Length -gt $Root.Length) { if (Test-Path -LiteralPath "$cur\.git") { return $cur } $cur = Split-Path $cur -Parent } return $null } function Test-Tracked([string]$path) { $repo = Get-RepoRoot (Split-Path $path -Parent) if (-not $repo) { return $false } $rel = $path.Substring($repo.Length + 1) $hit = git -C $repo ls-files -- $rel 2>$null | Select-Object -First 1 return [bool]$hit } function Get-SizeBytes([string]$path) { # robocopy /L counts fast and copes with long paths $out = robocopy $path "$env:TEMP\__nul__" /L /S /NJH /BYTES /FP /NC /NDL /NFL /XJ /R:0 /W:0 2>$null $line = $out | Where-Object { $_ -match '^\s*Bytes\s*:' } | Select-Object -First 1 if ($line -match ':\s*(\d+)') { return [int64]$Matches[1] } return [int64]0 } $found = New-Object System.Collections.Generic.List[object] function Walk([IO.DirectoryInfo]$dir, [int]$depth) { if ($depth -gt $MaxDepth) { return } foreach ($d in (Get-ChildItem -LiteralPath $dir.FullName -Directory -Force -ErrorAction SilentlyContinue)) { if ($d.Name -match '^(\.git|\.venv.*|venv|env)$' -or ($d.Attributes -band [IO.FileAttributes]::ReparsePoint)) { continue } $t = Get-Tier $d if ($t) { $found.Add([pscustomobject]@{ Tier = $t; Dir = $d }) continue # never descend into a candidate } Walk $d ($depth + 1) } } $projects = Get-ChildItem -LiteralPath $Root -Directory -Force | Where-Object { $_.Name -ne '_Tools' } if ($Project) { $projects = $projects | Where-Object { $Project -contains $_.Name } } foreach ($p in $projects) { Walk $p 1 } $rows = foreach ($f in $found) { if ($Tier -notcontains $f.Tier) { continue } $path = $f.Dir.FullName $tracked = Test-Tracked $path [pscustomobject]@{ Tier = $f.Tier MB = [math]::Round((Get-SizeBytes $path) / 1MB) Skip = if ($tracked) { 'git-tracked' } else { '' } Path = $path.Substring($Root.Length + 1) } } $rows = @($rows | Sort-Object Tier, @{ e = 'MB'; Descending = $true }) $todo = @($rows | Where-Object { -not $_.Skip }) if ($OnlyList.Count) { $todo = @($todo | Where-Object { $OnlyList -contains $_.Path }) } if (-not $Json) { $big = @($rows | Where-Object { $_.MB -ge 1 }) $big | Format-Table -AutoSize | Out-String -Width 220 | Write-Host if ($rows.Count -gt $big.Count) { Write-Host ("+ {0} small folders under 1 MB (e.g. __pycache__)" -f ($rows.Count - $big.Count)) } foreach ($t in $Tier) { $gb = [math]::Round((($todo | Where-Object Tier -eq $t | Measure-Object MB -Sum).Sum) / 1024, 2) Write-Host ("{0,-8} {1,7} GB" -f $t, $gb) } Write-Host ("{0} folders deletable, {1} skipped as git-tracked" -f $todo.Count, ($rows.Count - $todo.Count)) } if (-not $Apply) { if ($Json) { ConvertTo-Json -InputObject @($rows) -Compress } else { Write-Host 'Dry run. Add -Apply to delete.' } exit 0 } $freeBefore = (Get-PSDrive ($Root.Substring(0, 1))).Free $deleted = @(); $failed = @() foreach ($r in $todo) { $full = Join-Path $Root $r.Path if (-not $Json) { Write-Host "delete $($r.Path)" } # rd with \\?\ prefix handles paths longer than 260 chars (node_modules) # errors (locked files) go to nul; the Test-Path below reports leftovers and the loop continues cmd /c "rd /s /q `"\\?\$full`" 2>nul" if (Test-Path -LiteralPath $full) { $failed += $r.Path if (-not $Json) { Write-Warning "not fully deleted: $($r.Path)" } } else { $deleted += $r.Path } } $freeAfter = (Get-PSDrive ($Root.Substring(0, 1))).Free $freedGB = [math]::Round(($freeAfter - $freeBefore) / 1GB, 2) if ($Json) { ConvertTo-Json -InputObject @{ deleted = @($deleted); failed = @($failed); freedGB = $freedGB } -Compress } else { Write-Host ("Freed {0} GB" -f $freedGB) } exit 0