diff --git a/.github/workflows/build_all.yml b/.github/workflows/build_all.yml index 1918c009db..ac65eddf11 100644 --- a/.github/workflows/build_all.yml +++ b/.github/workflows/build_all.yml @@ -23,6 +23,8 @@ on: - 'scripts/test_check_exception_throw_info.ps1' - 'build_release_macos.sh' - 'scripts/build_preset_cache.*' + - 'scripts/prune_cached_presets.ps1' + - 'scripts/test_prune_cached_presets.ps1' - 'scripts/flatpak/**' - 'scripts/msix/**' - 'tests/**' @@ -47,6 +49,8 @@ on: - 'scripts/test_check_exception_throw_info.ps1' - 'build_release_macos.sh' - 'scripts/build_preset_cache.*' + - 'scripts/prune_cached_presets.ps1' + - 'scripts/test_prune_cached_presets.ps1' - 'scripts/flatpak/**' - 'scripts/msix/**' - 'tests/**' @@ -92,6 +96,10 @@ jobs: shell: powershell run: .\scripts\test_check_exception_throw_info.ps1 + - name: Run the preset prune tests + shell: powershell + run: .\scripts\test_prune_cached_presets.ps1 + build_linux: strategy: fail-fast: false diff --git a/scripts/build_preset_cache.bat b/scripts/build_preset_cache.bat index 3529f7591e..0bbc82d2b4 100644 --- a/scripts/build_preset_cache.bat +++ b/scripts/build_preset_cache.bat @@ -18,7 +18,8 @@ rem into (resources\profiles). Pruning it deletes the checkout's rem own preset JSONs, which is a packaging step - not something a rem build should do to a working tree by surprise. CI passes it. rem -rem Shipping deletes, so it is a CI packaging step. A vendor's own .json +rem Shipping deletes (scripts\prune_cached_presets.ps1 does them, in one run for +rem all targets), so it is a CI packaging step. A vendor's own .json rem goes along with its preset JSONs: the cache carries the vendor profile and rem the version it was built at, so discovery, version checks and installing all rem read it there. Only a vendor that has a cache is pruned, so non-vendor JSONs @@ -94,13 +95,24 @@ del /q "%PROFILES%\*.cache" 2>nul "%TOOL%" --path "%PROFILES%" --log_level 2 if errorlevel 1 exit /b %errorlevel% +set "TARGETS=" :next_target -if "%~1"=="" exit /b 0 +if "%~1"=="" goto :prune call :ship "%~1" if errorlevel 1 exit /b 1 shift goto :next_target +rem One run for every target: the vendor profiles are read before any pruning, +rem so a target that is the source does not take the preset lists away from the +rem targets after it, and one process does all the deleting. +:prune +if not defined TARGETS exit /b 0 +powershell -NoProfile -ExecutionPolicy Bypass -File "%REPO_ROOT%\scripts\prune_cached_presets.ps1" -Profiles "%PROFILES%" %TARGETS% +exit /b %errorlevel% + +rem Check a target and queue it for pruning; install the caches into it unless +rem it is the source, which only gets pruned. :ship set "TARGET=%~1" if not exist "%TARGET%\" ( @@ -108,40 +120,15 @@ if not exist "%TARGET%\" ( exit /b 1 ) for %%d in ("%TARGET%") do set "TARGET=%%~fd" +rem A trailing backslash would escape the closing quote on the PowerShell command line. +if "%TARGET:~-1%"=="\" set "TARGET=%TARGET:~0,-1%" if /i "%TARGET%"=="%PROFILES%" if not defined PRUNE_SOURCE ( echo %TARGET%: skipped - this is where the caches were generated. echo Pass --prune-source to prune it; that deletes this checkout's preset JSONs. exit /b 0 ) if /i not "%TARGET%"=="%PROFILES%" copy /y "%PROFILES%\*.opc" "%TARGET%\" >nul - -set /a SHIPPED=0 -set /a PRUNED=0 -for %%c in ("%PROFILES%\*.opc") do ( - set /a SHIPPED+=1 - set "VENDOR=%%~nc" - rem Prune the vendor directory first: it reads the profile's preset list, - rem and with --prune-source the profile is the target's own. - if exist "%TARGET%\!VENDOR!\" ( - call :prune_listed_presets "%PROFILES%\!VENDOR!.json" "%TARGET%\!VENDOR!" - rem Deepest first, so a directory the delete above emptied goes too; rd - rem refuses the ones still holding covers, meshes or data files. - for /f "delims=" %%d in ('dir /s /b /ad "%TARGET%\!VENDOR!" 2^>nul ^| sort /r') do rd "%%d" 2>nul - ) - if exist "%TARGET%\!VENDOR!.json" ( - del /q "%TARGET%\!VENDOR!.json" - set /a PRUNED+=1 - ) -) -echo %TARGET%: !SHIPPED! caches, dropped !PRUNED! preset JSONs -exit /b 0 - -rem Delete the preset files a vendor profile (%1) lists from its vendor -rem directory (%2). Data files beside them (BBL's filaments_color_codes.json, -rem cli_config.json, ...) are read at runtime, so they ship. -:prune_listed_presets -if not exist "%~1" exit /b 0 -for /f %%n in ('powershell -NoProfile -ExecutionPolicy Bypass -Command "$n = 0; $p = ConvertFrom-Json (Get-Content -LiteralPath '%~1' -Raw -Encoding UTF8); foreach ($k in 'machine_model_list','process_list','filament_list','machine_list') { foreach ($e in $p.$k) { $f = Join-Path '%~2' $e.sub_path; if (Test-Path -LiteralPath $f -PathType Leaf) { Remove-Item -LiteralPath $f; $n++ } } }; $n"') do set /a PRUNED+=%%n +set TARGETS=%TARGETS% "%TARGET%" exit /b 0 :find_tool diff --git a/scripts/build_preset_cache.sh b/scripts/build_preset_cache.sh index 265534c753..71d2c2db6c 100755 --- a/scripts/build_preset_cache.sh +++ b/scripts/build_preset_cache.sh @@ -163,9 +163,9 @@ for target in "$@"; do for cache in "$profiles_dir"/*.opc; do vendor=$(basename "$cache" .opc) shipped=$(( shipped + 1 )) - # Read the list before the profile goes: with --prune-source it is the - # same file as the target's. - listed=$(listed_presets "$profiles_dir/$vendor.json") + # Read the list from the target's own profile, before it goes: the + # source profile may already be gone when an earlier target pruned it. + listed=$(listed_presets "$resolved/$vendor.json") if [ -f "$resolved/$vendor.json" ]; then rm -f "$resolved/$vendor.json" pruned=$(( pruned + 1 )) diff --git a/scripts/prune_cached_presets.ps1 b/scripts/prune_cached_presets.ps1 new file mode 100644 index 0000000000..8a88a5f43a --- /dev/null +++ b/scripts/prune_cached_presets.ps1 @@ -0,0 +1,84 @@ +<# +.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 .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 + .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 +} diff --git a/scripts/test_prune_cached_presets.ps1 b/scripts/test_prune_cached_presets.ps1 new file mode 100644 index 0000000000..1b8fca24c8 --- /dev/null +++ b/scripts/test_prune_cached_presets.ps1 @@ -0,0 +1,116 @@ +<# +.SYNOPSIS + Tests scripts/prune_cached_presets.ps1 on a small made-up profiles tree. + +.DESCRIPTION + Each case gets a fresh tree with two cached vendors, one vendor without a cache, data files + beside the presets and a copy of it all standing in for an install tree, adjusts it with its + Before block if it has one, then runs the prune in its own powershell -File process, as + build_preset_cache.bat does, and asserts on the exit code, the output and which files are left. + +.EXAMPLE + powershell -File scripts/test_prune_cached_presets.ps1 +#> +$ErrorActionPreference = 'Stop' + +$prune = Join-Path $PSScriptRoot 'prune_cached_presets.ps1' +$work = Join-Path ([IO.Path]::GetTempPath()) 'prune_cached_presets_tests' + +# Paths are written Windows-style; the swap lets the suite also run under pwsh elsewhere. +function Write-File([string] $path, [string] $text) { + $path = $path.Replace('\', [IO.Path]::DirectorySeparatorChar) + New-Item -ItemType Directory -Force -Path (Split-Path $path) | Out-Null + Set-Content -Path $path -Encoding ascii -Value $text +} + +# A source profiles directory as generate_system_cache leaves it, and an install tree copied from it. +function New-Fixture { + Remove-Item -Recurse -Force $work -ErrorAction SilentlyContinue + $src = Join-Path $work 'src' + Write-File "$src\Acme.json" ('{"machine_model_list":[{"sub_path":"machine/Acme One.json"}],' + + '"process_list":[{"sub_path":"process/0.20mm Standard @Acme.json"}],' + + '"filament_list":[{"sub_path":"filament/Acme PLA.json"},{"sub_path":"filament/not shipped.json"}],' + + '"machine_list":[{"sub_path":"machine/Acme One 0.4 nozzle.json"}]}') + Write-File "$src\Acme.opc" 'cache' + foreach ($preset in 'machine\Acme One.json', 'machine\Acme One 0.4 nozzle.json', + 'process\0.20mm Standard @Acme.json', 'filament\Acme PLA.json') { + Write-File "$src\Acme\$preset" '{}' + } + Write-File "$src\Acme\machine\Acme One_cover.png" 'png' + Write-File "$src\Acme\filament\colors.json" '{}' + Write-File "$src\Acme\shapes\bed.stl" 'solid' + Write-File "$src\Beta.json" '{"process_list":[{"sub_path":"process/0.20mm Standard @Beta.json"}]}' + Write-File "$src\Beta.opc" 'cache' + Write-File "$src\Beta\process\0.20mm Standard @Beta.json" '{}' + Write-File "$src\Gamma.json" '{"filament_list":[{"sub_path":"filament/Gamma PLA.json"}]}' + Write-File "$src\Gamma\filament\Gamma PLA.json" '{}' + Write-File "$src\blacklist.json" '{}' + Copy-Item -Recurse $src (Join-Path $work 'install') + New-Item -ItemType Directory -Force -Path (Join-Path $work 'empty') | Out-Null +} + +$cases = @( + @{ Name = 'the source is pruned first and the install tree after it still loses its presets' + Args = @('-Profiles', '{work}\src', '{work}\src', '{work}\install'); Exit = 0 + Contains = @('{work}\src: 2 caches, dropped 7 preset JSONs', '{work}\install: 2 caches, dropped 7 preset JSONs') + NotExists = @('{work}\src\Acme.json', '{work}\src\Acme\filament\Acme PLA.json', + '{work}\install\Acme.json', '{work}\install\Acme\machine\Acme One.json', + '{work}\install\Acme\machine\Acme One 0.4 nozzle.json', + '{work}\install\Acme\process', '{work}\install\Beta.json', '{work}\install\Beta\process') + Exists = @('{work}\install\Acme.opc', '{work}\install\Acme\machine\Acme One_cover.png', + '{work}\install\Acme\filament\colors.json', '{work}\install\Acme\shapes\bed.stl', + '{work}\install\Beta', '{work}\install\Gamma.json', '{work}\install\Gamma\filament\Gamma PLA.json', + '{work}\install\blacklist.json') } + @{ Name = 'pruning only the install tree leaves the source alone' + Args = @('-Profiles', '{work}\src', '{work}\install'); Exit = 0 + Contains = @('{work}\install: 2 caches, dropped 7 preset JSONs') + NotExists = @('{work}\install\Acme.json') + Exists = @('{work}\src\Acme.json', '{work}\src\Acme\filament\Acme PLA.json') } + @{ Name = 'a missing target is named' + Args = @('-Profiles', '{work}\src', '{work}\nowhere'); Exit = 1 + Contains = @('profiles directory not found') } + @{ Name = 'a source without caches is refused' + Args = @('-Profiles', '{work}\empty', '{work}\install'); Exit = 1 + Contains = @('no .opc caches') } + @{ Name = 'a cache whose vendor profile is already gone is refused' + Before = { Remove-Item -LiteralPath (Join-Path $work 'src/Beta.json') } + Args = @('-Profiles', '{work}\src', '{work}\install'); Exit = 1 + Contains = @('no Beta.json beside Beta.opc') } +) + +function Resolve-Placeholders([string[]] $values) { + foreach ($value in $values) { $value.Replace('{work}', $work).Replace('\', [IO.Path]::DirectorySeparatorChar) } +} + +$failed = 0 +foreach ($case in $cases) { + New-Fixture + if ($case.Before) { & $case.Before } + $ErrorActionPreference = 'Continue' + $output = (& powershell -NoProfile -File $prune @(Resolve-Placeholders $case.Args) 2>&1 | Out-String) + $code = $LASTEXITCODE + $ErrorActionPreference = 'Stop' + $problems = @() + if ($code -ne $case.Exit) { $problems += "exit $code, expected $($case.Exit)" } + foreach ($text in (Resolve-Placeholders $case.Contains)) { + if (-not $output.Contains($text)) { $problems += "no '$text' in the output" } + } + foreach ($path in (Resolve-Placeholders $case.Exists)) { + if (-not (Test-Path -LiteralPath $path)) { $problems += "$path is gone" } + } + foreach ($path in (Resolve-Placeholders $case.NotExists)) { + if (Test-Path -LiteralPath $path) { $problems += "$path is still there" } + } + if ($problems) { + $failed++ + " FAIL $($case.Name)" + $problems | ForEach-Object { " $_" } + $output.TrimEnd() -split "`r?`n" | ForEach-Object { " | $_" } + } else { + " ok $($case.Name)" + } +} + +Remove-Item -Recurse -Force $work -ErrorAction SilentlyContinue +"$($cases.Count - $failed) passed, $failed failed" +exit [int]($failed -gt 0)