diff --git a/README.md b/README.md index 66cc439..0ac5414 100644 --- a/README.md +++ b/README.md @@ -55,8 +55,8 @@ has diverged is preserved and reported as a failed self-update. Machine-scope WinGet operations may request administrator approval. Config Central preserves installer diagnostics, continues independent packages after an item failure, reports reboot requirements, and never restarts Windows. Close -a running portable application before updating it; previous versions are kept -so a failed activation does not remove the working copy. +a running portable application before updating it. The working version remains +available until its replacement is fully activated, then older versions are removed. PDF Presenter is the first portable release package. The desktop profile follows its preview channel, verifies `SHA256SUMS`, and creates a Start menu shortcut at diff --git a/TASKS.md b/TASKS.md index 87bf15e..af566e1 100644 --- a/TASKS.md +++ b/TASKS.md @@ -162,3 +162,4 @@ open until it is run on disposable machines with the required account setups. - [x] Run portable release updates before declared WinGet updates in `update-software` and document the complete workflow in the README. - [x] Refresh Config Central from an immutable remote snapshot before `update-software` applies portable and declared WinGet updates (163 Linux Pester tests pass; 5 Windows-only skips). - [x] Register PDF Presenter as a per-user `.pdf` handler and document the one-time supported Windows Default Apps selection (167 Linux Pester tests pass; Windows acceptance remains open above). +- [x] Remove superseded portable versions after successful activation while retaining the working version on failed updates and retrying interrupted cleanup (168 Linux Pester tests pass; 5 Windows-only skips). diff --git a/docs/advanced_commands.md b/docs/advanced_commands.md index 1d6e330..0f67622 100644 --- a/docs/advanced_commands.md +++ b/docs/advanced_commands.md @@ -378,8 +378,9 @@ Default files live under `%LOCALAPPDATA%\ConfigCentral\apps\idle-org\pdf-presenter`. `launch.ps1` is the stable terminal launcher; the Start menu shortcut under `Config Central\idle-org` targets `pdf-presenter.exe` directly and uses its embedded icon. `current.json` -records the active version and hashes. Older version directories are retained, -and removing the profile entry does not uninstall anything. +records the active version and hashes. A successful update removes older managed +version directories. The working version remains active when download, +verification, or setup fails. Removing the profile entry does not uninstall anything. The package also declares `displayName` and `fileAssociations`, which register PDF Presenter per user as a `.pdf` handler with Windows Default Apps. The open @@ -399,8 +400,9 @@ retry. Missing managed files are repaired. Locally changed binaries or launchers are conflicts: inspect and save those changes elsewhere, then remove the affected file to let the next apply repair it. `-ForceConfigs` does not override portable package conflicts. Never change the hash of an existing release to bypass a -checksum mismatch. Persistent application settings must live outside the version -directory; extra files in an older version are preserved but are not migrated. +checksum mismatch. Cleanup failures are warnings and are retried on the next run +without another download. Persistent application settings must live outside the +managed version directory because older versions are deleted after activation. Linux Pester tests cover archive validation, hash verification, partial failure, retry, version policies, conflict handling, launcher errors and elevation diff --git a/docs/manifest-contract.md b/docs/manifest-contract.md index f20d538..be7dc75 100644 --- a/docs/manifest-contract.md +++ b/docs/manifest-contract.md @@ -111,10 +111,12 @@ Central does not overwrite Windows' protected default-app user choice; the user selects the registered handler once in Windows Settings or Explorer. Apply downloads and verifies the pinned archive, rejects unsafe ZIP paths and -links, stages a new directory, and only then switches the receipt. Old versions -are retained. Close the application before an update; running instances block -replacement. Failures retain the previous receipt, and reruns recover incomplete -installs. A newer installed version is left alone with a warning. Changing the +links, stages a new directory, and only then switches the receipt. After +activation, older managed version directories are removed. Download, +verification, or setup failures retain the previous receipt and working version. +Cleanup failures warn and retry without downloading on the next run. Close the +application before an update; running instances block replacement. A newer +installed version is left alone with a warning. Changing the archive hash without changing its version is rejected. Removing the manifest entry does not remove files or shortcuts. Config-only runs skip these packages. diff --git a/src/PortablePackages.psm1 b/src/PortablePackages.psm1 index 5287146..eb40dc1 100644 --- a/src/PortablePackages.psm1 +++ b/src/PortablePackages.psm1 @@ -120,6 +120,28 @@ function Set-PortableFileAssociations { } } +function Get-PortablePreviousVersionPaths { + param($Paths, [Parameter(Mandatory)][string]$CurrentDirectory) + $versionsRoot = Join-Path $Paths.Root 'versions' + if (-not (Test-Path -LiteralPath $versionsRoot -PathType Container)) { return @() } + Assert-PortableNoLinks $versionsRoot + $current = [IO.Path]::GetFullPath((Join-Path $Paths.Root $CurrentDirectory)) + foreach ($directory in Get-ChildItem -LiteralPath $versionsRoot -Directory) { + if ($directory.Name -notmatch '^[0-9.]+-[a-f0-9]{32}$') { continue } + Assert-PortableNoLinks $directory.FullName + if (-not [string]::Equals([IO.Path]::GetFullPath($directory.FullName), $current, [StringComparison]::OrdinalIgnoreCase)) { + $directory.FullName + } + } +} + +function Remove-PortablePreviousVersions { + param($Paths, [Parameter(Mandatory)][string]$CurrentDirectory) + $previous = @(Get-PortablePreviousVersionPaths -Paths $Paths -CurrentDirectory $CurrentDirectory) + foreach ($path in $previous) { Remove-Item -LiteralPath $path -Recurse -Force -ErrorAction Stop } + return $previous.Count +} + function Invoke-PortableReleaseRequest { param([Parameter(Mandatory)][string]$Uri, [Parameter(Mandatory)][ValidateSet('Json', 'Text')][string]$Kind) try { $response = Invoke-WebRequest -Uri $Uri -MaximumRedirection 5 -ErrorAction Stop } @@ -238,6 +260,10 @@ function Get-PortablePackagePlan { elseif ([version]$receipt.version -gt [version]$Package.version) { $action = 'Warning'; $reason = "Installed $($receipt.version) is newer than requested $($Package.version)" } elseif ($receipt.version -eq $Package.version -and $receipt.executable -eq $Package.executable) { $action = 'None'; $reason = "Pinned version $($Package.version) is installed and verified" } else { $reason = "Move $($receipt.version) to pinned $($Package.version)" } + $previousVersions = @(Get-PortablePreviousVersionPaths -Paths $paths -CurrentDirectory $receipt.directory) + if ($action -eq 'None' -and $previousVersions.Count) { + $action = 'Cleanup'; $reason = 'Remove previous portable versions' + } } elseif (Test-Path -LiteralPath $paths.Launcher) { Assert-PortableNoLinks $paths.Launcher @@ -342,6 +368,18 @@ function Invoke-PortablePackageOperation { if ($plan.action -eq 'Conflict') { return [pscustomobject]@{ name = $plan.name; status = 'Conflict'; rebootRequired = $false; exitCode = 1; message = $plan.reason } } + if ($plan.action -eq 'Cleanup') { + $paths = Get-PortablePaths $plan.package $Operation.dataRoot + Assert-PortableStopped $paths.Root + $receipt = Get-Content -LiteralPath $paths.Receipt -Raw | ConvertFrom-Json -AsHashtable + try { + $removed = Remove-PortablePreviousVersions -Paths $paths -CurrentDirectory $receipt.directory + return [pscustomobject]@{ name = $plan.name; status = 'Succeeded'; rebootRequired = $false; exitCode = 0; message = "Removed $removed previous portable version(s)" } + } + catch { + return [pscustomobject]@{ name = $plan.name; status = 'Warning'; rebootRequired = $false; exitCode = 0; message = "Could not remove previous portable versions: $($_.Exception.Message). Rerun cc update-software to retry." } + } + } if ($plan.action -in @('None', 'Warning')) { return [pscustomobject]@{ name = $plan.name; status = $plan.action; rebootRequired = $false; exitCode = 0; message = $plan.reason } } @@ -379,8 +417,13 @@ function Invoke-PortablePackageOperation { $temporary = Join-Path $staging 'current.json' $receipt | ConvertTo-Json -Depth 8 | Set-Content -LiteralPath $temporary -Encoding utf8NoBOM [IO.File]::Move($temporary, $paths.Receipt, $true) + try { $removed = Remove-PortablePreviousVersions -Paths $paths -CurrentDirectory $directory } + catch { + return [pscustomobject]@{ name = $plan.name; status = 'Warning'; rebootRequired = $false; exitCode = 0; message = "Installed portable version $($package.version), but could not remove previous versions: $($_.Exception.Message). Rerun cc update-software to retry." } + } $associationMessage = if ($package.PSObject.Properties['fileAssociations']) { "; registered for $(@($package.fileAssociations) -join ', ') (choose it once in Windows Default Apps)" } else { '' } - [pscustomobject]@{ name = $plan.name; status = 'Succeeded'; rebootRequired = $false; exitCode = 0; message = "Installed portable version $($package.version); previous versions retained$associationMessage" } + $cleanupMessage = if ($removed) { "; removed $removed previous version(s)" } else { '' } + [pscustomobject]@{ name = $plan.name; status = 'Succeeded'; rebootRequired = $false; exitCode = 0; message = "Installed portable version $($package.version)$cleanupMessage$associationMessage" } } catch { [pscustomobject]@{ name = $Operation.name; status = 'Failed'; rebootRequired = $false; exitCode = 1; message = $_.Exception.Message } diff --git a/tests/PortablePackages.Tests.ps1 b/tests/PortablePackages.Tests.ps1 index 782ebdd..2da9221 100644 --- a/tests/PortablePackages.Tests.ps1 +++ b/tests/PortablePackages.Tests.ps1 @@ -33,16 +33,18 @@ Describe 'Portable package lifecycle' { (Invoke-PortablePackageOperation $again).status | Should -Be 'None' Should -Invoke Invoke-WebRequest -Times 1 } - It 'retains the previous version and unmanaged settings on update' { + It 'removes the previous version only after activating the update' { $paths = Get-PortablePaths $package $root (Invoke-PortablePackageOperation (Get-PortablePackagePlan $package $root)).status | Should -Be 'Succeeded' $old = Get-Content $paths.Receipt -Raw | ConvertFrom-Json $settings = Join-Path $paths.Root "$($old.directory)/settings.json" 'user settings' | Set-Content $settings + $unmanaged = Join-Path $paths.Root 'versions/unmanaged-settings' + [void][IO.Directory]::CreateDirectory($unmanaged) $package.version = '2.0' (Invoke-PortablePackageOperation (Get-PortablePackagePlan $package $root)).status | Should -Be 'Succeeded' - Test-Path (Join-Path $paths.Root "$($old.directory)/bundle/app.exe") | Should -BeTrue - Get-Content $settings | Should -Be 'user settings' + Test-Path (Join-Path $paths.Root $old.directory) | Should -BeFalse + Test-Path $unmanaged | Should -BeTrue (Get-Content $paths.Receipt -Raw | ConvertFrom-Json).version | Should -Be '2.0' } It 'repairs missing files but refuses modified files' { @@ -95,6 +97,8 @@ Describe 'Portable package lifecycle' { $result.status | Should -Be 'Failed' $result.message | Should -Match 'SHA-256' Get-Content $paths.Receipt -Raw | Should -Be $before + $old = Get-Content $paths.Receipt -Raw | ConvertFrom-Json + Test-Path (Join-Path $paths.Root $old.directory) | Should -BeTrue $package.sha256 = $hash Mock Set-PortableShortcut { throw 'Shortcut failure' } (Invoke-PortablePackageOperation (Get-PortablePackagePlan $package $root)).status | Should -Be 'Failed' @@ -105,6 +109,17 @@ Describe 'Portable package lifecycle' { } (Invoke-PortablePackageOperation (Get-PortablePackagePlan $package $root)).status | Should -Be 'Succeeded' } + It 'retries interrupted old-version cleanup without another download' { + $paths = Get-PortablePaths $package $root + [void](Invoke-PortablePackageOperation (Get-PortablePackagePlan $package $root)) + $old = Join-Path $paths.Root ('versions/0.9-' + ('a' * 32)) + [void][IO.Directory]::CreateDirectory($old) + [IO.File]::WriteAllText((Join-Path $old 'old.exe'), 'old') + (Get-PortablePackagePlan $package $root).action | Should -Be 'Cleanup' + (Invoke-PortablePackageOperation (Get-PortablePackagePlan $package $root)).status | Should -Be 'Succeeded' + Test-Path $old | Should -BeFalse + Should -Invoke Invoke-WebRequest -Times 1 -Exactly + } It 'recovers a first install interrupted after launcher creation' { Mock Set-PortableShortcut { throw 'Shortcut failure' } (Invoke-PortablePackageOperation (Get-PortablePackagePlan $package $root)).status | Should -Be 'Failed'