Improve vcpkgExtractFile. Also merge vcpkgRemoveDirectory/File

This commit is contained in:
Alexander Karatarakis 2017-12-08 15:16:35 -08:00
parent 583ee9ee91
commit 1f3013bea3
2 changed files with 20 additions and 25 deletions

View File

@ -25,7 +25,7 @@ function vcpkgCreateParentDirectoryIfNotExists([Parameter(Mandatory=$true)][stri
}
}
function vcpkgRemoveDirectory([Parameter(Mandatory=$true)][string]$dirPath)
function vcpkgRemoveItem([Parameter(Mandatory=$true)][string]$dirPath)
{
if (Test-Path $dirPath)
{
@ -33,14 +33,6 @@ function vcpkgRemoveDirectory([Parameter(Mandatory=$true)][string]$dirPath)
}
}
function vcpkgRemoveFile([Parameter(Mandatory=$true)][string]$filePath)
{
if (Test-Path $filePath)
{
Remove-Item $filePath -Force
}
}
function vcpkgHasCommand([Parameter(Mandatory=$true)][string]$commandName)
{
return [bool](Get-Command -Name $commandName -ErrorAction SilentlyContinue)
@ -118,7 +110,7 @@ function vcpkgDownloadFile( [Parameter(Mandatory=$true)][string]$url,
vcpkgCreateParentDirectoryIfNotExists $downloadPath
$downloadPartPath = "$downloadPath.part"
vcpkgRemoveFile $downloadPartPath
vcpkgRemoveItem $downloadPartPath
$wc = New-Object System.Net.WebClient
$proxyAuth = !$wc.Proxy.IsBypassed($url)
@ -144,7 +136,7 @@ function vcpkgDownloadFile( [Parameter(Mandatory=$true)][string]$url,
catch [System.Exception]
{
# If BITS fails for any reason, delete any potentially partially downloaded files and continue
vcpkgRemoveFile $downloadPartPath
vcpkgRemoveItem $downloadPartPath
}
}
@ -154,14 +146,21 @@ function vcpkgDownloadFile( [Parameter(Mandatory=$true)][string]$url,
}
function vcpkgExtractFile( [Parameter(Mandatory=$true)][string]$file,
[Parameter(Mandatory=$true)][string]$destinationDir)
[Parameter(Mandatory=$true)][string]$destinationDir,
[Parameter(Mandatory=$true)][string]$outFilename)
{
vcpkgCreateParentDirectoryIfNotExists $destinationDir
$destinationPartial = "$destinationDir-partially_extracted"
vcpkgCreateDirectoryIfNotExists $destinationDir
$output = "$destinationDir/$outFilename"
vcpkgRemoveItem $output
$destinationPartial = "$destinationDir/partially-extracted"
vcpkgRemoveDirectory $destinationPartial
vcpkgRemoveItem $destinationPartial
vcpkgCreateDirectoryIfNotExists $destinationPartial
$shell = new-object -com shell.application
$zip = $shell.NameSpace($file)
$itemCount = $zip.Items().Count
if (vcpkgHasCommand -commandName 'Microsoft.PowerShell.Archive\Expand-Archive')
{
Write-Verbose("Extracting with Microsoft.PowerShell.Archive\Expand-Archive")
@ -175,8 +174,6 @@ function vcpkgExtractFile( [Parameter(Mandatory=$true)][string]$file,
else
{
Write-Verbose("Extracting via shell")
$shell = new-object -com shell.application
$zip = $shell.NameSpace($file)
foreach($item in $zip.items())
{
# Piping to Out-Null is used to block until finished
@ -184,16 +181,14 @@ function vcpkgExtractFile( [Parameter(Mandatory=$true)][string]$file,
}
}
$hasASingleItem = (Get-ChildItem $destinationPartial | Measure-Object).Count -eq 1;
if ($hasASingleItem)
if ($itemCount -eq 1)
{
Move-Item -Path "$destinationPartial\*" -Destination $destinationDir
vcpkgRemoveDirectory $destinationPartial
Move-Item -Path "$destinationPartial\*" -Destination $output
vcpkgRemoveItem $destinationPartial
}
else
{
Move-Item -Path $destinationPartial -Destination $destinationDir
Move-Item -Path $destinationPartial -Destination $output
}
}

View File

@ -88,8 +88,8 @@ function SelectProgram([Parameter(Mandatory=$true)][string]$Dependency)
{
if (-not (Test-Path $executableFromDownload))
{
$extractFolderName = (Get-ChildItem $downloadPath).BaseName
vcpkgExtractFile -File $downloadPath -DestinationDir "$downloadsDir\$extractFolderName"
$outFilename = (Get-ChildItem $downloadPath).BaseName
vcpkgExtractFile -File $downloadPath -DestinationDir $downloadsDir -outFilename $outFilename
}
}
elseif($extractionType -eq $ExtractionType_SELF_EXTRACTING_7Z)