Skip to content

Commit c91aa1f

Browse files
committed
refac: Expand-PSDependArchive
internal function to cater for legacy and new ps versions
1 parent bdd908c commit c91aa1f

File tree

3 files changed

+31
-14
lines changed

3 files changed

+31
-14
lines changed

PSDepend2/PSDependScripts/GitHub.ps1

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -456,18 +456,8 @@ if(($PSDependAction -contains 'Install') -and $ShouldInstall)
456456
return
457457
}
458458

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

472462
# Remove the zip file
473463
Remove-Item $OutFile -Force -Confirm:$false

PSDepend2/PSDependScripts/Terraform.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ if ($PSDependAction -eq "Install" -and $InstallNeeded) {
135135
Write-Verbose "Version of zip found at $DownloadPath"
136136
}
137137

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

141141
if ($Dependency.AddToPath) {
142142
Write-Verbose "Setting PATH to`n$($Path, $env:PATH -join ';' | Out-String)"
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
function Expand-PSDependArchive {
2+
[CmdletBinding()]
3+
param (
4+
[String]
5+
$Path,
6+
7+
[String]
8+
$DestinationPath,
9+
10+
[Switch]
11+
$Force
12+
)
13+
14+
end {
15+
# Use Windows unzip method as otherwise Expand-Archive exists and that runs on all platforms
16+
if ($null -eq $(Get-Command -Name Expand-Archive -ErrorAction SilentlyContinue)) {
17+
Write-Verbose "Extracting using legacy unzip method"
18+
$ZipFile = (New-Object -com shell.application).NameSpace($Path)
19+
$ZipDestination = (New-Object -com shell.application).NameSpace($DestinationPath)
20+
$ZipDestination.CopyHere($ZipFile.Items())
21+
}
22+
else {
23+
Write-Verbose "Extracting using current Expand-Archive function"
24+
Expand-Archive -Path $Path -DestinationPath $DestinationPath -Force:$Force
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)