From f83fa145deca22db4f4dcba06f832a477d74feaf Mon Sep 17 00:00:00 2001 From: Rashil Gandhi <46838874+rashil2000@users.noreply.github.com> Date: Wed, 16 Feb 2022 10:36:23 +0530 Subject: [PATCH] feat(scoop-cache): Handle multiple apps, show more information (#4738) Co-authored-by: Hsiao-nan Cheung --- CHANGELOG.md | 1 + libexec/scoop-cache.ps1 | 67 +++++++++++++++++++++++++---------------- 2 files changed, 42 insertions(+), 26 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 26c5b07d..05e9ca31 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ - **scoop-bucket:** List more detailed information for buckets ([#4704](https://github.com/ScoopInstaller/Scoop/pull/4704)) - **scoop-list:** Allow list manipulation ([#4718](https://github.com/ScoopInstaller/Scoop/issues/4718)) - **scoop-list:** Show last-updated time [#4723](https://github.com/ScoopInstaller/Scoop/issues/4723)) +- **scoop-cache:** Handle multiple apps and show detailed information ([#4738](https://github.com/ScoopInstaller/Scoop/pull/4738)) ### Bug Fixes diff --git a/libexec/scoop-cache.ps1 b/libexec/scoop-cache.ps1 index 1c172e75..b4eccc77 100644 --- a/libexec/scoop-cache.ps1 +++ b/libexec/scoop-cache.ps1 @@ -1,4 +1,4 @@ -# Usage: scoop cache show|rm [app] +# Usage: scoop cache show|rm [app(s)] # Summary: Show or clear the download cache # Help: Scoop caches downloads so you don't need to download the same files # when you uninstall and re-install the same version of an app. @@ -10,48 +10,63 @@ # # To clear everything in your cache, use: # scoop cache rm * -param($cmd, $app) +param($cmd) -. "$psscriptroot\..\lib\help.ps1" - -reset_aliases +. "$PSScriptRoot\..\lib\help.ps1" function cacheinfo($file) { - $app, $version, $url = $file.name -split '#' - $size = filesize $file.length - return new-object psobject -prop @{ app=$app; version=$version; url=$url; size=$size } + $app, $version, $url = $file.Name -split '#' + New-Object PSObject -Property @{ Name = $app; Version = $version; Length = $file.Length; URL = $url } } -function show($app) { - $files = @(Get-ChildItem "$cachedir" | Where-Object { $_.name -match "^$app" }) - $total_length = ($files | Measure-Object length -sum).sum -as [double] +function cacheshow($app) { + if (!$app -or $app -eq '*') { + $app = '.*?' + } else { + $app = '(' + ($app -join '|') + ')' + } + $files = @(Get-ChildItem $cachedir | Where-Object -Property Name -Value "^$app#" -Match) + $totalLength = ($files | Measure-Object -Property Length -Sum).Sum - $f_app = @{ expression={"$($_.app) ($($_.version))" }} - $f_url = @{ expression={$_.url};alignment='right'} - $f_size = @{ expression={$_.size}; alignment='right'} + $files | ForEach-Object { cacheinfo $_ } | Select-Object Name, Version, Length, URL + Write-Host "Total: $($files.Length) $(pluralize $files.Length 'file' 'files'), $(filesize $totalLength)" -ForegroundColor Yellow +} - $files | ForEach-Object { cacheinfo $_ } | Format-Table $f_size, $f_app, $f_url -auto -hide +function cacheremove($app) { + if (!$app) { + 'ERROR: missing' + my_usage + exit 1 + } elseif ($app -eq '*') { + $files = @(Get-ChildItem $cachedir) + } else { + $app = '(' + ($app -join '|') + ')' + $files = @(Get-ChildItem $cachedir | Where-Object -Property Name -Value "^$app#" -Match) + } + $totalLength = ($files | Measure-Object -Property Length -Sum).Sum - "Total: $($files.length) $(pluralize $files.length 'file' 'files'), $(filesize $total_length)" + $files | ForEach-Object { + $curr = cacheinfo $_ + Write-Host "Removing $($curr.URL)..." + Remove-Item $_.FullName + if(Test-Path "$cachedir\$($curr.Name).txt") { + Remove-Item "$cachedir\$($curr.Name).txt" + } + } + + Write-Host "Deleted: $($files.Length) $(pluralize $files.Length 'file' 'files'), $(filesize $totalLength)" -ForegroundColor Yellow } switch($cmd) { 'rm' { - if(!$app) { 'ERROR: missing'; my_usage; exit 1 } - Remove-Item "$cachedir\$app#*" - if(test-path("$cachedir\$app.txt")) { - Remove-Item "$cachedir\$app.txt" - } + cacheremove $Args } 'show' { - show $app - } - '' { - show + cacheshow $Args } default { - my_usage + cacheshow (@($cmd) + $Args) } }