From dc207a2c891fe6deb2710ccde0abf48078f64fcd Mon Sep 17 00:00:00 2001 From: Alexander Karatarakis Date: Thu, 12 Apr 2018 18:15:38 -0700 Subject: [PATCH] Restore powershell extracting because shell may not be available (see #3252) --- scripts/VcpkgPowershellUtils.ps1 | 40 ++++++++++++++++++++++++-------- scripts/fetchTool.ps1 | 4 ++-- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/scripts/VcpkgPowershellUtils.ps1 b/scripts/VcpkgPowershellUtils.ps1 index 3ab301c552d..fdd89e7b9e7 100644 --- a/scripts/VcpkgPowershellUtils.ps1 +++ b/scripts/VcpkgPowershellUtils.ps1 @@ -1,3 +1,8 @@ +function vcpkgHasModule([Parameter(Mandatory=$true)][string]$moduleName) +{ + return [bool](Get-Module -ListAvailable -Name $moduleName) +} + function vcpkgHasProperty([Parameter(Mandatory=$true)][AllowNull()]$object, [Parameter(Mandatory=$true)]$propertyName) { if ($object -eq $null) @@ -183,9 +188,9 @@ function vcpkgDownloadFileWithAria2( [Parameter(Mandatory=$true)][string]$ari Move-Item -Path $downloadPartPath -Destination $downloadPath } -function vcpkgExtractFile( [Parameter(Mandatory=$true)][string]$sevenZipExe, - [Parameter(Mandatory=$true)][string]$archivePath, - [Parameter(Mandatory=$true)][string]$destinationDir) +function vcpkgExtractFileWith7z([Parameter(Mandatory=$true)][string]$sevenZipExe, + [Parameter(Mandatory=$true)][string]$archivePath, + [Parameter(Mandatory=$true)][string]$destinationDir) { vcpkgRemoveItem $destinationDir $destinationPartial = "$destinationDir.partial" @@ -200,20 +205,35 @@ function vcpkgExtractFile( [Parameter(Mandatory=$true)][string]$sevenZipExe, Rename-Item -Path "$destinationPartial" -NewName $destinationDir } -function vcpkgExtractZipFileWithShell( [Parameter(Mandatory=$true)][string]$archivePath, - [Parameter(Mandatory=$true)][string]$destinationDir) +function vcpkgExtractZipFile( [Parameter(Mandatory=$true)][string]$archivePath, + [Parameter(Mandatory=$true)][string]$destinationDir) { vcpkgRemoveItem $destinationDir $destinationPartial = "$destinationDir.partial" vcpkgRemoveItem $destinationPartial vcpkgCreateDirectoryIfNotExists $destinationPartial - $shell = new-object -com shell.application - $zip = $shell.NameSpace($(Get-Item $archivePath).fullname) - foreach($item in $zip.items()) + + if (vcpkgHasCommand -commandName 'Microsoft.PowerShell.Archive\Expand-Archive') { - # Piping to Out-Null is used to block until finished - $shell.Namespace($destinationPartial).copyhere($item) | Out-Null + Write-Verbose("Extracting with Microsoft.PowerShell.Archive\Expand-Archive") + Microsoft.PowerShell.Archive\Expand-Archive -path $archivePath -destinationpath $destinationPartial + } + elseif (vcpkgHasCommand -commandName 'Pscx\Expand-Archive') + { + Write-Verbose("Extracting with Pscx\Expand-Archive") + Pscx\Expand-Archive -path $archivePath -OutputPath $destinationPartial + } + else + { + Write-Verbose("Extracting via shell") + $shell = new-object -com shell.application + $zip = $shell.NameSpace($(Get-Item $archivePath).fullname) + foreach($item in $zip.items()) + { + # Piping to Out-Null is used to block until finished + $shell.Namespace($destinationPartial).copyhere($item) | Out-Null + } } Rename-Item -Path "$destinationPartial" -NewName $destinationDir diff --git a/scripts/fetchTool.ps1 b/scripts/fetchTool.ps1 index be18656c763..dd3f0f9f49e 100644 --- a/scripts/fetchTool.ps1 +++ b/scripts/fetchTool.ps1 @@ -73,7 +73,7 @@ function fetchToolInternal([Parameter(Mandatory=$true)][string]$tool) # Extract aria2 with shell because we need it to download 7zip if ($tool -eq "7zip920" -or $tool -eq "aria2") { - vcpkgExtractZipFileWithShell -ArchivePath $downloadPath -DestinationDir $toolPath + vcpkgExtractZipFile -ArchivePath $downloadPath -DestinationDir $toolPath } elseif ($tool -eq "7zip") { @@ -88,7 +88,7 @@ function fetchToolInternal([Parameter(Mandatory=$true)][string]$tool) else { $sevenZipExe = fetchToolInternal "7zip" - vcpkgExtractFile -sevenZipExe "$sevenZipExe" -ArchivePath $downloadPath -DestinationDir $toolPath + vcpkgExtractFileWith7z -sevenZipExe "$sevenZipExe" -ArchivePath $downloadPath -DestinationDir $toolPath } Write-Host "Extracting $tool... done." }