Skip to content

Commit

Permalink
refac: Expand-PSDependArchive
Browse files Browse the repository at this point in the history
internal function to cater for legacy and new ps versions
  • Loading branch information
CorpoC committed May 25, 2022
1 parent bdd908c commit c91aa1f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
14 changes: 2 additions & 12 deletions PSDepend2/PSDependScripts/GitHub.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -456,18 +456,8 @@ if(($PSDependAction -contains 'Install') -and $ShouldInstall)
return
}

# Extract the zip file
if (($script:IsWindows) -and ($psEdition -eq 'Desktop') -and ($null -eq $(Get-Command -Name Expand-Archive)))
{
$ZipFile = (New-Object -com shell.application).NameSpace($OutFile)
$ZipDestination = (New-Object -com shell.application).NameSpace($OutPath)
$ZipDestination.CopyHere($ZipFile.Items())
}
else
{
# If not on Windows "Expand-Archive" should be available as PS version 6 is considered minimum.
Expand-Archive $OutFile -DestinationPath $OutPath
}
# Use our internal implementation to cater for Windows PS 5.1 and below + pwsh 6+
Expand-PSDependArchive -Path $OutFile -DestinationPath $OutPath

# Remove the zip file
Remove-Item $OutFile -Force -Confirm:$false
Expand Down
4 changes: 2 additions & 2 deletions PSDepend2/PSDependScripts/Terraform.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ if ($PSDependAction -eq "Install" -and $InstallNeeded) {
Write-Verbose "Version of zip found at $DownloadPath"
}

$Out = Expand-Archive -Path $DownloadPath -DestinationPath $Path -Force -PassThru
Write-Verbose "Terraform installed to $Out"
Expand-PSDependArchive -Path $DownloadPath -DestinationPath $Path -Force
Write-Verbose "Terraform installed to $Path"

if ($Dependency.AddToPath) {
Write-Verbose "Setting PATH to`n$($Path, $env:PATH -join ';' | Out-String)"
Expand Down
27 changes: 27 additions & 0 deletions PSDepend2/Private/Expand-PSDependArchive.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
function Expand-PSDependArchive {
[CmdletBinding()]
param (
[String]
$Path,

[String]
$DestinationPath,

[Switch]
$Force
)

end {
# Use Windows unzip method as otherwise Expand-Archive exists and that runs on all platforms
if ($null -eq $(Get-Command -Name Expand-Archive -ErrorAction SilentlyContinue)) {
Write-Verbose "Extracting using legacy unzip method"
$ZipFile = (New-Object -com shell.application).NameSpace($Path)
$ZipDestination = (New-Object -com shell.application).NameSpace($DestinationPath)
$ZipDestination.CopyHere($ZipFile.Items())
}
else {
Write-Verbose "Extracting using current Expand-Archive function"
Expand-Archive -Path $Path -DestinationPath $DestinationPath -Force:$Force
}
}
}

0 comments on commit c91aa1f

Please sign in to comment.