Skip to content

Commit

Permalink
refactor(bucket): Move 'Find-Manifest' and 'list_buckets' to 'buckets' (
Browse files Browse the repository at this point in the history
  • Loading branch information
niheaven authored Mar 23, 2022
1 parent ced36b2 commit 32de4c5
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 108 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

### Code Refactoring

- **bucket:** Move 'Find-Manifest' and 'list_buckets' to 'buckets' ([#4814](https://github.com/ScoopInstaller/Scoop/issues/4814))
- **relpath:** Use `$PSScriptRoot` instead of `relpath` ([#4793](https://github.com/ScoopInstaller/Scoop/issues/4793))
- **reset_aliases:** Move core function of `reset_aliases` to `scoop` ([#4794](https://github.com/ScoopInstaller/Scoop/issues/4794))

Expand Down
135 changes: 103 additions & 32 deletions lib/buckets.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ function Find-BucketDirectory {
)

# Handle info passing empty string as bucket ($install.bucket)
if(($null -eq $Name) -or ($Name -eq '')) { $Name = 'main' }
if (($null -eq $Name) -or ($Name -eq '')) {
$Name = 'main'
}
$bucket = "$bucketsdir\$Name"

if ((Test-Path "$bucket\bucket") -and !$Root) {
Expand All @@ -37,7 +39,7 @@ function bucketdir($name) {
function known_bucket_repos {
$json = "$PSScriptRoot\..\buckets.json"

return Get-Content $json -raw | convertfrom-json -ea stop
return Get-Content $json -Raw | ConvertFrom-Json -ErrorAction stop
}

function known_bucket_repo($name) {
Expand All @@ -46,11 +48,11 @@ function known_bucket_repo($name) {
}

function known_buckets {
known_bucket_repos | ForEach-Object { $_.psobject.properties | Select-Object -expand 'name' }
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$', '' }
return Get-ChildItem $dir | Where-Object { $_.Name.EndsWith('.json') } | ForEach-Object { $_.Name -replace '.json$', '' }
}

function Get-LocalBucket {
Expand All @@ -68,65 +70,134 @@ function buckets {
return Get-LocalBucket
}

function find_manifest($app, $bucket) {
if ($bucket) {
$manifest = manifest $app $bucket
if ($manifest) { return $manifest, $bucket }
return $null
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
}
}
}

foreach($bucket in Get-LocalBucket) {
$manifest = manifest $app $bucket
if($manifest) { return $manifest, $bucket }
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 add_bucket($name, $repo) {
if (!$name) { "<name> missing"; $usage_add; exit 1 }
if (!$repo) {
$repo = known_bucket_repo $name
if (!$repo) { "Unknown bucket '$name'. Try specifying <repo>."; $usage_add; exit 1 }
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)) {
abort "Git is required for buckets. Run 'scoop install git' and try again."
error "Git is required for buckets. Run 'scoop install git' and try again."
return 1
}

$dir = Find-BucketDirectory $name -Root
if (test-path $dir) {
if (Test-Path $dir) {
warn "The '$name' bucket already exists. Use 'scoop bucket rm $name' to remove it."
exit 0
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
Write-Host 'Checking repo... ' -NoNewline
$out = git_cmd ls-remote $repo 2>&1
if ($lastexitcode -ne 0) {
abort "'$repo' doesn't look like a valid git repository`n`nError given:`n$out"
if ($LASTEXITCODE -ne 0) {
error "'$repo' doesn't look like a valid git repository`n`nError given:`n$out"
return 1
}
write-host 'ok'
Write-Host 'OK'

ensure $bucketsdir > $null
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) {
if (!$name) { "<name> missing"; $usage_rm; exit 1 }
$dir = Find-BucketDirectory $name -Root
if (!(test-path $dir)) {
abort "'$name' bucket not found."
if (!(Test-Path $dir)) {
error "'$name' bucket not found."
return 1
}

Remove-Item $dir -r -force -ea stop
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) {
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
Expand All @@ -135,15 +206,15 @@ function new_issue_msg($app, $bucket, $title, $body) {
$url = "https://$($Matches.Provider)/$($Matches.User)/$($Matches.Repo)"
}

if(!$url) { return 'Please contact the bucket maintainer!' }
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) {
if ($body) {
$url += "&body=$body"
}
}
Expand Down
32 changes: 0 additions & 32 deletions lib/install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -83,38 +83,6 @@ function install_app($app, $architecture, $global, $suggested, $use_cache = $tru
show_notes $manifest $dir $original_dir $persist_dir
}

function locate($app, $bucket) {
Show-DeprecatedWarning $MyInvocation 'Find-Manifest'
return Find-Manifest $app $bucket
}

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 {
# check buckets
$manifest, $bucket = find_manifest $app $bucket

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 dl_with_cache($app, $version, $url, $to, $cookies = $null, $use_cache = $true) {
$cached = fullpath (cache_path $app $version $url)

Expand Down
73 changes: 39 additions & 34 deletions libexec/scoop-bucket.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -23,42 +23,47 @@ param($cmd, $name, $repo)
. "$PSScriptRoot\..\lib\buckets.ps1"
. "$PSScriptRoot\..\lib\help.ps1"

$usage_add = "usage: scoop bucket add <name> [<repo>]"
$usage_rm = "usage: scoop bucket rm <name>"
$usage_add = 'usage: scoop bucket add <name> [<repo>]'
$usage_rm = 'usage: scoop bucket rm <name>'

function list_buckets {
$buckets = @()

foreach ($bucket in Get-LocalBucket) {
$source = Find-BucketDirectory $bucket -Root
$manifests = (
Get-ChildItem "$source\bucket" -Force -Recurse -ErrorAction SilentlyContinue |
Measure-Object | Select-Object -ExpandProperty Count
)
$updated = 'N/A'
if ((Test-Path (Join-Path $source '.git')) -and (Get-Command git -ErrorAction SilentlyContinue)) {
$updated = git -C $source log --format='%aD' -n 1 | Get-Date
$source = git -C $source config remote.origin.url
} else {
$updated = (Get-Item "$source\bucket").LastWriteTime
$source = friendly_path $source
switch ($cmd) {
'add' {
if (!$name) {
'<name> missing'
$usage_add
exit 1
}
$buckets += New-Object PSObject -Property @{
Name = $bucket
Source = $source
Updated = $updated
Manifests = $manifests
if (!$repo) {
$repo = known_bucket_repo $name
if (!$repo) {
"Unknown bucket '$name'. Try specifying <repo>."
$usage_add
exit 1
}
}
$status = add_bucket $name $repo
exit $status
}
'rm' {
if (!$name) {
'<name> missing'
$usage_rm
exit 1
}
$status = rm_bucket $name
exit $status
}
'list' {
list_buckets
exit 0
}
'known' {
known_buckets
exit 0
}
default {
"scoop bucket: cmd '$cmd' not supported"
my_usage
exit 1
}
return $buckets | Select-Object Name, Source, Updated, Manifests
}

switch ($cmd) {
'add' { add_bucket $name $repo }
'rm' { rm_bucket $name }
'list' { list_buckets }
'known' { known_buckets }
default { "scoop bucket: cmd '$cmd' not supported"; my_usage; exit 1 }
}

exit 0
19 changes: 11 additions & 8 deletions libexec/scoop-home.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,20 @@ param($app)
. "$PSScriptRoot\..\lib\manifest.ps1"
. "$PSScriptRoot\..\lib\buckets.ps1"

if($app) {
$manifest, $bucket = find_manifest $app
if($manifest) {
if([string]::isnullorempty($manifest.homepage)) {
if ($app) {
$null, $manifest, $bucket, $null = Find-Manifest $app
if ($manifest) {
if ($manifest.homepage) {
Start-Process $manifest.homepage
} else {
abort "Could not find homepage in manifest for '$app'."
}
Start-Process $manifest.homepage
}
else {
} else {
abort "Could not find manifest for '$app'."
}
} else { my_usage }
} else {
my_usage
exit 1
}

exit 0
2 changes: 1 addition & 1 deletion libexec/scoop-virustotal.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ Function Submit-ToVirusTotal ($url, $app, $do_scan, $retrying=$False) {
$apps | ForEach-Object {
$app = $_
# write-host $app
$manifest, $bucket = find_manifest $app
$null, $manifest, $bucket, $null = Find-Manifest $app
if(!$manifest) {
$exit_code = $exit_code -bor $_ERR_NO_INFO
warn "$app`: manifest not found"
Expand Down
1 change: 0 additions & 1 deletion test/Import-Bucket-Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,3 @@ Describe 'manifest validates against the schema' -Tag 'Manifests' {
}
}
}

0 comments on commit 32de4c5

Please sign in to comment.