From ad6198eeb4e4d5d5934a4c3b8a8b817f2f1da453 Mon Sep 17 00:00:00 2001 From: Mark Huo Date: Thu, 16 May 2019 00:08:08 +0800 Subject: [PATCH 1/7] fix(config): show correct output when removing a config value (#3462) --- libexec/scoop-config.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libexec/scoop-config.ps1 b/libexec/scoop-config.ps1 index 26cd92f2ce..c0fb6b39c5 100644 --- a/libexec/scoop-config.ps1 +++ b/libexec/scoop-config.ps1 @@ -1,6 +1,6 @@ # Usage: scoop config [rm] name [value] # Summary: Get or set configuration values -# Help: The scoop configuration file is saved at ~/.scoop. +# Help: The scoop configuration file is saved at ~/.config/scoop/config.json. # # To get a configuration setting: # @@ -37,7 +37,7 @@ if(!$name) { my_usage; exit 1 } if($name -like 'rm') { set_config $value $null | Out-Null - Write-Output "'$name' has been removed" + Write-Output "'$value' has been removed" } elseif($null -ne $value) { set_config $name $value | Out-Null Write-Output "'$name' has been set to '$value'" From b9deb57d03d19271c4397a8700702e89041de42b Mon Sep 17 00:00:00 2001 From: Hsiao-nan Cheung Date: Thu, 16 May 2019 01:18:28 +0800 Subject: [PATCH 2/7] refactor(core): Add more generic 'Invoke-ExternalCommand' instead of 'run' (#3432) --- lib/core.ps1 | 85 +++++++++++++++++++++++++++++- lib/decompress.ps1 | 126 +++++++++++++++++++++++++++------------------ lib/install.ps1 | 37 ++----------- 3 files changed, 162 insertions(+), 86 deletions(-) diff --git a/lib/core.ps1 b/lib/core.ps1 index 3d5c6813e4..7c4a4d481b 100644 --- a/lib/core.ps1 +++ b/lib/core.ps1 @@ -233,7 +233,7 @@ function Get-AppFilePath { } Function Test-CommandAvailable { - Param ( + param ( [String]$Name ) Return [Boolean](Get-Command $Name -ErrorAction Ignore) @@ -366,6 +366,89 @@ function is_local($path) { } # operations + +function run($exe, $arg, $msg, $continue_exit_codes) { + Show-DeprecatedWarning $MyInvocation 'Invoke-ExternalCommand' + Invoke-ExternalCommand -FilePath $exe -ArgumentList $arg -Activity $msg -ContinueExitCodes $continue_exit_codes +} + +function Invoke-ExternalCommand { + [CmdletBinding()] + [OutputType([Boolean])] + param ( + [Parameter(Mandatory = $true, + Position = 0)] + [Alias("Path")] + [ValidateNotNullOrEmpty()] + [String] + $FilePath, + [Parameter(Position = 1)] + [Alias("Args")] + [String[]] + $ArgumentList, + [Switch] + $RunAs, + [Alias("Msg")] + [String] + $Activity, + [Alias("cec")] + [Hashtable] + $ContinueExitCodes, + [Alias("Log")] + [String] + $LogPath + ) + if ($Activity) { + Write-Host "$Activity " -NoNewline + } + $Process = New-Object System.Diagnostics.Process + $Process.StartInfo.FileName = $FilePath + $Process.StartInfo.Arguments = ($ArgumentList | Select-Object -Unique) -join ' ' + $Process.StartInfo.UseShellExecute = $false + if ($LogPath) { + if ($FilePath -match '(^|\W)msiexec($|\W)') { + $Process.StartInfo.Arguments += " /lwe `"$LogPath`"" + } else { + $Process.StartInfo.RedirectStandardOutput = $true + } + } + if ($RunAs) { + $Process.StartInfo.Verb = 'RunAs' + } + try { + $Process.Start() | Out-Null + } catch { + if ($Activity) { + Write-Host "error." -ForegroundColor DarkRed + } + error $_.Exception.Message + return $false + } + if ($LogPath -and ($FilePath -notmatch '(^|\W)msiexec($|\W)')) { + Out-File -FilePath $LogPath -Encoding ASCII -Append -InputObject $Process.StandardOutput.ReadToEnd() + } + $Process.WaitForExit() + if ($Process.ExitCode -ne 0) { + if ($ContinueExitCodes -and ($ContinueExitCodes.ContainsKey($Process.ExitCode))) { + if ($Activity) { + Write-Host "done." -ForegroundColor DarkYellow + } + warn $ContinueExitCodes[$Process.ExitCode] + return $true + } else { + if ($Activity) { + Write-Host "error." -ForegroundColor DarkRed + } + error "Exit code was $($Process.ExitCode)!" + return $false + } + } + if ($Activity) { + Write-Host "done." -ForegroundColor Green + } + return $true +} + function dl($url,$to) { $wc = New-Object Net.Webclient $wc.headers.add('Referer', (strip_filename $url)) diff --git a/lib/decompress.ps1 b/lib/decompress.ps1 index 8e71c9f185..171a0bc141 100644 --- a/lib/decompress.ps1 +++ b/lib/decompress.ps1 @@ -1,7 +1,7 @@ function Test-7zipRequirement { [CmdletBinding(DefaultParameterSetName = "URL")] [OutputType([Boolean])] - param( + param ( [Parameter(Mandatory = $true, ParameterSetName = "URL")] [String[]] $URL, @@ -23,7 +23,7 @@ function Test-7zipRequirement { function Test-LessmsiRequirement { [CmdletBinding()] [OutputType([Boolean])] - param( + param ( [Parameter(Mandatory = $true)] [String[]] $URL @@ -37,48 +37,53 @@ function Test-LessmsiRequirement { function Expand-7zipArchive { [CmdletBinding()] - param( + param ( [Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)] [String] $Path, [Parameter(Position = 1)] [String] $DestinationPath = (Split-Path $Path), - [ValidateSet("All", "Skip", "Rename")] - [String] - $Overwrite, [Parameter(ValueFromRemainingArguments = $true)] [String] $Switches, + [ValidateSet("All", "Skip", "Rename")] + [String] + $Overwrite, [Switch] $Removal ) - $LogLocation = "$(Split-Path $Path)\7zip.log" - switch ($Overwrite) { - "All" { $Switches += " -aoa" } - "Skip" { $Switches += " -aos" } - "Rename" { $Switches += " -aou" } - } if ((get_config 7ZIPEXTRACT_USE_EXTERNAL)) { try { - 7z x "$Path" -o"$DestinationPath" (-split $Switches) -y | Out-File $LogLocation + $7zPath = (Get-Command '7z' -CommandType Application | Select-Object -First 1).Source } catch [System.Management.Automation.CommandNotFoundException] { 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." } } else { - & (Get-HelperPath -Helper 7zip) x "$Path" -o"$DestinationPath" (-split $Switches) -y | Out-File $LogLocation + $7zPath = Get-HelperPath -Helper 7zip + } + $LogPath = "$(Split-Path $Path)\7zip.log" + $ArgList = @('x', "`"$Path`"", "-o`"$DestinationPath`"", '-y') + if ($Switches) { + $ArgList += (-split $Switches) } - if ($LASTEXITCODE -ne 0) { - abort "Failed to extract files from $Path.`nLog file:`n $(friendly_path $LogLocation)" + switch ($Overwrite) { + "All" { $ArgList += "-aoa" } + "Skip" { $ArgList += "-aos" } + "Rename" { $ArgList += "-aou" } + } + $Status = Invoke-ExternalCommand $7zPath $ArgList -LogPath $LogPath + if (!$Status) { + abort "Failed to extract files from $Path.`nLog file:`n $(friendly_path $LogPath)" } - if (Test-Path $LogLocation) { - Remove-Item $LogLocation -Force + if (Test-Path $LogPath) { + Remove-Item $LogPath -Force } - if ((strip_ext $Path) -match '\.tar$' -or $Path -match '\.tgz$') { + if ((strip_ext $Path) -match '\.tar$' -or $Path -match '\.t[abgpx]z2?$') { # Check for tar - $ArchivedFile = & (Get-HelperPath -Helper 7zip) l "$Path" - if ($LASTEXITCODE -eq 0) { - $TarFile = $ArchivedFile[-3] -replace '.{53}(.*)', '$1' # get inner tar file name + $Status = Invoke-ExternalCommand $7zPath @('l', "`"$Path`"") -LogPath $LogPath + if ($Status) { + $TarFile = (Get-Content -Path $LogPath)[-4] -replace '.{53}(.*)', '$1' # get inner tar file name Expand-7zipArchive "$DestinationPath\$TarFile" $DestinationPath -Removal } else { abort "Failed to list files in $Path.`nNot a 7-Zip supported archive file." @@ -92,34 +97,42 @@ function Expand-7zipArchive { function Expand-MsiArchive { [CmdletBinding()] - param( + param ( [Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)] [String] $Path, [Parameter(Position = 1)] [String] $DestinationPath = (Split-Path $Path), + [Parameter(ValueFromRemainingArguments = $true)] + [String] + $Switches, [Switch] $Removal ) - $LogLocation = "$(Split-Path $Path)\msi.log" if ((get_config MSIEXTRACT_USE_LESSMSI)) { - & (Get-HelperPath -Helper Lessmsi) x "$Path" "$DestinationPath\" | Out-File $LogLocation - if ($LASTEXITCODE -ne 0) { - abort "Failed to extract files from $Path.`nLog file:`n $(friendly_path $LogLocation)" - } - if (Test-Path "$DestinationPath\SourceDir") { - movedir "$DestinationPath\SourceDir" "$DestinationPath" | Out-Null - } + $MsiPath = Get-HelperPath -Helper Lessmsi + $ArgList = @('x', "`"$Path`"", "`"$DestinationPath\\`"") } else { - $ok = run 'msiexec' @('/a', "`"$Path`"", '/qn', "TARGETDIR=`"$DestinationPath`"", "/lwe `"$LogLocation`"") - if (!$ok) { - abort "Failed to extract files from $Path.`nLog file:`n $(friendly_path $LogLocation)" - } + $MsiPath = 'msiexec.exe' + $ArgList = @('/a', "`"$Path`"", '/qn', "TARGETDIR=`"$DestinationPath`"") + } + $LogPath = "$(Split-Path $Path)\msi.log" + if ($Switches) { + $ArgList += (-split $Switches) + } + $Status = Invoke-ExternalCommand $MsiPath $ArgList -LogPath $LogPath + if (!$Status) { + abort "Failed to extract files from $Path.`nLog file:`n $(friendly_path $LogPath)" + } + if (Test-Path "$DestinationPath\SourceDir") { + movedir "$DestinationPath\SourceDir" "$DestinationPath" | Out-Null + } + if (($DestinationPath -ne (Split-Path $Path)) -and (Test-Path "$DestinationPath\$(fname $Path)")) { Remove-Item "$DestinationPath\$(fname $Path)" -Force } - if (Test-Path $LogLocation) { - Remove-Item $LogLocation -Force + if (Test-Path $LogPath) { + Remove-Item $LogPath -Force } if ($Removal) { # Remove original archive file @@ -129,7 +142,7 @@ function Expand-MsiArchive { function Expand-InnoArchive { [CmdletBinding()] - param( + param ( [Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)] [String] $Path, @@ -142,13 +155,17 @@ function Expand-InnoArchive { [Switch] $Removal ) - $LogLocation = "$(Split-Path $Path)\innounp.log" - & (Get-HelperPath -Helper Innounp) -x -d"$DestinationPath" -c'{app}' "$Path" (-split $Switches) -y | Out-File $LogLocation - if ($LASTEXITCODE -ne 0) { - abort "Failed to extract files from $Path.`nLog file:`n $(friendly_path $LogLocation)" + $LogPath = "$(Split-Path $Path)\innounp.log" + $ArgList = @('-x', "-d`"$DestinationPath`"", "-c`{app`}", "`"$Path`"", '-y') + if ($Switches) { + $ArgList += (-split $Switches) } - if (Test-Path $LogLocation) { - Remove-Item $LogLocation -Force + $Status = Invoke-ExternalCommand (Get-HelperPath -Helper Innounp) $ArgList -LogPath $LogPath + if (!$Status) { + abort "Failed to extract files from $Path.`nLog file:`n $(friendly_path $LogPath)" + } + if (Test-Path $LogPath) { + Remove-Item $LogPath -Force } if ($Removal) { # Remove original archive file @@ -158,7 +175,7 @@ function Expand-InnoArchive { function Expand-ZipArchive { [CmdletBinding()] - param( + param ( [Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)] [String] $Path, @@ -211,16 +228,23 @@ function Expand-DarkArchive { [Parameter(Position = 1)] [String] $DestinationPath = (Split-Path $Path), + [Parameter(ValueFromRemainingArguments = $true)] + [String] + $Switches, [Switch] $Removal ) - $LogLocation = "$(Split-Path $Path)\dark.log" - & (Get-HelperPath -Helper Dark) -nologo -x "$Path" "$DestinationPath" | Out-File $LogLocation - if ($LASTEXITCODE -ne 0) { - abort "Failed to extract files from $Path.`nLog file:`n $(friendly_path $LogLocation)" + $LogPath = "$(Split-Path $Path)\dark.log" + $ArgList = @('-nologo', "-x `"$DestinationPath`"", "`"$Path`"") + if ($Switches) { + $ArgList += (-split $Switches) + } + $Status = Invoke-ExternalCommand (Get-HelperPath -Helper Dark) $ArgList -LogPath $LogPath + if (!$Status) { + abort "Failed to extract files from $Path.`nLog file:`n $(friendly_path $LogPath)" } - if (Test-Path $LogLocation) { - Remove-Item $LogLocation -Force + if (Test-Path $LogPath) { + Remove-Item $LogPath -Force } if ($Removal) { # Remove original archive file diff --git a/lib/install.ps1 b/lib/install.ps1 index 4b3e49567a..5bdc603966 100644 --- a/lib/install.ps1 +++ b/lib/install.ps1 @@ -686,37 +686,6 @@ function args($config, $dir, $global) { @() } -function run($exe, $arg, $msg, $continue_exit_codes) { - if($msg) { write-host "$msg " -nonewline } - try { - #Allow null/no arguments to be passed - $parameters = @{ } - if ($arg) - { - $parameters.arg = $arg; - } - - # Don't use Start-Process -Wait - # https://github.com/PowerShell/PowerShell/issues/6561 - $proc = start-process $exe -ea stop -passthru @parameters - $proc | Wait-Process - - - if($proc.exitcode -ne 0) { - if($continue_exit_codes -and ($continue_exit_codes.containskey($proc.exitcode))) { - warn $continue_exit_codes[$proc.exitcode] - return $true - } - write-host "Exit code was $($proc.exitcode)."; return $false - } - } catch { - write-host -f darkred $_.exception.tostring() - return $false - } - if($msg) { Write-Host "done." -f Green } - return $true -} - function run_installer($fname, $manifest, $architecture, $dir, $global) { # MSI or other installer $msi = msi $manifest $architecture @@ -753,7 +722,7 @@ function install_msi($fname, $dir, $msi) { $continue_exit_codes = @{ 3010 = "a restart is required to complete installation" } - $installed = run 'msiexec' $arg "Running installer..." $continue_exit_codes + $installed = Invoke-ExternalCommand 'msiexec' $arg -Activity "Running installer..." -ContinueExitCodes $continue_exit_codes if(!$installed) { abort "Installation aborted. You might need to run 'scoop uninstall $app' before trying again." } @@ -785,7 +754,7 @@ function install_prog($fname, $dir, $installer, $global) { if($prog.endswith('.ps1')) { & $prog @arg } else { - $installed = run $prog $arg "Running installer..." + $installed = Invoke-ExternalCommand $prog $arg -Activity "Running installer..." if(!$installed) { abort "Installation aborted. You might need to run 'scoop uninstall $app' before trying again." } @@ -837,7 +806,7 @@ function run_uninstaller($manifest, $architecture, $dir) { if($exe.endswith('.ps1')) { & $exe @arg } else { - $uninstalled = run $exe $arg "Running uninstaller..." $continue_exit_codes + $uninstalled = Invoke-ExternalCommand $exe $arg -Activity "Running uninstaller..." -ContinueExitCodes $continue_exit_codes if(!$uninstalled) { abort "Uninstallation aborted." } } } From e6b2e3d31e246d2af70bd32807159b761fb65e0d Mon Sep 17 00:00:00 2001 From: Hsiao-nan Cheung Date: Thu, 16 May 2019 23:59:42 +0800 Subject: [PATCH 3/7] feature(decompress): Add 'ExtractDir' to 'Expand-...' functions (#3466) * feature(decompress): Add 'ExtractDir' to 'Expand-...' functions * Fix 'TrimEnd' and nested 7z --- lib/decompress.ps1 | 55 ++++++++++++++++++++++++++++++++++++---------- lib/install.ps1 | 27 +---------------------- 2 files changed, 45 insertions(+), 37 deletions(-) diff --git a/lib/decompress.ps1 b/lib/decompress.ps1 index 171a0bc141..df53604a42 100644 --- a/lib/decompress.ps1 +++ b/lib/decompress.ps1 @@ -44,6 +44,8 @@ function Expand-7zipArchive { [Parameter(Position = 1)] [String] $DestinationPath = (Split-Path $Path), + [String] + $ExtractDir, [Parameter(ValueFromRemainingArguments = $true)] [String] $Switches, @@ -64,6 +66,10 @@ function Expand-7zipArchive { } $LogPath = "$(Split-Path $Path)\7zip.log" $ArgList = @('x', "`"$Path`"", "-o`"$DestinationPath`"", '-y') + $IsTar = ((strip_ext $Path) -match '\.tar$') -or ($Path -match '\.t[abgpx]z2?$') + if (!$IsTar -and $ExtractDir) { + $ArgList += "-ir!$ExtractDir\*" + } if ($Switches) { $ArgList += (-split $Switches) } @@ -74,17 +80,20 @@ function Expand-7zipArchive { } $Status = Invoke-ExternalCommand $7zPath $ArgList -LogPath $LogPath if (!$Status) { - abort "Failed to extract files from $Path.`nLog file:`n $(friendly_path $LogPath)" + abort "Failed to extract files from $Path.`nLog file:`n $(friendly_path $LogPath)`n$(new_issue_msg $app $bucket 'decompress error')" + } + if (!$IsTar -and $ExtractDir) { + movedir "$DestinationPath\$ExtractDir" $DestinationPath | Out-Null } if (Test-Path $LogPath) { Remove-Item $LogPath -Force } - if ((strip_ext $Path) -match '\.tar$' -or $Path -match '\.t[abgpx]z2?$') { + if ($IsTar) { # Check for tar $Status = Invoke-ExternalCommand $7zPath @('l', "`"$Path`"") -LogPath $LogPath if ($Status) { $TarFile = (Get-Content -Path $LogPath)[-4] -replace '.{53}(.*)', '$1' # get inner tar file name - Expand-7zipArchive "$DestinationPath\$TarFile" $DestinationPath -Removal + Expand-7zipArchive -Path "$DestinationPath\$TarFile" -DestinationPath $DestinationPath -ExtractDir $ExtractDir -Removal } else { abort "Failed to list files in $Path.`nNot a 7-Zip supported archive file." } @@ -104,18 +113,21 @@ function Expand-MsiArchive { [Parameter(Position = 1)] [String] $DestinationPath = (Split-Path $Path), + [String] + $ExtractDir, [Parameter(ValueFromRemainingArguments = $true)] [String] $Switches, [Switch] $Removal ) + $DestinationPath = $DestinationPath.TrimEnd("\") if ((get_config MSIEXTRACT_USE_LESSMSI)) { $MsiPath = Get-HelperPath -Helper Lessmsi $ArgList = @('x', "`"$Path`"", "`"$DestinationPath\\`"") } else { $MsiPath = 'msiexec.exe' - $ArgList = @('/a', "`"$Path`"", '/qn', "TARGETDIR=`"$DestinationPath`"") + $ArgList = @('/a', "`"$Path`"", '/qn', "TARGETDIR=`"$DestinationPath\\SourceDir`"") } $LogPath = "$(Split-Path $Path)\msi.log" if ($Switches) { @@ -123,10 +135,16 @@ function Expand-MsiArchive { } $Status = Invoke-ExternalCommand $MsiPath $ArgList -LogPath $LogPath if (!$Status) { - abort "Failed to extract files from $Path.`nLog file:`n $(friendly_path $LogPath)" + abort "Failed to extract files from $Path.`nLog file:`n $(friendly_path $LogPath)`n$(new_issue_msg $app $bucket 'decompress error')" } - if (Test-Path "$DestinationPath\SourceDir") { - movedir "$DestinationPath\SourceDir" "$DestinationPath" | Out-Null + if ($ExtractDir -and (Test-Path "$DestinationPath\SourceDir")) { + movedir "$DestinationPath\SourceDir\$ExtractDir" $DestinationPath | Out-Null + Remove-Item "$DestinationPath\SourceDir" -Recurse -Force + } elseif ($ExtractDir) { + Get-ChildItem $DestinationPath -Exclude $ExtractDir | Remove-Item -Recurse -Force + movedir "$DestinationPath\$ExtractDir" $DestinationPath | Out-Null + } elseif (Test-Path "$DestinationPath\SourceDir") { + movedir "$DestinationPath\SourceDir" $DestinationPath | Out-Null } if (($DestinationPath -ne (Split-Path $Path)) -and (Test-Path "$DestinationPath\$(fname $Path)")) { Remove-Item "$DestinationPath\$(fname $Path)" -Force @@ -149,6 +167,8 @@ function Expand-InnoArchive { [Parameter(Position = 1)] [String] $DestinationPath = (Split-Path $Path), + [String] + $ExtractDir, [Parameter(ValueFromRemainingArguments = $true)] [String] $Switches, @@ -156,13 +176,13 @@ function Expand-InnoArchive { $Removal ) $LogPath = "$(Split-Path $Path)\innounp.log" - $ArgList = @('-x', "-d`"$DestinationPath`"", "-c`{app`}", "`"$Path`"", '-y') + $ArgList = @('-x', "-d`"$DestinationPath`"", "-c`{app`}\$ExtractDir", "`"$Path`"", '-y') if ($Switches) { $ArgList += (-split $Switches) } $Status = Invoke-ExternalCommand (Get-HelperPath -Helper Innounp) $ArgList -LogPath $LogPath if (!$Status) { - abort "Failed to extract files from $Path.`nLog file:`n $(friendly_path $LogPath)" + abort "Failed to extract files from $Path.`nLog file:`n $(friendly_path $LogPath)`n$(new_issue_msg $app $bucket 'decompress error')" } if (Test-Path $LogPath) { Remove-Item $LogPath -Force @@ -182,6 +202,8 @@ function Expand-ZipArchive { [Parameter(Position = 1)] [String] $DestinationPath = (Split-Path $Path), + [String] + $ExtractDir, [Switch] $Removal ) @@ -213,6 +235,10 @@ function Expand-ZipArchive { # Compatible with Pscx (https://github.com/Pscx/Pscx) Microsoft.PowerShell.Archive\Expand-Archive -Path $Path -DestinationPath $DestinationPath -Force } + if ($ExtractDir) { + Get-ChildItem $DestinationPath -Exclude $ExtractDir | Remove-Item -Recurse -Force + movedir "$DestinationPath\$ExtractDir" $DestinationPath | Out-Null + } if ($Removal) { # Remove original archive file Remove-Item $Path -Force @@ -221,19 +247,22 @@ function Expand-ZipArchive { function Expand-DarkArchive { [CmdletBinding()] - param( + param ( [Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)] [String] $Path, [Parameter(Position = 1)] [String] $DestinationPath = (Split-Path $Path), + [String] + $ExtractDir, [Parameter(ValueFromRemainingArguments = $true)] [String] $Switches, [Switch] $Removal ) + $DestinationPath = $DestinationPath.TrimEnd("\") $LogPath = "$(Split-Path $Path)\dark.log" $ArgList = @('-nologo', "-x `"$DestinationPath`"", "`"$Path`"") if ($Switches) { @@ -241,11 +270,15 @@ function Expand-DarkArchive { } $Status = Invoke-ExternalCommand (Get-HelperPath -Helper Dark) $ArgList -LogPath $LogPath if (!$Status) { - abort "Failed to extract files from $Path.`nLog file:`n $(friendly_path $LogPath)" + abort "Failed to extract files from $Path.`nLog file:`n $(friendly_path $LogPath)`n$(new_issue_msg $app $bucket 'decompress error')" } if (Test-Path $LogPath) { Remove-Item $LogPath -Force } + if ($ExtractDir) { + Get-ChildItem $DestinationPath -Exclude $ExtractDir | Remove-Item -Recurse -Force + movedir "$DestinationPath\$ExtractDir" $DestinationPath | Out-Null + } if ($Removal) { # Remove original archive file Remove-Item $Path -Force diff --git a/lib/install.ps1 b/lib/install.ps1 index 5bdc603966..6cdb8fc140 100644 --- a/lib/install.ps1 +++ b/lib/install.ps1 @@ -555,33 +555,8 @@ function dl_urls($app, $version, $manifest, $bucket, $architecture, $dir, $use_c Write-Host "Extracting " -NoNewline Write-Host $fname -f Cyan -NoNewline Write-Host " ... " -NoNewline - ensure "$dir\_tmp" | Out-Null - & $extract_fn "$dir\$fname" "$dir\_tmp" -Removal - if ($extract_to) { - ensure "$dir\$extract_to" | Out-Null - } - # fails if zip contains long paths (e.g. atom.json) - #cp "$dir\_tmp\$extract_dir\*" "$dir\$extract_to" -r -force -ea stop - try { - movedir "$dir\_tmp\$extract_dir" "$dir\$extract_to" - } - catch { - error $_ - abort $(new_issue_msg $app $bucket "extract_dir error") - } - - if(Test-Path "$dir\_tmp") { # might have been moved by movedir - try { - Remove-Item -r -force "$dir\_tmp" -ea stop - } catch [system.io.pathtoolongexception] { - & "$env:COMSPEC" /c "rmdir /s /q $dir\_tmp" - } catch [system.unauthorizedaccessexception] { - warn "Couldn't remove $dir\_tmp: unauthorized access." - } - } - + & $extract_fn -Path "$dir\$fname" -DestinationPath "$dir\$extract_to" -ExtractDir $extract_dir -Removal Write-Host "done." -f Green - $extracted++ } } From f5fdc275ca27166c7fd862932f4ad2e124708f20 Mon Sep 17 00:00:00 2001 From: Hsiao-nan Cheung Date: Fri, 17 May 2019 11:53:57 +0800 Subject: [PATCH 4/7] fix(decompress): '$ExtractDir' removed original extract file by accident (#3470) --- lib/decompress.ps1 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/decompress.ps1 b/lib/decompress.ps1 index df53604a42..33ec71a9c8 100644 --- a/lib/decompress.ps1 +++ b/lib/decompress.ps1 @@ -141,7 +141,7 @@ function Expand-MsiArchive { movedir "$DestinationPath\SourceDir\$ExtractDir" $DestinationPath | Out-Null Remove-Item "$DestinationPath\SourceDir" -Recurse -Force } elseif ($ExtractDir) { - Get-ChildItem $DestinationPath -Exclude $ExtractDir | Remove-Item -Recurse -Force + Get-ChildItem $DestinationPath -Exclude $ExtractDir, $Path | Remove-Item -Recurse -Force movedir "$DestinationPath\$ExtractDir" $DestinationPath | Out-Null } elseif (Test-Path "$DestinationPath\SourceDir") { movedir "$DestinationPath\SourceDir" $DestinationPath | Out-Null @@ -236,7 +236,7 @@ function Expand-ZipArchive { Microsoft.PowerShell.Archive\Expand-Archive -Path $Path -DestinationPath $DestinationPath -Force } if ($ExtractDir) { - Get-ChildItem $DestinationPath -Exclude $ExtractDir | Remove-Item -Recurse -Force + Get-ChildItem $DestinationPath -Exclude $ExtractDir, $Path | Remove-Item -Recurse -Force movedir "$DestinationPath\$ExtractDir" $DestinationPath | Out-Null } if ($Removal) { @@ -276,7 +276,7 @@ function Expand-DarkArchive { Remove-Item $LogPath -Force } if ($ExtractDir) { - Get-ChildItem $DestinationPath -Exclude $ExtractDir | Remove-Item -Recurse -Force + Get-ChildItem $DestinationPath -Exclude $ExtractDir, $Path | Remove-Item -Recurse -Force movedir "$DestinationPath\$ExtractDir" $DestinationPath | Out-Null } if ($Removal) { From a775d452ff31838fa7398de8bb09883d879e1ddd Mon Sep 17 00:00:00 2001 From: Hsiao-nan Cheung Date: Sat, 18 May 2019 00:19:51 +0800 Subject: [PATCH 5/7] fix(decompress): '$ExtractDir' error with '.zip' and subdir (#3472) For some `extract_dir` in .zip that has sub-dir, the former one would remove parent dir and get error (flac, flac-1.3.2-win\win64), now use temp dir instead. And also fix .msi, in case of some installer that don't have `SourceDir`. `Expand-DarkArchive` will be used in wix extraction, so it doesn't need `-ExtractDir`. `Expand-7zipArchive` and `Expand-InnoArchive` works well. Tested: - calibre-normal (msi, PFiles\Calibre2) - flac (zip, flac-1.3.2-win\win64) - pkg-config (zip, multiple url and extract_dir) BTW, `Expand-Archive` has `-PassThru` param in PowerShell 6+, and it will be more convinient to support `-ExtractDir` with it. I'll check function's source code and plan to rewrite `Expand-ZipArchive`. Fix #3473, ref https://github.com/lukesampson/scoop/issues/3473#issuecomment-493446461 --- lib/decompress.ps1 | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/lib/decompress.ps1 b/lib/decompress.ps1 index 33ec71a9c8..3dc3f73106 100644 --- a/lib/decompress.ps1 +++ b/lib/decompress.ps1 @@ -122,6 +122,10 @@ function Expand-MsiArchive { $Removal ) $DestinationPath = $DestinationPath.TrimEnd("\") + if ($ExtractDir) { + $OriDestinationPath = $DestinationPath + $DestinationPath = "$DestinationPath\_tmp" + } if ((get_config MSIEXTRACT_USE_LESSMSI)) { $MsiPath = Get-HelperPath -Helper Lessmsi $ArgList = @('x', "`"$Path`"", "`"$DestinationPath\\`"") @@ -138,11 +142,11 @@ function Expand-MsiArchive { abort "Failed to extract files from $Path.`nLog file:`n $(friendly_path $LogPath)`n$(new_issue_msg $app $bucket 'decompress error')" } if ($ExtractDir -and (Test-Path "$DestinationPath\SourceDir")) { - movedir "$DestinationPath\SourceDir\$ExtractDir" $DestinationPath | Out-Null - Remove-Item "$DestinationPath\SourceDir" -Recurse -Force + movedir "$DestinationPath\SourceDir\$ExtractDir" $OriDestinationPath | Out-Null + Remove-Item $DestinationPath -Recurse -Force } elseif ($ExtractDir) { - Get-ChildItem $DestinationPath -Exclude $ExtractDir, $Path | Remove-Item -Recurse -Force - movedir "$DestinationPath\$ExtractDir" $DestinationPath | Out-Null + movedir "$DestinationPath\$ExtractDir" $OriDestinationPath | Out-Null + Remove-Item $DestinationPath -Recurse -Force } elseif (Test-Path "$DestinationPath\SourceDir") { movedir "$DestinationPath\SourceDir" $DestinationPath | Out-Null } @@ -207,6 +211,10 @@ function Expand-ZipArchive { [Switch] $Removal ) + if ($ExtractDir) { + $OriDestinationPath = $DestinationPath + $DestinationPath = "$DestinationPath\_tmp" + } # All methods to unzip the file require .NET4.5+ if ($PSVersionTable.PSVersion.Major -lt 5) { Add-Type -AssemblyName System.IO.Compression.FileSystem @@ -236,8 +244,8 @@ function Expand-ZipArchive { Microsoft.PowerShell.Archive\Expand-Archive -Path $Path -DestinationPath $DestinationPath -Force } if ($ExtractDir) { - Get-ChildItem $DestinationPath -Exclude $ExtractDir, $Path | Remove-Item -Recurse -Force - movedir "$DestinationPath\$ExtractDir" $DestinationPath | Out-Null + movedir "$DestinationPath\$ExtractDir" $OriDestinationPath | Out-Null + Remove-Item $DestinationPath -Recurse -Force } if ($Removal) { # Remove original archive file @@ -254,15 +262,12 @@ function Expand-DarkArchive { [Parameter(Position = 1)] [String] $DestinationPath = (Split-Path $Path), - [String] - $ExtractDir, [Parameter(ValueFromRemainingArguments = $true)] [String] $Switches, [Switch] $Removal ) - $DestinationPath = $DestinationPath.TrimEnd("\") $LogPath = "$(Split-Path $Path)\dark.log" $ArgList = @('-nologo', "-x `"$DestinationPath`"", "`"$Path`"") if ($Switches) { @@ -275,10 +280,6 @@ function Expand-DarkArchive { if (Test-Path $LogPath) { Remove-Item $LogPath -Force } - if ($ExtractDir) { - Get-ChildItem $DestinationPath -Exclude $ExtractDir, $Path | Remove-Item -Recurse -Force - movedir "$DestinationPath\$ExtractDir" $DestinationPath | Out-Null - } if ($Removal) { # Remove original archive file Remove-Item $Path -Force From 1b84fe04fcb26814c664c9d0641418a9caba8754 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20=C4=8C=C3=A1bera?= Date: Sun, 26 May 2019 11:51:06 +0200 Subject: [PATCH 6/7] fix(bin): Checkhashes downloading twice when architecture properties does hot have url property (#3479) --- bin/checkhashes.ps1 | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bin/checkhashes.ps1 b/bin/checkhashes.ps1 index 5233a96dd6..72161455d5 100644 --- a/bin/checkhashes.ps1 +++ b/bin/checkhashes.ps1 @@ -70,15 +70,15 @@ foreach ($single in Get-ChildItem $Dir "$App.json") { $urls = @() $hashes = @() - if ($manifest.architecture) { + if ($manifest.url) { + $manifest.url | ForEach-Object { $urls += $_ } + $manifest.hash | ForEach-Object { $hashes += $_ } + } elseif ($manifest.architecture) { # First handle 64bit url $manifest '64bit' | ForEach-Object { $urls += $_ } hash $manifest '64bit' | ForEach-Object { $hashes += $_ } url $manifest '32bit' | ForEach-Object { $urls += $_ } hash $manifest '32bit' | ForEach-Object { $hashes += $_ } - } elseif ($manifest.url) { - $manifest.url | ForEach-Object { $urls += $_ } - $manifest.hash | ForEach-Object { $hashes += $_ } } else { err $name 'Manifest does not contain URL property.' continue From 81e3cf61b6a25d6ccedebe5b55dc3bd70c53e54b Mon Sep 17 00:00:00 2001 From: Hsiao-nan Cheung Date: Thu, 30 May 2019 20:55:48 +0800 Subject: [PATCH 7/7] feat(compress): Allow 'Expand-InnoArchive -ExtractDir' to accept '{xxx}' (#3487) --- lib/decompress.ps1 | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/decompress.ps1 b/lib/decompress.ps1 index 3dc3f73106..4606ce4c18 100644 --- a/lib/decompress.ps1 +++ b/lib/decompress.ps1 @@ -180,7 +180,12 @@ function Expand-InnoArchive { $Removal ) $LogPath = "$(Split-Path $Path)\innounp.log" - $ArgList = @('-x', "-d`"$DestinationPath`"", "-c`{app`}\$ExtractDir", "`"$Path`"", '-y') + $ArgList = @('-x', "-d`"$DestinationPath`"", "`"$Path`"", '-y') + switch -Regex ($ExtractDir) { + "^[^{].*" { $ArgList += "-c{app}\$ExtractDir" } + "^{.*" { $ArgList += "-c$ExtractDir" } + Default { $ArgList += "-c{app}" } + } if ($Switches) { $ArgList += (-split $Switches) }