From 616be623a9592e33158a121cccc2e9d95b2b4db7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20=C4=8C=C3=A1bera?= Date: Sat, 16 May 2020 15:03:02 +0200 Subject: [PATCH] lint: error, warn, success -> Write-UserMessage #13, #21, #22 --- bin/auto-pr.ps1 | 4 +-- bin/checkver.ps1 | 4 +-- bin/uninstall.ps1 | 11 +++++---- lib/Diagnostic.ps1 | 2 +- lib/Helpers.ps1 | 4 +-- lib/Update.ps1 | 1 - lib/buckets.ps1 | 8 +++--- lib/core.ps1 | 45 ++++++++++++++++----------------- lib/depends.ps1 | 2 +- lib/install.ps1 | 48 +++++++++++++++++++----------------- lib/manifest.ps1 | 11 ++++++--- lib/psmodules.ps1 | 2 +- lib/uninstall.ps1 | 17 +++++++------ libexec/scoop-cache.ps1 | 2 +- libexec/scoop-cleanup.ps1 | 26 ++++++------------- libexec/scoop-depends.ps1 | 3 +-- libexec/scoop-download.ps1 | 14 +++++------ libexec/scoop-hold.ps1 | 11 +++++---- libexec/scoop-install.ps1 | 27 ++++++++++++++------ libexec/scoop-list.ps1 | 2 +- libexec/scoop-reset.ps1 | 32 +++++++++++------------- libexec/scoop-search.ps1 | 10 +++++--- libexec/scoop-status.ps1 | 15 ++++++++--- libexec/scoop-unhold.ps1 | 11 +++++---- libexec/scoop-uninstall.ps1 | 16 +++++++----- libexec/scoop-update.ps1 | 6 +++-- libexec/scoop-virustotal.ps1 | 2 +- 27 files changed, 181 insertions(+), 155 deletions(-) diff --git a/bin/auto-pr.ps1 b/bin/auto-pr.ps1 index 0e8927e9e3..84ebd70984 100644 --- a/bin/auto-pr.ps1 +++ b/bin/auto-pr.ps1 @@ -115,7 +115,7 @@ function pull_requests($json, [String] $app, [String] $upstream, [String] $manif execute "hub push origin $branch" if ($LASTEXITCODE -gt 0) { - error "Push failed! (hub push origin $branch)" + Write-UserMessage -Message "Push failed! (hub push origin $branch)" -Err execute 'hub reset' return } @@ -168,7 +168,7 @@ hub diff --name-only | ForEach-Object { $app = ([System.IO.Path]::GetFileNameWithoutExtension($manifest)) $json = parse_json $manifest if (!$json.version) { - error "Invalid manifest: $manifest ..." + Write-UserMessage -Message "Invalid manifest: $manifest ..." -Err return } $version = $json.version diff --git a/bin/checkver.ps1 b/bin/checkver.ps1 index 78e8e8d49a..710bb252be 100644 --- a/bin/checkver.ps1 +++ b/bin/checkver.ps1 @@ -113,7 +113,7 @@ $Queue | ForEach-Object { if ($json.checkver -eq 'github') { if (!$json.homepage.StartsWith('https://github.com/')) { - error "$name checkver expects the homepage to be a github repository" + Write-UserMessage -Message "$name checkver expects the homepage to be a github repository" -Err } $url = $json.homepage + '/releases/latest' $regex = $githubRegex @@ -310,7 +310,7 @@ while ($in_progress -gt 0) { } autoupdate $App $Dir $json $ver $matchesHashtable } catch { - error $_.Exception.Message + Write-UserMessage -Message $_.Exception.Message -Err } } } diff --git a/bin/uninstall.ps1 b/bin/uninstall.ps1 index 53565a4282..7aee8eedf8 100644 --- a/bin/uninstall.ps1 +++ b/bin/uninstall.ps1 @@ -16,15 +16,16 @@ param( } if ($global -and !(is_admin)) { - error 'You need admin rights to uninstall globally.' + Write-UserMessage -Message 'You need admin rights to uninstall globally.' -Err exit 1 } +$message = 'This will uninstall Scoop and all the programs that have been installed with Scoop!' if ($purge) { - warn 'This will uninstall Scoop, all the programs that have been installed with Scoop and all persisted data!' -} else { - warn 'This will uninstall Scoop and all the programs that have been installed with Scoop!' + $message = 'This will uninstall Scoop, all the programs that have been installed with Scoop and all persisted data!' } + +Write-UserMessage -Message $message -Warning $yn = Read-Host 'Are you sure? (yN)' if ($yn -notlike 'y*') { exit } @@ -70,4 +71,4 @@ if ($purge) { remove_from_path (shimdir $false) if ($global) { remove_from_path (shimdir $true) } -success 'Scoop has been uninstalled.' +Write-UserMessage -Message 'Scoop has been uninstalled.' -Success diff --git a/lib/Diagnostic.ps1 b/lib/Diagnostic.ps1 index 2b8d1c59f7..2fccea65e2 100644 --- a/lib/Diagnostic.ps1 +++ b/lib/Diagnostic.ps1 @@ -1,7 +1,7 @@ <# Diagnostic tests. Return $true if the test passed, otherwise $false. -Use 'warn' to highlight the issue, and follow up with the recommended actions to rectify. +Use 'Write-UserMessage -Warning' to highlight the issue, and follow up with the recommended actions to rectify. #> 'core', 'buckets', 'decompress' | ForEach-Object { diff --git a/lib/Helpers.ps1 b/lib/Helpers.ps1 index 7a91065fce..d1c9981463 100644 --- a/lib/Helpers.ps1 +++ b/lib/Helpers.ps1 @@ -44,8 +44,8 @@ function Write-UserMessage { if ($Success) { $Severity = 'Success' } switch ($Severity) { - 'Info' { $sev = 'INFO '; $foreColor = 'DarkGray' } - 'Warning' { $sev = 'WARN '; $foreColor = 'DarkYellow' } + 'Info' { $sev = 'INFO '; $foreColor = 'DarkGray' } + 'Warning' { $sev = 'WARN '; $foreColor = 'DarkYellow' } 'Error' { $sev = 'ERROR '; $foreColor = 'DarkRed' } 'Success' { $sev = ''; $foreColor = 'DarkGreen' } default { diff --git a/lib/Update.ps1 b/lib/Update.ps1 index bf7d3bb253..4fb16df3bf 100644 --- a/lib/Update.ps1 +++ b/lib/Update.ps1 @@ -187,7 +187,6 @@ function Update-Scoop { Write-UserMessage -Message 'Scoop was updated successfully!' -Success } - function Update-App { <# .SYNOPSIS diff --git a/lib/buckets.ps1 b/lib/buckets.ps1 index 2be8f93f6a..a0adda1209 100644 --- a/lib/buckets.ps1 +++ b/lib/buckets.ps1 @@ -1,4 +1,6 @@ -. "$PSScriptRoot\core.ps1" +'core', 'git' | ForEach-Object { + . "$PSScriptRoot\$_.ps1" +} $bucketsdir = "$scoopdir\buckets" $SCOOP_BUCKETS_DIRECTORY = $bucketsdir @@ -83,7 +85,7 @@ function add_bucket($name, $repo) { $dir = Find-BucketDirectory $name -Root if (test-path $dir) { - warn "The '$name' bucket already exists. Use 'scoop bucket rm $name' to remove it." + Write-UserMessage -Message "The '$name' bucket already exists. Use 'scoop bucket rm $name' to remove it." -Warning exit 0 } @@ -97,7 +99,7 @@ function add_bucket($name, $repo) { ensure $bucketsdir > $null $dir = ensure $dir git_clone "$repo" "`"$dir`"" -q - success "The $name bucket was added successfully." + Write-UserMessage -Message "The $name bucket was added successfully." -Success } function rm_bucket($name) { diff --git a/lib/core.ps1 b/lib/core.ps1 index b999ef0c68..2161949802 100644 --- a/lib/core.ps1 +++ b/lib/core.ps1 @@ -33,8 +33,8 @@ function Show-DeprecatedWarning { #> param($Invocation, [String] $New) - warn ('"{0}" will be deprecated. Please change your code/manifest to use "{1}"' -f $Invocation.MyCommand.Name, $New) - Write-Host " -> $($Invocation.PSCommandPath):$($Invocation.ScriptLineNumber):$($Invocation.OffsetInLine)" -ForegroundColor DarkGray + Write-UserMessage -Message ('"{0}" will be deprecated. Please change your code/manifest to use "{1}"' -f $Invocation.MyCommand.Name, $New) -Warning + Write-UserMessage -Message " -> $($Invocation.PSCommandPath):$($Invocation.ScriptLineNumber):$($Invocation.OffsetInLine)" -Color DarkGray } function load_cfg($file) { @@ -45,7 +45,7 @@ function load_cfg($file) { try { return (Get-Content $file -Raw | ConvertFrom-Json -ErrorAction Stop) } catch { - Write-Host "ERROR loading $file`: $($_.exception.message)" + Write-UserMessage -Message "loading $file`: $($_.Exception.Message)" -Err } } @@ -105,7 +105,7 @@ function setup_proxy() { [net.webrequest]::defaultwebproxy.credentials = new-object net.networkcredential($username, $password) } } catch { - warn "Failed to use proxy '$proxy': $($_.exception.message)" + Write-UserMessage -Message "Failed to use proxy '$proxy': $($_.exception.message)" -Warning } } @@ -435,10 +435,8 @@ function Invoke-ExternalCommand { try { $Process.Start() | Out-Null } catch { - if ($Activity) { - Write-Host "error." -ForegroundColor DarkRed - } - error $_.Exception.Message + if ($Activity) { Write-UserMessage -Message 'error.' -Color DarkRed } + Write-UserMessage -Message $_.Exception.Message -Err return $false } if ($LogPath -and ($FilePath -notmatch '(^|\W)msiexec($|\W)')) { @@ -450,19 +448,15 @@ function Invoke-ExternalCommand { if ($Activity) { Write-Host "done." -ForegroundColor DarkYellow } - warn $ContinueExitCodes[$Process.ExitCode] + Write-UserMessage -Message $ContinueExitCodes[$Process.ExitCode] -Warning return $true } else { - if ($Activity) { - Write-Host "error." -ForegroundColor DarkRed - } - error "Exit code was $($Process.ExitCode)!" + if ($Activity) { Write-UserMessage -Message 'error.' -Color DarkRed } + Write-UserMessage -Message "Exit code was $($Process.ExitCode)!" -Err return $false } } - if ($Activity) { - Write-Host "done." -ForegroundColor Green - } + if ($Activity) { Write-Host "done." -ForegroundColor Green } return $true } @@ -668,22 +662,23 @@ function Confirm-InstallationStatus { if (installed $App $true) { $Installed += , @($App, $true) } elseif (installed $App $false) { - error "'$App' isn't installed globally, but it is installed for your account." - warn "Try again without the --global (or -g) flag instead." + Write-UserMessage -Message "'$App' isn't installed globally, but it is installed for your account." -Err + Write-UserMessage -Message "Try again without the --global (or -g) flag instead." -Warning } else { - error "'$App' isn't installed." + Write-UserMessage -Message "'$App' isn't installed." -Err } } else { if (installed $App $false) { $Installed += , @($App, $false) } elseif (installed $App $true) { - error "'$App' isn't installed for your account, but it is installed globally." - warn "Try again with the --global (or -g) flag instead." + Write-UserMessage -Message "'$App' isn't installed for your account, but it is installed globally." -Err + Write-UserMessage -Message "Try again with the --global (or -g) flag instead." -Warning } else { - error "'$App' isn't installed." + Write-UserMessage -Message "'$App' isn't installed." -Err } } } + return , $Installed } @@ -976,8 +971,10 @@ $configFile = "$configHome\scoop\config.json" if ((Test-Path "$env:USERPROFILE\.scoop") -and !(Test-Path $configFile)) { New-Item -ItemType Directory (Split-Path -Path $configFile) -ErrorAction Ignore | Out-Null Move-Item "$env:USERPROFILE\.scoop" $configFile - write-host "WARN Scoop configuration has been migrated from '~/.scoop'" -f darkyellow - write-host "WARN to '$configFile'" -f darkyellow + Write-UserMessage -Warning -Message @( + "Scoop configuration has been migrated from '~/.scoop'" + "to '$configFile'" + ) } # Load Scoop config diff --git a/lib/depends.ps1 b/lib/depends.ps1 index 88d06efe00..3b4028e42b 100644 --- a/lib/depends.ps1 +++ b/lib/depends.ps1 @@ -28,7 +28,7 @@ function dep_resolve($app, $arch, $resolved, $unresolved) { if (!$manifest) { if (((Get-LocalBucket) -notcontains $bucket) -and $bucket) { - warn "Bucket '$bucket' not installed. Add it with 'scoop bucket add $bucket' or 'scoop bucket add $bucket '." + Write-UserMessage -Message "Bucket '$bucket' not installed. Add it with 'scoop bucket add $bucket' or 'scoop bucket add $bucket '." -Warning } abort "Couldn't find manifest for '$app'$(if(!$bucket) { '.' } else { " from '$bucket' bucket." })" } diff --git a/lib/install.ps1 b/lib/install.ps1 index 23c4f5536f..b6dc1c010e 100644 --- a/lib/install.ps1 +++ b/lib/install.ps1 @@ -5,9 +5,10 @@ function nightly_version($date, $quiet = $false) { $date_str = $date.tostring("yyyyMMdd") if (!$quiet) { - warn "This is a nightly version. Downloaded files won't be verified." + Write-UserMessage -Message "This is a nightly version. Downloaded files won't be verified." -Warning } - "nightly-$date_str" + + return "nightly-$date_str" } function install_app($app, $architecture, $global, $suggested, $use_cache = $true, $check_hash = $true) { @@ -68,7 +69,7 @@ function install_app($app, $architecture, $global, $suggested, $use_cache = $tru $suggested[$app] = $manifest.suggest } - success "'$app' ($version) was installed successfully!" + Write-UserMessage -Message "'$app' ($version) was installed successfully!" -Success show_notes $manifest $dir $original_dir $persist_dir } @@ -286,11 +287,14 @@ function dl_with_cache_aria2($app, $version, $manifest, $architecture, $dir, $co Write-Host "Starting download with aria2 ..." Invoke-Expression $aria2 - if ($lastexitcode -gt 0) { - error "Download failed! (Error $lastexitcode) $(aria_exit_code $lastexitcode)" - error $urlstxt_content - error $aria2 - abort $(new_issue_msg $app $bucket "download via aria2 failed") + if ($LASTEXITCODE -gt 0) { + Write-UserMessage -Err -Message @( + "Download failed! (Error $LASTEXITCODE) $(aria_exit_code $LASTEXITCODE)" + $urlstxt_content + $aria2 + ) + + abort (new_issue_msg $app $bucket "download via aria2 failed") } # remove aria2 input file when done @@ -312,15 +316,15 @@ function dl_with_cache_aria2($app, $version, $manifest, $architecture, $dir, $co $manifest_hash = hash_for_url $manifest $url $architecture $ok, $err = check_hash $data.$url.source $manifest_hash $(show_app $app $bucket) if (!$ok) { - error $err + Write-UserMessage -Message $err -Err if (test-path $data.$url.source) { # rm cached file Remove-Item -force $data.$url.source } if ($url.Contains('sourceforge.net')) { - Write-Host -f yellow 'SourceForge.net is known for causing hash validation fails. Please try again before opening a ticket.' + Write-UserMessage -Message 'SourceForge.net is known for causing hash validation fails. Please try again before opening a ticket.' -Color Yellow } - abort $(new_issue_msg $app $bucket "hash check failed") + abort (new_issue_msg $app $bucket "hash check failed") } } @@ -501,7 +505,7 @@ function dl_progress($read, $total, $url) { function dl_urls($app, $version, $manifest, $bucket, $architecture, $dir, $use_cache = $true, $check_hash = $true) { # we only want to show this warning once - if (!$use_cache) { warn "Cache is being ignored." } + if (!$use_cache) { Write-UserMessage -Message "Cache is being ignored." -Warning } # can be multiple urls: if there are, then msi or installer should go last, # so that $fname is set properly @@ -536,7 +540,7 @@ function dl_urls($app, $version, $manifest, $bucket, $architecture, $dir, $use_c $manifest_hash = hash_for_url $manifest $url $architecture $ok, $err = check_hash "$dir\$fname" $manifest_hash $(show_app $app $bucket) if (!$ok) { - error $err + Write-UserMessage -Message $err -Err $cached = cache_path $app $version $url if (test-path $cached) { # rm cached file @@ -571,7 +575,7 @@ function dl_urls($app, $version, $manifest, $bucket, $architecture, $dir, $use_c } elseif ($fname -match '\.msi$') { # check manifest doesn't use deprecated install method if (msi $manifest $architecture) { - warn "MSI install is deprecated. If you maintain this manifest, please refer to the manifest reference docs." + Write-UserMessage -Message "MSI install is deprecated. If you maintain this manifest, please refer to the manifest reference docs." -Warning } else { $extract_fn = 'Expand-MsiArchive' } @@ -633,7 +637,7 @@ function hash_for_url($manifest, $url, $arch) { function check_hash($file, $hash, $app_name) { $file = fullpath $file if (!$hash) { - warn "Warning: No hash in manifest. SHA256 for '$(fname $file)' is:`n $(compute_hash $file 'sha256')" + Write-UserMessage -Message "Warning: No hash in manifest. SHA256 for '$(fname $file)' is:`n $(compute_hash $file 'sha256')" -Warning return $true, $null } @@ -676,7 +680,7 @@ function compute_hash($file, $algname) { return [string]::join('', $hexbytes) } } catch { - error $_.exception.message + Write-UserMessage -Message $_.exception.message -Err } finally { if ($fs) { $fs.dispose() } if ($alg) { $alg.dispose() } @@ -800,10 +804,10 @@ function run_uninstaller($manifest, $architecture, $dir) { $exe = "$dir\$($uninstaller.file)" $arg = args $uninstaller.args if (!(is_in_dir $dir $exe)) { - warn "Error in manifest: Installer $exe is outside the app directory, skipping." + Write-UserMessage -Message "Error in manifest: Installer $exe is outside the app directory, skipping." -Warning $exe = $null; } elseif (!(test-path $exe)) { - warn "Uninstaller $exe is missing, skipping." + Write-UserMessage -Message "Uninstaller $exe is missing, skipping." -Warning $exe = $null; } } @@ -847,15 +851,15 @@ function create_shims($manifest, $dir, $global, $arch) { function rm_shim($name, $shimdir) { $shim = "$shimdir\$name.ps1" + # Handle no shim from failed install if (!(test-path $shim)) { - # handle no shim from failed install - warn "Shim for '$name' is missing. Skipping." + Write-UserMessage -Message "Shim for '$name' is missing. Skipping." -Warning } else { write-output "Removing shim for '$name'." Remove-Item $shim } - # other shim types might be present + # Other shim types might be present '', '.exe', '.shim', '.cmd' | ForEach-Object { if (test-path -Path "$shimdir\$name$_" -PathType leaf) { Remove-Item "$shimdir\$name$_" @@ -944,7 +948,7 @@ function ensure_install_dir_not_in_path($dir, $global) { if (!$global) { $fixed, $removed = find_dir_or_subdir (env 'path' $true) "$dir" if ($removed) { - $removed | ForEach-Object { warn "Installer added '$_' to system path. You might want to remove this manually (requires admin permission)." } + $removed | ForEach-Object { Write-UserMessage -Message "Installer added '$_' to system path. You might want to remove this manually (requires admin permission)." -Warning } } } } diff --git a/lib/manifest.ps1 b/lib/manifest.ps1 index 0655205613..e0c94eae86 100644 --- a/lib/manifest.ps1 +++ b/lib/manifest.ps1 @@ -19,7 +19,8 @@ function url_manifest($url) { $wc.Headers.Add('User-Agent', (Get-UserAgent)) $str = $wc.downloadstring($url) } catch [system.management.automation.methodinvocationexception] { - warn "error: $($_.exception.innerexception.message)" + # TODO: ??? + Write-UserMessage -Message "error: $($_.Exception.InnerException.Message)" -Warning } catch { throw } @@ -90,7 +91,7 @@ function default_architecture { try { $arch = ensure_architecture $arch } catch { - warn 'Invalid default architecture configured. Determining default system architecture' + Write-UserMessage -Message 'Invalid default architecture configured. Determining default system architecture' -Warning $arch = $system } } @@ -116,8 +117,10 @@ function generate_user_manifest($app, $bucket, $version) { if ("$($manifest.version)" -eq "$version") { return manifest_path $app $bucket } - warn "Given version ($version) does not match manifest ($($manifest.version))" - warn "Attempting to generate manifest for '$app' ($version)" + Write-UserMessage -Warning -Message @( + "Given version ($version) does not match manifest ($($manifest.version))" + "Attempting to generate manifest for '$app' ($version)" + ) if (!($manifest.autoupdate)) { abort "'$app' does not have autoupdate capability`r`ncouldn't find manifest for '$app@$version'" diff --git a/lib/psmodules.ps1 b/lib/psmodules.ps1 index a43ba57d9f..e80465362c 100644 --- a/lib/psmodules.ps1 +++ b/lib/psmodules.ps1 @@ -23,7 +23,7 @@ function install_psmodule($manifest, $dir, $global) { write-host "Linking $(friendly_path $linkfrom) => $(friendly_path $dir)" if (test-path $linkfrom) { - warn "$(friendly_path $linkfrom) already exists. It will be replaced." + Write-UserMessage -Message "$(friendly_path $linkfrom) already exists. It will be replaced." -Warning & "$env:COMSPEC" /c "rmdir `"$linkfrom`"" } diff --git a/lib/uninstall.ps1 b/lib/uninstall.ps1 index 3320dc9af2..e09fa66dd1 100644 --- a/lib/uninstall.ps1 +++ b/lib/uninstall.ps1 @@ -49,9 +49,12 @@ function Uninstall-ScoopApplication { $plPr = pluralize $processes.Count 'Process' 'Processes' $plId = pluralize $processes.Count 'ID' 'IDs' $plIs = pluralize $processes.Count 'is' 'are' - error 'Application is still running!' - error "$plPr with following $plId $plIs blocking uninstallation:" - error ($processes.Id -join ", ") + Write-UserMessage -Err -Message @( + 'Application is still running!' + "$plPr with following $plId $plIs blocking uninstallation:" + ($processes.Id -join ', ') + ) + return $false } @@ -64,7 +67,7 @@ function Uninstall-ScoopApplication { try { Test-Path $dir -ErrorAction Stop | Out-Null } catch [UnauthorizedAccessException] { - error "Access denied: $dir. You might need to restart." + Write-UserMessage -Message "Access denied: $dir. You might need to restart." -Err return $false } @@ -96,7 +99,7 @@ function Uninstall-ScoopApplication { Remove-Item $dir -Recurse -Force -ErrorAction Stop } catch { if (Test-Path $dir) { - error "Couldn't remove '$(friendly_path $dir)'; it may be in use." + Write-UserMessage -Message "Couldn't remove '$(friendly_path $dir)'; it may be in use." -Err return $false } } @@ -110,7 +113,7 @@ function Uninstall-ScoopApplication { unlink_persist_data $dir Remove-Item $dir -Recurse -Force -ErrorAction Stop } catch { - error "Couldn't remove '$(friendly_path $dir)'; it may be in use." + Write-UserMessage -Message "Couldn't remove '$(friendly_path $dir)'; it may be in use." -Err return $false } } @@ -135,7 +138,7 @@ function Uninstall-ScoopApplication { try { Remove-Item $persist_dir -Recurse -Force -ErrorAction Stop } catch { - error "Couldn't remove '$(friendly_path $persist_dir)'" + Write-UserMessage -Message "Couldn't remove '$(friendly_path $persist_dir)'" -Err return $false } } diff --git a/libexec/scoop-cache.ps1 b/libexec/scoop-cache.ps1 index 803c1fa5ab..5b07970435 100644 --- a/libexec/scoop-cache.ps1 +++ b/libexec/scoop-cache.ps1 @@ -38,7 +38,7 @@ function show($app) { switch ($cmd) { 'rm' { - if (!$app) { 'ERROR: missing'; my_usage; exit 1 } + if (!$app) { Write-UserMessage -Message 'ERROR: missing' -Err; my_usage; exit 1 } Remove-Item "$cachedir\$app#*" if (Test-Path("$cachedir\$app.txt")) { Remove-Item "$cachedir\$app.txt" diff --git a/libexec/scoop-cleanup.ps1 b/libexec/scoop-cleanup.ps1 index 32a01b8895..5312fe4bff 100644 --- a/libexec/scoop-cleanup.ps1 +++ b/libexec/scoop-cleanup.ps1 @@ -9,13 +9,9 @@ # -g, --global Cleanup a globally installed app # -k, --cache Remove outdated download cache -. "$PSScriptRoot\..\lib\core.ps1" -. "$PSScriptRoot\..\lib\manifest.ps1" -. "$PSScriptRoot\..\lib\buckets.ps1" -. "$PSScriptRoot\..\lib\versions.ps1" -. "$PSScriptRoot\..\lib\getopt.ps1" -. "$PSScriptRoot\..\lib\help.ps1" -. "$PSScriptRoot\..\lib\install.ps1" +'core', 'manifest', 'buckets', 'versions', 'getopt', 'help', 'install' | ForEach-Object { + . "$PSScriptRoot\..\lib\$_.ps1" +} reset_aliases @@ -24,11 +20,9 @@ if ($err) { "scoop cleanup: $err"; exit 1 } $global = $opt.g -or $opt.global $cache = $opt.k -or $opt.cache -if (!$apps) { 'ERROR: missing'; my_usage; exit 1 } +if (!$apps) { Write-UserMessage -Message 'ERROR: missing' -Err; my_usage; exit 1 } -if ($global -and !(is_admin)) { - 'ERROR: you need admin rights to cleanup global apps'; exit 1 -} +if ($global -and !(is_admin)) { Write-UserMessage -Message 'ERROR: you need admin rights to cleanup global apps' -Err; exit 1 } function cleanup($app, $global, $verbose, $cache) { $current_version = current_version $app $global @@ -37,7 +31,7 @@ function cleanup($app, $global, $verbose, $cache) { } $versions = versions $app $global | Where-Object { $_ -ne $current_version -and $_ -ne 'current' } if (!$versions) { - if ($verbose) { success "$app is already clean" } + if ($verbose) { Write-UserMessage -Message "$app is already clean" -Success } return } @@ -68,13 +62,9 @@ if ($apps) { # $apps is now a list of ($app, $global) tuples $apps | ForEach-Object { cleanup @_ $verbose $cache } - if ($cache) { - Remove-Item "$cachedir\*.download" -ErrorAction Ignore - } + if ($cache) { Remove-Item "$cachedir\*.download" -ErrorAction Ignore } - if (!$verbose) { - success 'Everything is shiny now!' - } + if (!$verbose) { Write-UserMessage -Message 'Everything is shiny now!' -Success } } exit 0 diff --git a/libexec/scoop-depends.ps1 b/libexec/scoop-depends.ps1 index 812a700422..751b09a09f 100644 --- a/libexec/scoop-depends.ps1 +++ b/libexec/scoop-depends.ps1 @@ -14,8 +14,7 @@ reset_aliases $opt, $apps, $err = getopt $args 'a:' 'arch=' $app = $apps[0] -# FIXME: -if (!$app) { error ' missing'; my_usage; exit 1 } +if (!$app) { Write-UserMessage -Message ' missing' -Err; my_usage; exit 1 } $architecture = default_architecture try { diff --git a/libexec/scoop-download.ps1 b/libexec/scoop-download.ps1 index 35c466e699..b51fca57ec 100644 --- a/libexec/scoop-download.ps1 +++ b/libexec/scoop-download.ps1 @@ -19,7 +19,7 @@ reset_aliases $opt, $application, $err = getopt $args 'sba:u:' 'skip', 'all-architectures', 'arch=', 'utility=' if ($err) { # TODO: Stop-ScoopExecution - error "scoop install: $err" + Write-UserMessage -Message "scoop install: $err" -Err exit 1 } @@ -37,7 +37,7 @@ if ($opt.b -or $opt.'all-architectures') { $architecture = '32bit', '64bit' } if (-not $application) { # TODO:? Extend Stop-ScoopExecution with -Usage switch - error ' missing' + Write-UserMessage -Message ' missing' -Err my_usage exit 1 } @@ -65,7 +65,7 @@ foreach ($app in $application) { debug $cleanAppName debug $foundBucket debug $appName - error 'Found application name or bucket is not same as requested' + Write-UserMessage -Message 'Found application name or bucket is not same as requested' -Err continue } @@ -73,7 +73,7 @@ foreach ($app in $application) { if (($null -ne $version) -and ($manifest.version -ne $version)) { $generated = generate_user_manifest $appName $bucket $version if ($null -eq $generated) { - error 'Manifest cannot be generated with provided version' + Write-UserMessage -Message 'Manifest cannot be generated with provided version' -Err continue } $manifest = parse_json($generated) @@ -99,12 +99,12 @@ foreach ($app in $application) { $ok, $err = check_hash $source $manifestHash (show_app $appName $bucket) if (!$ok) { - error $err + Write-UserMessage -Message $err -Err if (Test-Path $source) { Remove-Item $source -Force } if ($url -like '*sourceforge.net*') { - warn 'SourceForge.net is known for causing hash validation fails. Please try again before opening a ticket.' + Write-UserMessage -Message 'SourceForge.net is known for causing hash validation fails. Please try again before opening a ticket.' -Warning } - error (new_issue_msg $appName $bucket 'hash check failed') + Write-UserMessage -Message (new_issue_msg $appName $bucket 'hash check failed') -Err continue } } diff --git a/libexec/scoop-hold.ps1 b/libexec/scoop-hold.ps1 index 1f2d65fd06..c185b08f7e 100644 --- a/libexec/scoop-hold.ps1 +++ b/libexec/scoop-hold.ps1 @@ -12,12 +12,13 @@ if (!$apps) { exit 1 } -$apps | ForEach-Object { - $app = $_ +$exitCode = 0 +foreach ($app in $apps) { $global = installed $app $true if (!(installed $app)) { - error "'$app' is not installed." + Write-UserMessage -Message "'$app' is not installed." -Err + $exitCode = 1 return } @@ -27,7 +28,7 @@ $apps | ForEach-Object { $json | Get-Member -MemberType Properties | ForEach-Object { $install.Add($_.Name, $json.($_.Name)) } $install.hold = $true save_install_info $install $dir - success "$app is now held and can not be updated anymore." + Write-UserMessage -Message "$app is now held and can not be updated anymore." -Success } -exit $exitcode +exit $exitCode diff --git a/libexec/scoop-install.ps1 b/libexec/scoop-install.ps1 index 22c41cb593..463986949f 100644 --- a/libexec/scoop-install.ps1 +++ b/libexec/scoop-install.ps1 @@ -39,16 +39,24 @@ function is_installed($app, $global) { $version = @(versions $app $global)[-1] if (!(install_info $app $version $global)) { - error "It looks like a previous installation of $app failed.`nRun 'scoop uninstall $app$(gf $global)' before retrying the install." + Write-UserMessage -Err -Message @( + "It looks like a previous installation of $app failed." + "Run 'scoop uninstall $app$(gf $global)' before retrying the install." + ) } - warn "'$app' ($version) is already installed.`nUse 'scoop update $app$(gf $global)' to install a new version." + Write-UserMessage -Warning -Message @( + "'$app' ($version) is already installed.", + "Use 'scoop update $app$(gf $global)' to install a new version." + ) + return $true } + return $false } $opt, $apps, $err = getopt $args 'gfiksa:' 'global', 'force', 'independent', 'no-cache', 'skip', 'arch=' -if ($err) { "scoop install: $err"; exit 1 } +if ($err) { Write-UserMessage -Message "scoop install: $err" -Err; exit 1 } $global = $opt.g -or $opt.global $check_hash = !($opt.s -or $opt.skip) @@ -61,13 +69,14 @@ try { abort "ERROR: $_" } -if (!$apps) { error ' missing'; my_usage; exit 1 } +if (!$apps) { Write-UserMessage -Message ' missing' -Err; my_usage; exit 1 } if ($global -and !(is_admin)) { abort 'ERROR: you need admin rights to install global apps' } if (is_scoop_outdated) { + # TODO: scoop update } @@ -115,14 +124,18 @@ $apps, $skip = prune_installed $apps $global $skip | Where-Object { $explicit_apps -contains $_ } | ForEach-Object { $app, $null, $null = parse_app $_ $version = @(versions $app $global)[-1] - warn "'$app' ($version) is already installed. Skipping." + Write-UserMessage -Message "'$app' ($version) is already installed. Skipping." -Warning } $suggested = @{ }; + if (Test-Aria2Enabled) { - warn "Scoop uses 'aria2c' for multi-connection downloads." - warn "Should it cause issues, run 'scoop config aria2-enabled false' to disable it." + Write-UserMessage -Warning -Message @( + "Scoop uses 'aria2c' for multi-connection downloads." + "Should it cause issues, run 'scoop config aria2-enabled false' to disable it." + ) } + $apps | ForEach-Object { install_app $_ $architecture $global $suggested $use_cache $check_hash } show_suggestions $suggested diff --git a/libexec/scoop-list.ps1 b/libexec/scoop-list.ps1 index b3794041c9..5bbbba89f4 100644 --- a/libexec/scoop-list.ps1 +++ b/libexec/scoop-list.ps1 @@ -23,7 +23,7 @@ $orderInstalled = $opt.i -or $opt.installed $orderUpdated = $opt.u -or $opt.updated $reverse = $opt.r -or $opt.reverse # TODO: Stop-ScoopExecution -if ($orderUpdated -and $orderInstalled) { error '--installed and --updated parameters cannot be used simultaneously'; exit 1 } +if ($orderUpdated -and $orderInstalled) { Write-UserMessage -Message '--installed and --updated parameters cannot be used simultaneously' -Err; exit 1 } $def_arch = default_architecture $locA = appsdir $false diff --git a/libexec/scoop-reset.ps1 b/libexec/scoop-reset.ps1 index 5d6e231211..cf79992a06 100644 --- a/libexec/scoop-reset.ps1 +++ b/libexec/scoop-reset.ps1 @@ -16,7 +16,7 @@ reset_aliases $opt, $apps, $err = getopt $args if ($err) { "scoop reset: $err"; exit 1 } -if (!$apps) { error ' missing'; my_usage; exit 1 } +if (!$apps) { Write-UserMessage -Message ' missing' -Err; my_usage; exit 1 } if ($apps -eq '*') { $local = installed_apps $false | ForEach-Object { , @($_, $false) } @@ -24,44 +24,42 @@ if ($apps -eq '*') { $apps = @($local) + @($global) } +$exitCode = 0 $apps | ForEach-Object { ($app, $global) = $_ $app, $bucket, $version = parse_app $app - if (($global -eq $null) -and (installed $app $true)) { - # set global flag when running reset command on specific app - $global = $true - } + # Set global flag when running reset command on specific app + if (($null -eq $global) -and (installed $app $true)) { $global = $true } - if ($app -eq 'scoop') { - # skip scoop - return - } + # Skip scoop + if ($app -eq 'scoop') { return } if (!(installed $app)) { - error "'$app' isn't installed" + Write-UserMessage -Message "'$app' isn't installed" -Err + $exitCode = 1 return } - if ($null -eq $version) { - $version = current_version $app $global - } + if ($null -eq $version) { $version = current_version $app $global } $manifest = installed_manifest $app $version $global # if this is null we know the version they're resetting to # is not installed if ($manifest -eq $null) { - error "'$app ($version)' isn't installed" + Write-UserMessage -Message "'$app ($version)' isn't installed" -Err + $exitCode = 1 return } if ($global -and !(is_admin)) { - warn "'$app' ($version) is a global app. You need admin rights to reset it. Skipping." + Write-UserMessage -Message "'$app' ($version) is a global app. You need admin rights to reset it. Skipping." -Warning + $exitCode = 1 return } - write-host "Resetting $app ($version)." + Write-UserMessage "Resetting $app ($version)." $dir = resolve-path (versiondir $app $version $global) $original_dir = $dir @@ -81,4 +79,4 @@ $apps | ForEach-Object { persist_permission $manifest $global } -exit 0 +exit $exitCode diff --git a/libexec/scoop-search.ps1 b/libexec/scoop-search.ps1 index 5abc3c6579..7432904318 100644 --- a/libexec/scoop-search.ps1 +++ b/libexec/scoop-search.ps1 @@ -5,10 +5,11 @@ # If used with [query], shows app names that match the query. # Without [query], shows all the available apps. param($query) -. "$PSScriptRoot\..\lib\core.ps1" -. "$PSScriptRoot\..\lib\buckets.ps1" -. "$PSScriptRoot\..\lib\manifest.ps1" -. "$PSScriptRoot\..\lib\versions.ps1" + +# TODO: Refactor +'core', 'buckets', 'manifest', 'versions' | ForEach-Object { + . "$PSScriptRoot\..\lib\$_.ps1" +} reset_aliases @@ -114,6 +115,7 @@ Get-LocalBucket | ForEach-Object { if (!$local_results -and !(github_ratelimit_reached)) { $remote_results = search_remotes $query + # FIXME if (!$remote_results) { [console]::error.writeline("No matches found."); exit 1 } $remote_results } diff --git a/libexec/scoop-status.ps1 b/libexec/scoop-status.ps1 index 88644659e3..3536c15aa8 100644 --- a/libexec/scoop-status.ps1 +++ b/libexec/scoop-status.ps1 @@ -25,14 +25,17 @@ if (test-path "$currentdir\.git") { } if ($needs_update) { - warn "Scoop is out of date. Run 'scoop update' to get the latest changes." -} else { success "Scoop is up to date." } + Write-UserMessage -Message "Scoop is out of date. Run 'scoop update' to get the latest changes." -Warning +} else { + Write-UserMessage -Message "Scoop is up to date." -Success +} $failed = @() $outdated = @() $removed = @() $missing_deps = @() $onhold = @() +$exitCode = 0 $true, $false | ForEach-Object { # local and global apps $global = $_ @@ -62,6 +65,7 @@ $true, $false | ForEach-Object { # local and global apps if ($outdated) { write-host -f DarkCyan 'Updates are available for:' + $exitCode = 1 $outdated.keys | ForEach-Object { $versions = $outdated.$_ " $_`: $($versions[0]) -> $($versions[1])" @@ -78,6 +82,7 @@ if ($onhold) { if ($removed) { write-host -f DarkCyan 'These app manifests have been removed:' + $exitCode = 2 $removed.keys | ForEach-Object { " $_" } @@ -85,12 +90,14 @@ if ($removed) { if ($failed) { write-host -f DarkCyan 'These apps failed to install:' + $exitCode = 2 $failed.keys | ForEach-Object { " $_" } } if ($missing_deps) { + $exitCode = 2 write-host -f DarkCyan 'Missing runtime dependencies:' $missing_deps | ForEach-Object { $app, $deps = $_ @@ -99,7 +106,7 @@ if ($missing_deps) { } if (!$old -and !$removed -and !$failed -and !$missing_deps -and !$needs_update) { - success "Everything is ok!" + Write-UserMessage -Message 'Everything is ok!' -Success } -exit 0 +exit $exitCode diff --git a/libexec/scoop-unhold.ps1 b/libexec/scoop-unhold.ps1 index 78fbd64689..29f7a99e7a 100644 --- a/libexec/scoop-unhold.ps1 +++ b/libexec/scoop-unhold.ps1 @@ -12,12 +12,13 @@ if (!$apps) { exit 1 } -$apps | ForEach-Object { - $app = $_ +$exitCode = 0 +foreach ($app in $apps) { $global = installed $app $true if (!(installed $app)) { - error "'$app' is not installed." + Write-UserMessage -Message "'$app' is not installed." -Err + $exitCode = 1 return } @@ -27,7 +28,7 @@ $apps | ForEach-Object { $json | Get-Member -MemberType Properties | ForEach-Object { $install.Add($_.Name, $json.($_.Name)) } $install.hold = $null save_install_info $install $dir - success "$app is no longer held and can be updated again." + Write-UserMessage -Message "$app is no longer held and can be updated again." -Success } -exit $exitcode +exit $exitCode diff --git a/libexec/scoop-uninstall.ps1 b/libexec/scoop-uninstall.ps1 index 1ae1abc5ce..a139f5e7c1 100644 --- a/libexec/scoop-uninstall.ps1 +++ b/libexec/scoop-uninstall.ps1 @@ -16,7 +16,7 @@ reset_aliases $opt, $apps, $err = getopt $args 'gp' 'global', 'purge' if ($err) { - error "scoop uninstall: $err" + Write-UserMessage -Message "scoop uninstall: $err" -Err exit 1 } @@ -24,13 +24,13 @@ $global = $opt.g -or $opt.global $purge = $opt.p -or $opt.purge if (!$apps) { - error ' missing' + Write-UserMessage -Message ' missing' -Err my_usage exit 1 } if ($global -and !(is_admin)) { - error 'You need admin rights to uninstall global apps.' + Write-UserMessage -Message 'You need admin rights to uninstall global apps.' -Err exit 1 } @@ -42,13 +42,17 @@ if ($apps -eq 'scoop') { $apps = Confirm-InstallationStatus $apps -Global:$global if (!$apps) { exit 0 } +$exitCode = 0 :app_loop foreach ($_ in $apps) { ($app, $global) = $_ $result = Uninstall-ScoopApplication -App $app -Global:$global -Purge:$purge -Older - if ($result -eq $false) { continue } + if ($result -eq $false) { + $exitCode = 1 + continue + } - success "'$app' was uninstalled." + Write-UserMessage -Message "'$app' was uninstalled." -Success } -exit 0 +exit $exitCode diff --git a/libexec/scoop-update.ps1 b/libexec/scoop-update.ps1 index 4d94300e6b..7be2a091a8 100644 --- a/libexec/scoop-update.ps1 +++ b/libexec/scoop-update.ps1 @@ -70,8 +70,10 @@ if (!$apps) { } if ($outdatedApplications -and (Test-Aria2Enabled)) { - Write-UserMessage -Message 'Scoop uses ''aria2c'' for multi-conneciton downloads.', - 'In case of issues with downloading, run ''scoop config aria2-enabled $false'' to disable aria2.' -Warning + Write-UserMessage -Warning -Message @( + 'Scoop uses ''aria2c'' for multi-conneciton downloads.' + 'In case of issues with downloading, run ''scoop config aria2-enabled $false'' to disable aria2.' + ) } $c = $outdatedApplications.Count diff --git a/libexec/scoop-virustotal.ps1 b/libexec/scoop-virustotal.ps1 index d70cb11bb0..e0d710e6f2 100644 --- a/libexec/scoop-virustotal.ps1 +++ b/libexec/scoop-virustotal.ps1 @@ -71,7 +71,7 @@ foreach ($app in $apps) { if ($hash) { $exitCode = $exitCode -bor (Search-VirusTotal $hash $app) } else { - warn "${app}: Can't find hash for $url" + Write-UserMessage -Message "${app}: Can't find hash for $url" -Warning } } catch [Exception] { $exitCode = $exitCode -bor $VT_ERR.Exception