Skip to content

Commit

Permalink
lint: error, warn, success -> Write-UserMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
Ash258 committed May 16, 2020
1 parent d805aa1 commit 616be62
Show file tree
Hide file tree
Showing 27 changed files with 181 additions and 155 deletions.
4 changes: 2 additions & 2 deletions bin/auto-pr.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions bin/checkver.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}
}
11 changes: 6 additions & 5 deletions bin/uninstall.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/Diagnostic.ps1
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions lib/Helpers.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 0 additions & 1 deletion lib/Update.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@ function Update-Scoop {
Write-UserMessage -Message 'Scoop was updated successfully!' -Success
}


function Update-App {
<#
.SYNOPSIS
Expand Down
8 changes: 5 additions & 3 deletions lib/buckets.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
. "$PSScriptRoot\core.ps1"
'core', 'git' | ForEach-Object {
. "$PSScriptRoot\$_.ps1"
}

$bucketsdir = "$scoopdir\buckets"
$SCOOP_BUCKETS_DIRECTORY = $bucketsdir
Expand Down Expand Up @@ -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
}

Expand All @@ -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) {
Expand Down
45 changes: 21 additions & 24 deletions lib/core.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
}
}

Expand Down Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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)')) {
Expand All @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/depends.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 <repo>'."
Write-UserMessage -Message "Bucket '$bucket' not installed. Add it with 'scoop bucket add $bucket' or 'scoop bucket add $bucket <repo>'." -Warning
}
abort "Couldn't find manifest for '$app'$(if(!$bucket) { '.' } else { " from '$bucket' bucket." })"
}
Expand Down
48 changes: 26 additions & 22 deletions lib/install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -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")
}
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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'
}
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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() }
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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$_"
Expand Down Expand Up @@ -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 }
}
}
}
Expand Down
Loading

0 comments on commit 616be62

Please sign in to comment.