Skip to content

Commit

Permalink
feat(decompress): Support 'INNOSETUP_USE_INNOEXTRACT' config and -Use…
Browse files Browse the repository at this point in the history
…Innoextract switch (#96)
  • Loading branch information
Ash258 committed Feb 28, 2021
1 parent 0f57f64 commit ccd416e
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 23 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### 0.6-pre2

- **decompress**: Support `INNOSETUP_USE_INNOEXTRACT` config option and `Expand-InnoArchive -UseInnoextract`
- **format**: Extract checkver fixes into own function and add generic adjust property function
- **schema**
- Remove deprecated short properties
Expand Down
9 changes: 7 additions & 2 deletions lib/core.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ function Get-HelperPath {
[OutputType([String])]
param(
[Parameter(Mandatory, ValueFromPipeline)]
[ValidateSet('7zip', 'Lessmsi', 'Innounp', 'Dark', 'Aria2', 'Zstd')]
[ValidateSet('7zip', 'Lessmsi', 'Innounp', 'Dark', 'Aria2', 'Zstd', 'Innoextract')]
[String] $Helper
)

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

Expand Down Expand Up @@ -573,6 +574,9 @@ function Invoke-ExternalCommand {
$Process.StartInfo.Verb = 'RunAs'
}

debug $Process.StartInfo.FileName
debug $Process.StartInfo.Arguments

try {
$Process.Start() | Out-Null
} catch {
Expand Down Expand Up @@ -639,6 +643,7 @@ function isFileLocked([string]$path) {

function is_directory([String] $path) { return (Test-Path $path) -and (Get-Item $path) -is [System.IO.DirectoryInfo] }

# Move content of directory into different directory
function movedir {
[CmdletBinding()]
param ($from, $to)
Expand Down
80 changes: 61 additions & 19 deletions lib/decompress.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ function Test-ZstdRequirement {
return $File -match '\.zst$'
}
}

function _decompressErrorPrompt($path, $log) {
return @("Decompress error|-Failed to extract files from $path.", "Log file:", " $(friendly_path $log)") -join "`n"
}
#endregion helpers

function Expand-7zipArchive {
Expand Down Expand Up @@ -116,9 +120,12 @@ function Expand-7zipArchive {
begin {
if (get_config '7ZIPEXTRACT_USE_EXTERNAL' $false) {
try {
$7zPath = (Get-Command '7z' -CommandType 'Application' | Select-Object -First 1).Source
$7zPath = (Get-Command '7z' -CommandType 'Application' -ErrorAction 'Stop' | Select-Object -First 1).Source
} catch [System.Management.Automation.CommandNotFoundException] {
throw [ScoopException] "Cannot find external 7-Zip (7z.exe) while '7ZIPEXTRACT_USE_EXTERNAL' is 'true'!`nRun 'scoop config 7ZIPEXTRACT_USE_EXTERNAL false' or install 7zip manually and try again." # TerminatingError thrown
throw [ScoopException] (
"Cannot find external 7-Zip (7z.exe) while '7ZIPEXTRACT_USE_EXTERNAL' is 'true'!",
"Run 'scoop config 7ZIPEXTRACT_USE_EXTERNAL false' or install 7zip manually and try again." -join "`n"
) # TerminatingError thrown
}
} else {
$7zPath = Get-HelperPath -Helper '7zip'
Expand Down Expand Up @@ -146,7 +153,7 @@ function Expand-7zipArchive {
}

if (!$status) {
throw [ScoopException] "Decompress error|-Failed to extract files from $Path.`nLog file:`n $(friendly_path $LogPath)" # TerminatingError thrown
throw [ScoopException] (_decompressErrorPrompt $Path $logPath) # TerminatingError thrown
}
if (!$isTar -and $ExtractDir) {
movedir (Join-Path $DestinationPath $ExtractDir) $DestinationPath | Out-Null
Expand Down Expand Up @@ -219,7 +226,7 @@ function Expand-MsiArchive {
$status = Invoke-ExternalCommand $msiPath $argList -LogPath $logPath

if (!$status) {
throw [ScoopException] "Decompress error|-Failed to extract files from $Path.`nLog file:`n $(friendly_path $logPath)" # TerminatingError thrown
throw [ScoopException] (_decompressErrorPrompt $Path $logPath) # TerminatingError thrown
}

$sourceDir = Join-Path $DestinationPath 'SourceDir'
Expand Down Expand Up @@ -269,30 +276,65 @@ function Expand-InnoArchive {
[String] $ExtractDir,
[Parameter(ValueFromRemainingArguments)]
[String] $Switches,
[Switch] $Removal
[Switch] $Removal,
[Switch] $UseInnoextract
)

process {
$logPath = Split-Path $Path -Parent | Join-Path -ChildPath 'innounp.log'
$argList = @('-x', "-d`"$DestinationPath`"", "`"$Path`"", '-y')

switch -Regex ($ExtractDir) {
'^[^{].*' { $argList += "-c{app}\$ExtractDir" }
'^{.*' { $argList += "-c$ExtractDir" }
default { $argList += '-c{app}' }
$DestinationPath = $DestinationPath.TrimEnd('\').TrimEnd('/')
$isInnoextract = (get_config 'INNOSETUP_USE_INNOEXTRACT' $false) -or $UseInnoextract
if ($isInnoextract) {
Write-UserMessage -Message 'Using innoextract is experimental' -Warning

$logPath = Split-Path $Path -Parent | Join-Path -ChildPath 'innoextract.log'
$argList = @('--extract', '--output-dir', """$DestinationPath""", '--default-language', '"enu"')
$innoPath = Get-HelperPath -Helper 'Innoextract'
$inno = 'innoextract'

switch -Regex ($ExtractDir) {
'^[^{].*' {
$toMove = "app\$ExtractDir"
$argList += '--include', """$toMove"""
}
'^{.*' {
$toMove = (($ExtractDir -replace '{') -replace '}') -replace '_', '$' # TODO: ?? _ => $
$argList += '--include', """$toMove"""
}
default {
$toMove = 'app'
$argList += '--include', """$toMove"""
}
}
} else {
$logPath = Split-Path $Path -Parent | Join-Path -ChildPath 'innounp.log'
$argList = @('-x', "-d`"$DestinationPath`"", '-y')
$innoPath = Get-HelperPath -Helper 'Innounp'
$inno = 'innounp'

switch -Regex ($ExtractDir) {
'^[^{].*' { $argList += "-c""{app}\$ExtractDir""" }
'^{.*' { $argList += "-c""$ExtractDir""" }
default { $argList += '-c"{app}"' }
}
}

if ($Switches) { $argList += (-split $Switches) }
$argList += """$Path"""

try {
# TODO: Find out extract_dir issue.
# When there is no specified directory in archive innounp will just exit with 0 and version of file
$status = Invoke-ExternalCommand (Get-HelperPath -Helper 'Innounp') $argList -LogPath $logPath
$status = Invoke-ExternalCommand $innoPath $argList -LogPath $logPath
} catch [System.Management.Automation.ParameterBindingException] {
throw [ScoopException] '''innounp'' is not installed or cannot be used' # TerminatingError thrown
throw [ScoopException] "'$inno' is not installed or cannot be used" # TerminatingError thrown
}

if (!$status) {
throw [ScoopException] "Decompress error|-Failed to extract files from $Path.`nLog file:`n $(friendly_path $logPath)" # TerminatingError thrown
throw [ScoopException] (_decompressErrorPrompt $Path $logPath) # TerminatingError thrown
}

# Innoextract --include do not extract the directory, it only filter the content
# Need to manually move the nested directories
if ($isInnoextract) { movedir "$DestinationPath\$toMove" $DestinationPath | Out-Null }

if (Test-Path $logPath) { Remove-Item $logPath -Force }

# Remove original archive file
Expand Down Expand Up @@ -405,7 +447,7 @@ function Expand-DarkArchive {
}

if (!$status) {
throw [ScoopException] "Decompress error|-Failed to extract files from $Path.`nLog file:`n $(friendly_path $logPath)" # TerminatingError thrown
throw [ScoopException] (_decompressErrorPrompt $Path $logPath) # TerminatingError thrown
}
if (Test-Path $logPath) { Remove-Item $logPath -Force }

Expand Down Expand Up @@ -471,7 +513,7 @@ function Expand-ZstdArchive {

$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)"
throw [ScoopException] (_decompressErrorPrompt $_path $_log) # TerminatingError thrown
}

Remove-Item -Path $_log -ErrorAction 'SilentlyContinue' -Force
Expand Down
16 changes: 14 additions & 2 deletions lib/depends.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,13 @@ function script_deps($script) {
if (($script -like '*Expand-DarkArchive *') -and !(Test-HelperInstalled -Helper 'Dark')) { $deps += 'dark' }
if (($script -like '*Expand-7zipArchive *') -and !(Test-HelperInstalled -Helper '7zip')) { $deps += '7zip' }
if (($script -like '*Expand-MsiArchive *') -and !(Test-HelperInstalled -Helper 'Lessmsi')) { $deps += 'lessmsi' }
if (($script -like '*Expand-InnoArchive *') -and !(Test-HelperInstalled -Helper 'Innounp')) { $deps += 'innounp' }
if ($script -like '*Expand-InnoArchive *') {
if ((get_config 'INNOSETUP_USE_INNOEXTRACT' $false) -or ($script -like '* -UseInnoextract *')) {
if (!(Test-HelperInstalled -Helper 'InnoExtract')) { $deps += 'innoextract' }
} else {
if (!(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' }
Expand All @@ -91,9 +97,15 @@ function install_deps($manifest, $arch) {
$deps = @()
$urls = url $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 ($manifest.innosetup) {
if (get_config 'INNOSETUP_USE_INNOEXTRACT' $false) {
if (!(Test-HelperInstalled -Helper 'Innoextract')) { $deps += 'innoextract' }
} else {
if (!(Test-HelperInstalled -Helper 'Innounp')) { $deps += 'innounp' }
}
}
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' }
Expand Down
1 change: 1 addition & 0 deletions supporting/completion/scoop.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ local configOptions = parser({
'aria2-options',
'debug' .. booleanParser,
'default-architecture' .. architectureParser,
'INNOSETUP_USE_INNOEXTRACT' .. booleanParser,
'MSIEXTRACT_USE_LESSMSI' .. booleanParser,
'NO_JUNCTIONS' .. booleanParser,
'SCOOP_REPO',
Expand Down

0 comments on commit ccd416e

Please sign in to comment.