Skip to content

Commit 765c95e

Browse files
rashil2000niheaven
authored andcommitted
feat(scoop-cache): Handle multiple apps, show more information (ScoopInstaller#4738)
Co-authored-by: Hsiao-nan Cheung <niheaven@gmail.com>
1 parent f0dd46d commit 765c95e

2 files changed

Lines changed: 42 additions & 26 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- **scoop-bucket:** List more detailed information for buckets ([#4704](https://github.com/ScoopInstaller/Scoop/pull/4704))
1111
- **scoop-list:** Allow list manipulation ([#4718](https://github.com/ScoopInstaller/Scoop/issues/4718))
1212
- **scoop-list:** Show last-updated time [#4723](https://github.com/ScoopInstaller/Scoop/issues/4723))
13+
- **scoop-cache:** Handle multiple apps and show detailed information ([#4738](https://github.com/ScoopInstaller/Scoop/pull/4738))
1314

1415
### Bug Fixes
1516

libexec/scoop-cache.ps1

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Usage: scoop cache show|rm [app]
1+
# Usage: scoop cache show|rm [app(s)]
22
# Summary: Show or clear the download cache
33
# Help: Scoop caches downloads so you don't need to download the same files
44
# when you uninstall and re-install the same version of an app.
@@ -10,48 +10,63 @@
1010
#
1111
# To clear everything in your cache, use:
1212
# scoop cache rm *
13-
param($cmd, $app)
13+
param($cmd)
1414

15-
. "$psscriptroot\..\lib\help.ps1"
16-
17-
reset_aliases
15+
. "$PSScriptRoot\..\lib\help.ps1"
1816

1917
function cacheinfo($file) {
20-
$app, $version, $url = $file.name -split '#'
21-
$size = filesize $file.length
22-
return new-object psobject -prop @{ app=$app; version=$version; url=$url; size=$size }
18+
$app, $version, $url = $file.Name -split '#'
19+
New-Object PSObject -Property @{ Name = $app; Version = $version; Length = $file.Length; URL = $url }
2320
}
2421

25-
function show($app) {
26-
$files = @(Get-ChildItem "$cachedir" | Where-Object { $_.name -match "^$app" })
27-
$total_length = ($files | Measure-Object length -sum).sum -as [double]
22+
function cacheshow($app) {
23+
if (!$app -or $app -eq '*') {
24+
$app = '.*?'
25+
} else {
26+
$app = '(' + ($app -join '|') + ')'
27+
}
28+
$files = @(Get-ChildItem $cachedir | Where-Object -Property Name -Value "^$app#" -Match)
29+
$totalLength = ($files | Measure-Object -Property Length -Sum).Sum
2830

29-
$f_app = @{ expression={"$($_.app) ($($_.version))" }}
30-
$f_url = @{ expression={$_.url};alignment='right'}
31-
$f_size = @{ expression={$_.size}; alignment='right'}
31+
$files | ForEach-Object { cacheinfo $_ } | Select-Object Name, Version, Length, URL
3232

33+
Write-Host "Total: $($files.Length) $(pluralize $files.Length 'file' 'files'), $(filesize $totalLength)" -ForegroundColor Yellow
34+
}
3335

34-
$files | ForEach-Object { cacheinfo $_ } | Format-Table $f_size, $f_app, $f_url -auto -hide
36+
function cacheremove($app) {
37+
if (!$app) {
38+
'ERROR: <app(s)> missing'
39+
my_usage
40+
exit 1
41+
} elseif ($app -eq '*') {
42+
$files = @(Get-ChildItem $cachedir)
43+
} else {
44+
$app = '(' + ($app -join '|') + ')'
45+
$files = @(Get-ChildItem $cachedir | Where-Object -Property Name -Value "^$app#" -Match)
46+
}
47+
$totalLength = ($files | Measure-Object -Property Length -Sum).Sum
3548

36-
"Total: $($files.length) $(pluralize $files.length 'file' 'files'), $(filesize $total_length)"
49+
$files | ForEach-Object {
50+
$curr = cacheinfo $_
51+
Write-Host "Removing $($curr.URL)..."
52+
Remove-Item $_.FullName
53+
if(Test-Path "$cachedir\$($curr.Name).txt") {
54+
Remove-Item "$cachedir\$($curr.Name).txt"
55+
}
56+
}
57+
58+
Write-Host "Deleted: $($files.Length) $(pluralize $files.Length 'file' 'files'), $(filesize $totalLength)" -ForegroundColor Yellow
3759
}
3860

3961
switch($cmd) {
4062
'rm' {
41-
if(!$app) { 'ERROR: <app> missing'; my_usage; exit 1 }
42-
Remove-Item "$cachedir\$app#*"
43-
if(test-path("$cachedir\$app.txt")) {
44-
Remove-Item "$cachedir\$app.txt"
45-
}
63+
cacheremove $Args
4664
}
4765
'show' {
48-
show $app
49-
}
50-
'' {
51-
show
66+
cacheshow $Args
5267
}
5368
default {
54-
my_usage
69+
cacheshow (@($cmd) + $Args)
5570
}
5671
}
5772

0 commit comments

Comments
 (0)