Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(manifest): Rename 'Find-Manifest()' to 'Get-Manifest()' #4966

Merged
merged 2 commits into from
Jun 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
- **scoop-search:** Require files in 'bucket' dir for remote known buckets ([#4943](https://github.com/ScoopInstaller/Scoop/issues/4943))
- **update:** Prevent uninstall when update ([#4949](https://github.com/ScoopInstaller/Scoop/issues/4949))

### Code Refactoring

- **manifest:** Rename 'Find-Manifest()' to 'Get-Manifest() ([#4966](https://github.com/ScoopInstaller/Scoop/issues/4966))

### Documentation

- **readme:** Update license badge ([#4929](https://github.com/ScoopInstaller/Scoop/issues/4929))
Expand Down
35 changes: 1 addition & 34 deletions lib/buckets.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -72,39 +72,6 @@ function buckets {
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 (
Expand Down Expand Up @@ -197,7 +164,7 @@ function rm_bucket($name) {
}

function new_issue_msg($app, $bucket, $title, $body) {
$app, $manifest, $bucket, $url = Find-Manifest $app $bucket
$app, $manifest, $bucket, $url = Get-Manifest "$bucket/$app"
$url = known_bucket_repo $bucket
$bucket_path = "$bucketsdir\$bucket"

Expand Down
9 changes: 5 additions & 4 deletions lib/core.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -941,11 +941,12 @@ function applist($apps, $global) {
return ,@($apps | ForEach-Object { ,@($_, $global) })
}

function parse_app([string] $app) {
if($app -match '(?:(?<bucket>[a-zA-Z0-9-_.]+)\/)?(?<app>.*\.json$|[a-zA-Z0-9-_.]+)(?:@(?<version>.*))?') {
return $matches['app'], $matches['bucket'], $matches['version']
function parse_app([string]$app) {
if ($app -match '^(?:(?<bucket>[a-zA-Z0-9-_.]+)/)?(?<app>.*\.json$|[a-zA-Z0-9-_.]+)(?:@(?<version>.*))?$') {
return $Matches['app'], $Matches['bucket'], $Matches['version']
} else {
return $app, $null, $null
}
return $app, $null, $null
}

function show_app($app, $bucket, $version) {
Expand Down
3 changes: 1 addition & 2 deletions lib/depends.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ function Get-Dependency {
$Unresolved = @()
)
process {
$AppName, $bucket, $null = parse_app $AppName
$AppName, $manifest, $bucket, $null = Get-Manifest $AppName
$Unresolved += $AppName
$null, $manifest, $null, $null = Find-Manifest $AppName $bucket

if (!$manifest) {
if (((Get-LocalBucket) -notcontains $bucket) -and $bucket) {
Expand Down
3 changes: 1 addition & 2 deletions lib/install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ function nightly_version($date, $quiet = $false) {
}

function install_app($app, $architecture, $global, $suggested, $use_cache = $true, $check_hash = $true) {
$app, $bucket, $null = parse_app $app
$app, $manifest, $bucket, $url = Find-Manifest $app $bucket
$app, $manifest, $bucket, $url = Get-Manifest $app

if(!$manifest) {
abort "Couldn't find manifest for '$app'$(if($url) { " at the URL $url" })."
Expand Down
51 changes: 43 additions & 8 deletions lib/manifest.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,47 @@ function url_manifest($url) {
$str | convertfrom-json
}

function Get-Manifest($app) {
$bucket, $manifest, $url = $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 {
$app, $bucket, $version = parse_app $app
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
if (!$app.EndsWith('.json')) {
$app += '.json'
}
if (Test-Path $app) {
$url = Convert-Path $app
$app = appname_from_url $url
$manifest = url_manifest $url
}
}
}
return $app, $manifest, $bucket, $url
}

function manifest($app, $bucket, $url) {
if($url) { return url_manifest $url }
if ($url) { return url_manifest $url }
parse_json (manifest_path $app $bucket)
}

function save_installed_manifest($app, $bucket, $dir, $url) {
if($url) {
if ($url) {
$wc = New-Object Net.Webclient
$wc.Headers.Add('User-Agent', (Get-UserAgent))
$wc.downloadstring($url) > "$dir\manifest.json"
Expand All @@ -51,7 +85,7 @@ function save_install_info($info, $dir) {

function install_info($app, $version, $global) {
$path = "$(versiondir $app $version $global)\install.json"
if(!(test-path $path)) { return $null }
if (!(Test-Path $path)) { return $null }
parse_json $path
}

Expand All @@ -73,20 +107,21 @@ function default_architecture {
}

function arch_specific($prop, $manifest, $architecture) {
if($manifest.architecture) {
if ($manifest.architecture) {
$val = $manifest.architecture.$architecture.$prop
if($val) { return $val } # else fallback to generic prop
if ($val) { return $val } # else fallback to generic prop
}

if($manifest.$prop) { return $manifest.$prop }
if ($manifest.$prop) { return $manifest.$prop }
}

function supports_architecture($manifest, $architecture) {
return -not [String]::IsNullOrEmpty((arch_specific 'url' $manifest $architecture))
}

function generate_user_manifest($app, $bucket, $version) { # 'autoupdate.ps1' 'buckets.ps1' 'manifest.ps1'
$null, $manifest, $bucket, $null = Find-Manifest $app $bucket
function generate_user_manifest($app, $bucket, $version) {
# 'autoupdate.ps1' 'buckets.ps1' 'manifest.ps1'
$app, $manifest, $bucket, $null = Get-Manifest "$bucket/$app"
if ("$($manifest.version)" -eq "$version") {
return manifest_path $app $bucket
}
Expand Down
5 changes: 2 additions & 3 deletions libexec/scoop-cat.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
param($app)

. "$PSScriptRoot\..\lib\json.ps1" # 'ConvertToPrettyJson'
. "$PSScriptRoot\..\lib\manifest.ps1" # 'Find-Manifest' (indirectly)
. "$PSScriptRoot\..\lib\manifest.ps1" # 'Get-Manifest'

if (!$app) { error '<app> missing'; my_usage; exit 1 }

$app, $bucket, $null = parse_app $app
$app, $manifest, $bucket, $url = Find-Manifest $app $bucket
$null, $manifest, $bucket, $url = Get-Manifest $app

if ($manifest) {
$style = get_config cat_style
Expand Down
4 changes: 2 additions & 2 deletions libexec/scoop-download.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
. "$PSScriptRoot\..\lib\getopt.ps1"
. "$PSScriptRoot\..\lib\json.ps1" # 'autoupdate.ps1' (indirectly)
. "$PSScriptRoot\..\lib\autoupdate.ps1" # 'generate_user_manifest' (indirectly)
. "$PSScriptRoot\..\lib\manifest.ps1" # 'default_architecture' 'generate_user_manifest' 'Find-Manifest' (indirectly)
. "$PSScriptRoot\..\lib\manifest.ps1" # 'default_architecture' 'generate_user_manifest' 'Get-Manifest'
. "$PSScriptRoot\..\lib\install.ps1"

$opt, $apps, $err = getopt $args 'fhua:' 'force', 'no-hash-check', 'no-update-scoop', 'arch='
Expand Down Expand Up @@ -51,7 +51,7 @@ foreach ($curr_app in $apps) {
$bucket = $version = $app = $manifest = $url = $null

$app, $bucket, $version = parse_app $curr_app
$app, $manifest, $bucket, $url = Find-Manifest $app $bucket
$app, $manifest, $bucket, $url = Get-Manifest "$bucket/$app"

info "Starting download for $app..."

Expand Down
4 changes: 2 additions & 2 deletions libexec/scoop-home.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
# Summary: Opens the app homepage
param($app)

. "$PSScriptRoot\..\lib\manifest.ps1" # 'Find-Manifest' (indirectly)
. "$PSScriptRoot\..\lib\manifest.ps1" # 'Get-Manifest'

if ($app) {
$null, $manifest, $bucket, $null = Find-Manifest $app
$null, $manifest, $bucket, $null = Get-Manifest $app
if ($manifest) {
if ($manifest.homepage) {
Start-Process $manifest.homepage
Expand Down
26 changes: 8 additions & 18 deletions libexec/scoop-info.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# -v, --verbose Show full paths and URLs

. "$PSScriptRoot\..\lib\getopt.ps1"
. "$PSScriptRoot\..\lib\manifest.ps1" # 'Find-Manifest' (indirectly)
. "$PSScriptRoot\..\lib\manifest.ps1" # 'Get-Manifest'
. "$PSScriptRoot\..\lib\versions.ps1" # 'Get-InstalledVersion'

$opt, $app, $err = getopt $args 'v' 'verbose'
Expand All @@ -13,31 +13,21 @@ $verbose = $opt.v -or $opt.verbose

if (!$app) { my_usage; exit 1 }

if ($app -match '^(ht|f)tps?://|\\\\') {
# check if $app is a URL or UNC path
$url = $app
$app = appname_from_url $url
$global = installed $app $true
$status = app_status $app $global
$manifest = url_manifest $url
$manifest_file = $url
} else {
# else $app is a normal app name
$global = installed $app $true
$app, $bucket, $null = parse_app $app
$status = app_status $app $global
$app, $manifest, $bucket, $url = Find-Manifest $app $bucket
}
$app, $manifest, $bucket, $url = Get-Manifest $app

if (!$manifest) {
abort "Could not find manifest for '$(show_app $app)' in local buckets."
}

$global = installed $app $true
$status = app_status $app $global
$install = install_info $app $status.version $global
$status.installed = $bucket -and $install.bucket -eq $bucket
$version_output = $manifest.version
if (!$manifest_file) {
$manifest_file = if ($bucket) { manifest_path $app $bucket } else { $url }
$manifest_file = if ($bucket) {
manifest_path $app $bucket
} else {
$url
}

if ($verbose) {
Expand Down
2 changes: 1 addition & 1 deletion libexec/scoop-install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
. "$PSScriptRoot\..\lib\getopt.ps1"
. "$PSScriptRoot\..\lib\json.ps1" # 'autoupdate.ps1' 'manifest.ps1' (indirectly)
. "$PSScriptRoot\..\lib\autoupdate.ps1" # 'generate_user_manifest' (indirectly)
. "$PSScriptRoot\..\lib\manifest.ps1" # 'default_architecture' 'generate_user_manifest' 'Select-CurrentVersion' (indirectly)
. "$PSScriptRoot\..\lib\manifest.ps1" # 'default_architecture' 'generate_user_manifest' 'Get-Manifest' 'Select-CurrentVersion' (indirectly)
. "$PSScriptRoot\..\lib\install.ps1"
. "$PSScriptRoot\..\lib\decompress.ps1"
. "$PSScriptRoot\..\lib\shortcuts.ps1"
Expand Down
2 changes: 1 addition & 1 deletion libexec/scoop-uninstall.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# -p, --purge Remove all persistent data

. "$PSScriptRoot\..\lib\getopt.ps1"
. "$PSScriptRoot\..\lib\manifest.ps1" # 'Select-CurrentVersion' (indirectly)
. "$PSScriptRoot\..\lib\manifest.ps1" # 'Get-Manifest' 'Select-CurrentVersion' (indirectly)
. "$PSScriptRoot\..\lib\install.ps1"
. "$PSScriptRoot\..\lib\shortcuts.ps1"
. "$PSScriptRoot\..\lib\psmodules.ps1"
Expand Down
27 changes: 13 additions & 14 deletions libexec/scoop-virustotal.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
# -u, --no-update-scoop Don't update Scoop before checking if it's outdated

. "$PSScriptRoot\..\lib\getopt.ps1"
. "$PSScriptRoot\..\lib\manifest.ps1" # 'Find-Manifest' (indirectly)
. "$PSScriptRoot\..\lib\manifest.ps1" # 'Get-Manifest'
. "$PSScriptRoot\..\lib\json.ps1" # 'json_path'
. "$PSScriptRoot\..\lib\install.ps1" # 'hash_for_url'
. "$PSScriptRoot\..\lib\depends.ps1" # 'Get-Dependency'
Expand Down Expand Up @@ -155,8 +155,8 @@ Function Submit-ToVirusTotal ($url, $app, $do_scan, $retrying = $False) {
$api_key = get_config virustotal_api_key
if ($do_scan -and !$api_key -and !$warned_no_api_key) {
$warned_no_api_key = $true
info "Submitting unknown apps needs a VirusTotal API key. " +
"Set it up with`n`tscoop config virustotal_api_key <API key>"
info 'Submitting unknown apps needs a VirusTotal API key. ' +
"Set it up with`n`tscoop config virustotal_api_key <API key>"

}
if (!$do_scan -or !$api_key) {
Expand All @@ -167,14 +167,14 @@ Function Submit-ToVirusTotal ($url, $app, $do_scan, $retrying = $False) {
try {
# Follow redirections (for e.g. sourceforge URLs) because
# VirusTotal analyzes only "direct" download links
$url = $url.Split("#").GetValue(0)
$url = $url.Split('#').GetValue(0)
$new_redir = $url
do {
$orig_redir = $new_redir
$new_redir = Submit-RedirectedUrl $orig_redir
} while ($orig_redir -ne $new_redir)
$requests += 1
$result = Invoke-WebRequest -Uri "https://www.virustotal.com/vtapi/v2/url/scan" -Body @{apikey=$api_key;url=$new_redir} -Method Post -UseBasicParsing
$result = Invoke-WebRequest -Uri 'https://www.virustotal.com/vtapi/v2/url/scan' -Body @{apikey = $api_key; url = $new_redir } -Method Post -UseBasicParsing
$submitted = $result.StatusCode -eq 200
if ($submitted) {
warn "$app`: not found`: submitted $url"
Expand All @@ -191,22 +191,21 @@ Function Submit-ToVirusTotal ($url, $app, $do_scan, $retrying = $False) {
Submit-ToVirusTotal $new_redir $app $do_scan $True
} else {
warn "$app`: VirusTotal submission of $url failed`:`n" +
"`tAPI returned $($result.StatusCode) after retrying"
"`tAPI returned $($result.StatusCode) after retrying"
}
} catch [Exception] {
warn "$app`: VirusTotal submission failed`: $($_.Exception.Message)"
return
}
}

$apps | ForEach-Object {
$app = $_
foreach ($app in $apps) {
# write-host $app
$null, $manifest, $bucket, $null = Find-Manifest $app
if(!$manifest) {
$null, $manifest, $bucket, $null = Get-Manifest $app
if (!$manifest) {
$exit_code = $exit_code -bor $_ERR_NO_INFO
warn "$app`: manifest not found"
return
continue
}

$urls = script:url $manifest $architecture
Expand All @@ -215,17 +214,17 @@ $apps | ForEach-Object {
$hash = hash_for_url $manifest $url $architecture

try {
if($hash) {
if ($hash) {
$exit_code = $exit_code -bor (Search-VirusTotal $hash $app)
} else {
warn "$app`: Can't find hash for $url"
}
} catch [Exception] {
$exit_code = $exit_code -bor $_ERR_EXCEPTION
if ($_.Exception.Message -like "*(404)*") {
if ($_.Exception.Message -like '*(404)*') {
Submit-ToVirusTotal $url $app ($opt.scan -or $opt.s)
} else {
if ($_.Exception.Message -match "\(204|429\)") {
if ($_.Exception.Message -match '\(204|429\)') {
abort "$app`: VirusTotal request failed`: $($_.Exception.Message)", $exit_code
}
warn "$app`: VirusTotal request failed`: $($_.Exception.Message)"
Expand Down
Loading