Skip to content

Commit b421879

Browse files
committed
decompress.ps1: Refactored (w/ install.ps1, core.ps1)
1 parent 065ff35 commit b421879

5 files changed

Lines changed: 154 additions & 149 deletions

File tree

lib/core.ps1

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -273,59 +273,6 @@ function isFileLocked([string]$path) {
273273
}
274274
}
275275

276-
function extract_zip($path, $to) {
277-
if (!(test-path $path)) { abort "can't find $path to unzip"}
278-
try { add-type -assembly "System.IO.Compression.FileSystem" -ea stop }
279-
catch { unzip_old $path $to; return } # for .net earlier than 4.5
280-
$retries = 0
281-
while ($retries -le 10) {
282-
if ($retries -eq 10) {
283-
if (7zip_installed) {
284-
extract_7zip $path $to $false
285-
return
286-
} else {
287-
abort "Unzip failed: Windows can't unzip because a process is locking the file.`nRun 'scoop install 7zip' and try again."
288-
}
289-
}
290-
if (isFileLocked $path) {
291-
write-host "Waiting for $path to be unlocked by another process... ($retries/10)"
292-
$retries++
293-
Start-Sleep -s 2
294-
} else {
295-
break
296-
}
297-
}
298-
299-
try {
300-
[io.compression.zipfile]::extracttodirectory($path,$to)
301-
} catch [system.io.pathtoolongexception] {
302-
# try to fall back to 7zip if path is too long
303-
if(7zip_installed) {
304-
extract_7zip $path $to $false
305-
return
306-
} else {
307-
abort "Unzip failed: Windows can't handle the long paths in this zip file.`nRun 'scoop install 7zip' and try again."
308-
}
309-
} catch [system.io.ioexception] {
310-
if (7zip_installed) {
311-
extract_7zip $path $to $false
312-
return
313-
} else {
314-
abort "Unzip failed: Windows can't handle the file names in this zip file.`nRun 'scoop install 7zip' and try again."
315-
}
316-
} catch {
317-
abort "Unzip failed: $_"
318-
}
319-
}
320-
321-
function unzip_old($path,$to) {
322-
# fallback for .net earlier than 4.5
323-
$shell = (new-object -com shell.application -strict)
324-
$zipfiles = $shell.namespace("$path").items()
325-
$to = ensure $to
326-
$shell.namespace("$to").copyHere($zipfiles, 4) # 4 = don't show progress dialog
327-
}
328-
329276
function is_directory([String] $path) {
330277
return (Test-Path $path) -and (Get-Item $path) -is [System.IO.DirectoryInfo]
331278
}

lib/decompress.ps1

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,95 @@ function extract_7zip($path, $to, $recurse) {
2929

3030
if($recurse) { Remove-Item $path } # clean up intermediate files
3131
}
32+
33+
function unpack_inno($fname, $manifest, $dir) {
34+
if(!$manifest.innosetup) { return }
35+
36+
write-host "Unpacking innosetup... " -nonewline
37+
innounp -x -d"$dir\_scoop_unpack" "$dir\$fname" > "$dir\innounp.log"
38+
if($lastexitcode -ne 0) {
39+
abort "Failed to unpack innosetup file. See $dir\innounp.log"
40+
}
41+
42+
Get-ChildItem "$dir\_scoop_unpack\{app}" -r | Move-Item -dest "$dir" -force
43+
44+
Remove-Item -r -force "$dir\_scoop_unpack"
45+
46+
Remove-Item "$dir\$fname"
47+
Write-Host "done." -f Green
48+
}
49+
50+
function extract_msi($path, $to) {
51+
$logfile = "$(split-path $path)\msi.log"
52+
$ok = run 'msiexec' @('/a', "`"$path`"", '/qn', "TARGETDIR=`"$to`"", "/lwe `"$logfile`"")
53+
if(!$ok) { abort "Failed to extract files from $path.`nLog file:`n $(friendly_path $logfile)" }
54+
if(test-path $logfile) { Remove-Item $logfile }
55+
}
56+
57+
function lessmsi_config ($extract_dir) {
58+
$extract_fn = 'extract_lessmsi'
59+
if ($extract_dir) {
60+
$extract_dir = join-path SourceDir $extract_dir
61+
} else {
62+
$extract_dir = "SourceDir"
63+
}
64+
65+
$extract_fn, $extract_dir
66+
}
67+
68+
function extract_lessmsi($path, $to) {
69+
Invoke-Expression "lessmsi x `"$path`" `"$to\`""
70+
}
71+
72+
function extract_zip($path, $to) {
73+
if (!(test-path $path)) { abort "can't find $path to unzip"}
74+
try { add-type -assembly "System.IO.Compression.FileSystem" -ea stop }
75+
catch { unzip_old $path $to; return } # for .net earlier than 4.5
76+
$retries = 0
77+
while ($retries -le 10) {
78+
if ($retries -eq 10) {
79+
if (7zip_installed) {
80+
extract_7zip $path $to $false
81+
return
82+
} else {
83+
abort "Unzip failed: Windows can't unzip because a process is locking the file.`nRun 'scoop install 7zip' and try again."
84+
}
85+
}
86+
if (isFileLocked $path) {
87+
write-host "Waiting for $path to be unlocked by another process... ($retries/10)"
88+
$retries++
89+
Start-Sleep -s 2
90+
} else {
91+
break
92+
}
93+
}
94+
95+
try {
96+
[io.compression.zipfile]::extracttodirectory($path, $to)
97+
} catch [system.io.pathtoolongexception] {
98+
# try to fall back to 7zip if path is too long
99+
if (7zip_installed) {
100+
extract_7zip $path $to $false
101+
return
102+
} else {
103+
abort "Unzip failed: Windows can't handle the long paths in this zip file.`nRun 'scoop install 7zip' and try again."
104+
}
105+
} catch [system.io.ioexception] {
106+
if (7zip_installed) {
107+
extract_7zip $path $to $false
108+
return
109+
} else {
110+
abort "Unzip failed: Windows can't handle the file names in this zip file.`nRun 'scoop install 7zip' and try again."
111+
}
112+
} catch {
113+
abort "Unzip failed: $_"
114+
}
115+
}
116+
117+
function unzip_old($path, $to) {
118+
# fallback for .net earlier than 4.5
119+
$shell = (new-object -com shell.application -strict)
120+
$zipfiles = $shell.namespace("$path").items()
121+
$to = ensure $to
122+
$shell.namespace("$to").copyHere($zipfiles, 4) # 4 = don't show progress dialog
123+
}

