Files
OrcaSlicer/scripts/prune_cached_presets.ps1
T
HanifKoh 7436883c19 Prune the Cached Preset JSONs Once for All Windows Targets (#16926)
* Prune the Cached Preset JSONs Once for All Windows Targets

build_preset_cache.bat pruned each target on its own, reading every
vendor's preset list from the checkout's <vendor>.json. CI names the
checkout as the first target, and pruning it deletes those files, so the
install tree that the portable zip and the MSIX are packed from only
lost the 71 vendor JSONs and kept all 14,600 preset JSONs next to the
caches. The prune also started one PowerShell per vendor and deleted
file by file, which took 28 minutes on the Windows arm64 runner against
under a minute on x64, and set the wall clock of every PR.

The pruning moves to prune_cached_presets.ps1, run once for all targets:
it reads every vendor profile before touching any target and deletes in
a single process, printing how long each target took. The batch script
only validates the targets, installs the caches and queues them.

A test suite exercises the script on a made-up profiles tree, including
the source-then-install-tree order CI uses, and runs in the Windows build
script tests job.

* Resolve the Prune Script Next to the Repo Root, Not %0

The prune label runs after the argument loop has shifted %0 along with
the rest, so %~dp0 named the last target's directory and PowerShell
could not find the script. REPO_ROOT was captured before any shift.

* Read the Preset List From the Target's Own Profile in the Shell Script

The shell script read each vendor's preset list from the source profiles,
which is gone once an earlier --prune-source target has been pruned. No
caller passes the source as a target today, so this is the same latent
dependency the batch file had, removed before it bites.

Ported from https://github.com/OrcaSlicer/OrcaSlicer/pull/16975.

---------

Co-authored-by: Lam Wei Lun <weilun.lam@gmail.com>
(cherry picked from commit 541ff1da58)
2026-11-01 09:53:53 +08:00

85 lines
3.7 KiB
PowerShell

<#
.SYNOPSIS
Deletes the preset JSONs the vendor caches replace from profiles directories about to ship.
.DESCRIPTION
build_preset_cache.bat runs this once, after generate_system_cache wrote one <vendor>.opc per
vendor into the source profiles directory, naming every directory to make ship-ready. Only a
vendor that has a cache is pruned, and only the preset files its vendor profile lists: data
files beside them (BBL's filaments_color_codes.json, cli_config.json, ...) are read at runtime
and stay, as do directories still holding covers, meshes or those data files. The vendor's own
<vendor>.json goes too: the cache carries the vendor profile and the version it was built at.
Every vendor profile is read from the source before any target is touched. A target may be the
source directory itself, and pruning it deletes the vendor profiles, so reading them first is
what keeps the lists intact for the targets after it. One process does all the deleting, which
keeps this fast on a runner where starting PowerShell or deleting a file is slow.
.PARAMETER Profiles
The profiles directory the caches were generated into.
.PARAMETER Targets
The profiles directories to prune. Each already holds the caches.
.EXAMPLE
powershell -File scripts\prune_cached_presets.ps1 -Profiles resources\profiles build\OrcaSlicer\resources\profiles
#>
[CmdletBinding()]
param(
[Parameter(Mandatory)] [string] $Profiles,
[Parameter(Mandatory, ValueFromRemainingArguments)] [string[]] $Targets
)
$ErrorActionPreference = 'Stop'
$Profiles = [IO.Path]::GetFullPath($Profiles)
$caches = @(Get-ChildItem -LiteralPath $Profiles -Filter *.opc -File)
if ($caches.Count -eq 0) { throw "no .opc caches in $Profiles" }
# vendor -> the preset paths its profile lists, relative to the vendor directory
$listed = @{}
foreach ($cache in $caches) {
$vendor = $cache.BaseName
$profile = Join-Path $Profiles "$vendor.json"
if (-not [IO.File]::Exists($profile)) { throw "no $vendor.json beside $vendor.opc in $Profiles" }
$paths = @()
$json = ConvertFrom-Json ([IO.File]::ReadAllText($profile))
foreach ($list in 'machine_model_list', 'process_list', 'filament_list', 'machine_list') {
foreach ($entry in $json.$list) {
if ($entry.sub_path) { $paths += $entry.sub_path }
}
}
$listed[$vendor] = $paths
}
foreach ($target in $Targets) {
$watch = [Diagnostics.Stopwatch]::StartNew()
$target = [IO.Path]::GetFullPath($target)
if (-not [IO.Directory]::Exists($target)) { throw "profiles directory not found: $target" }
$pruned = 0
foreach ($vendor in $listed.Keys) {
$dir = Join-Path $target $vendor
if ([IO.Directory]::Exists($dir)) {
foreach ($sub in $listed[$vendor]) {
$file = Join-Path $dir $sub
if ([IO.File]::Exists($file)) {
[IO.File]::Delete($file)
$pruned++
}
}
# Deepest first, so a directory the deletes above emptied goes too.
$dirs = @([IO.Directory]::GetDirectories($dir, '*', [IO.SearchOption]::AllDirectories))
foreach ($candidate in ($dirs | Sort-Object Length -Descending)) {
if (-not [IO.Directory]::EnumerateFileSystemEntries($candidate).GetEnumerator().MoveNext()) {
[IO.Directory]::Delete($candidate)
}
}
}
$profile = Join-Path $target "$vendor.json"
if ([IO.File]::Exists($profile)) {
[IO.File]::Delete($profile)
$pruned++
}
}
'{0}: {1} caches, dropped {2} preset JSONs in {3:n1} s' -f $target, $caches.Count, $pruned, $watch.Elapsed.TotalSeconds
}