Skip to content

Commit

Permalink
feat(core): New cache filename format (#5929)
Browse files Browse the repository at this point in the history
Signed-off-by: Chawye Hsu <[email protected]>
Co-authored-by: Hsiao-nan Cheung <[email protected]>
  • Loading branch information
chawyehsu and niheaven authored Apr 29, 2024
1 parent 3b34497 commit 36026f1
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### Features

- **scoop-search:** Use SQLite for caching apps to speed up local search ([#5851](https://github.com/ScoopInstaller/Scoop/issues/5851), [#5918](https://github.com/ScoopInstaller/Scoop/issues/5918))
- **core:** New cache filename format ([#5929](https://github.com/ScoopInstaller/Scoop/issues/5929)

### Bug Fixes

Expand Down
17 changes: 16 additions & 1 deletion lib/core.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,22 @@ function currentdir($app, $global) {
function persistdir($app, $global) { "$(basedir $global)\persist\$app" }
function usermanifestsdir { "$(basedir)\workspace" }
function usermanifest($app) { "$(usermanifestsdir)\$app.json" }
function cache_path($app, $version, $url) { "$cachedir\$app#$version#$($url -replace '[^\w\.\-]+', '_')" }
function cache_path($app, $version, $url) {
$underscoredUrl = $url -replace '[^\w\.\-]+', '_'
$filePath = "$cachedir\$app#$version#$underscoredUrl"

# NOTE: Scoop cache files migration. Remove this 6 months after the feature ships.
if (Test-Path $filePath) {
return $filePath
}

$urlStream = [System.IO.MemoryStream]::new([System.Text.Encoding]::UTF8.GetBytes($url))
$sha = (Get-FileHash -Algorithm SHA256 -InputStream $urlStream).Hash.ToLower().Substring(0, 7)
$extension = [System.IO.Path]::GetExtension($url)
$filePath = $filePath -replace "$underscoredUrl", "$sha$extension"

return $filePath
}

# apps
function sanitary_path($path) { return [regex]::replace($path, "[/\\?:*<>|]", "") }
Expand Down
4 changes: 2 additions & 2 deletions libexec/scoop-cache.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ param($cmd)

function cacheinfo($file) {
$app, $version, $url = $file.Name -split '#'
New-Object PSObject -Property @{ Name = $app; Version = $version; Length = $file.Length; URL = $url }
New-Object PSObject -Property @{ Name = $app; Version = $version; Length = $file.Length }
}

function cacheshow($app) {
Expand All @@ -28,7 +28,7 @@ function cacheshow($app) {
$files = @(Get-ChildItem $cachedir | Where-Object -Property Name -Value "^$app#" -Match)
$totalLength = ($files | Measure-Object -Property Length -Sum).Sum

$files | ForEach-Object { cacheinfo $_ } | Select-Object Name, Version, Length, URL
$files | ForEach-Object { cacheinfo $_ } | Select-Object Name, Version, Length

Write-Host "Total: $($files.Length) $(pluralize $files.Length 'file' 'files'), $(filesize $totalLength)" -ForegroundColor Yellow
}
Expand Down
17 changes: 17 additions & 0 deletions test/Scoop-Core.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,23 @@ Describe 'get_app_name_from_shim' -Tag 'Scoop', 'Windows' {
}
}

Describe 'cache_path' -Tag 'Scoop' {
It 'returns the correct cache path for a given input' {
$url = 'https://example.com/git.zip'
$ret = cache_path 'git' '2.44.0' $url
$inputStream = [System.IO.MemoryStream]::new([System.Text.Encoding]::UTF8.GetBytes($url))
$sha = (Get-FileHash -Algorithm SHA256 -InputStream $inputStream).Hash.ToLower().Substring(0, 7)
$ret | Should -Be "$cachedir\git#2.44.0#$sha.zip"
}

# # NOTE: Remove this 6 months after the feature ships.
It 'returns the old format cache path for a given input' {
Mock Test-Path { $true }
$ret = cache_path 'git' '2.44.0' 'https://example.com/git.zip'
$ret | Should -Be "$cachedir\git#2.44.0#https_example.com_git.zip"
}
}

Describe 'sanitary_path' -Tag 'Scoop' {
It 'removes invalid path characters from a string' {
$path = 'test?.json'
Expand Down

0 comments on commit 36026f1

Please sign in to comment.