@@ -18,7 +18,9 @@ function Find-BucketDirectory {
1818 )
1919
2020 # Handle info passing empty string as bucket ($install.bucket)
21- if (($null -eq $Name ) -or ($Name -eq ' ' )) { $Name = ' main' }
21+ if (($null -eq $Name ) -or ($Name -eq ' ' )) {
22+ $Name = ' main'
23+ }
2224 $bucket = " $bucketsdir \$Name "
2325
2426 if ((Test-Path " $bucket \bucket" ) -and ! $Root ) {
@@ -37,7 +39,7 @@ function bucketdir($name) {
3739function known_bucket_repos {
3840 $json = " $PSScriptRoot \..\buckets.json"
3941
40- return Get-Content $json - raw | convertfrom-json - ea stop
42+ return Get-Content $json - Raw | ConvertFrom-Json - ErrorAction stop
4143}
4244
4345function known_bucket_repo ($name ) {
@@ -46,11 +48,11 @@ function known_bucket_repo($name) {
4648}
4749
4850function known_buckets {
49- known_bucket_repos | ForEach-Object { $_.psobject.properties | Select-Object - expand ' name' }
51+ known_bucket_repos | ForEach-Object { $_.PSObject.Properties | Select-Object - Expand ' name' }
5052}
5153
5254function apps_in_bucket ($dir ) {
53- return Get-ChildItem $dir | Where-Object { $_.Name.endswith (' .json' ) } | ForEach-Object { $_.Name -replace ' .json$' , ' ' }
55+ return Get-ChildItem $dir | Where-Object { $_.Name.EndsWith (' .json' ) } | ForEach-Object { $_.Name -replace ' .json$' , ' ' }
5456}
5557
5658function Get-LocalBucket {
@@ -68,65 +70,134 @@ function buckets {
6870 return Get-LocalBucket
6971}
7072
71- function find_manifest ($app , $bucket ) {
72- if ($bucket ) {
73- $manifest = manifest $app $bucket
74- if ($manifest ) { return $manifest , $bucket }
75- return $null
73+ function Find-Manifest ($app , $bucket ) {
74+ $manifest , $url = $null , $null
75+
76+ # check if app is a URL or UNC path
77+ if ($app -match ' ^(ht|f)tps?://|\\\\' ) {
78+ $url = $app
79+ $app = appname_from_url $url
80+ $manifest = url_manifest $url
81+ } else {
82+ if ($bucket ) {
83+ $manifest = manifest $app $bucket
84+ } else {
85+ foreach ($bucket in Get-LocalBucket ) {
86+ $manifest = manifest $app $bucket
87+ if ($manifest ) { break }
88+ }
89+ }
90+
91+ if (! $manifest ) {
92+ # couldn't find app in buckets: check if it's a local path
93+ $path = $app
94+ if (! $path.endswith (' .json' )) { $path += ' .json' }
95+ if (Test-Path $path ) {
96+ $url = " $ ( Resolve-Path $path ) "
97+ $app = appname_from_url $url
98+ $manifest , $bucket = url_manifest $url
99+ }
100+ }
76101 }
77102
78- foreach ($bucket in Get-LocalBucket ) {
79- $manifest = manifest $app $bucket
80- if ($manifest ) { return $manifest , $bucket }
103+ return $app , $manifest , $bucket , $url
104+ }
105+
106+ function Convert-RepositoryUri {
107+ [CmdletBinding ()]
108+ param (
109+ [Parameter (Mandatory , Position = 0 , ValueFromPipeline = $true )]
110+ [String ] $Uri
111+ )
112+
113+ process {
114+ # https://git-scm.com/docs/git-clone#_git_urls
115+ # https://regex101.com/r/xGmwRr/1
116+ if ($Uri -match ' (?:@|/{1,3})(?:www\.|.*@)?(?<provider>[^/]+?)(?::\d+)?[:/](?<user>.+)/(?<repo>.+?)(?:\.git)?/?$' ) {
117+ $Matches.provider , $Matches.user , $Matches.repo -join ' /'
118+ } else {
119+ error " $Uri is not a valid Git URL!"
120+ error " Please see https://git-scm.com/docs/git-clone#_git_urls for valid ones."
121+ return $null
122+ }
81123 }
82124}
83125
84- function add_bucket ($name , $repo ) {
85- if (! $name ) { " <name> missing" ; $usage_add ; exit 1 }
86- if (! $repo ) {
87- $repo = known_bucket_repo $name
88- if (! $repo ) { " Unknown bucket '$name '. Try specifying <repo>." ; $usage_add ; exit 1 }
126+ function list_buckets {
127+ $buckets = @ ()
128+ Get-LocalBucket | ForEach-Object {
129+ $bucket = [Ordered ]@ { Name = $_ }
130+ $path = Find-BucketDirectory $_ - Root
131+ if ((Test-Path (Join-Path $path ' .git' )) -and (Get-Command git - ErrorAction SilentlyContinue)) {
132+ $bucket.Source = git - C $path config remote.origin.url
133+ $bucket.Updated = git - C $path log -- format= ' %aD' - n 1 | Get-Date
134+ } else {
135+ $bucket.Source = friendly_path $path
136+ $bucket.Updated = (Get-Item " $path \bucket" ).LastWriteTime
137+ }
138+ $bucket.Manifests = Get-ChildItem " $path \bucket" - Force - Recurse - ErrorAction SilentlyContinue |
139+ Measure-Object | Select-Object - ExpandProperty Count
140+ $buckets += [PSCustomObject ]$bucket
89141 }
142+ $buckets
143+ }
90144
145+ function add_bucket ($name , $repo ) {
91146 if (! (Test-CommandAvailable git)) {
92- abort " Git is required for buckets. Run 'scoop install git' and try again."
147+ error " Git is required for buckets. Run 'scoop install git' and try again."
148+ return 1
93149 }
94150
95151 $dir = Find-BucketDirectory $name - Root
96- if (test-path $dir ) {
152+ if (Test-Path $dir ) {
97153 warn " The '$name ' bucket already exists. Use 'scoop bucket rm $name ' to remove it."
98- exit 0
154+ return 2
155+ }
156+
157+ $uni_repo = Convert-RepositoryUri - Uri $repo
158+ if ($null -eq $uni_repo ) {
159+ return 1
160+ }
161+ foreach ($bucket in Get-LocalBucket ) {
162+ $remote = git - C " $bucketsdir \$bucket " config -- get remote.origin.url
163+ if ((Convert-RepositoryUri - Uri $remote ) -eq $uni_repo ) {
164+ warn " Bucket $bucket already exists for $repo "
165+ return 2
166+ }
99167 }
100168
101- write-host ' Checking repo... ' - nonewline
169+ Write-Host ' Checking repo... ' - NoNewline
102170 $out = git_cmd ls- remote $repo 2>&1
103- if ($lastexitcode -ne 0 ) {
104- abort " '$repo ' doesn't look like a valid git repository`n`n Error given:`n $out "
171+ if ($LASTEXITCODE -ne 0 ) {
172+ error " '$repo ' doesn't look like a valid git repository`n`n Error given:`n $out "
173+ return 1
105174 }
106- write-host ' ok '
175+ Write-Host ' OK '
107176
108- ensure $bucketsdir > $null
177+ ensure $bucketsdir | Out-Null
109178 $dir = ensure $dir
110179 git_cmd clone " $repo " " `" $dir `" " - q
111180 success " The $name bucket was added successfully."
181+ return 0
112182}
113183
114184function rm_bucket ($name ) {
115- if (! $name ) { " <name> missing" ; $usage_rm ; exit 1 }
116185 $dir = Find-BucketDirectory $name - Root
117- if (! (test-path $dir )) {
118- abort " '$name ' bucket not found."
186+ if (! (Test-Path $dir )) {
187+ error " '$name ' bucket not found."
188+ return 1
119189 }
120190
121- Remove-Item $dir - r - force - ea stop
191+ Remove-Item $dir - Recurse - Force - ErrorAction Stop
192+ return 0
122193}
123194
124195function new_issue_msg ($app , $bucket , $title , $body ) {
125196 $app , $manifest , $bucket , $url = Find-Manifest $app $bucket
126197 $url = known_bucket_repo $bucket
127198 $bucket_path = " $bucketsdir \$bucket "
128199
129- if (Test-path $bucket_path ) {
200+ if (Test-Path $bucket_path ) {
130201 $remote = Invoke-Expression " git -C '$bucket_path ' config --get remote.origin.url"
131202 # Support ssh and http syntax
132203 # git@PROVIDER:USER/REPO.git
@@ -135,15 +206,15 @@ function new_issue_msg($app, $bucket, $title, $body) {
135206 $url = " https://$ ( $Matches.Provider ) /$ ( $Matches.User ) /$ ( $Matches.Repo ) "
136207 }
137208
138- if (! $url ) { return ' Please contact the bucket maintainer!' }
209+ if (! $url ) { return ' Please contact the bucket maintainer!' }
139210
140211 # Print only github repositories
141212 if ($url -like ' *github*' ) {
142213 $title = [System.Web.HttpUtility ]::UrlEncode(" $app @$ ( $manifest.version ) : $title " )
143214 $body = [System.Web.HttpUtility ]::UrlEncode($body )
144215 $url = $url -replace ' \.git$' , ' '
145216 $url = " $url /issues/new?title=$title "
146- if ($body ) {
217+ if ($body ) {
147218 $url += " &body=$body "
148219 }
149220 }
0 commit comments