forked from ScoopInstaller/Scoop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scoop-search.ps1
122 lines (103 loc) · 3.37 KB
/
scoop-search.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# Usage: scoop search [query]
# Summary: Search available apps
# Help: Searches for apps that are available to install.
#
# If used with [query], shows app names that match the query.
# Without [query], shows all the available apps.
param($query)
. "$psscriptroot\..\lib\core.ps1"
. "$psscriptroot\..\lib\buckets.ps1"
. "$psscriptroot\..\lib\manifest.ps1"
. "$psscriptroot\..\lib\versions.ps1"
reset_aliases
function bin_match($manifest, $query) {
if(!$manifest.bin) { return $false }
foreach($bin in $manifest.bin) {
$exe, $alias, $args = $bin
$fname = split-path $exe -leaf -ea stop
if((strip_ext $fname) -match $query) { return $fname }
if($alias -match $query) { return $alias }
}
$false
}
function search_bucket($bucket, $query) {
$apps = apps_in_bucket (bucketdir $bucket) | % {
@{ name = $_ }
}
if($query) {
try {
$query = new-object regex $query, 'IgnoreCase'
} catch {
abort "Invalid regular expression: $($_.exception.innerexception.message)"
}
$apps = $apps | ? {
if($_.name -match $query) { return $true }
$bin = bin_match (manifest $_.name $bucket) $query
if($bin) {
$_.bin = $bin; return $true;
}
}
}
$apps | % { $_.version = (latest_version $_.name $bucket); $_ }
}
function download_json($url) {
$progressPreference = 'silentlycontinue'
$result = invoke-webrequest $url -UseBasicParsing | select -exp 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
}
function search_remote($bucket, $query) {
$repo = known_bucket_repo $bucket
$uri = [system.uri]($repo)
if ($uri.absolutepath -match '/([a-zA-Z0-9]*)/([a-zA-Z0-9-]*)(.git|/)?') {
$user = $matches[1]
$repo_name = $matches[2]
$api_link = "https://api.github.com/repos/$user/$repo_name/git/trees/HEAD?recursive=1"
$result = download_json $api_link | select -exp tree |? {
$_.path -match "(($query[a-zA-Z0-9-]*).json)"
} |% { $matches[2] }
}
$result
}
function search_remotes($query) {
$buckets = known_bucket_repos
$names = $buckets | get-member -m noteproperty | select -exp name
$results = $names |? { !(test-path $(bucketdir $_)) } |% {
@{"bucket" = $_; "results" = (search_remote $_ $query)}
} |? { $_.results }
if ($results.count -gt 0) {
"Results from other known buckets..."
"(add them using 'scoop bucket add <name>')"
""
}
$results |% {
"'$($_.bucket)'' bucket:"
$_.results |% { " $_" }
""
}
}
@($null) + @(buckets) | % { # $null is main bucket
$res = search_bucket $_ $query
$local_results = $local_results -or $res
if($res) {
$name = "$_"
if(!$_) { $name = "main" }
"'$name' bucket:"
$res | % {
$item = " $($_.name) ($($_.version))"
if($_.bin) { $item += " --> includes '$($_.bin)'" }
$item
}
""
}
}
if (!$local_results -and !(github_ratelimit_reached)) {
$remote_results = search_remotes $query
if(!$remote_results) { [console]::error.writeline("No matches found."); exit 1 }
$remote_results
}
exit 0