From 44635149484d7de248d08e2fbf130d6fabea89ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20=C4=8C=C3=A1bera?= Date: Sun, 21 Apr 2019 13:02:19 +0200 Subject: [PATCH] [buckets] Optimize buckets function (#3341) * Optimize buckets function * Use Verb-Noun naming * Add Show-DeprecatedWarning function to core --- bin/scoop.ps1 | 2 +- lib/buckets.ps1 | 21 +++++++++++++++------ lib/core.ps1 | 16 ++++++++++++++++ lib/depends.ps1 | 2 +- libexec/scoop-bucket.ps1 | 2 +- libexec/scoop-search.ps1 | 2 +- libexec/scoop-update.ps1 | 2 +- 7 files changed, 36 insertions(+), 11 deletions(-) diff --git a/bin/scoop.ps1 b/bin/scoop.ps1 index d0bad60f..1d7662ce 100644 --- a/bin/scoop.ps1 +++ b/bin/scoop.ps1 @@ -19,7 +19,7 @@ if ('--version' -contains $cmd -or (!$cmd -and '-v' -contains $args)) { write-host "" Pop-Location - buckets | ForEach-Object { + Get-LocalBucket | ForEach-Object { Push-Location $(bucketdir $_) if(test-path '.git') { write-host "'$_' bucket:" diff --git a/lib/buckets.ps1 b/lib/buckets.ps1 index a8e975c2..109cedff 100644 --- a/lib/buckets.ps1 +++ b/lib/buckets.ps1 @@ -1,3 +1,5 @@ +. "$PSScriptRoot\core.ps1" + $bucketsdir = "$scoopdir\buckets" <# @@ -39,12 +41,19 @@ function apps_in_bucket($dir) { return Get-ChildItem $dir | Where-Object { $_.Name.endswith('.json') } | ForEach-Object { $_.Name -replace '.json$', '' } } +function Get-LocalBucket { + <# + .SYNOPSIS + List all local buckets. + #> + + return (Get-ChildItem $bucketsdir).Name +} + function buckets { - $buckets = @() - if(test-path $bucketsdir) { - Get-ChildItem $bucketsdir | ForEach-Object { $buckets += $_.Name } - } - return $buckets + Show-DeprecatedWarning $MyInvocation 'Get-LocalBucket' + + return Get-LocalBucket } function find_manifest($app, $bucket) { @@ -54,7 +63,7 @@ function find_manifest($app, $bucket) { return $null } - $buckets = @($null) + @(buckets) # null for main bucket + $buckets = @($null) + @(Get-LocalBucket) # null for main bucket foreach($bucket in $buckets) { $manifest = manifest $app $bucket if($manifest) { return $manifest, $bucket } diff --git a/lib/core.ps1 b/lib/core.ps1 index 51c34f7a..148f1253 100644 --- a/lib/core.ps1 +++ b/lib/core.ps1 @@ -42,6 +42,22 @@ function Get-UserAgent() { return "Scoop/1.0 (+http://scoop.sh/) PowerShell/$($PSVersionTable.PSVersion.Major).$($PSVersionTable.PSVersion.Minor) (Windows NT $([System.Environment]::OSVersion.Version.Major).$([System.Environment]::OSVersion.Version.Minor); $(if($env:PROCESSOR_ARCHITECTURE -eq 'AMD64'){'Win64; x64; '})$(if($env:PROCESSOR_ARCHITEW6432 -eq 'AMD64'){'WOW64; '})$PSEdition)" } +function Show-DeprecatedWarning { + <# + .SYNOPSIS + Print deprecated warning for functions, which will be deleted in near future. + .PARAMETER Invocation + Invocation to identify location of line. + Just pass $MyInvocation. + .PARAMETER New + New command name. + #> + param($Invocation, [String] $New) + + warn ('"{0}" will be deprecated. Please change your code/manifest to use "{1}"' -f $Invocation.MyCommand.Name, $New) + Write-Host " -> $($Invocation.PSCommandPath):$($Invocation.ScriptLineNumber):$($Invocation.OffsetInLine)" -ForegroundColor DarkGray +} + # helper functions function coalesce($a, $b) { if($a) { return $a } $b } diff --git a/lib/depends.ps1 b/lib/depends.ps1 index 8ed20e38..f8cd2bdc 100644 --- a/lib/depends.ps1 +++ b/lib/depends.ps1 @@ -25,7 +25,7 @@ function dep_resolve($app, $arch, $resolved, $unresolved) { $null, $manifest, $null, $null = locate $app $bucket if(!$manifest) { - if(((buckets) -notcontains $bucket) -and $bucket) { + if(((Get-LocalBucket) -notcontains $bucket) -and $bucket) { warn "Bucket '$bucket' not installed. Add it with 'scoop bucket add $bucket' or 'scoop bucket add $bucket '." } abort "Couldn't find manifest for '$app'$(if(!$bucket) { '.' } else { " from '$bucket' bucket." })" diff --git a/libexec/scoop-bucket.ps1 b/libexec/scoop-bucket.ps1 index 59fa7a66..2c75cb7c 100644 --- a/libexec/scoop-bucket.ps1 +++ b/libexec/scoop-bucket.ps1 @@ -32,7 +32,7 @@ $usage_rm = "usage: scoop bucket rm " switch($cmd) { 'add' { add_bucket $name $repo } 'rm' { rm_bucket $name } - 'list' { buckets } + 'list' { Get-LocalBucket } 'known' { known_buckets } default { "scoop bucket: cmd '$cmd' not supported"; my_usage; exit 1 } } diff --git a/libexec/scoop-search.ps1 b/libexec/scoop-search.ps1 index c6b2c331..83a0bdc7 100644 --- a/libexec/scoop-search.ps1 +++ b/libexec/scoop-search.ps1 @@ -97,7 +97,7 @@ function search_remotes($query) { } } -@($null) + @(buckets) | ForEach-Object { # $null is main bucket +@($null) + @(Get-LocalBucket) | ForEach-Object { # $null is main bucket $res = search_bucket $_ $query $local_results = $local_results -or $res if($res) { diff --git a/libexec/scoop-update.ps1 b/libexec/scoop-update.ps1 index 492eb759..4efc437e 100644 --- a/libexec/scoop-update.ps1 +++ b/libexec/scoop-update.ps1 @@ -100,7 +100,7 @@ function update_scoop() { ensure_scoop_in_path shim "$currentdir\bin\scoop.ps1" $false - @(buckets) | ForEach-Object { + Get-LocalBucket | ForEach-Object { write-host "Updating '$_' bucket..." Push-Location (bucketdir $_) git_pull -q