Skip to content

Commit c906219

Browse files
niheavenbrian6932
authored andcommitted
refactor(install): Separate archive extraction from downloader (ScoopInstaller#5951)
1 parent c49b8a6 commit c906219

3 files changed

Lines changed: 77 additions & 50 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
- **core:** Fix "Invoke-ExternalCommand" quoting rules ([#5945](https://github.com/ScoopInstaller/Scoop/issues/5945))
1717
- **scoop-info:** Fix download size estimating ([#5958](https://github.com/ScoopInstaller/Scoop/issues/5958))
1818

19+
### Code Refactoring
20+
21+
- **install:** Separate archive extraction from downloader ([#5951](https://github.com/ScoopInstaller/Scoop/issues/5951))
22+
1923
## [v0.4.1](https://github.com/ScoopInstaller/Scoop/compare/v0.4.0...v0.4.1) - 2024-04-25
2024

2125
### Bug Fixes

lib/decompress.ps1

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,71 @@
1+
# Description: Functions for decompressing archives or installers
2+
3+
function Invoke-Extraction {
4+
param (
5+
[string]
6+
$Path,
7+
[string[]]
8+
$Name,
9+
[psobject]
10+
$Manifest,
11+
[Alias('Arch', 'Architecture')]
12+
[string]
13+
$ProcessorArchitecture
14+
)
15+
16+
# 'url', 'extract_dir' and 'extract_to' are paired
17+
$uri = @(url $Manifest $ProcessorArchitecture)
18+
$extractDir = @(extract_dir $Manifest $ProcessorArchitecture)
19+
$extractTo = @(extract_to $Manifest $ProcessorArchitecture)
20+
21+
for ($i = 0; $i -lt $Name.Length; $i++) {
22+
$fnArgs = @{
23+
Path = Join-Path $Path $Name[$i]
24+
DestinationPath = Join-Path $Path $extractTo[$i]
25+
ExtractDir = $extractDir[$i]
26+
}
27+
# work out extraction method, if applicable
28+
$extractFn = $null
29+
switch -regex ($fnArgs.Path) {
30+
'\.zip$' {
31+
if ((Test-HelperInstalled -Helper 7zip) -or ((get_config 7ZIPEXTRACT_USE_EXTERNAL) -and (Test-CommandAvailable 7z))) {
32+
$extractFn = 'Expand-7zipArchive'
33+
} else {
34+
$extractFn = 'Expand-ZipArchive'
35+
}
36+
continue
37+
}
38+
'\.msi$' {
39+
$extractFn = 'Expand-MsiArchive'
40+
continue
41+
}
42+
'\.exe$' {
43+
if ($Manifest.innosetup) {
44+
$extractFn = 'Expand-InnoArchive'
45+
}
46+
continue
47+
}
48+
{ Test-ZstdRequirement -Uri $_ } {
49+
# Check Zstd first
50+
$extractFn = 'Expand-ZstdArchive'
51+
continue
52+
}
53+
{ Test-7zipRequirement -Uri $_ } {
54+
# Then check 7zip
55+
$extractFn = 'Expand-7zipArchive'
56+
continue
57+
}
58+
}
59+
if ($extractFn) {
60+
Write-Host 'Extracting ' -NoNewline
61+
Write-Host $(url_remote_filename $uri[$i]) -ForegroundColor Cyan -NoNewline
62+
Write-Host ' ... ' -NoNewline
63+
& $extractFn @fnArgs -Removal
64+
Write-Host 'done.' -ForegroundColor Green
65+
}
66+
}
67+
}
68+
169
function Expand-7zipArchive {
270
[CmdletBinding()]
371
param (

lib/install.ps1

Lines changed: 5 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,10 @@ function install_app($app, $architecture, $global, $suggested, $use_cache = $tru
5050
$persist_dir = persistdir $app $global
5151

5252
$fname = Invoke-ScoopDownload $app $version $manifest $bucket $architecture $dir $use_cache $check_hash
53+
Invoke-Extraction -Path $dir -Name $fname -Manifest $manifest -ProcessorArchitecture $architecture
5354
Invoke-HookScript -HookType 'pre_install' -Manifest $manifest -Arch $architecture
5455

55-
run_installer $fname $manifest $architecture $dir $global
56+
run_installer @($fname)[-1] $manifest $architecture $dir $global
5657
ensure_install_dir_not_in_path $dir $global
5758
$dir = link_current $dir
5859
create_shims $manifest $dir $global $architecture
@@ -539,21 +540,12 @@ function Invoke-ScoopDownload ($app, $version, $manifest, $bucket, $architecture
539540
# we only want to show this warning once
540541
if (!$use_cache) { warn 'Cache is being ignored.' }
541542

542-
# can be multiple urls: if there are, then installer should go last,
543-
# so that $fname is set properly
543+
# can be multiple urls: if there are, then installer should go last to make 'installer.args' section work
544544
$urls = @(script:url $manifest $architecture)
545545

546546
# can be multiple cookies: they will be used for all HTTP requests.
547547
$cookies = $manifest.cookie
548548

549-
$fname = $null
550-
551-
# extract_dir and extract_to in manifest are like queues: for each url that
552-
# needs to be extracted, will get the next dir from the queue
553-
$extract_dirs = @(extract_dir $manifest $architecture)
554-
$extract_tos = @(extract_to $manifest $architecture)
555-
$extracted = 0
556-
557549
# download first
558550
if (Test-Aria2Enabled) {
559551
Invoke-CachedAria2Download $app $version $manifest $architecture $dir $cookies $use_cache $check_hash
@@ -587,44 +579,7 @@ function Invoke-ScoopDownload ($app, $version, $manifest, $bucket, $architecture
587579
}
588580
}
589581

590-
foreach ($url in $urls) {
591-
$fname = url_filename $url
592-
593-
$extract_dir = $extract_dirs[$extracted]
594-
$extract_to = $extract_tos[$extracted]
595-
596-
# work out extraction method, if applicable
597-
$extract_fn = $null
598-
if ($manifest.innosetup) {
599-
$extract_fn = 'Expand-InnoArchive'
600-
} elseif ($fname -match '\.zip$') {
601-
# Use 7zip when available (more fast)
602-
if (((get_config USE_EXTERNAL_7ZIP) -and (Test-CommandAvailable 7z)) -or (Test-HelperInstalled -Helper 7zip)) {
603-
$extract_fn = 'Expand-7zipArchive'
604-
} else {
605-
$extract_fn = 'Expand-ZipArchive'
606-
}
607-
} elseif ($fname -match '\.msi$') {
608-
$extract_fn = 'Expand-MsiArchive'
609-
} elseif (Test-ZstdRequirement -Uri $fname) {
610-
# Zstd first
611-
$extract_fn = 'Expand-ZstdArchive'
612-
} elseif (Test-7zipRequirement -Uri $fname) {
613-
# 7zip
614-
$extract_fn = 'Expand-7zipArchive'
615-
}
616-
617-
if ($extract_fn) {
618-
Write-Host 'Extracting ' -NoNewline
619-
Write-Host $fname -f Cyan -NoNewline
620-
Write-Host ' ... ' -NoNewline
621-
& $extract_fn -Path "$dir\$fname" -DestinationPath "$dir\$extract_to" -ExtractDir $extract_dir -Removal
622-
Write-Host 'done.' -f Green
623-
$extracted++
624-
}
625-
}
626-
627-
$fname # returns the last downloaded file
582+
return $urls.ForEach({ url_filename $_ })
628583
}
629584

630585
function cookie_header($cookies) {
@@ -710,7 +665,7 @@ function run_installer($fname, $manifest, $architecture, $dir, $global) {
710665
return
711666
}
712667
if ($installer) {
713-
$prog = "$dir\$(coalesce $installer.file "$fname")"
668+
$prog = "$dir\$(coalesce $installer.file $fname)"
714669
if (!(is_in_dir $dir $prog)) {
715670
abort "Error in manifest: Installer $prog is outside the app directory."
716671
}

0 commit comments

Comments
 (0)