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

decompress.ps1: Refactored (w/ install.ps1, core.ps1) (edited) #3169

Merged
merged 2 commits into from
Mar 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion bin/install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ Write-Output 'Downloading...'
dl $zipurl $zipfile

'Extracting...'
extract_zip $zipfile "$dir\_tmp"
Add-Type -Assembly "System.IO.Compression.FileSystem"
[IO.Compression.ZipFile]::ExtractToDirectory($zipfile,"$dir\_tmp")
Copy-Item "$dir\_tmp\scoop-master\*" $dir -r -force
Remove-Item "$dir\_tmp" -r -force
Remove-Item $zipfile
Expand Down
53 changes: 0 additions & 53 deletions lib/core.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -273,59 +273,6 @@ function isFileLocked([string]$path) {
}
}

function extract_zip($path, $to) {
if (!(test-path $path)) { abort "can't find $path to unzip"}
try { add-type -assembly "System.IO.Compression.FileSystem" -ea stop }
catch { unzip_old $path $to; return } # for .net earlier than 4.5
$retries = 0
while ($retries -le 10) {
if ($retries -eq 10) {
if (7zip_installed) {
extract_7zip $path $to $false
return
} else {
abort "Unzip failed: Windows can't unzip because a process is locking the file.`nRun 'scoop install 7zip' and try again."
}
}
if (isFileLocked $path) {
write-host "Waiting for $path to be unlocked by another process... ($retries/10)"
$retries++
Start-Sleep -s 2
} else {
break
}
}

try {
[io.compression.zipfile]::extracttodirectory($path,$to)
} catch [system.io.pathtoolongexception] {
# try to fall back to 7zip if path is too long
if(7zip_installed) {
extract_7zip $path $to $false
return
} else {
abort "Unzip failed: Windows can't handle the long paths in this zip file.`nRun 'scoop install 7zip' and try again."
}
} catch [system.io.ioexception] {
if (7zip_installed) {
extract_7zip $path $to $false
return
} else {
abort "Unzip failed: Windows can't handle the file names in this zip file.`nRun 'scoop install 7zip' and try again."
}
} catch {
abort "Unzip failed: $_"
}
}

function unzip_old($path,$to) {
# fallback for .net earlier than 4.5
$shell = (new-object -com shell.application -strict)
$zipfiles = $shell.namespace("$path").items()
$to = ensure $to
$shell.namespace("$to").copyHere($zipfiles, 4) # 4 = don't show progress dialog
}

function is_directory([String] $path) {
return (Test-Path $path) -and (Get-Item $path) -is [System.IO.DirectoryInfo]
}
Expand Down
92 changes: 92 additions & 0 deletions lib/decompress.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,95 @@ function extract_7zip($path, $to, $recurse) {

if($recurse) { Remove-Item $path } # clean up intermediate files
}

function extract_msi($path, $to) {
$logfile = "$(split-path $path)\msi.log"
$ok = run 'msiexec' @('/a', "`"$path`"", '/qn', "TARGETDIR=`"$to`"", "/lwe `"$logfile`"")
if(!$ok) { abort "Failed to extract files from $path.`nLog file:`n $(friendly_path $logfile)" }
if(test-path $logfile) { Remove-Item $logfile }
}

function lessmsi_config ($extract_dir) {
$extract_fn = 'extract_lessmsi'
if ($extract_dir) {
$extract_dir = join-path SourceDir $extract_dir
} else {
$extract_dir = "SourceDir"
}

$extract_fn, $extract_dir
}

function extract_lessmsi($path, $to) {
Invoke-Expression "lessmsi x `"$path`" `"$to\`""
}

function unpack_inno($fname, $manifest, $dir) {
if (!$manifest.innosetup) { return }

write-host "Unpacking innosetup... " -nonewline
innounp -x -d"$dir\_scoop_unpack" "$dir\$fname" > "$dir\innounp.log"
if ($lastexitcode -ne 0) {
abort "Failed to unpack innosetup file. See $dir\innounp.log"
}

Get-ChildItem "$dir\_scoop_unpack\{app}" -r | Move-Item -dest "$dir" -force

Remove-Item -r -force "$dir\_scoop_unpack"

Remove-Item "$dir\$fname"
Write-Host "done." -f Green
}

