Skip to content

Commit faaf863

Browse files
committed
fix tar on windows
1 parent bd30746 commit faaf863

File tree

1 file changed

+38
-43
lines changed

1 file changed

+38
-43
lines changed

src/powershell-extended/Invoke-NerdFontInstaller.ps1

Lines changed: 38 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -815,48 +815,43 @@ begin {
815815
function Expand-FromArchiveType {
816816
param (
817817
[string]$SourceFile,
818-
[string]$DestinationFolder
818+
[string]$DestinationFolder,
819+
[string]$FileExtension,
820+
[string]$Executable
819821
)
820822

821823
# Define a mapping table for command templates
822824
$commandTemplates = @{
823-
'tar.xz' = 'tar -xJf "{0}" -C "{1}"'
824-
'tar.bz2' = 'tar -xjf "{0}" -C "{1}"'
825-
'tar.gz' = 'tar -xzf "{0}" -C "{1}"'
826-
'tar' = 'tar -xf "{0}" -C "{1}"'
827-
'xz' = 'xz -d "{0}" -C "{1}"'
828-
'7z' = '7z x "{0}" -o"{1}"'
829-
'bzip2' = 'bzip2 -d "{0}" -C "{1}"'
830-
'gzip' = 'gzip -d "{0}" -C "{1}"'
825+
'tar.xz' = '{0} -xJf "{1}" -C "{2}"'
826+
'tar.bz2' = '{0} -xjf "{1}" -C "{2}"'
827+
'tar.gz' = '{0} -xzf "{1}" -C "{2}"'
828+
'tar' = '{0} -xf "{1}" -C "{2}"'
829+
'xz' = '{0} -d "{1}" -C "{2}"'
830+
'7z' = '{0} x "{1}" -o"{2}"'
831+
'bzip2' = '{0} -d "{1}" -C "{2}"'
832+
'gzip' = '{0} -d "{1}" -C "{2}"'
831833
}
832834

833-
# Extract the full extension, including multi-part extensions
834-
$fileName = [System.IO.Path]::GetFileName($SourceFile)
835-
$fileExtension = $commandTemplates.Keys | Where-Object { $fileName.EndsWith($_) } | Select-Object -First 1
836-
837-
if ($null -eq $fileExtension) {
838-
if ($fileName.EndsWith('.zip')) {
839-
# Use .NET functions to extract zip files
840-
Add-Type -AssemblyName System.IO.Compression.FileSystem
841-
[System.IO.Compression.ZipFile]::ExtractToDirectory($SourceFile, $DestinationFolder)
842-
Write-Verbose "Extracted zip file using .NET functions."
843-
}
844-
else {
845-
throw "Unsupported archive format: $fileName"
846-
}
835+
if ($null -eq $Executable) {
836+
throw "Unsupported archive format: $FileExtension"
837+
}
838+
elseif ($Executable -eq 'powershell') {
839+
# Use .NET functions to extract zip files
840+
Add-Type -AssemblyName System.IO.Compression.FileSystem
841+
[System.IO.Compression.ZipFile]::ExtractToDirectory($SourceFile, $DestinationFolder)
842+
Write-Verbose "Extracted zip file using .NET functions."
847843
}
848844
else {
849-
$commandTemplate = $commandTemplates[$fileExtension]
850-
$command = $commandTemplate -f $SourceFile, $DestinationFolder
845+
$commandTemplate = $commandTemplates[$FileExtension]
846+
$command = $commandTemplate -f $Executable, $SourceFile, $DestinationFolder
851847
Write-Verbose "Running command: $command"
852848

853849
# Split the command into the executable and its arguments
854850
$commandParts = $command -split ' ', 2
855-
$executable = $commandParts[0]
856851
$arguments = if ($commandParts.Length -gt 1) { $commandParts[1] } else { "" }
857852

858853
# Execute the external command
859-
Start-Process -FilePath $executable -ArgumentList $arguments -NoNewWindow -Wait
854+
Start-Process -FilePath $Executable -ArgumentList $arguments -NoNewWindow -Wait
860855
}
861856
}
862857
#endregion Functions -------------------------------------------------------
@@ -978,30 +973,30 @@ begin {
978973
$archivePreferenceOrder = @('tar.xz', '7z', 'tar.bz2', 'tar.gz', 'zip', 'tar')
979974

980975
# ZIP is natively supported in PowerShell
981-
[void]$supportedArchiveFormats.Add([pscustomobject]@{FileExtension = 'zip'; Command = 'powershell' })
976+
[void]$supportedArchiveFormats.Add([pscustomobject]@{FileExtension = 'zip'; Executable = 'powershell' })
982977

983978
if ($IsMacOS -or $IsLinux) {
984979
# Prefer tar if available
985980
if (Get-Command tar -ErrorAction Ignore) {
986-
if (Test-TarSupportsFormat 'xz') { [void]$supportedArchiveFormats.Add([pscustomobject]@{FileExtension = 'tar.xz'; Command = 'tar' }) }
987-
if (Test-TarSupportsFormat 'bzip2') { [void]$supportedArchiveFormats.Add([pscustomobject]@{FileExtension = 'tar.bz2'; Command = 'tar' }) }
988-
if (Test-TarSupportsFormat 'gz') { [void]$supportedArchiveFormats.Add([pscustomobject]@{FileExtension = 'tar.gz'; Command = 'tar' }) }
989-
[void]$supportedArchiveFormats.Add([pscustomobject]@{FileExtension = 'tar'; Command = 'tar' })
981+
if (Test-TarSupportsFormat 'xz') { [void]$supportedArchiveFormats.Add([pscustomobject]@{FileExtension = 'tar.xz'; Executable = 'tar' }) }
982+
if (Test-TarSupportsFormat 'bzip2') { [void]$supportedArchiveFormats.Add([pscustomobject]@{FileExtension = 'tar.bz2'; Executable = 'tar' }) }
983+
if (Test-TarSupportsFormat 'gz') { [void]$supportedArchiveFormats.Add([pscustomobject]@{FileExtension = 'tar.gz'; Executable = 'tar' }) }
984+
[void]$supportedArchiveFormats.Add([pscustomobject]@{FileExtension = 'tar'; Executable = 'tar' })
990985
}
991986
# Check for individual tools
992-
if (Get-Command xz -ErrorAction Ignore) { [void]$supportedArchiveFormats.Add([pscustomobject]@{FileExtension = 'tar.xz'; Command = 'xz' }) }
993-
if (Get-Command 7z -ErrorAction Ignore) { [void]$supportedArchiveFormats.Add([pscustomobject]@{FileExtension = '7z'; Command = '7z' }) }
994-
if (Get-Command bzip2 -ErrorAction Ignore) { [void]$supportedArchiveFormats.Add([pscustomobject]@{FileExtension = 'tar.bz2'; Command = 'bzip2' }) }
995-
if (Get-Command gzip -ErrorAction Ignore) { [void]$supportedArchiveFormats.Add([pscustomobject]@{FileExtension = 'tar.gz'; Command = 'gzip' }) }
987+
if (Get-Command xz -ErrorAction Ignore) { [void]$supportedArchiveFormats.Add([pscustomobject]@{FileExtension = 'tar.xz'; Executable = 'xz' }) }
988+
if (Get-Command 7z -ErrorAction Ignore) { [void]$supportedArchiveFormats.Add([pscustomobject]@{FileExtension = '7z'; Executable = '7z' }) }
989+
if (Get-Command bzip2 -ErrorAction Ignore) { [void]$supportedArchiveFormats.Add([pscustomobject]@{FileExtension = 'tar.bz2'; Executable = 'bzip2' }) }
990+
if (Get-Command gzip -ErrorAction Ignore) { [void]$supportedArchiveFormats.Add([pscustomobject]@{FileExtension = 'tar.gz'; Executable = 'gzip' }) }
996991
}
997992
else {
998993
if (Get-Command tar -ErrorAction Ignore) {
999-
if (Test-TarSupportsFormat 'xz') { [void]$supportedArchiveFormats.Add([pscustomobject]@{FileExtension = 'tar.xz'; Command = 'tar' }) }
1000-
if (Test-TarSupportsFormat 'bzip2') { [void]$supportedArchiveFormats.Add([pscustomobject]@{FileExtension = 'tar.bz2'; Command = 'tar' }) }
1001-
if (Test-TarSupportsFormat 'gz') { [void]$supportedArchiveFormats.Add([pscustomobject]@{FileExtension = 'tar.gz'; Command = 'tar' }) }
1002-
[void]$supportedArchiveFormats.Add([pscustomobject]@{FileExtension = 'tar'; Command = 'tar' })
994+
if (Test-TarSupportsFormat 'xz') { [void]$supportedArchiveFormats.Add([pscustomobject]@{FileExtension = 'tar.xz'; Executable = 'tar' }) }
995+
if (Test-TarSupportsFormat 'bzip2') { [void]$supportedArchiveFormats.Add([pscustomobject]@{FileExtension = 'tar.bz2'; Executable = 'tar' }) }
996+
if (Test-TarSupportsFormat 'gz') { [void]$supportedArchiveFormats.Add([pscustomobject]@{FileExtension = 'tar.gz'; Executable = 'tar' }) }
997+
[void]$supportedArchiveFormats.Add([pscustomobject]@{FileExtension = 'tar'; Executable = 'tar' })
1003998
}
1004-
if (Get-Command 7z -ErrorAction Ignore) { [void]$supportedArchiveFormats.Add([pscustomobject]@{FileExtension = '7z'; Command = '7z' }) }
999+
if (Get-Command 7z -ErrorAction Ignore) { [void]$supportedArchiveFormats.Add([pscustomobject]@{FileExtension = '7z'; Executable = '7z' }) }
10051000
}
10061001

10071002
# Sort the supportedArchiveFormats based on the preference order and remove duplicates
@@ -1057,7 +1052,7 @@ process {
10571052

10581053
Write-Verbose "Font archive URL: $assetUrl"
10591054
Write-Verbose "Font archive format: $($archiveFormat.FileExtension)"
1060-
Write-Verbose "Font archive extract command: $($archiveFormat.Command)"
1055+
Write-Verbose "Font archive extract executable: $($archiveFormat.Executable)"
10611056

10621057
if (
10631058
$PSCmdlet.ShouldProcess(
@@ -1102,7 +1097,7 @@ process {
11021097
else {
11031098
Write-Verbose "Extracting font files to $extractPath"
11041099
$null = [System.IO.Directory]::CreateDirectory($extractPath)
1105-
Expand-FromArchiveType -SourceFile $archivePath -DestinationFolder $extractPath
1100+
Expand-FromArchiveType -SourceFile $archivePath -DestinationFolder $extractPath -FileExtension $archiveFormat.FileExtension -Executable $archiveFormat.Executable
11061101
}
11071102

11081103
# Determine search paths for font files based in $Variant parameter

0 commit comments

Comments
 (0)