lib/install.ps1

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -592,18 +592,6 @@ function dl_urls($app, $version, $manifest, $bucket, $architecture, $dir, $use_c
592592
$fname # returns the last downloaded file
593593
}
594594

595-
function lessmsi_config ($extract_dir) {
596-
$extract_fn = 'extract_lessmsi'
597-
if ($extract_dir) {
598-
$extract_dir = join-path SourceDir $extract_dir
599-
}
600-
else {
601-
$extract_dir = "SourceDir"
602-
}
603-
604-
$extract_fn, $extract_dir
605-
}
606-
607595
function cookie_header($cookies) {
608596
if(!$cookies) { return }
609597

@@ -739,23 +727,6 @@ function run($exe, $arg, $msg, $continue_exit_codes) {
739727
return $true
740728
}
741729

742-
function unpack_inno($fname, $manifest, $dir) {
743-
if(!$manifest.innosetup) { return }
744-
745-
write-host "Unpacking innosetup... " -nonewline
746-
innounp -x -d"$dir\_scoop_unpack" "$dir\$fname" > "$dir\innounp.log"
747-
if($lastexitcode -ne 0) {
748-
abort "Failed to unpack innosetup file. See $dir\innounp.log"
749-
}
750-
751-
Get-ChildItem "$dir\_scoop_unpack\{app}" -r | Move-Item -dest "$dir" -force
752-
753-
Remove-Item -r -force "$dir\_scoop_unpack"
754-
755-
Remove-Item "$dir\$fname"
756-
Write-Host "done." -f Green
757-
}
758-
759730
function run_installer($fname, $manifest, $architecture, $dir, $global) {
760731
# MSI or other installer
761732
$msi = msi $manifest $architecture
@@ -800,17 +771,6 @@ function install_msi($fname, $dir, $msi) {
800771
Remove-Item $msifile
801772
}
802773

803-
function extract_msi($path, $to) {
804-
$logfile = "$(split-path $path)\msi.log"
805-
$ok = run 'msiexec' @('/a', "`"$path`"", '/qn', "TARGETDIR=`"$to`"", "/lwe `"$logfile`"")
806-
if(!$ok) { abort "Failed to extract files from $path.`nLog file:`n $(friendly_path $logfile)" }
807-
if(test-path $logfile) { Remove-Item $logfile }
808-
}
809-
810-
function extract_lessmsi($path, $to) {
811-
Invoke-Expression "lessmsi x `"$path`" `"$to\`""
812-
}
813-
814774
# deprecated
815775
# get-wmiobject win32_product is slow and checks integrity of each installed program,
816776
# so this uses the [wmi] type accelerator instead

test/Scoop-Core.Tests.ps1

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -61,62 +61,6 @@ describe "movedir" -Tag 'Scoop' {
6161
}
6262
}
6363

64-
describe "unzip_old" -Tag 'Scoop' {
65-
beforeall {
66-
$working_dir = setup_working "unzip_old"
67-
}
68-
69-
function test-unzip($from) {
70-
$to = strip_ext $from
71-
72-
if(is_unix) {
73-
unzip_old ($from -replace '\\','/') ($to -replace '\\','/')
74-
} else {
75-
unzip_old ($from -replace '/','\') ($to -replace '/','\')
76-
}
77-
78-
$to
79-
}
80-
81-
context "zip file size is zero bytes" {
82-
$zerobyte = "$working_dir\zerobyte.zip"
83-
$zerobyte | should -exist
84-
85-
it "unzips file with zero bytes without error" -skip:$isUnix {
86-
# some combination of pester, COM (used within unzip_old), and Win10 causes a bugged return value from test-unzip
87-
# `$to = test-unzip $zerobyte` * RETURN_VAL has a leading space and complains of $null usage when used in PoSH functions
88-
$to = ([string](test-unzip $zerobyte)).trimStart()
89-
90-
$to | should -not -match '^\s'
91-
$to | should -not -benullorempty
92-
93-
$to | should -exist
94-
95-
(Get-ChildItem $to).count | should -be 0
96-
}
97-
}
98-
99-
context "zip file is small in size" {
100-
$small = "$working_dir\small.zip"
101-
$small | should -exist
102-
103-
it "unzips file which is small in size" -skip:$isUnix {
104-
# some combination of pester, COM (used within unzip_old), and Win10 causes a bugged return value from test-unzip
105-
# `$to = test-unzip $small` * RETURN_VAL has a leading space and complains of $null usage when used in PoSH functions
106-
$to = ([string](test-unzip $small)).trimStart()
107-
108-
$to | should -not -match '^\s'
109-
$to | should -not -benullorempty
110-
111-
$to | should -exist
112-
113-
# these don't work for some reason on appveyor
114-
#join-path $to "empty" | should -exist
115-
#(gci $to).count | should -be 1
116-
}
117-
}
118-
}
119-
12064
describe "shim" -Tag 'Scoop' {
12165
beforeall {
12266
$working_dir = setup_working "shim"

test/Scoop-Decompress.Tests.ps1

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
. "$psscriptroot\Scoop-TestLib.ps1"
2+
. "$psscriptroot\..\lib\core.ps1"
3+
. "$psscriptroot\..\lib\decompress.ps1"
4+
. "$psscriptroot\..\lib\unix.ps1"
5+
6+
$isUnix = is_unix
7+
8+
describe "unzip_old" -Tag 'Scoop' {
9+
beforeall {
10+
$working_dir = setup_working "unzip_old"
11+
}
12+
13+
function test-unzip($from) {
14+
$to = strip_ext $from
15+
16+
if(is_unix) {
17+
unzip_old ($from -replace '\\','/') ($to -replace '\\','/')
18+
} else {
19+
unzip_old ($from -replace '/','\') ($to -replace '/','\')
20+
}
21+
22+
$to
23+
}
24+
25+
context "zip file size is zero bytes" {
26+
$zerobyte = "$working_dir\zerobyte.zip"
27+
$zerobyte | should -exist
28+
29+
it "unzips file with zero bytes without error" -skip:$isUnix {
30+
# some combination of pester, COM (used within unzip_old), and Win10 causes a bugged return value from test-unzip
31+
# `$to = test-unzip $zerobyte` * RETURN_VAL has a leading space and complains of $null usage when used in PoSH functions
32+
$to = ([string](test-unzip $zerobyte)).trimStart()
33+
34+
$to | should -not -match '^\s'
35+
$to | should -not -benullorempty
36+
37+
$to | should -exist
38+
39+
(Get-ChildItem $to).count | should -be 0
40+
}
41+
}
42+
43+
context "zip file is small in size" {
44+
$small = "$working_dir\small.zip"
45+
$small | should -exist
46+
47+
it "unzips file which is small in size" -skip:$isUnix {
48+
# some combination of pester, COM (used within unzip_old), and Win10 causes a bugged return value from test-unzip
49+
# `$to = test-unzip $small` * RETURN_VAL has a leading space and complains of $null usage when used in PoSH functions
50+
$to = ([string](test-unzip $small)).trimStart()
51+
52+
$to | should -not -match '^\s'
53+
$to | should -not -benullorempty
54+
55+
$to | should -exist
56+
57+
# these don't work for some reason on appveyor
58+
#join-path $to "empty" | should -exist
59+
#(gci $to).count | should -be 1
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)