Skip to content

Commit

Permalink
refactor(decompress): Abort => Set-TerminatingError
Browse files Browse the repository at this point in the history
  • Loading branch information
Ash258 committed Jul 30, 2020
1 parent 446a5d7 commit dee46c7
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 31 deletions.
2 changes: 1 addition & 1 deletion lib/Helpers.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function Set-TerminatingError {
.PARAMETER ID
Specifies the global identifier of the error condition.
#>
param([String] $Title, [String] $ID = 'Scoop')
param([Alias('Message')] [String] $Title, [String] $ID = 'Scoop')

if ($PSCmdlet) {
$PSCmdlet.ThrowTerminatingError(
Expand Down
16 changes: 10 additions & 6 deletions lib/core.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -523,9 +523,13 @@ function isFileLocked([string]$path) {

function is_directory([String] $path) { return (Test-Path $path) -and (Get-Item $path) -is [System.IO.DirectoryInfo] }

function movedir($from, $to) {
$from = $from.trimend('\')
$to = $to.trimend('\')
function movedir {
[CmdletBinding()]
param ($from, $to)

$from = $from.TrimEnd('\')
$parent = Split-Path $from -Parent
$to = $to.TrimEnd('\')

$proc = New-Object System.Diagnostics.Process
$proc.StartInfo.FileName = 'robocopy.exe'
Expand All @@ -540,7 +544,8 @@ function movedir($from, $to) {

if ($proc.ExitCode -ge 8) {
debug $out
throw "Could not find '$(fname $from)'! (error $($proc.ExitCode))"
# TODO: Consider different title
Set-TerminatingError -Title "Decompress Error|-Could not find '$(fname $from) in $parent'! (error $($proc.ExitCode))"
}

# Wait for robocopy to terminate its threads
Expand Down Expand Up @@ -577,8 +582,7 @@ function warn_on_overwrite($shim_ps1, $path) {
}

function shim($path, $global, $name, $arg) {
# TODO: Stop-ScoopExecution: throw
if (!(Test-Path $path)) { abort "Can't shim '$(fname $path)': couldn't find '$path'." }
if (!(Test-Path $path)) { Set-TerminatingError -Title "Shim creation fail|-Can not shim '$(fname $path)': could not find '$path'" }

$abs_shimdir = shimdir $global | ensure
if (!$name) { $name = strip_ext (fname $path) }
Expand Down
45 changes: 23 additions & 22 deletions lib/decompress.ps1
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# TODO: Core import is messing up with download progress 🤷‍♂️🤷‍♂️
'Helpers' | ForEach-Object {
. (Join-Path $PSScriptRoot "$_.ps1")
}

function Test-7zipRequirement {
[CmdletBinding(DefaultParameterSetName = 'URL')]
[OutputType([Boolean])]
Expand Down Expand Up @@ -51,8 +56,7 @@ function Expand-7zipArchive {
try {
$7zPath = (Get-Command '7z' -CommandType Application | Select-Object -First 1).Source
} catch [System.Management.Automation.CommandNotFoundException] {
# TODO: Stop-ScoopExecution: throw
abort "Cannot find external 7-Zip (7z.exe) while '7ZIPEXTRACT_USE_EXTERNAL' is 'true'!`nRun 'scoop config 7ZIPEXTRACT_USE_EXTERNAL false' or install 7-Zip manually and try again."
Set-TerminatingError -Title "Ignore|-Cannot find external 7-Zip (7z.exe) while '7ZIPEXTRACT_USE_EXTERNAL' is 'true'!`nRun 'scoop config 7ZIPEXTRACT_USE_EXTERNAL false' or install 7-Zip manually and try again."
}
} else {
$7zPath = Get-HelperPath -Helper 7zip
Expand All @@ -77,12 +81,11 @@ function Expand-7zipArchive {
try {
$Status = Invoke-ExternalCommand $7zPath $ArgList -LogPath $LogPath
} catch [System.Management.Automation.ParameterBindingException] {
Write-UserMessage -Message '''7zip'' is not installed or cannot be used' -Err
Set-TerminatingError -Title 'Ignore|-''7zip'' is not installed or cannot be used'
}

if (!$Status) {
# TODO: Stop-ScoopExecution: throw
abort "Failed to extract files from $Path.`nLog file:`n $(friendly_path $LogPath)`n$(new_issue_msg $app $bucket 'decompress error')"
Set-TerminatingError -Title "Decompress error|-Failed to extract files from $Path.`nLog file:`n $(friendly_path $LogPath)"
}
if (!$IsTar -and $ExtractDir) {
movedir (Join-Path $DestinationPath $ExtractDir) $DestinationPath | Out-Null
Expand All @@ -96,8 +99,7 @@ function Expand-7zipArchive {
$TarFile = (Get-Content -Path $LogPath)[-4] -replace '.{53}(.*)', '$1' # get inner tar file name
Expand-7zipArchive -Path (Join-Path $DestinationPath $TarFile) -DestinationPath $DestinationPath -ExtractDir $ExtractDir -Removal
} else {
# TODO: Stop-ScoopExecution
abort "Failed to list files in $Path.`nNot a 7-Zip supported archive file."
Set-TerminatingError -Title "Decompress error|-Failed to list files in $Path.`nNot a 7-Zip supported archive file."
}
}

Expand Down Expand Up @@ -136,8 +138,7 @@ function Expand-MsiArchive {

$Status = Invoke-ExternalCommand $MsiPath $ArgList -LogPath $LogPath
if (!$Status) {
# TODO: Stop-ScoopExecution
abort "Failed to extract files from $Path.`nLog file:`n $(friendly_path $LogPath)`n$(new_issue_msg $app $bucket 'decompress error')"
Set-TerminatingError -Title "Decompress error|-Failed to extract files from $Path.`nLog file:`n $(friendly_path $LogPath)"
}

$sourceDir = Join-Path $DestinationPath 'SourceDir'
Expand All @@ -154,6 +155,7 @@ function Expand-MsiArchive {
$fnamePath = Join-Path $DestinationPath (fname $Path)
if (($DestinationPath -ne (Split-Path $Path)) -and (Test-Path $fnamePath)) { Remove-Item $fnamePath -Force }
if (Test-Path $LogPath) { Remove-Item $LogPath -Force }

# Remove original archive file
if ($Removal) { Remove-Item $Path -Force }
}
Expand Down Expand Up @@ -181,15 +183,17 @@ function Expand-InnoArchive {
if ($Switches) { $ArgList += (-split $Switches) }

try {
# TODO: Find out extract_dir issue.
# When there is no specified directory in archive innounp will just exit with 0 and version of file
$Status = Invoke-ExternalCommand (Get-HelperPath -Helper Innounp) $ArgList -LogPath $LogPath
} catch [System.Management.Automation.ParameterBindingException] {
Write-UserMessage -Message '''innounp'' is not installed or cannot be used' -Err
Set-TerminatingError -Title 'Ignore|-''innounp'' is not installed or cannot be used'
}
if (!$Status) {
# TODO: Stop-ScoopExecution: throw
abort "Failed to extract files from $Path.`nLog file:`n $(friendly_path $LogPath)`n$(new_issue_msg $app $bucket 'decompress error')"
Set-TerminatingError -Title "Decompress error|-Failed to extract files from $Path.`nLog file:`n $(friendly_path $LogPath)"
}
if (Test-Path $LogPath) { Remove-Item $LogPath -Force }

# Remove original archive file
if ($Removal) { Remove-Item $Path -Force }
}
Expand All @@ -216,25 +220,22 @@ function Expand-ZipArchive {
try {
[System.IO.Compression.ZipFile]::ExtractToDirectory($Path, $DestinationPath)
} catch [System.IO.PathTooLongException] {
# try to fall back to 7zip if path is too long
# Try to fall back to 7zip if path is too long
if (Test-HelperInstalled -Helper 7zip) {
Expand-7zipArchive $Path $DestinationPath -Removal:$Removal
return
} else {
# TODO: Stop-ScoopExecution: throw
abort "Unzip failed: Windows can't handle the long paths in this zip file.`nRun 'scoop install 7zip' and try again."
Set-TerminatingError -Title "Ignore|-Unzip failed: Windows can not handle the long paths in this zip file.`nRun 'scoop install 7zip' and try again."
}
} catch [System.IO.IOException] {
if (Test-HelperInstalled -Helper 7zip) {
Expand-7zipArchive $Path $DestinationPath -Removal:$Removal
return
} else {
# TODO: Stop-ScoopExecution: throw
abort "Unzip failed: Windows can't handle the file names in this zip file.`nRun 'scoop install 7zip' and try again."
Set-TerminatingError -Title "Ignore|-Unzip failed: Windows can not handle the file names in this zip file.`nRun 'scoop install 7zip' and try again."
}
} catch {
# TODO: Stop-ScoopExecution: throw
abort "Unzip failed: $_"
Set-TerminatingError -Title "Decompress error|-Unzip failed: $_"
}
} else {
# Use Expand-Archive to unzip in PowerShell 5+
Expand Down Expand Up @@ -268,13 +269,13 @@ function Expand-DarkArchive {
try {
$Status = Invoke-ExternalCommand (Get-HelperPath -Helper Dark) $ArgList -LogPath $LogPath
} catch [System.Management.Automation.ParameterBindingException] {
Write-UserMessage -Message '''dark'' is not installed or cannot be used' -Err
Set-TerminatingError -Title 'Ignore|-''dark'' is not installed or cannot be used'
}
if (!$Status) {
# TODO: Stop-ScoopExecution: throw
abort "Failed to extract files from $Path.`nLog file:`n $(friendly_path $LogPath)`n$(new_issue_msg $app $bucket 'decompress error')"
Set-TerminatingError -Title "Decompress error|-Failed to extract files from $Path.`nLog file:`n $(friendly_path $LogPath)"
}
if (Test-Path $LogPath) { Remove-Item $LogPath -Force }

# Remove original archive file
if ($Removal) { Remove-Item $Path -Force }
}
Expand Down
4 changes: 2 additions & 2 deletions lib/install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -888,8 +888,8 @@ function create_shims($manifest, $dir, $global, $arch) {
} else {
$bin = search_in_path $target
}
# TODO: Stop-ScoopExecution: throw
if (!$bin) { abort "Can't shim '$target': File doesn't exist." }

if (!$bin) { Set-TerminatingError -Title "Shim creation fail|-Can not shim '$target': File does not exist" }

shim $bin $global $name (substitute $arg @{ '$dir' = $dir; '$original_dir' = $original_dir; '$persist_dir' = $persist_dir })
}
Expand Down

0 comments on commit dee46c7

Please sign in to comment.