-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathbuckets.ps1
More file actions
222 lines (188 loc) · 6.56 KB
/
buckets.ps1
File metadata and controls
222 lines (188 loc) · 6.56 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
$bucketsdir = "$scoopdir\buckets"
function Find-BucketDirectory {
<#
.DESCRIPTION
Return full path for bucket with given name.
Main bucket will be returned as default.
.PARAMETER Name
Name of bucket.
.PARAMETER Root
Root folder of bucket repository will be returned instead of 'bucket' subdirectory (if exists).
#>
param(
[string] $Name = 'main',
[switch] $Root
)
# Handle info passing empty string as bucket ($install.bucket)
if (($null -eq $Name) -or ($Name -eq '')) {
$Name = 'main'
}
$bucket = "$bucketsdir\$Name"
if ((Test-Path "$bucket\bucket") -and !$Root) {
$bucket = "$bucket\bucket"
}
return $bucket
}
function bucketdir($name) {
Show-DeprecatedWarning $MyInvocation 'Find-BucketDirectory'
return Find-BucketDirectory $name
}
function known_bucket_repos {
$json = "$PSScriptRoot\..\buckets.json"
return Get-Content $json -Raw | ConvertFrom-Json -ErrorAction stop
}
function known_bucket_repo($name) {
$buckets = known_bucket_repos
$buckets.$name
}
function known_buckets {
known_bucket_repos | ForEach-Object { $_.PSObject.Properties | Select-Object -Expand 'name' }
}
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 -Directory $bucketsdir).Name
}
function buckets {
Show-DeprecatedWarning $MyInvocation 'Get-LocalBucket'
return Get-LocalBucket
}
function Find-Manifest($app, $bucket) {
$manifest, $url = $null, $null
# check if app is a URL or UNC path
if ($app -match '^(ht|f)tps?://|\\\\') {
$url = $app
$app = appname_from_url $url
$manifest = url_manifest $url
} else {
if ($bucket) {
$manifest = manifest $app $bucket
} else {
foreach ($bucket in Get-LocalBucket) {
$manifest = manifest $app $bucket
if ($manifest) { break }
}
}
if (!$manifest) {
# couldn't find app in buckets: check if it's a local path
$path = $app
if (!$path.endswith('.json')) { $path += '.json' }
if (Test-Path $path) {
$url = "$(Resolve-Path $path)"
$app = appname_from_url $url
$manifest, $bucket = url_manifest $url
}
}
}
return $app, $manifest, $bucket, $url
}
function Convert-RepositoryUri {
[CmdletBinding()]
param (
[Parameter(Mandatory, Position = 0, ValueFromPipeline = $true)]
[String] $Uri
)
process {
# https://git-scm.com/docs/git-clone#_git_urls
# https://regex101.com/r/xGmwRr/1
if ($Uri -match '(?:@|/{1,3})(?:www\.|.*@)?(?<provider>[^/]+?)(?::\d+)?[:/](?<user>.+)/(?<repo>.+?)(?:\.git)?/?$') {
$Matches.provider, $Matches.user, $Matches.repo -join '/'
} else {
error "$Uri is not a valid Git URL!"
error "Please see https://git-scm.com/docs/git-clone#_git_urls for valid ones."
return $null
}
}
}
function list_buckets {
$buckets = @()
Get-LocalBucket | ForEach-Object {
$bucket = [Ordered]@{ Name = $_ }
$path = Find-BucketDirectory $_ -Root
if ((Test-Path (Join-Path $path '.git')) -and (Get-Command git -ErrorAction SilentlyContinue)) {
$bucket.Source = git -C $path config remote.origin.url
$bucket.Updated = git -C $path log --format='%aD' -n 1 | Get-Date
} else {
$bucket.Source = friendly_path $path
$bucket.Updated = (Get-Item "$path\bucket").LastWriteTime
}
$bucket.Manifests = Get-ChildItem "$path\bucket" -Force -Recurse -ErrorAction SilentlyContinue |
Measure-Object | Select-Object -ExpandProperty Count
$buckets += [PSCustomObject]$bucket
}
$buckets
}
function add_bucket($name, $repo) {
if (!(Test-CommandAvailable git)) {
error "Git is required for buckets. Run 'scoop install git' and try again."
return 1
}
$dir = Find-BucketDirectory $name -Root
if (Test-Path $dir) {
warn "The '$name' bucket already exists. Use 'scoop bucket rm $name' to remove it."
return 2
}
$uni_repo = Convert-RepositoryUri -Uri $repo
if ($null -eq $uni_repo) {
return 1
}
foreach ($bucket in Get-LocalBucket) {
$remote = git -C "$bucketsdir\$bucket" config --get remote.origin.url
if ((Convert-RepositoryUri -Uri $remote) -eq $uni_repo) {
warn "Bucket $bucket already exists for $repo"
return 2
}
}
Write-Host 'Checking repo... ' -NoNewline
$out = git_cmd ls-remote $repo 2>&1
if ($LASTEXITCODE -ne 0) {
error "'$repo' doesn't look like a valid git repository`n`nError given:`n$out"
return 1
}
Write-Host 'OK'
ensure $bucketsdir | Out-Null
$dir = ensure $dir
git_cmd clone "$repo" "`"$dir`"" -q
success "The $name bucket was added successfully."
return 0
}
function rm_bucket($name) {
$dir = Find-BucketDirectory $name -Root
if (!(Test-Path $dir)) {
error "'$name' bucket not found."
return 1
}
Remove-Item $dir -Recurse -Force -ErrorAction Stop
return 0
}
function new_issue_msg($app, $bucket, $title, $body) {
$app, $manifest, $bucket, $url = Find-Manifest $app $bucket
$url = known_bucket_repo $bucket
$bucket_path = "$bucketsdir\$bucket"
if (Test-Path $bucket_path) {
$remote = Invoke-Expression "git -C '$bucket_path' config --get remote.origin.url"
# Support ssh and http syntax
# git@PROVIDER:USER/REPO.git
# https://PROVIDER/USER/REPO.git
$remote -match '(@|:\/\/)(?<provider>.+)[:/](?<user>.*)\/(?<repo>.*)(\.git)?$' | Out-Null
$url = "https://$($Matches.Provider)/$($Matches.User)/$($Matches.Repo)"
}
if (!$url) { return 'Please contact the bucket maintainer!' }
# Print only github repositories
if ($url -like '*github*') {
$title = [System.Web.HttpUtility]::UrlEncode("$app@$($manifest.version): $title")
$body = [System.Web.HttpUtility]::UrlEncode($body)
$url = $url -replace '\.git$', ''
$url = "$url/issues/new?title=$title"
if ($body) {
$url += "&body=$body"
}
}
$msg = "`nPlease try again or create a new issue by using the following link and paste your console output:"
return "$msg`n$url"
}