From c8f5b3bb46f7cb4cdb75173e2cd736f93c2a16bd Mon Sep 17 00:00:00 2001 From: Ash258 Date: Thu, 30 Jul 2020 20:41:01 +0200 Subject: [PATCH 1/2] refactor: Straightforward abort deprecation #46, #53 I did not expect that full deprecation will be that easy, so the original PR grow a bit and got really messy. This is fraction of the previous PR with the easiest changes. - Helper function - Strict and safe `abort` replacement without additional side checks --- bin/auto-pr.ps1 | 11 ++--- bin/uninstall.ps1 | 11 ++--- lib/Helpers.ps1 | 95 +++++++++++++++++++++++++++++++++++++ lib/Update.ps1 | 8 ++-- lib/buckets.ps1 | 2 +- lib/depends.ps1 | 2 +- libexec/scoop-install.ps1 | 10 +--- libexec/scoop-uninstall.ps1 | 9 +++- libexec/scoop-update.ps1 | 1 + 9 files changed, 119 insertions(+), 30 deletions(-) diff --git a/bin/auto-pr.ps1 b/bin/auto-pr.ps1 index 88d0e96d2e..e71b84386b 100644 --- a/bin/auto-pr.ps1 +++ b/bin/auto-pr.ps1 @@ -52,7 +52,7 @@ param( [Switch] $SkipCheckver ) -'manifest', 'json' | ForEach-Object { +'Helpers', 'manifest', 'json' | ForEach-Object { . (Join-Path $PSScriptRoot "..\lib\$_.ps1") } @@ -77,17 +77,14 @@ Optional options: } if (!(Get-Command -Name 'hub' -CommandType Application -ErrorAction SilentlyContinue)) { - # TODO: Stop-ScoopExecution - Write-UserMessage -Message 'hub is required! Please refer to ''https://hub.github.com/'' to find out how to get hub for your platform.' -Warning - exit 1 + Stop-ScoopExecution -Message 'hub is required! Please refer to ''https://hub.github.com/'' to find out how to get hub for your platform.' } function execute($cmd) { Write-Host $cmd -ForegroundColor Green $output = Invoke-Expression $cmd - # TODO: Stop-ScoopExecution - if ($LASTEXITCODE -gt 0) { abort "^^^ Error! See above ^^^ (last command: $cmd)" } + if ($LASTEXITCODE -gt 0) { Stop-ScoopExecution -Message "^^^ Error! See above ^^^ (last command: $cmd)" } return $output } @@ -137,7 +134,7 @@ a new version of [$app]($homepage) is available. hub pull-request -m "$msg" -b '$upstream' -h '$branch' if ($LASTEXITCODE -gt 0) { execute 'hub reset' - abort "Pull Request failed! (hub pull-request -m '${app}: Update to version $version' -b '$upstream' -h '$branch')" + Stop-ScoopExecution -Message "Pull Request failed! (hub pull-request -m '${app}: Update to version $version' -b '$upstream' -h '$branch')" } } diff --git a/bin/uninstall.ps1 b/bin/uninstall.ps1 index 0d7c267697..4e250aa252 100644 --- a/bin/uninstall.ps1 +++ b/bin/uninstall.ps1 @@ -11,14 +11,12 @@ param( [bool] $purge ) -'core', 'install', 'shortcuts', 'Versions', 'manifest', 'uninstall' | ForEach-Object { +'core', 'Helpers', 'install', 'shortcuts', 'Versions', 'manifest', 'uninstall' | ForEach-Object { . (Join-Path $PSScriptRoot "..\lib\$_.ps1") } if ($global -and !(is_admin)) { - # TODO: Stop-ScoopExecution - Write-UserMessage -Message 'You need admin rights to uninstall globally.' -Err - exit 1 + Stop-ScoopExecution -Message 'Admin privileges are required for globall uninstallation.' -ExitCode 4 } $message = 'This will uninstall Scoop and all the programs that have been installed with Scoop!' @@ -36,8 +34,7 @@ function rm_dir($dir) { try { Remove-Item $dir -ErrorAction Stop -Recurse -Force } catch { - # TODO: Stop-ScoopExecution - abort "Couldn't remove $(friendly_path $dir): $_" + Stop-ScoopExecution -Message "Couldn't remove $(friendly_path $dir): $_" } } @@ -60,7 +57,7 @@ installed_apps $false | ForEach-Object { # local apps if ($result -eq $false) { $errors += 1 } } -if ($errors -gt 0) { abort 'Not all apps could be deleted. Try again or restart.' } +if ($errors -gt 0) { Stop-ScoopExecution -Message 'Not all apps could be deleted. Try again or restart.' } if ($purge) { rm_dir $SCOOP_ROOT_DIRECTORY diff --git a/lib/Helpers.ps1 b/lib/Helpers.ps1 index d2d767367c..fdde834ace 100644 --- a/lib/Helpers.ps1 +++ b/lib/Helpers.ps1 @@ -79,6 +79,36 @@ function Write-UserMessage { } } +function Set-TerminatingError { + <# + .SYNOPSIS + Throw [ScoopException] helper for universal exception handling. + .DESCRIPTION + Format |- should be respected all the time as it will allow to dynamically post new issue prompts + in manifest scripts and mainly it is easy and unified way how to detect reportable problems. + Use 'Ignore|-
' If you do not want to show new issue prompt. Usually in problems not related to specific manifest + .PARAMETER Title + Specifies the exception message. + It should be in format '|-'. + .PARAMETER ID + Specifies the global identifier of the error condition. + #> + param([String] $Title, [String] $ID = 'Scoop') + + if ($PSCmdlet) { + $PSCmdlet.ThrowTerminatingError( + [System.Management.Automation.ErrorRecord]::new( + ([ScoopException]::new($Title)), + $ID, + [System.Management.Automation.ErrorCategory]::OpenError, + $null + ) + ) + } else { + throw [ScoopException]::new($Title) + } +} + function Stop-ScoopExecution { <# .SYNOPSIS @@ -267,3 +297,68 @@ function Reset-Alias { # Set default aliases $defautlAliases.Keys | ForEach-Object { _resetAlias $_ $defautlAliases[$_] } } + +function New-IssuePrompt { + <# + .SYNOPSIS + Prompt user to report a manifest problem to it's maintaners. + Post direct link in case of supported source control provides. + .PARAMETER Application + Specifies the application name. + .PARAMETER Bucket + Specifies the bucket to which application belong + .PARAMETER Title + Specifies the title of newly created issue. + .PARAMETER Body + Specifies more details to be posted as issue body. + #> + param([String] $Application, [String] $Bucket, [String] $Title, [String[]] $Body) + + $app, $manifest, $Bucket, $url = Find-Manifest $Application $Bucket + $url = known_bucket_repo $Bucket + $bucketPath = Join-Path $SCOOP_BUCKETS_DIRECTORY $bucket + + if ((Test-Path $bucketPath) -and (Join-Path $bucketPath '.git' | Test-Path -PathType Container)) { + $remote = Invoke-GitCmd -Repository $bucketPath -Command 'config' -Argument '--get','remote.origin.url' + # Support ssh and http syntax + # git@PROVIDER:USER/REPO.git + # https://PROVIDER/USER/REPO.git + # https://regex101.com/r/OMEqfV + if ($remote -match '(?:@|:\/\/)(?.+?)[:\/](?.*)\/(?.+?)(?:\.git)?$') { + $url = "https://$($Matches.Provider)/$($Matches.User)/$($Matches.Repo)" + } + } + + if (!$url) { + Write-UserMessage -Message 'Please contact the manifest maintainer!' -Color DarkRed + return + } + + $Title = [System.Web.HttpUtility]::UrlEncode("$Application@$($Manifest.version): $Title") + $Body = [System.Web.HttpUtility]::UrlEncode($Body) + $msg = "`nPlease try again" + + switch -Wildcard ($url) { + '*github.*' { + $url = $url -replace '\.git$' + $url = "$url/issues/new?title=$Title" + if ($body) { $url += "&body=$Body" } + $msg = "$msg or create a new issue by using the following link and paste your console output:" + } + default { + Write-UserMessage -Message 'Not supported platform' -Info + } + } + + Write-UserMessage -Message "$msg`n$url" -Color DarkRed +} + +#region Exceptions +class ScoopException: System.Exception { + $Message + + ScoopException([String] $Message) { + $this.Message = $Message + } +} +#endregion Exceptions diff --git a/lib/Update.ps1 b/lib/Update.ps1 index a0e8ad87a0..2008ab971c 100644 --- a/lib/Update.ps1 +++ b/lib/Update.ps1 @@ -34,8 +34,7 @@ function Update-ScoopCoreClone { Invoke-GitCmd -Command 'clone' -Argument '--quiet', '--single-branch', '--branch', """$Branch""", $Repo, """$newDir""" -Proxy # Check if scoop was successful downloaded - # TODO: Stop-ScoopExecution - if (!(Test-Path $newDir -PathType Container)) { abort 'Scoop update failed.' } + if (!(Test-Path $newDir -PathType Container)) { Stop-ScoopExecution -Message 'Scoop update failed.' } # Replace non-git scoop with the git version Remove-Item $TargetDirectory -ErrorAction Stop -Force -Recurse @@ -92,8 +91,7 @@ function Update-ScoopCorePull { $res = $LASTEXITCODE if ($SHOW_UPDATE_LOG) { Invoke-GitCmd @target -Command 'UpdateLog' -Argument """$previousCommit..HEAD""" } - # TODO: Stop-ScoopExecution - if ($res -ne 0) { abort 'Update failed.' } + if ($res -ne 0) { Stop-ScoopExecution -Message 'Update failed.' } } function Update-ScoopLocalBucket { @@ -308,6 +306,6 @@ function Update-App { $toUpdate = if ($install.url) { $install.url } else { "$bucket/$App" } - # TODO: Try catch + # Error catching should be handled on upper scope install_app $toUpdate $architecture $Global $Suggested (!$SkipCache) (!$SkipHashCheck) } diff --git a/lib/buckets.ps1 b/lib/buckets.ps1 index 36ab4d5010..977fd0644a 100644 --- a/lib/buckets.ps1 +++ b/lib/buckets.ps1 @@ -135,7 +135,7 @@ function Remove-Bucket { } } -# TODO: Migrate to helpers +# TODO: Drop/Deprecate function new_issue_msg($app, $bucket, $title, $body) { $app, $manifest, $bucket, $url = Find-Manifest $app $bucket $url = known_bucket_repo $bucket diff --git a/lib/depends.ps1 b/lib/depends.ps1 index 319dbe65a7..fb3de87c49 100644 --- a/lib/depends.ps1 +++ b/lib/depends.ps1 @@ -49,7 +49,7 @@ function dep_resolve($app, $arch, $resolved, $unresolved) { dep_resolve $dep $arch $resolved $unresolved } } - $resolved.add($app) | Out-Null + $resolved.Add($app) | Out-Null $unresolved = $unresolved -ne $app # Remove from unresolved } diff --git a/libexec/scoop-install.ps1 b/libexec/scoop-install.ps1 index 7b2e866226..97ead444a8 100644 --- a/libexec/scoop-install.ps1 +++ b/libexec/scoop-install.ps1 @@ -36,6 +36,7 @@ function is_installed($app, $global) { "It looks like a previous installation of $app failed." "Run 'scoop uninstall $app$(gf $global)' before retrying the install." ) + return $true } Write-UserMessage -Warning -Message @( "'$app' ($version) is already installed.", @@ -69,13 +70,6 @@ if ($global -and !(is_admin)) { Stop-ScoopExecution -Message 'Admin privileges a if (is_scoop_outdated) { Update-Scoop } -if ($apps.length -eq 1) { - $app, $null, $version = parse_app $apps - if ($null -eq $version -and (is_installed $app $global)) { - return - } -} - # Get any specific versions that need to be handled first $specific_versions = $apps | Where-Object { $null, $null, $version = parse_app $_ @@ -109,7 +103,7 @@ foreach ($sp in $specific_versions) { } $apps = @(($specific_versions_paths + $difference) | Where-Object { $_ } | Sort-Object -Unique) -# remember which were explictly requested so that we can +# Remember which were explictly requested so that we can # differentiate after dependencies are added $explicit_apps = $apps diff --git a/libexec/scoop-uninstall.ps1 b/libexec/scoop-uninstall.ps1 index bb86f5243e..032f1f964f 100644 --- a/libexec/scoop-uninstall.ps1 +++ b/libexec/scoop-uninstall.ps1 @@ -43,7 +43,14 @@ $problems = 0 :app_loop foreach ($_ in $apps) { ($app, $global) = $_ - $result = Uninstall-ScoopApplication -App $app -Global:$global -Purge:$purge -Older + try { + $result = Uninstall-ScoopApplication -App $app -Global:$global -Purge:$purge -Older + } catch { + ++$problems + Write-UserMessage -Message $_.Exception.Message -Err + continue + } + if ($result -eq $false) { ++$problems continue diff --git a/libexec/scoop-update.ps1 b/libexec/scoop-update.ps1 index 1fefd0b7bb..d1719dc89b 100644 --- a/libexec/scoop-update.ps1 +++ b/libexec/scoop-update.ps1 @@ -82,6 +82,7 @@ if (!$apps) { } catch { ++$problems Write-UserMessage -Message $_.Exception.Message -Err + continue } } } From 6608b003f4bab8089f70eb5484eeb9e79ae66948 Mon Sep 17 00:00:00 2001 From: Ash258 Date: Thu, 30 Jul 2020 20:44:49 +0200 Subject: [PATCH 2/2] fix --- libexec/scoop-uninstall.ps1 | 1 + 1 file changed, 1 insertion(+) diff --git a/libexec/scoop-uninstall.ps1 b/libexec/scoop-uninstall.ps1 index 032f1f964f..01bd0c8504 100644 --- a/libexec/scoop-uninstall.ps1 +++ b/libexec/scoop-uninstall.ps1 @@ -43,6 +43,7 @@ $problems = 0 :app_loop foreach ($_ in $apps) { ($app, $global) = $_ + $result = $false try { $result = Uninstall-ScoopApplication -App $app -Global:$global -Purge:$purge -Older } catch {