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

This commit is contained in:
Hsiao-nan Cheung authored and GitHub committed 2022-06-07 09:31:30 +08:00
1 parent 78c1bc45b4
commit ccd067b2b1
14 files changed
+95 -102

No files matched your search

+4
View File
@@ -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))
+1 -34
View File
@@ -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 (
@@ -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"
+5 -4
View File
@@ -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) {
+1 -2
View File
@@ -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) {
+1 -2
View File
@@ -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" })."
+43 -8
View File
@@ -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"
@@ -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
}
@@ -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
}
+2 -3
View File
@@ -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
+2 -2
View File
@@ -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='
@@ -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..."
+2 -2
View File
@@ -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
+8 -18
View File
@@ -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'
@@ -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) {
+1 -1
View File
@@ -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"
+1 -1
View File
@@ -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"
+13 -14
View File
@@ -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'
@@ -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) {
@@ -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"
@@ -191,7 +191,7 @@ 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)"
@@ -199,14 +199,13 @@ Function Submit-ToVirusTotal ($url, $app, $do_scan, $retrying = $False) {
}
}
$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
@@ -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)"
+11 -11
View File
@@ -69,30 +69,30 @@ Describe 'Package Dependencies' -Tag 'Scoop' {
BeforeAll {
Mock Test-HelperInstalled { $false }
Mock get_config { $true } -ParameterFilter { $name -eq 'MSIEXTRACT_USE_LESSMSI' }
Mock Find-Manifest { $null, @{}, $null, $null } -ParameterFilter { $AppName -eq 'lessmsi' }
Mock Find-Manifest { $null, @{ url = 'test.msi' }, $null, $null } -ParameterFilter { $AppName -eq '7zip' }
Mock Find-Manifest { $null, @{}, $null, $null } -ParameterFilter { $AppName -eq 'innounp' }
Mock Get-Manifest { 'lessmsi', @{}, $null, $null } -ParameterFilter { $app -eq 'lessmsi' }
Mock Get-Manifest { '7zip', @{ url = 'test.msi' }, $null, $null } -ParameterFilter { $app -eq '7zip' }
Mock Get-Manifest { 'innounp', @{}, $null, $null } -ParameterFilter { $app -eq 'innounp' }
}
It 'Resolve install dependencies' {
Mock Find-Manifest { $null, @{ url = 'test.7z' }, $null, $null }
Mock Get-Manifest { 'test', @{ url = 'test.7z' }, $null, $null }
Get-Dependency -AppName 'test' -Architecture '32bit' | Should -Be @('lessmsi', '7zip', 'test')
Mock Find-Manifest { $null, @{ innosetup = $true }, $null, $null }
Mock Get-Manifest { 'test', @{ innosetup = $true }, $null, $null }
Get-Dependency -AppName 'test' -Architecture '32bit' | Should -Be @('innounp', 'test')
}
It 'Resolve script dependencies' {
Mock Find-Manifest { $null, @{ pre_install = 'Expand-7zipArchive ' }, $null, $null }
Mock Get-Manifest { 'test', @{ pre_install = 'Expand-7zipArchive ' }, $null, $null }
Get-Dependency -AppName 'test' -Architecture '32bit' | Should -Be @('lessmsi', '7zip', 'test')
}
It 'Resolve runtime dependencies' {
Mock Find-Manifest { $null, @{}, $null, $null } -ParameterFilter { $AppName -eq 'depends' }
Mock Find-Manifest { $null, @{ depends = 'depends' }, $null, $null }
Mock Get-Manifest { 'depends', @{}, $null, $null } -ParameterFilter { $app -eq 'depends' }
Mock Get-Manifest { 'test', @{ depends = 'depends' }, $null, $null }
Get-Dependency -AppName 'test' -Architecture '32bit' | Should -Be @('depends', 'test')
}
It 'Keep bucket name of app' {
Mock Find-Manifest { $null, @{}, $null, $null } -ParameterFilter { $AppName -eq 'bucket/depends' }
Mock Find-Manifest { $null, @{ depends = 'bucket/depends' }, $null, $null }
Get-Dependency -AppName 'anotherbucket/test' -Architecture '32bit' | Should -Be @('bucket/depends', 'anotherbucket/test')
Mock Get-Manifest { 'depends', @{}, 'anotherbucket', $null } -ParameterFilter { $app -eq 'anotherbucket/depends' }
Mock Get-Manifest { 'test', @{ depends = 'anotherbucket/depends' }, 'bucket', $null }
Get-Dependency -AppName 'bucket/test' -Architecture '32bit' | Should -Be @('anotherbucket/depends', 'bucket/test')
}
}
}