function extract_zip($path, $to) {
if (!(test-path $path)) { abort "can't find $path to unzip"}
try { add-type -assembly "System.IO.Compression.FileSystem" -ea stop }
catch { unzip_old $path $to; return } # for .net earlier than 4.5
$retries = 0
while ($retries -le 10) {
if ($retries -eq 10) {
if (7zip_installed) {
extract_7zip $path $to $false
return
} else {
abort "Unzip failed: Windows can't unzip because a process is locking the file.`nRun 'scoop install 7zip' and try again."
}
}
if (isFileLocked $path) {
write-host "Waiting for $path to be unlocked by another process... ($retries/10)"
$retries++
Start-Sleep -s 2
} else {
break
}
}

try {
[io.compression.zipfile]::extracttodirectory($path, $to)
} catch [system.io.pathtoolongexception] {
# try to fall back to 7zip if path is too long
if (7zip_installed) {
extract_7zip $path $to $false
return
} else {
abort "Unzip failed: Windows can't handle the long paths in this zip file.`nRun 'scoop install 7zip' and try again."
}
} catch [system.io.ioexception] {
if (7zip_installed) {
extract_7zip $path $to $false
return
} else {
abort "Unzip failed: Windows can't handle the file names in this zip file.`nRun 'scoop install 7zip' and try again."
}
} catch {
abort "Unzip failed: $_"
}
}

function unzip_old($path, $to) {
# fallback for .net earlier than 4.5
$shell = (new-object -com shell.application -strict)
$zipfiles = $shell.namespace("$path").items()
$to = ensure $to
$shell.namespace("$to").copyHere($zipfiles, 4) # 4 = don't show progress dialog
}
40 changes: 0 additions & 40 deletions lib/install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -592,18 +592,6 @@ function dl_urls($app, $version, $manifest, $bucket, $architecture, $dir, $use_c
$fname # returns the last downloaded file
}

function lessmsi_config ($extract_dir) {
$extract_fn = 'extract_lessmsi'
if ($extract_dir) {
$extract_dir = join-path SourceDir $extract_dir
}
else {
$extract_dir = "SourceDir"
}

$extract_fn, $extract_dir
}

