Skip to content

Commit

Permalink
feat(scoop-cache): Allow multiple apps to be passed (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ash258 committed Apr 24, 2021
1 parent 0bf3db5 commit a644111
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 43 deletions.
8 changes: 5 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

### 0.6-pre3

- **Completion**: Respect `SCOOP_CACHE` environment for `cache rm` completion
- **scoop-cache**: Allow multiple apps to be passed as argument
- **scoop-(un)hold**: Detect and show error when global option is missing for globally installed application
- **Core**: Use `Legacy` command argument passing
- **autoupdate**: Archive old versions of manifest when executing checkver/autoupdate
- **Autoupdate**: Archive old versions of manifest when executing checkver/autoupdate
- **Git**: Always use `--no-pager` option
- **checkup**: Test Windows Defender exlusions only when executed with administrator privileges
- **scoop-checkup**: Test Windows Defender exlusions only when executed with administrator privileges
- Remove automatic config migration
- **config**: Do not support `rootPath`, `globalPath`, `cachePath` config options
- **Config**: Do not support `rootPath`, `globalPath`, `cachePath` config options
- **checkver**:
- Prevent hitting GitHub rate limits
- GitHub checkver will use `api.github.com/repos` and github token from environment `GITHUB_TOKEN` or config option `githubToken`
Expand Down
52 changes: 52 additions & 0 deletions lib/Cache.ps1
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)"
}
}
74 changes: 35 additions & 39 deletions libexec/scoop-cache.ps1
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
5 changes: 4 additions & 1 deletion supporting/completion/Scoop-Completion.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ if (!((Get-Command 'scoop' -ErrorAction 'SilentlyContinue') -or (Get-Command 'sh

$script:SCOOP_DIRECTORY = $env:SCOOP, "$env:USERPROFILE\scoop" | Where-Object { ![String]::IsNullOrEmpty($_) } | Select-Object -First 1
$script:SCOOP_GLOBAL_DIRECTORY = $env:SCOOP_GLOBAL, "$env:ProgramData\scoop" | Where-Object { ![String]::IsNullOrEmpty($_) } | Select-Object -First 1
$script:SCOOP_CACHE_DIRECTORY = $env:SCOOP_CACHE, "$SCOOP_DIRECTORY\cache" | Where-Object { ![String]::IsNullOrEmpty($_) } | Select-Object -First 1
$script:SCOOP_COMMANDS = @(
'alias'
'bucket'
Expand Down Expand Up @@ -170,9 +171,11 @@ function script:Get-LocallyAvailableApplicationsByScoop($Filter) {
}

function script:Get-ScoopCachedFile($Filter) {
$files = Get-ChildItem $SCOOP_DIRECTORY 'cache\*' -File -Name
if (!(Test-Path -LiteralPath $SCOOP_CACHE_DIRECTORY)) { return @() }

$files = Get-ChildItem -Path "$SCOOP_CACHE_DIRECTORY\*#*" -File -Name
$res = @()

foreach ($f in $files) { $res += ($f -split '#')[0] }

return @($res | Select-Object -Unique) -like "$Filter*"
Expand Down

0 comments on commit a644111

Please sign in to comment.