|
1 | | -# Usage: scoop cache show|rm [app] |
| 1 | +# Usage: scoop cache show|rm [app(s)] |
2 | 2 | # Summary: Show or clear the download cache |
3 | 3 | # Help: Scoop caches downloads so you don't need to download the same files |
4 | 4 | # when you uninstall and re-install the same version of an app. |
|
10 | 10 | # |
11 | 11 | # To clear everything in your cache, use: |
12 | 12 | # scoop cache rm * |
13 | | -param($cmd, $app) |
| 13 | +param($cmd) |
14 | 14 |
|
15 | | -. "$psscriptroot\..\lib\help.ps1" |
16 | | - |
17 | | -reset_aliases |
| 15 | +. "$PSScriptRoot\..\lib\help.ps1" |
18 | 16 |
|
19 | 17 | 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 } |
23 | 20 | } |
24 | 21 |
|
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 |
28 | 30 |
|
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 |
32 | 32 |
|
| 33 | + Write-Host "Total: $($files.Length) $(pluralize $files.Length 'file' 'files'), $(filesize $totalLength)" -ForegroundColor Yellow |
| 34 | +} |
33 | 35 |
|
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 |
35 | 48 |
|
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 |
37 | 59 | } |
38 | 60 |
|
39 | 61 | switch($cmd) { |
40 | 62 | '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 |
46 | 64 | } |
47 | 65 | 'show' { |
48 | | - show $app |
49 | | - } |
50 | | - '' { |
51 | | - show |
| 66 | + cacheshow $Args |
52 | 67 | } |
53 | 68 | default { |
54 | | - my_usage |
| 69 | + cacheshow (@($cmd) + $Args) |
55 | 70 | } |
56 | 71 | } |
57 | 72 |
|
|
0 commit comments