Skip to content

Commit

Permalink
feature(decompress): Add 'ExtractDir' to 'Expand-...' functions (Scoo…
Browse files Browse the repository at this point in the history
…pInstaller#3466)

* feature(decompress): Add 'ExtractDir' to 'Expand-...' functions

* Fix 'TrimEnd' and nested 7z
  • Loading branch information
niheaven authored and r15ch13 committed Jun 24, 2019
1 parent 61d1c1c commit 2fafcb8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 37 deletions.
55 changes: 44 additions & 11 deletions lib/decompress.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ function Expand-7zipArchive {
[Parameter(Position = 1)]
[String]
$DestinationPath = (Split-Path $Path),
[String]
$ExtractDir,
[Parameter(ValueFromRemainingArguments = $true)]
[String]
$Switches,
Expand All @@ -64,6 +66,10 @@ function Expand-7zipArchive {
}
$LogPath = "$(Split-Path $Path)\7zip.log"
$ArgList = @('x', "`"$Path`"", "-o`"$DestinationPath`"", '-y')
$IsTar = ((strip_ext $Path) -match '\.tar$') -or ($Path -match '\.t[abgpx]z2?$')
if (!$IsTar -and $ExtractDir) {
$ArgList += "-ir!$ExtractDir\*"
}
if ($Switches) {
$ArgList += (-split $Switches)
}
Expand All @@ -74,17 +80,20 @@ function Expand-7zipArchive {
}
$Status = Invoke-ExternalCommand $7zPath $ArgList -LogPath $LogPath
if (!$Status) {
abort "Failed to extract files from $Path.`nLog file:`n $(friendly_path $LogPath)"
abort "Failed to extract files from $Path.`nLog file:`n $(friendly_path $LogPath)`n$(new_issue_msg $app $bucket 'decompress error')"
}
if (!$IsTar -and $ExtractDir) {
movedir "$DestinationPath\$ExtractDir" $DestinationPath | Out-Null
}
if (Test-Path $LogPath) {
Remove-Item $LogPath -Force
}
if ((strip_ext $Path) -match '\.tar$' -or $Path -match '\.t[abgpx]z2?$') {
if ($IsTar) {
# Check for tar
$Status = Invoke-ExternalCommand $7zPath @('l', "`"$Path`"") -LogPath $LogPath
if ($Status) {
$TarFile = (Get-Content -Path $LogPath)[-4] -replace '.{53}(.*)', '$1' # get inner tar file name
Expand-7zipArchive "$DestinationPath\$TarFile" $DestinationPath -Removal
Expand-7zipArchive -Path "$DestinationPath\$TarFile" -DestinationPath $DestinationPath -ExtractDir $ExtractDir -Removal
} else {
abort "Failed to list files in $Path.`nNot a 7-Zip supported archive file."
}
Expand All @@ -104,29 +113,38 @@ function Expand-MsiArchive {
[Parameter(Position = 1)]
[String]
$DestinationPath = (Split-Path $Path),
[String]
$ExtractDir,
[Parameter(ValueFromRemainingArguments = $true)]
[String]
$Switches,
[Switch]
$Removal
)
$DestinationPath = $DestinationPath.TrimEnd("\")
if ((get_config MSIEXTRACT_USE_LESSMSI)) {
$MsiPath = Get-HelperPath -Helper Lessmsi
$ArgList = @('x', "`"$Path`"", "`"$DestinationPath\\`"")
} else {
$MsiPath = 'msiexec.exe'
$ArgList = @('/a', "`"$Path`"", '/qn', "TARGETDIR=`"$DestinationPath`"")
$ArgList = @('/a', "`"$Path`"", '/qn', "TARGETDIR=`"$DestinationPath\\SourceDir`"")
}
$LogPath = "$(Split-Path $Path)\msi.log"
if ($Switches) {
$ArgList += (-split $Switches)
}
$Status = Invoke-ExternalCommand $MsiPath $ArgList -LogPath $LogPath
if (!$Status) {
abort "Failed to extract files from $Path.`nLog file:`n $(friendly_path $LogPath)"
abort "Failed to extract files from $Path.`nLog file:`n $(friendly_path $LogPath)`n$(new_issue_msg $app $bucket 'decompress error')"
}
if (Test-Path "$DestinationPath\SourceDir") {
movedir "$DestinationPath\SourceDir" "$DestinationPath" | Out-Null
if ($ExtractDir -and (Test-Path "$DestinationPath\SourceDir")) {
movedir "$DestinationPath\SourceDir\$ExtractDir" $DestinationPath | Out-Null
Remove-Item "$DestinationPath\SourceDir" -Recurse -Force
} elseif ($ExtractDir) {
Get-ChildItem $DestinationPath -Exclude $ExtractDir | Remove-Item -Recurse -Force
movedir "$DestinationPath\$ExtractDir" $DestinationPath | Out-Null
} elseif (Test-Path "$DestinationPath\SourceDir") {
movedir "$DestinationPath\SourceDir" $DestinationPath | Out-Null
}
if (($DestinationPath -ne (Split-Path $Path)) -and (Test-Path "$DestinationPath\$(fname $Path)")) {
Remove-Item "$DestinationPath\$(fname $Path)" -Force
Expand All @@ -149,20 +167,22 @@ function Expand-InnoArchive {
[Parameter(Position = 1)]
[String]
$DestinationPath = (Split-Path $Path),
[String]
$ExtractDir,
[Parameter(ValueFromRemainingArguments = $true)]
[String]
$Switches,
[Switch]
$Removal
)
$LogPath = "$(Split-Path $Path)\innounp.log"
$ArgList = @('-x', "-d`"$DestinationPath`"", "-c`{app`}", "`"$Path`"", '-y')
$ArgList = @('-x', "-d`"$DestinationPath`"", "-c`{app`}\$ExtractDir", "`"$Path`"", '-y')
if ($Switches) {
$ArgList += (-split $Switches)
}
$Status = Invoke-ExternalCommand (Get-HelperPath -Helper Innounp) $ArgList -LogPath $LogPath
if (!$Status) {
abort "Failed to extract files from $Path.`nLog file:`n $(friendly_path $LogPath)"
abort "Failed to extract files from $Path.`nLog file:`n $(friendly_path $LogPath)`n$(new_issue_msg $app $bucket 'decompress error')"
}
if (Test-Path $LogPath) {
Remove-Item $LogPath -Force
Expand All @@ -182,6 +202,8 @@ function Expand-ZipArchive {
[Parameter(Position = 1)]
[String]
$DestinationPath = (Split-Path $Path),
[String]
$ExtractDir,
[Switch]
$Removal
)
Expand Down Expand Up @@ -213,6 +235,10 @@ function Expand-ZipArchive {
# Compatible with Pscx (https://github.com/Pscx/Pscx)
Microsoft.PowerShell.Archive\Expand-Archive -Path $Path -DestinationPath $DestinationPath -Force
}
if ($ExtractDir) {
Get-ChildItem $DestinationPath -Exclude $ExtractDir | Remove-Item -Recurse -Force
movedir "$DestinationPath\$ExtractDir" $DestinationPath | Out-Null
}
if ($Removal) {
# Remove original archive file
Remove-Item $Path -Force
Expand All @@ -221,31 +247,38 @@ function Expand-ZipArchive {

function Expand-DarkArchive {
[CmdletBinding()]
param(
param (
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]
[String]
$Path,
[Parameter(Position = 1)]
[String]
$DestinationPath = (Split-Path $Path),
[String]
$ExtractDir,
[Parameter(ValueFromRemainingArguments = $true)]
[String]
$Switches,
[Switch]
$Removal
)
$DestinationPath = $DestinationPath.TrimEnd("\")
$LogPath = "$(Split-Path $Path)\dark.log"
$ArgList = @('-nologo', "-x `"$DestinationPath`"", "`"$Path`"")
if ($Switches) {
$ArgList += (-split $Switches)
}
$Status = Invoke-ExternalCommand (Get-HelperPath -Helper Dark) $ArgList -LogPath $LogPath
if (!$Status) {
abort "Failed to extract files from $Path.`nLog file:`n $(friendly_path $LogPath)"
abort "Failed to extract files from $Path.`nLog file:`n $(friendly_path $LogPath)`n$(new_issue_msg $app $bucket 'decompress error')"
}
if (Test-Path $LogPath) {
Remove-Item $LogPath -Force
}
if ($ExtractDir) {
Get-ChildItem $DestinationPath -Exclude $ExtractDir | Remove-Item -Recurse -Force
movedir "$DestinationPath\$ExtractDir" $DestinationPath | Out-Null
}
if ($Removal) {
# Remove original archive file
Remove-Item $Path -Force
Expand Down
27 changes: 1 addition & 26 deletions lib/install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -555,33 +555,8 @@ function dl_urls($app, $version, $manifest, $bucket, $architecture, $dir, $use_c
Write-Host "Extracting " -NoNewline
Write-Host $fname -f Cyan -NoNewline
Write-Host " ... " -NoNewline
ensure "$dir\_tmp" | Out-Null
& $extract_fn "$dir\$fname" "$dir\_tmp" -Removal
if ($extract_to) {
ensure "$dir\$extract_to" | Out-Null
}
# fails if zip contains long paths (e.g. atom.json)
#cp "$dir\_tmp\$extract_dir\*" "$dir\$extract_to" -r -force -ea stop
try {
movedir "$dir\_tmp\$extract_dir" "$dir\$extract_to"
}
catch {
error $_
abort $(new_issue_msg $app $bucket "extract_dir error")
}

if(Test-Path "$dir\_tmp") { # might have been moved by movedir
try {
Remove-Item -r -force "$dir\_tmp" -ea stop
} catch [system.io.pathtoolongexception] {
& "$env:COMSPEC" /c "rmdir /s /q $dir\_tmp"
} catch [system.unauthorizedaccessexception] {
warn "Couldn't remove $dir\_tmp: unauthorized access."
}
}

& $extract_fn -Path "$dir\$fname" -DestinationPath "$dir\$extract_to" -ExtractDir $extract_dir -Removal
Write-Host "done." -f Green

$extracted++
}
}
Expand Down

0 comments on commit 2fafcb8

Please sign in to comment.