Skip to content

Commit

Permalink
feat(scoop-cache): Handle multiple apps, show more information (Scoop…
Browse files Browse the repository at this point in the history
…Installer#4738)

Co-authored-by: Hsiao-nan Cheung <niheaven@gmail.com>
  • Loading branch information
rashil2000 and niheaven authored Feb 16, 2022
1 parent 21c5c6e commit f83fa14
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 26 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
67 changes: 41 additions & 26 deletions libexec/scoop-cache.ps1
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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: <app(s)> 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: <app> 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)
}
}

Expand Down

0 comments on commit f83fa14

Please sign in to comment.