From 087816ba63d190d59f29cd16c3817cf63a186f25 Mon Sep 17 00:00:00 2001 From: Chawye Hsu Date: Fri, 2 Jan 2026 17:24:13 +0800 Subject: [PATCH] fix: exit gracefully when scoop is already installed Signed-off-by: Chawye Hsu --- install.ps1 | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/install.ps1 b/install.ps1 index b213c59..e217128 100644 --- a/install.ps1 +++ b/install.ps1 @@ -88,33 +88,36 @@ function Write-InstallInfo { $host.UI.RawUI.ForegroundColor = $backup } -function Deny-Install { +function Exit-Install { param( - [String] $message, - [Int] $errorCode = 1 + [Int] $ErrorCode = 1 ) - Write-InstallInfo -String $message -ForegroundColor DarkRed - Write-InstallInfo 'Abort.' - # Don't abort if invoked with iex that would close the PS session if ($IS_EXECUTED_FROM_IEX) { break } else { - exit $errorCode + exit $ErrorCode } } +function Deny-Install { + param( + [String] $Message, + [Int] $ErrorCode = 1 + ) + + Write-InstallInfo -String $Message -ForegroundColor DarkRed + Write-InstallInfo 'Abort.' + Exit-Install -ErrorCode $ErrorCode +} + function Test-LanguageMode { if ($ExecutionContext.SessionState.LanguageMode -ne 'FullLanguage') { + # `Write-InstallInfo` cannot be used here as it depends on FullLanguage mode Write-Output 'Scoop requires PowerShell FullLanguage mode to run, current PowerShell environment is restricted.' Write-Output 'Abort.' - - if ($IS_EXECUTED_FROM_IEX) { - break - } else { - exit $errorCode - } + Exit-Install } } @@ -167,7 +170,7 @@ function Test-Prerequisite { # Test if scoop is installed, by checking if scoop command exists. if (Test-CommandAvailable('scoop')) { - Deny-Install "Scoop is already installed. Run 'scoop update' to get the latest version." + Deny-Install "Scoop is already installed. Run 'scoop update' to get the latest version." -ErrorCode 0 } } @@ -457,7 +460,7 @@ function Add-ShimsDirToPath { } if (!($h -eq '\')) { - $friendlyPath = "$SCOOP_SHIMS_DIR" -Replace ([Regex]::Escape($h)), '~\' + $friendlyPath = "$SCOOP_SHIMS_DIR" -replace ([Regex]::Escape($h)), '~\' Write-InstallInfo "Adding $friendlyPath to your path." } else { Write-InstallInfo "Adding $SCOOP_SHIMS_DIR to your path." @@ -593,12 +596,12 @@ function Install-Scoop { } Write-Verbose "Cloning $SCOOP_PACKAGE_GIT_REPO to $SCOOP_APP_DIR" git clone -q $SCOOP_PACKAGE_GIT_REPO $SCOOP_APP_DIR - if (-Not $?) { + if (-not $?) { throw 'Cloning failed. Falling back to downloading zip files.' } Write-Verbose "Cloning $SCOOP_MAIN_BUCKET_GIT_REPO to $SCOOP_MAIN_BUCKET_DIR" git clone -q $SCOOP_MAIN_BUCKET_GIT_REPO $SCOOP_MAIN_BUCKET_DIR - if (-Not $?) { + if (-not $?) { throw 'Cloning failed. Falling back to downloading zip files.' } $downloadZipsRequired = $False