Skip to content

Commit 79cf33d

Browse files
authored
refactor(download): Move download-related functions to 'download.ps1' (#6095)
1 parent 7f99c49 commit 79cf33d

18 files changed

+836
-821
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
### Bug Fixes
44

5-
- **decompress**: `Expand-7zipArchive` Only delete temp dir / `$extractDir` if it is empty ([#6092](https://github.com/ScoopInstaller/Scoop/issues/6092))
5+
- **decompress**: `Expand-7zipArchive` only delete temp dir / `$extractDir` if it is empty ([#6092](https://github.com/ScoopInstaller/Scoop/issues/6092))
6+
7+
### Code Refactoring
8+
9+
- **download:** Move download-related functions to 'download.ps1' ([#6095](https://github.com/ScoopInstaller/Scoop/issues/6095))
610

711
## [v0.5.2](https://github.com/ScoopInstaller/Scoop/compare/v0.5.1...v0.5.2) - 2024-07-26
812

bin/checkhashes.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ param(
4646
. "$PSScriptRoot\..\lib\autoupdate.ps1"
4747
. "$PSScriptRoot\..\lib\json.ps1"
4848
. "$PSScriptRoot\..\lib\versions.ps1"
49-
. "$PSScriptRoot\..\lib\install.ps1"
49+
. "$PSScriptRoot\..\lib\download.ps1"
5050

5151
$Dir = Convert-Path $Dir
5252
if ($ForceUpdate) { $Update = $true }

bin/checkurls.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ param(
2828

2929
. "$PSScriptRoot\..\lib\core.ps1"
3030
. "$PSScriptRoot\..\lib\manifest.ps1"
31-
. "$PSScriptRoot\..\lib\install.ps1"
31+
. "$PSScriptRoot\..\lib\download.ps1"
3232

3333
$Dir = Convert-Path $Dir
3434
$Queue = @()

bin/checkver.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ param(
7373
. "$PSScriptRoot\..\lib\buckets.ps1"
7474
. "$PSScriptRoot\..\lib\json.ps1"
7575
. "$PSScriptRoot\..\lib\versions.ps1"
76-
. "$PSScriptRoot\..\lib\install.ps1" # needed for hash generation
76+
. "$PSScriptRoot\..\lib\download.ps1"
7777

7878
if ($App -ne '*' -and (Test-Path $App -PathType Leaf)) {
7979
$Dir = Split-Path $App

bin/describe.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ param(
2323
. "$PSScriptRoot\..\lib\core.ps1"
2424
. "$PSScriptRoot\..\lib\manifest.ps1"
2525
. "$PSScriptRoot\..\lib\description.ps1"
26+
. "$PSScriptRoot\..\lib\download.ps1"
2627

2728
$Dir = Convert-Path $Dir
2829
$Queue = @()

lib/autoupdate.ps1

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
11
# Must included with 'json.ps1'
2+
3+
function format_hash([String] $hash) {
4+
$hash = $hash.toLower()
5+
switch ($hash.Length) {
6+
32 { $hash = "md5:$hash" } # md5
7+
40 { $hash = "sha1:$hash" } # sha1
8+
64 { $hash = $hash } # sha256
9+
128 { $hash = "sha512:$hash" } # sha512
10+
default { $hash = $null }
11+
}
12+
return $hash
13+
}
14+
215
function find_hash_in_rdf([String] $url, [String] $basename) {
316
$xml = $null
417
try {

lib/core.ps1

Lines changed: 0 additions & 176 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,6 @@ function Optimize-SecurityProtocol {
6363
}
6464
}
6565

66-
function Get-Encoding($wc) {
67-
if ($null -ne $wc.ResponseHeaders -and $wc.ResponseHeaders['Content-Type'] -match 'charset=([^;]*)') {
68-
return [System.Text.Encoding]::GetEncoding($Matches[1])
69-
} else {
70-
return [System.Text.Encoding]::GetEncoding('utf-8')
71-
}
72-
}
73-
74-
function Get-UserAgent() {
75-
return "Scoop/1.0 (+http://scoop.sh/) PowerShell/$($PSVersionTable.PSVersion.Major).$($PSVersionTable.PSVersion.Minor) (Windows NT $([System.Environment]::OSVersion.Version.Major).$([System.Environment]::OSVersion.Version.Minor); $(if(${env:ProgramFiles(Arm)}){'ARM64; '}elseif($env:PROCESSOR_ARCHITECTURE -eq 'AMD64'){'Win64; x64; '})$(if($env:PROCESSOR_ARCHITEW6432 -in 'AMD64','ARM64'){'WOW64; '})$PSEdition)"
76-
}
77-
7866
function Show-DeprecatedWarning {
7967
<#
8068
.SYNOPSIS
@@ -228,35 +216,6 @@ function Complete-ConfigChange {
228216
}
229217
}
230218

231-
function setup_proxy() {
232-
# note: '@' and ':' in password must be escaped, e.g. 'p@ssword' -> p\@ssword'
233-
$proxy = get_config PROXY
234-
if(!$proxy) {
235-
return
236-
}
237-
try {
238-
$credentials, $address = $proxy -split '(?<!\\)@'
239-
if(!$address) {
240-
$address, $credentials = $credentials, $null # no credentials supplied
241-
}
242-
243-
if($address -eq 'none') {
244-
[net.webrequest]::defaultwebproxy = $null
245-
} elseif($address -ne 'default') {
246-
[net.webrequest]::defaultwebproxy = new-object net.webproxy "http://$address"
247-
}
248-
249-
if($credentials -eq 'currentuser') {
250-
[net.webrequest]::defaultwebproxy.credentials = [net.credentialcache]::defaultcredentials
251-
} elseif($credentials) {
252-
$username, $password = $credentials -split '(?<!\\):' | ForEach-Object { $_ -replace '\\([@:])','$1' }
253-
[net.webrequest]::defaultwebproxy.credentials = new-object net.networkcredential($username, $password)
254-
}
255-
} catch {
256-
warn "Failed to use proxy '$proxy': $($_.exception.message)"
257-
}
258-
}
259-
260219
function Invoke-Git {
261220
[CmdletBinding()]
262221
[OutputType([String])]
@@ -584,10 +543,6 @@ function Test-HelperInstalled {
584543
return ![String]::IsNullOrWhiteSpace((Get-HelperPath -Helper $Helper))
585544
}
586545

587-
function Test-Aria2Enabled {
588-
return (Test-HelperInstalled -Helper Aria2) -and (get_config 'aria2-enabled' $true)
589-
}
590-
591546
function app_status($app, $global) {
592547
$status = @{}
593548
$status.installed = installed $app $global
@@ -639,28 +594,6 @@ function fname($path) { split-path $path -leaf }
639594
function strip_ext($fname) { $fname -replace '\.[^\.]*$', '' }
640595
function strip_filename($path) { $path -replace [regex]::escape((fname $path)) }
641596
function strip_fragment($url) { $url -replace (new-object uri $url).fragment }
642-
643-
function url_filename($url) {
644-
(split-path $url -leaf).split('?') | Select-Object -First 1
645-
}
646-
# Unlike url_filename which can be tricked by appending a
647-
# URL fragment (e.g. #/dl.7z, useful for coercing a local filename),
648-
# this function extracts the original filename from the URL.
649-
function url_remote_filename($url) {
650-
$uri = (New-Object URI $url)
651-
$basename = Split-Path $uri.PathAndQuery -Leaf
652-
If ($basename -match ".*[?=]+([\w._-]+)") {
653-
$basename = $matches[1]
654-
}
655-
If (($basename -notlike "*.*") -or ($basename -match "^[v.\d]+$")) {
656-
$basename = Split-Path $uri.AbsolutePath -Leaf
657-
}
658-
If (($basename -notlike "*.*") -and ($uri.Fragment -ne "")) {
659-
$basename = $uri.Fragment.Trim('/', '#')
660-
}
661-
return $basename
662-
}
663-
664597
function ensure($dir) {
665598
if (!(Test-Path -Path $dir)) {
666599
New-Item -Path $dir -ItemType Directory | Out-Null
@@ -1282,112 +1215,6 @@ function substitute($entity, [Hashtable] $params, [Bool]$regexEscape = $false) {
12821215
return $newentity
12831216
}
12841217

1285-
function format_hash([String] $hash) {
1286-
$hash = $hash.toLower()
1287-
switch ($hash.Length)
1288-
{
1289-
32 { $hash = "md5:$hash" } # md5
1290-
40 { $hash = "sha1:$hash" } # sha1
1291-
64 { $hash = $hash } # sha256
1292-
128 { $hash = "sha512:$hash" } # sha512
1293-
default { $hash = $null }
1294-
}
1295-
return $hash
1296-
}
1297-
1298-
function format_hash_aria2([String] $hash) {
1299-
$hash = $hash -split ':' | Select-Object -Last 1
1300-
switch ($hash.Length)
1301-
{
1302-
32 { $hash = "md5=$hash" } # md5
1303-
40 { $hash = "sha-1=$hash" } # sha1
1304-
64 { $hash = "sha-256=$hash" } # sha256
1305-
128 { $hash = "sha-512=$hash" } # sha512
1306-
default { $hash = $null }
1307-
}
1308-
return $hash
1309-
}
1310-
1311-
function get_hash([String] $multihash) {
1312-
$type, $hash = $multihash -split ':'
1313-
if(!$hash) {
1314-
# no type specified, assume sha256
1315-
$type, $hash = 'sha256', $multihash
1316-
}
1317-
1318-
if(@('md5','sha1','sha256', 'sha512') -notcontains $type) {
1319-
return $null, "Hash type '$type' isn't supported."
1320-
}
1321-
1322-
return $type, $hash.ToLower()
1323-
}
1324-
1325-
function Get-GitHubToken {
1326-
return $env:SCOOP_GH_TOKEN, (get_config GH_TOKEN) | Where-Object -Property Length -Value 0 -GT | Select-Object -First 1
1327-
}
1328-
1329-
function handle_special_urls($url)
1330-
{
1331-
# FossHub.com
1332-
if ($url -match "^(?:.*fosshub.com\/)(?<name>.*)(?:\/|\?dwl=)(?<filename>.*)$") {
1333-
$Body = @{
1334-
projectUri = $Matches.name;
1335-
fileName = $Matches.filename;
1336-
source = 'CF';
1337-
isLatestVersion = $true
1338-
}
1339-
if ((Invoke-RestMethod -Uri $url) -match '"p":"(?<pid>[a-f0-9]{24}).*?"r":"(?<rid>[a-f0-9]{24})') {
1340-
$Body.Add("projectId", $Matches.pid)
1341-
$Body.Add("releaseId", $Matches.rid)
1342-
}
1343-
$url = Invoke-RestMethod -Method Post -Uri "https://api.fosshub.com/download/" -ContentType "application/json" -Body (ConvertTo-Json $Body -Compress)
1344-
if ($null -eq $url.error) {
1345-
$url = $url.data.url
1346-
}
1347-
}
1348-
1349-
# Sourceforge.net
1350-
if ($url -match "(?:downloads\.)?sourceforge.net\/projects?\/(?<project>[^\/]+)\/(?:files\/)?(?<file>.*?)(?:$|\/download|\?)") {
1351-
# Reshapes the URL to avoid redirections
1352-
$url = "https://downloads.sourceforge.net/project/$($matches['project'])/$($matches['file'])"
1353-
}
1354-
1355-
# Github.com
1356-
if ($url -match 'github.com/(?<owner>[^/]+)/(?<repo>[^/]+)/releases/download/(?<tag>[^/]+)/(?<file>[^/#]+)(?<filename>.*)' -and ($token = Get-GitHubToken)) {
1357-
$headers = @{ "Authorization" = "token $token" }
1358-
$privateUrl = "https://api.github.com/repos/$($Matches.owner)/$($Matches.repo)"
1359-
$assetUrl = "https://api.github.com/repos/$($Matches.owner)/$($Matches.repo)/releases/tags/$($Matches.tag)"
1360-
1361-
if ((Invoke-RestMethod -Uri $privateUrl -Headers $headers).Private) {
1362-
$url = ((Invoke-RestMethod -Uri $assetUrl -Headers $headers).Assets | Where-Object -Property Name -EQ -Value $Matches.file).Url, $Matches.filename -join ''
1363-
}
1364-
}
1365-
1366-
return $url
1367-
}
1368-
1369-
function get_magic_bytes($file) {
1370-
if(!(Test-Path $file)) {
1371-
return ''
1372-
}
1373-
1374-
if((Get-Command Get-Content).parameters.ContainsKey('AsByteStream')) {
1375-
# PowerShell Core (6.0+) '-Encoding byte' is replaced by '-AsByteStream'
1376-
return Get-Content $file -AsByteStream -TotalCount 8
1377-
}
1378-
else {
1379-
return Get-Content $file -Encoding byte -TotalCount 8
1380-
}
1381-
}
1382-
1383-
function get_magic_bytes_pretty($file, $glue = ' ') {
1384-
if(!(Test-Path $file)) {
1385-
return ''
1386-
}
1387-
1388-
return (get_magic_bytes $file | ForEach-Object { $_.ToString('x2') }) -join $glue
1389-
}
1390-
13911218
function Out-UTF8File {
13921219
param(
13931220
[Parameter(Mandatory = $True, Position = 0)]
@@ -1473,6 +1300,3 @@ $scoopPathEnvVar = switch (get_config USE_ISOLATED_PATH) {
14731300

14741301
# OS information
14751302
$WindowsBuild = [System.Environment]::OSVersion.Version.Build
1476-
1477-
# Setup proxy globally
1478-
setup_proxy

0 commit comments

Comments
 (0)