function cookie_header($cookies) {
if(!$cookies) { return }

Expand Down Expand Up @@ -739,23 +727,6 @@ function run($exe, $arg, $msg, $continue_exit_codes) {
return $true
}

function unpack_inno($fname, $manifest, $dir) {
if(!$manifest.innosetup) { return }

write-host "Unpacking innosetup... " -nonewline
innounp -x -d"$dir\_scoop_unpack" "$dir\$fname" > "$dir\innounp.log"
if($lastexitcode -ne 0) {
abort "Failed to unpack innosetup file. See $dir\innounp.log"
}

Get-ChildItem "$dir\_scoop_unpack\{app}" -r | Move-Item -dest "$dir" -force

Remove-Item -r -force "$dir\_scoop_unpack"

Remove-Item "$dir\$fname"
Write-Host "done." -f Green
}

function run_installer($fname, $manifest, $architecture, $dir, $global) {
# MSI or other installer
$msi = msi $manifest $architecture
Expand Down Expand Up @@ -800,17 +771,6 @@ function install_msi($fname, $dir, $msi) {
Remove-Item $msifile
}

function extract_msi($path, $to) {
$logfile = "$(split-path $path)\msi.log"
$ok = run 'msiexec' @('/a', "`"$path`"", '/qn', "TARGETDIR=`"$to`"", "/lwe `"$logfile`"")
if(!$ok) { abort "Failed to extract files from $path.`nLog file:`n $(friendly_path $logfile)" }
if(test-path $logfile) { Remove-Item $logfile }
}

function extract_lessmsi($path, $to) {
Invoke-Expression "lessmsi x `"$path`" `"$to\`""
}

# deprecated
# get-wmiobject win32_product is slow and checks integrity of each installed program,
# so this uses the [wmi] type accelerator instead
Expand Down
56 changes: 0 additions & 56 deletions test/Scoop-Core.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -61,62 +61,6 @@ describe "movedir" -Tag 'Scoop' {
}
}

describe "unzip_old" -Tag 'Scoop' {
beforeall {
$working_dir = setup_working "unzip_old"
}

function test-unzip($from) {
$to = strip_ext $from

if(is_unix) {
unzip_old ($from -replace '\\','/') ($to -replace '\\','/')
} else {
unzip_old ($from -replace '/','\') ($to -replace '/','\')
}

$to
}

context "zip file size is zero bytes" {
$zerobyte = "$working_dir\zerobyte.zip"
$zerobyte | should -exist

it "unzips file with zero bytes without error" -skip:$isUnix {
# some combination of pester, COM (used within unzip_old), and Win10 causes a bugged return value from test-unzip
# `$to = test-unzip $zerobyte` * RETURN_VAL has a leading space and complains of $null usage when used in PoSH functions
$to = ([string](test-unzip $zerobyte)).trimStart()

$to | should -not -match '^\s'
$to | should -not -benullorempty

$to | should -exist

(Get-ChildItem $to).count | should -be 0
}
}

context "zip file is small in size" {
$small = "$working_dir\small.zip"
$small | should -exist

it "unzips file which is small in size" -skip:$isUnix {
# some combination of pester, COM (used within unzip_old), and Win10 causes a bugged return value from test-unzip
# `$to = test-unzip $small` * RETURN_VAL has a leading space and complains of $null usage when used in PoSH functions
$to = ([string](test-unzip $small)).trimStart()

$to | should -not -match '^\s'
$to | should -not -benullorempty

$to | should -exist

# these don't work for some reason on appveyor
#join-path $to "empty" | should -exist
#(gci $to).count | should -be 1
}
}
}

describe "shim" -Tag 'Scoop' {
beforeall {
$working_dir = setup_working "shim"
Expand Down
62 changes: 62 additions & 0 deletions test/Scoop-Decompress.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
. "$psscriptroot\Scoop-TestLib.ps1"
. "$psscriptroot\..\lib\core.ps1"
. "$psscriptroot\..\lib\decompress.ps1"
. "$psscriptroot\..\lib\unix.ps1"

$isUnix = is_unix

describe "unzip_old" -Tag 'Scoop' {
beforeall {
$working_dir = setup_working "unzip_old"
}

function test-unzip($from) {
$to = strip_ext $from

if(is_unix) {
unzip_old ($from -replace '\\','/') ($to -replace '\\','/')
} else {
unzip_old ($from -replace '/','\') ($to -replace '/','\')
}

$to
}

context "zip file size is zero bytes" {
$zerobyte = "$working_dir\zerobyte.zip"
$zerobyte | should -exist

it "unzips file with zero bytes without error" -skip:$isUnix {
# some combination of pester, COM (used within unzip_old), and Win10 causes a bugged return value from test-unzip
# `$to = test-unzip $zerobyte` * RETURN_VAL has a leading space and complains of $null usage when used in PoSH functions
$to = ([string](test-unzip $zerobyte)).trimStart()

$to | should -not -match '^\s'
$to | should -not -benullorempty

$to | should -exist

(Get-ChildItem $to).count | should -be 0
}
}

context "zip file is small in size" {
$small = "$working_dir\small.zip"
$small | should -exist

it "unzips file which is small in size" -skip:$isUnix {
# some combination of pester, COM (used within unzip_old), and Win10 causes a bugged return value from test-unzip
# `$to = test-unzip $small` * RETURN_VAL has a leading space and complains of $null usage when used in PoSH functions
$to = ([string](test-unzip $small)).trimStart()

$to | should -not -match '^\s'
$to | should -not -benullorempty

$to | should -exist

# these don't work for some reason on appveyor
#join-path $to "empty" | should -exist
#(gci $to).count | should -be 1
}
}
}