Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(decompress): Add Expand-ZstdArchive function #97

Merged
merged 17 commits into from Jan 1, 2021
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,7 @@

### 0.6-pre1

- **decompress**: Add `Expand-ZstdArchive` function for extracting standalone zstd archives
- **scoop-install**: Allow modules to be installed globally
- **scoop-install**: Prevent repeated installation of same manifest/url/local file
- **binaries**: Support YAML typed manifests
Expand Down
2 changes: 2 additions & 0 deletions lib/Diagnostic.ps1
Expand Up @@ -214,6 +214,8 @@ function Test-HelpersInstalled {
$result = $false
}

# TODO: Consider checking for zstd if it will be used by more vendors

return $result
}

Expand Down
5 changes: 3 additions & 2 deletions lib/core.ps1
Expand Up @@ -300,7 +300,7 @@ function Get-HelperPath {
[OutputType([String])]
param(
[Parameter(Mandatory, ValueFromPipeline)]
[ValidateSet('7zip', 'Lessmsi', 'Innounp', 'Dark', 'Aria2')]
[ValidateSet('7zip', 'Lessmsi', 'Innounp', 'Dark', 'Aria2', 'Zstd')]
[String] $Helper
)

Expand All @@ -310,6 +310,7 @@ function Get-HelperPath {
'Aria2' { $helperPath = Get-AppFilePath 'aria2' 'aria2c.exe' }
'Innounp' { $helperPath = Get-AppFilePath 'innounp' 'innounp.exe' }
'Lessmsi' { $helperPath = Get-AppFilePath 'lessmsi' 'lessmsi.exe' }
'Zstd' { $HelperPath = Get-AppFilePath 'zstd' 'zstd.exe' }
'7zip' {
$helperPath = Get-AppFilePath '7zip' '7z.exe'
if ([String]::IsNullOrEmpty($helperPath)) {
Expand Down Expand Up @@ -339,7 +340,7 @@ function Test-HelperInstalled {
[OutputType([bool])]
param(
[Parameter(Mandatory, ValueFromPipeline)]
[ValidateSet('7zip', 'Lessmsi', 'Innounp', 'Dark', 'Aria2')]
[ValidateSet('7zip', 'Lessmsi', 'Innounp', 'Dark', 'Aria2', 'Zstd')]
[String] $Helper
)

Expand Down
102 changes: 101 additions & 1 deletion lib/decompress.ps1
@@ -1,5 +1,5 @@
# TODO: Core import is messing up with download progress
'Helpers' | ForEach-Object {
'Helpers' | ForEach-Object { #, 'core' | ForEach-Object {
. (Join-Path $PSScriptRoot "$_.ps1")
}

Expand Down Expand Up @@ -62,6 +62,23 @@ function Test-LessmsiRequirement {
return $false
}
}

function Test-ZstdRequirement {
[CmdletBinding(DefaultParameterSetName = 'URL')]
[OutputType([Boolean])]
param (
[Parameter(Mandatory, ParameterSetName = 'URL')]
[String[]] $URL,
[Parameter(Mandatory, ParameterSetName = 'File')]
[String] $File
)

if ($URL) {
return ($URL | Where-Object { Test-ZstdRequirement -File $_ }).Count -gt 0
} else {
return $File -match '\.zst$'
}
}
#endregion helpers

function Expand-7zipArchive {
Expand All @@ -86,6 +103,7 @@ function Expand-7zipArchive {
[Parameter(Mandatory, Position = 0, ValueFromPipeline)]
[String] $Path,
[Parameter(Position = 1)]
[Alias('ExtractTo')]
[String] $DestinationPath = (Split-Path $Path),
[String] $ExtractDir,
[Parameter(ValueFromRemainingArguments)]
Expand Down Expand Up @@ -172,6 +190,7 @@ function Expand-MsiArchive {
[Parameter(Mandatory, Position = 0, ValueFromPipeline)]
[String] $Path,
[Parameter(Position = 1)]
[Alias('ExtractTo')]
[String] $DestinationPath = (Split-Path $Path),
[String] $ExtractDir,
[Parameter(ValueFromRemainingArguments)]
Expand Down Expand Up @@ -245,6 +264,7 @@ function Expand-InnoArchive {
[Parameter(Mandatory, Position = 0, ValueFromPipeline)]
[String] $Path,
[Parameter(Position = 1)]
[Alias('ExtractTo')]
[String] $DestinationPath = (Split-Path $Path),
[String] $ExtractDir,
[Parameter(ValueFromRemainingArguments)]
Expand Down Expand Up @@ -298,6 +318,7 @@ function Expand-ZipArchive {
[Parameter(Mandatory, Position = 0, ValueFromPipeline)]
[String] $Path,
[Parameter(Position = 1)]
[Alias('ExtractTo')]
[String] $DestinationPath = (Split-Path $Path),
[String] $ExtractDir,
[Switch] $Removal
Expand Down Expand Up @@ -365,6 +386,7 @@ function Expand-DarkArchive {
[Parameter(Mandatory, Position = 0, ValueFromPipeline)]
[String] $Path,
[Parameter(Position = 1)]
[Alias('ExtractTo')]
[String] $DestinationPath = (Split-Path $Path),
[Parameter(ValueFromRemainingArguments = $true)]
[String] $Switches,
Expand Down Expand Up @@ -392,6 +414,84 @@ function Expand-DarkArchive {
}
}

function Expand-ZstdArchive {
<#
.SYNOPSIS
Extract files from zstd archive.
The final extracted from zstd archive will be named same as original file, but without .zst extension.
.PARAMETER Path
Specifies the path to the zstd archive.
.PARAMETER DestinationPath
Specifies the location, where archive should be extracted to.
.PARAMETER ExtractDir
Specifies to extract only nested directory inside archive.
.PARAMETER Switches
Specifies additional parameters passed to the extraction.
.PARAMETER Overwrite
Specifies to override files with same name.
.PARAMETER Removal
Specifies to remove the archive after extraction is done.
.PARAMETER Skip7zip
Specifies to not extract resulted file of zstd extraction.
#>
[CmdletBinding()]
param (
[Parameter(Mandatory, Position = 0, ValueFromPipeline)]
[String] $Path,
[Parameter(Position = 1)]
[Alias('ExtractTo')]
[String] $DestinationPath,
[String] $ExtractDir,
[Parameter(ValueFromRemainingArguments)]
[String] $Switches,
[Switch] $Overwrite,
[Switch] $Removal,
[Switch] $Skip7zip
)

begin {
$zstdPath = Get-HelperPath -Helper 'Zstd'
if ($null -eq $zstdPath) { throw 'Ignore|-''zstd'' is not installed or cannot be used' } # TerminatingError thrown

$argList = @('-d', '-v')
if ($Switches) { $argList += (-split $Switches) }
if ($Overwrite) { $argList += '-f' }
}

process {
$_path = $Path
$_item = Get-Item $_path
$_log = Join-Path $_item.Directory.FullName 'zstd.log'
$_extractDir = $ExtractDir
$_dest = $DestinationPath
$_output = Join-Path $_dest $_item.BaseName

$_arg = $argList
$_arg += """$_path""", '-o', """$_output"""

$status = Invoke-ExternalCommand -Path $zstdPath -ArgumentList $_arg -LogPath $_log
if (!$status) {
throw "Decompress error|-Failed to extract files from $_path.`nLog file:`n $(friendly_path $_log)"
}

Remove-Item -Path $_log -ErrorAction 'SilentlyContinue' -Force

# There is no reason to consider that the output of zstd is something other then next archive, but who knows
if (!$Skip7zip) {
try {
Expand-7zipArchive -Path $_output -DestinationPath $_dest -ExtractDir $_extractDir -Removal
} catch {
# TODO?: Some meaningfull message??
throw $_
}
}
}

end {
if ($Removal) { Remove-Item -Path $Path -Force }
}
}

#region Deprecated
function extract_7zip($path, $to, $removal) {
Show-DeprecatedWarning $MyInvocation 'Expand-7zipArchive'
Expand Down
10 changes: 10 additions & 0 deletions lib/depends.ps1
Expand Up @@ -78,6 +78,11 @@ function script_deps($script) {
if ((($script -like '*Expand-7zipArchive *') -or ($script -like '*extract_7zip *')) -and !(Test-HelperInstalled -Helper '7zip')) { $deps += '7zip' }
if ((($script -like '*Expand-MsiArchive *') -or ($script -like '*extract_msi *')) -and !(Test-HelperInstalled -Helper 'Lessmsi')) { $deps += 'lessmsi' }
if ((($script -like '*Expand-InnoArchive *') -or ($script -like '*unpack_inno *')) -and !(Test-HelperInstalled -Helper 'Innounp')) { $deps += 'innounp' }
if (($script -like '*Expand-ZstdArchive *') -and !(Test-HelperInstaller -Helper 'Zstd')) {
# Ugly, unacceptable and horrible patch to cover the tar.zstd use cases
if (!(Test-HelperInstalled -Helper '7zip')) { $deps += '7zip' }
$deps += 'zstd'
}

return $deps
}
Expand All @@ -89,6 +94,11 @@ function install_deps($manifest, $arch) {
if ($manifest.innosetup -and !(Test-HelperInstalled -Helper 'Innounp')) { $deps += 'innounp' }
if ((Test-7zipRequirement -URL $urls) -and !(Test-HelperInstalled -Helper '7zip')) { $deps += '7zip' }
if ((Test-LessmsiRequirement -URL $urls) -and !(Test-HelperInstalled -Helper 'Lessmsi')) { $deps += 'lessmsi' }
if ((Test-ZstdRequirement -URL $urls) -and !(Test-HelperInstalled -Helper 'Zstd')) {
# Ugly, unacceptable and horrible patch to cover the tar.zstd use cases
if (!(Test-HelperInstalled -Helper '7zip')) { $deps += '7zip' }
$deps += 'zstd'
}

$pre_install = arch_specific 'pre_install' $manifest $arch
$installer = arch_specific 'installer' $manifest $arch
Expand Down
3 changes: 3 additions & 0 deletions lib/install.ps1
Expand Up @@ -624,6 +624,9 @@ function dl_urls($app, $version, $manifest, $bucket, $architecture, $dir, $use_c
} elseif (Test-7zipRequirement -File $fname) {
# 7zip
$extract_fn = 'Expand-7zipArchive'
} elseif (Test-ZstdRequirement -File $fname) {
# Zstd
$extract_fn = 'Expand-ZstdArchive'
}

if ($extract_fn) {
Expand Down
2 changes: 2 additions & 0 deletions libexec/scoop-download.ps1
Expand Up @@ -96,6 +96,7 @@ foreach ($app in $application) {
$title, $body = $_.Exception.Message -split '\|-'
if (!$body) { $body = $title }
Write-UserMessage -Message $body -Err
debug $_.InvocationInfo
if ($title -ne 'Ignore' -and ($title -ne $body)) { New-IssuePrompt -Application $appName -Bucket $bucket -Title $title -Body $body }

continue
Expand Down Expand Up @@ -133,6 +134,7 @@ foreach ($app in $application) {
$title, $body = $_.Exception.Message -split '\|-'
if (!$body) { $body = $title }
Write-UserMessage -Message $body -Err
debug $_.InvocationInfo
if ($title -ne 'Ignore' -and ($title -ne $body)) { New-IssuePrompt -Application $appName -Bucket $bucket -Title $title -Body $body }

continue
Expand Down
1 change: 1 addition & 0 deletions libexec/scoop-install.ps1
Expand Up @@ -182,6 +182,7 @@ foreach ($app in $apps) {
$title, $body = $_.Exception.Message -split '\|-'
if (!$body) { $body = $title }
Write-UserMessage -Message $body -Err
debug $_.InvocationInfo
if ($title -ne 'Ignore' -and ($title -ne $body)) { New-IssuePrompt -Application $cleanApp -Bucket $bucket -Title $title -Body $body }

continue
Expand Down
1 change: 1 addition & 0 deletions libexec/scoop-uninstall.ps1
Expand Up @@ -53,6 +53,7 @@ $problems = 0
$title, $body = $_.Exception.Message -split '\|-'
if (!$body) { $body = $title }
Write-UserMessage -Message $body -Err
debug $_.InvocationInfo
if ($title -ne 'Ignore' -and ($title -ne $body)) { New-IssuePrompt -Application $app -Bucket $bucket -Title $title -Body $body }

continue
Expand Down
1 change: 1 addition & 0 deletions libexec/scoop-update.ps1
Expand Up @@ -87,6 +87,7 @@ if (!$apps) {
$title, $body = $_.Exception.Message -split '\|-'
if (!$body) { $body = $title }
Write-UserMessage -Message $body -Err
debug $_.InvocationInfo
if ($title -ne 'Ignore' -and ($title -ne $body)) { New-IssuePrompt -Application $out[0] -Bucket $out[2] -Title $title -Body $body }

continue
Expand Down