forked from ScoopInstaller/Scoop
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(scoop-cache): Allow multiple apps to be passed (#141)
- Loading branch information
Showing
4 changed files
with
96 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
'core' | ForEach-Object { | ||
. (Join-Path $PSScriptRoot "$_.ps1") | ||
} | ||
|
||
function Get-CachedFileInfo { | ||
<# | ||
.SYNOPSIS | ||
Parse cached file into psobject with app, version, url and size properties. | ||
.PARAMETER File | ||
Specifies the cached file to be parsed. | ||
#> | ||
[CmdletBinding()] | ||
param([Parameter(Mandatory, ValueFromPipeline)] [System.IO.FileInfo] $File) | ||
|
||
process { | ||
$app, $version, $url = $File.Name -split '#' | ||
$size = filesize $File.Length | ||
|
||
return New-Object PSObject -Prop @{ 'app' = $app; 'version' = $version; 'url' = $url; 'size' = $size } | ||
} | ||
} | ||
|
||
function Show-CachedFileList { | ||
<# | ||
.SYNOPSIS | ||
Table representation of cached files including total size. | ||
.PARAMETER ApplicationFilter | ||
Specifies to filter only subset of applications. | ||
#> | ||
[CmdletBinding()] | ||
param( | ||
[Parameter(ValueFromPipeline)] | ||
[AllowEmptyCollection()] | ||
[AllowNull()] | ||
[String[]] $ApplicationFilter | ||
) | ||
|
||
process { | ||
$regex = $ApplicationFilter -join '|' | ||
if (!$ApplicationFilter) { $regex = '.*?' } | ||
|
||
$files = Get-ChildItem -LiteralPath $SCOOP_CACHE_DIRECTORY -File | Where-Object -Property 'Name' -Match -Value "^($regex)#" | ||
$totalSize = [double] ($files | Measure-Object -Property 'Length' -Sum).Sum | ||
|
||
$_app = @{ 'Expression' = { "$($_.app) ($($_.version))" } } | ||
$_url = @{ 'Expression' = { $_.url }; 'Alignment' = 'Right' } | ||
$_size = @{ 'Expression' = { $_.size }; 'Alignment' = 'Right' } | ||
|
||
$files | ForEach-Object { Get-CachedFileInfo -File $_ } | Format-Table -Property $_size, $_app, $_url -AutoSize -HideTableHeaders | ||
Write-Output "Total: $($files.Length) $(pluralize $files.Length 'file' 'files'), $(filesize $totalSize)" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,55 @@ | ||
# Usage: scoop cache [rm|show] [app] [options] | ||
# 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. | ||
# Usage: scoop cache [<SUBCOMMAND>] [<OPTIONS>] [<APP>...] | ||
# Summary: Show or clear the download cache. | ||
# Help: Scoop caches downloaded files to remove the need for repeated downloads of same files. | ||
# | ||
# You can use | ||
# scoop cache show | ||
# to see what's in the cache, and | ||
# scoop cache rm <app> | ||
# scoop cache rm git | ||
# to remove downloads for a specific app. | ||
# | ||
# To clear everything in your cache, use: | ||
# To clear everything in cache, use: | ||
# scoop cache rm * | ||
# | ||
# Subcommands: | ||
# rm Remove an application specific files from cache. | ||
# show Show an overview of all cached files. Default command when any is provided. | ||
# | ||
# Options: | ||
# -h, --help Show help for this command. | ||
|
||
param($cmd, $app) | ||
|
||
. (Join-Path $PSScriptRoot '..\lib\help.ps1') | ||
|
||
Reset-Alias | ||
|
||
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 } | ||
'core', 'Cache', 'getopt', 'help' | ForEach-Object { | ||
. (Join-Path $PSScriptRoot "..\lib\$_.ps1") | ||
} | ||
|
||
function show($app) { | ||
$files = @(Get-ChildItem $SCOOP_CACHE_DIRECTORY | Where-Object -Property 'Name' -Match "^$app") | ||
$total_length = ($files | Measure-Object length -Sum).sum -as [double] | ||
|
||
$f_app = @{ 'Expression' = { "$($_.app) ($($_.version))" } } | ||
$f_url = @{ 'Expression' = { $_.url }; 'Alignment' = 'Right' } | ||
$f_size = @{ 'Expression' = { $_.size }; 'Alignment' = 'Right' } | ||
|
||
|
||
$files | ForEach-Object { cacheinfo $_ } | Format-Table $f_size, $f_app, $f_url -AutoSize -HideTableHeaders | ||
Reset-Alias | ||
|
||
"Total: $($files.length) $(pluralize $files.length 'file' 'files'), $(filesize $total_length)" | ||
} | ||
$opt, $arguments, $err = getopt $args | ||
if ($err) { Stop-ScoopExecution -Message "scoop cache: $err" -ExitCode 2 } | ||
|
||
$cmd = if ($arguments[0]) { $arguments[0] } else { 'show' } | ||
$isShow = $cmd -eq 'show' | ||
$applications = $arguments[1..($arguments.Count)] | ||
$exitCode = 0 | ||
switch ($cmd) { | ||
'rm' { | ||
if (!$app) { Stop-ScoopExecution -Message 'Parameter <app> missing' -Usage (my_usage) } | ||
Join-Path $SCOOP_CACHE_DIRECTORY "$app#*" | Remove-Item -Force -Recurse | ||
Join-Path $SCOOP_CACHE_DIRECTORY "$app.txt" | Remove-Item -ErrorAction 'SilentlyContinue' -Force -Recurse | ||
} | ||
'show' { | ||
show $app | ||
} | ||
default { | ||
show | ||
$problems = 0 | ||
|
||
if ($cmd -notin @('rm', 'show')) { Stop-ScoopExecution -Message "Unknown subcommand: '$cmd'" -Usage (my_usage) } | ||
if (!$isShow -and !$applications) { Stop-ScoopExecution -Message 'Parameter <APP> is required for ''rm'' subcommand' -Usage (my_usage) } | ||
|
||
if ($isShow) { | ||
Show-CachedFileList -ApplicationFilter $applications | ||
} else { | ||
foreach ($app in $applications) { | ||
try { | ||
Join-Path $SCOOP_CACHE_DIRECTORY "$app#*" | Remove-Item -ErrorAction 'Stop' -Force -Recurse | ||
Join-Path $SCOOP_CACHE_DIRECTORY "$app.txt" | Remove-Item -ErrorAction 'SilentlyContinue' -Force -Recurse | ||
} catch { | ||
Write-UserMessage -Message "Removing ${app}: $($_.Exception.Message)" -Err | ||
++$problems | ||
} | ||
} | ||
} | ||
|
||
if ($problems -gt 0) { $exitCode = 10 + $problems } | ||
|
||
exit $exitCode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters