Skip to content

Commit

Permalink
refactor(scoop-search): Output PSObject, use API token (ScoopInstalle…
Browse files Browse the repository at this point in the history
…r#4997)

* refactor(scoop-search): Output PSObject, use API token

* warn about parsing error

* Update CHANGELOG.md

* Update scoop-search.ps1

* Apply suggestions from code review

Co-authored-by: Hsiao-nan Cheung <niheaven@gmail.com>

* separate lines

Co-authored-by: Hsiao-nan Cheung <niheaven@gmail.com>
  • Loading branch information
2 people authored and slaughtering committed Jul 7, 2022
1 parent 1d5a638 commit cff242e
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 39 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

- **shortcuts:** Fix network drive shortcut creation ([#4410](https://github.com/ScoopInstaller/Scoop/issues/4410))

### Code Refactoring

- **scoop-search:** Output PSObject, use API token ([#4997](https://github.com/ScoopInstaller/Scoop/issues/4997))

## [v0.2.2](https://github.com/ScoopInstaller/Scoop/compare/v0.2.1...v0.2.2) - 2022-06-21

### Features
Expand Down
8 changes: 6 additions & 2 deletions lib/manifest.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ function manifest_path($app, $bucket) {
}

function parse_json($path) {
if(!(test-path $path)) { return $null }
Get-Content $path -raw -Encoding UTF8 | convertfrom-json -ea stop
if (!(Test-Path $path)) { return $null }
try {
Get-Content $path -Raw -Encoding UTF8 | ConvertFrom-Json -ErrorAction Stop
} catch {
warn "Error parsing manifest at $path."
}
}

function url_manifest($url) {
Expand Down
102 changes: 65 additions & 37 deletions libexec/scoop-search.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,43 @@ param($query)
. "$PSScriptRoot\..\lib\manifest.ps1" # 'manifest'
. "$PSScriptRoot\..\lib\versions.ps1" # 'Get-LatestVersion'

$list = @()

try {
$query = New-Object Regex $query, 'IgnoreCase'
} catch {
abort "Invalid regular expression: $($_.Exception.InnerException.Message)"
}

$githubtoken = Get-GitHubToken
$authheader = @{}
if ($githubtoken) {
$authheader = @{'Authorization' = "token $githubtoken"}
}

function bin_match($manifest, $query) {
if(!$manifest.bin) { return $false }
foreach($bin in $manifest.bin) {
if (!$manifest.bin) { return $false }
$bins = foreach ($bin in $manifest.bin) {
$exe, $alias, $args = $bin
$fname = split-path $exe -leaf -ea stop
$fname = Split-Path $exe -Leaf -ErrorAction Stop

if((strip_ext $fname) -match $query) { return $fname }
if($alias -match $query) { return $alias }
if ((strip_ext $fname) -match $query) { $fname }
elseif ($alias -match $query) { $alias }
}
$false
if ($bins) { return $bins }
else { return $false }
}

function search_bucket($bucket, $query) {
$apps = apps_in_bucket (Find-BucketDirectory $bucket) | ForEach-Object {
@{ name = $_ }
}

if($query) {
try {
$query = new-object regex $query, 'IgnoreCase'
} catch {
abort "Invalid regular expression: $($_.exception.innerexception.message)"
}
$apps = apps_in_bucket (Find-BucketDirectory $bucket) | ForEach-Object { @{ name = $_ } }

if ($query) {
$apps = $apps | Where-Object {
if($_.name -match $query) { return $true }
if ($_.name -match $query) { return $true }
$bin = bin_match (manifest $_.name $bucket) $query
if($bin) {
$_.bin = $bin; return $true;
if ($bin) {
$_.bin = $bin
return $true
}
}
}
Expand All @@ -46,14 +54,19 @@ function search_bucket($bucket, $query) {

function download_json($url) {
$ProgressPreference = 'SilentlyContinue'
$result = Invoke-WebRequest $url -UseBasicParsing | Select-Object -ExpandProperty content | ConvertFrom-Json
$result = Invoke-WebRequest $url -UseBasicParsing -Headers $authheader | Select-Object -ExpandProperty content | ConvertFrom-Json
$ProgressPreference = 'Continue'
$result
}

function github_ratelimit_reached {
$api_link = 'https://api.github.com/rate_limit'
(download_json $api_link).rate.remaining -eq 0
$ret = (download_json $api_link).rate.remaining -eq 0
if ($ret) {
Write-Host "GitHub API rate limit reached.
Please try again later or configure your API token using 'scoop config gh_token <your token>'."
}
$ret
}

function search_remote($bucket, $query) {
Expand All @@ -72,44 +85,59 @@ function search_remote($bucket, $query) {

function search_remotes($query) {
$buckets = known_bucket_repos
$names = $buckets | get-member -m noteproperty | Select-Object -exp name
$names = $buckets | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty name

$results = $names | Where-Object { !(test-path $(Find-BucketDirectory $_)) } | ForEach-Object {
@{"bucket" = $_; "results" = (search_remote $_ $query)}
$results = $names | Where-Object { !(Test-Path $(Find-BucketDirectory $_)) } | ForEach-Object {
@{ "bucket" = $_; "results" = (search_remote $_ $query) }
} | Where-Object { $_.results }

if ($results.count -gt 0) {
"Results from other known buckets..."
"(add them using 'scoop bucket add <name>')"
""
Write-Host "Results from other known buckets...
(add them using 'scoop bucket add <bucket name>')"
}

$results | ForEach-Object {
"'$($_.bucket)' bucket (install using 'scoop install $($_.bucket)/<app>'):"
$_.results | ForEach-Object { " $_" }
""
$name = $_.bucket
$_.results | ForEach-Object {
$item = [ordered]@{}
$item.Name = $_
$item.Source = $name
$list += [PSCustomObject]$item
}
}

$list
}

Get-LocalBucket | ForEach-Object {
$res = search_bucket $_ $query
$local_results = $local_results -or $res
if($res) {
if ($res) {
$name = "$_"

Write-Host "'$name' bucket:"
$res | ForEach-Object {
$item = " $($_.name) ($($_.version))"
if($_.bin) { $item += " --> includes '$($_.bin)'" }
$item
$item = [ordered]@{}
$item.Name = $_.name
$item.Version = $_.version
$item.Source = $name
$item.Binaries = ""
if ($_.bin) { $item.Binaries = $_.bin -join ' | ' }
$list += [PSCustomObject]$item
}
""
}
}

if ($list.Length -gt 0) {
Write-Host "Results from local buckets..."
$list
}

if (!$local_results -and !(github_ratelimit_reached)) {
$remote_results = search_remotes $query
if(!$remote_results) { [console]::error.writeline("No matches found."); exit 1 }
if (!$remote_results) {
warn "No matches found."
exit 1
}
$remote_results
}

Expand Down

0 comments on commit cff242e

Please sign in to comment.