Skip to content

Commit 5b87c99

Browse files
authored
refactor(deps): Rewrite 'depends.ps1' (#4638)
1 parent 3ca1b1f commit 5b87c99

13 files changed

+291
-268
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
- **shim:** Fix PS1 shim error when in different drive in PS7 ([#4614](https://github.com/ScoopInstaller/Scoop/issues/4614))
1818
- **shim:** Fix `sh` shim error in WSL ([#4637](https://github.com/ScoopInstaller/Scoop/issues/4637))
1919

20+
### Code Refactoring
21+
22+
- **depends:** Rewrite 'depends.ps1' ([#4638](https://github.com/ScoopInstaller/Scoop/issues/4638))
23+
2024
### Builds
2125

2226
- **checkver:** Fix output with '-Version' ([#3774](https://github.com/ScoopInstaller/Scoop/issues/3774))

lib/core.ps1

+1-1
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ function app_status($app, $global) {
314314
}
315315

316316
$status.missing_deps = @()
317-
$deps = @(runtime_deps $manifest) | Where-Object {
317+
$deps = @($manifest.depends) | Where-Object {
318318
$app, $bucket, $null = parse_app $_
319319
return !(installed $app)
320320
}

lib/decompress.ps1

+11-113
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,3 @@
1-
function Test-7zipRequirement {
2-
[CmdletBinding(DefaultParameterSetName = "URL")]
3-
[OutputType([Boolean])]
4-
param (
5-
[Parameter(Mandatory = $true, ParameterSetName = "URL")]
6-
[String[]]
7-
$URL,
8-
[Parameter(Mandatory = $true, ParameterSetName = "File")]
9-
[String]
10-
$File
11-
)
12-
if ($URL) {
13-
if ((get_config 7ZIPEXTRACT_USE_EXTERNAL)) {
14-
return $false
15-
} else {
16-
return ($URL | Where-Object { Test-7zipRequirement -File $_ }).Count -gt 0
17-
}
18-
} else {
19-
return $File -match '\.((gz)|(tar)|(t[abgpx]z2?)|(lzma)|(bz2?)|(7z)|(rar)|(iso)|(xz)|(lzh)|(nupkg))(\.[^.]+)?$'
20-
}
21-
}
22-
23-
function Test-ZstdRequirement {
24-
[CmdletBinding(DefaultParameterSetName = "URL")]
25-
[OutputType([Boolean])]
26-
param (
27-
[Parameter(Mandatory = $true, ParameterSetName = "URL")]
28-
[String[]]
29-
$URL,
30-
[Parameter(Mandatory = $true, ParameterSetName = "File")]
31-
[String]
32-
$File
33-
)
34-
if ($URL) {
35-
return ($URL | Where-Object { Test-ZstdRequirement -File $_ }).Count -gt 0
36-
} else {
37-
return $File -match '\.zst$'
38-
}
39-
}
40-
41-
function Test-LessmsiRequirement {
42-
[CmdletBinding()]
43-
[OutputType([Boolean])]
44-
param (
45-
[Parameter(Mandatory = $true)]
46-
[String[]]
47-
$URL
48-
)
49-
if ((get_config MSIEXTRACT_USE_LESSMSI)) {
50-
return ($URL | Where-Object { $_ -match '\.msi$' }).Count -gt 0
51-
} else {
52-
return $false
53-
}
54-
}
55-
561
function Expand-7zipArchive {
572
[CmdletBinding()]
583
param (
@@ -67,17 +12,17 @@ function Expand-7zipArchive {
6712
[Parameter(ValueFromRemainingArguments = $true)]
6813
[String]
6914
$Switches,
70-
[ValidateSet("All", "Skip", "Rename")]
15+
[ValidateSet('All', 'Skip', 'Rename')]
7116
[String]
7217
$Overwrite,
7318
[Switch]
7419
$Removal
7520
)
7621
if ((get_config 7ZIPEXTRACT_USE_EXTERNAL)) {
7722
try {
78-
$7zPath = (Get-Command '7z' -CommandType Application | Select-Object -First 1).Source
23+
$7zPath = (Get-Command '7z' -CommandType Application -ErrorAction Stop | Select-Object -First 1).Source
7924
} catch [System.Management.Automation.CommandNotFoundException] {
80-
abort "Cannot find external 7-Zip (7z.exe) while '7ZIPEXTRACT_USE_EXTERNAL' is 'true'!`nRun 'scoop config 7ZIPEXTRACT_USE_EXTERNAL false' or install 7-Zip manually and try again."
25+
abort "`nCannot find external 7-Zip (7z.exe) while '7ZIPEXTRACT_USE_EXTERNAL' is 'true'!`nRun 'scoop config 7ZIPEXTRACT_USE_EXTERNAL false' or install 7-Zip manually and try again."
8126
}
8227
} else {
8328
$7zPath = Get-HelperPath -Helper 7zip
@@ -92,9 +37,9 @@ function Expand-7zipArchive {
9237
$ArgList += (-split $Switches)
9338
}
9439
switch ($Overwrite) {
95-
"All" { $ArgList += "-aoa" }
96-
"Skip" { $ArgList += "-aos" }
97-
"Rename" { $ArgList += "-aou" }
40+
'All' { $ArgList += '-aoa' }
41+
'Skip' { $ArgList += '-aos' }
42+
'Rename' { $ArgList += '-aou' }
9843
}
9944
$Status = Invoke-ExternalCommand $7zPath $ArgList -LogPath $LogPath
10045
if (!$Status) {
@@ -187,7 +132,7 @@ function Expand-MsiArchive {
187132
[Switch]
188133
$Removal
189134
)
190-
$DestinationPath = $DestinationPath.TrimEnd("\")
135+
$DestinationPath = $DestinationPath.TrimEnd('\')
191136
if ($ExtractDir) {
192137
$OriDestinationPath = $DestinationPath
193138
$DestinationPath = "$DestinationPath\_tmp"
@@ -248,9 +193,9 @@ function Expand-InnoArchive {
248193
$LogPath = "$(Split-Path $Path)\innounp.log"
249194
$ArgList = @('-x', "-d`"$DestinationPath`"", "`"$Path`"", '-y')
250195
switch -Regex ($ExtractDir) {
251-
"^[^{].*" { $ArgList += "-c{app}\$ExtractDir" }
252-
"^{.*" { $ArgList += "-c$ExtractDir" }
253-
Default { $ArgList += "-c{app}" }
196+
'^[^{].*' { $ArgList += "-c{app}\$ExtractDir" }
197+
'^{.*' { $ArgList += "-c$ExtractDir" }
198+
Default { $ArgList += '-c{app}' }
254199
}
255200
if ($Switches) {
256201
$ArgList += (-split $Switches)
@@ -286,34 +231,7 @@ function Expand-ZipArchive {
286231
$OriDestinationPath = $DestinationPath
287232
$DestinationPath = "$DestinationPath\_tmp"
288233
}
289-
# All methods to unzip the file require .NET4.5+
290-
if ($PSVersionTable.PSVersion.Major -lt 5) {
291-
Add-Type -AssemblyName System.IO.Compression.FileSystem
292-
try {
293-
[System.IO.Compression.ZipFile]::ExtractToDirectory($Path, $DestinationPath)
294-
} catch [System.IO.PathTooLongException] {
295-
# try to fall back to 7zip if path is too long
296-
if (Test-HelperInstalled -Helper 7zip) {
297-
Expand-7zipArchive $Path $DestinationPath -Removal
298-
return
299-
} else {
300-
abort "Unzip failed: Windows can't handle the long paths in this zip file.`nRun 'scoop install 7zip' and try again."
301-
}
302-
} catch [System.IO.IOException] {
303-
if (Test-HelperInstalled -Helper 7zip) {
304-
Expand-7zipArchive $Path $DestinationPath -Removal
305-
return
306-
} else {
307-
abort "Unzip failed: Windows can't handle the file names in this zip file.`nRun 'scoop install 7zip' and try again."
308-
}
309-
} catch {
310-
abort "Unzip failed: $_"
311-
}
312-
} else {
313-
# Use Expand-Archive to unzip in PowerShell 5+
314-
# Compatible with Pscx (https://github.com/Pscx/Pscx)
315-
Microsoft.PowerShell.Archive\Expand-Archive -Path $Path -DestinationPath $DestinationPath -Force
316-
}
234+
Expand-Archive -Path $Path -DestinationPath $DestinationPath -Force
317235
if ($ExtractDir) {
318236
movedir "$DestinationPath\$ExtractDir" $OriDestinationPath | Out-Null
319237
Remove-Item $DestinationPath -Recurse -Force
@@ -356,23 +274,3 @@ function Expand-DarkArchive {
356274
Remove-Item $Path -Force
357275
}
358276
}
359-
360-
function extract_7zip($path, $to, $removal) {
361-
Show-DeprecatedWarning $MyInvocation 'Expand-7zipArchive'
362-
Expand-7zipArchive -Path $path -DestinationPath $to -Removal:$removal @args
363-
}
364-
365-
function extract_msi($path, $to, $removal) {
366-
Show-DeprecatedWarning $MyInvocation 'Expand-MsiArchive'
367-
Expand-MsiArchive -Path $path -DestinationPath $to -Removal:$removal
368-
}
369-
370-
function unpack_inno($path, $to, $removal) {
371-
Show-DeprecatedWarning $MyInvocation 'Expand-InnoArchive'
372-
Expand-InnoArchive -Path $path -DestinationPath $to -Removal:$removal @args
373-
}
374-
375-
function extract_zip($path, $to, $removal) {
376-
Show-DeprecatedWarning $MyInvocation 'Expand-ZipArchive'
377-
Expand-ZipArchive -Path $path -DestinationPath $to -Removal:$removal
378-
}

0 commit comments

Comments
 (0)