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: Straightforward abort deprecation #55

Merged
merged 2 commits into from
Jul 30, 2020
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
11 changes: 4 additions & 7 deletions bin/auto-pr.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ param(
[Switch] $SkipCheckver
)

'manifest', 'json' | ForEach-Object {
'Helpers', 'manifest', 'json' | ForEach-Object {
. (Join-Path $PSScriptRoot "..\lib\$_.ps1")
}

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

Expand Down
11 changes: 4 additions & 7 deletions bin/uninstall.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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!'
Expand All @@ -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): $_"
}
}

Expand All @@ -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
Expand Down
95 changes: 95 additions & 0 deletions lib/Helpers.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,36 @@ function Write-UserMessage {
}
}

function Set-TerminatingError {
<#
.SYNOPSIS
Throw [ScoopException] helper for universal exception handling.
.DESCRIPTION
Format <Category>|-<detail> 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|-<details>' 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 '<Category>|-<detail>'.
.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
Expand Down Expand Up @@ -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 '(?:@|:\/\/)(?<provider>.+?)[:\/](?<user>.*)\/(?<repo>.+?)(?:\.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
8 changes: 3 additions & 5 deletions lib/Update.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion lib/buckets.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/depends.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
10 changes: 2 additions & 8 deletions libexec/scoop-install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down Expand Up @@ -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 $_
Expand Down Expand Up @@ -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

Expand Down
10 changes: 9 additions & 1 deletion libexec/scoop-uninstall.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,15 @@ $problems = 0
:app_loop foreach ($_ in $apps) {
($app, $global) = $_

$result = Uninstall-ScoopApplication -App $app -Global:$global -Purge:$purge -Older
$result = $false
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
Expand Down
1 change: 1 addition & 0 deletions libexec/scoop-update.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ if (!$apps) {
} catch {
++$problems
Write-UserMessage -Message $_.Exception.Message -Err
continue
}
}
}
Expand Down