Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(scoop-info): Show app installed/download size #4886

Merged
merged 20 commits into from
Jun 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## [Unreleased](https://github.com/ScoopInstaller/Scoop/compare/master...develop)

### Features

- **scoop-info:** Show app installed/download size ([#4886](https://github.com/ScoopInstaller/Scoop/issues/4886))

### Bug Fixes

- **chore:** Update help documentation ([#5002](https://github.com/ScoopInstaller/Scoop/issues/5002))
Expand Down
79 changes: 79 additions & 0 deletions libexec/scoop-info.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,85 @@ if ($status.installed) {
$installed_output += if ($verbose) { versiondir $app $_ $global } else { "$_$(if ($global) { " *global*" })" }
}
$item.Installed = $installed_output -join "`n"

if ($verbose) {
# Show size of installation
$appsdir = appsdir $global

# Collect file list from each location
$appFiles = Get-ChildItem $appsdir -Filter $app
$currentFiles = Get-ChildItem $appFiles -Filter (Select-CurrentVersion $app $global)
$persistFiles = Get-ChildItem $persist_dir -ErrorAction Ignore # Will fail if app does not persist data
$cacheFiles = Get-ChildItem $cachedir -Filter "$app#*"

# Get the sum of each file list
$fileTotals = @()
foreach ($fileType in ($appFiles, $currentFiles, $persistFiles, $cacheFiles)) {
if ($null -ne $fileType) {
$fileSum = (Get-ChildItem $fileType -Recurse | Measure-Object -Property Length -Sum).Sum
$fileTotals += coalesce $fileSum 0
} else {
$fileTotals += 0
}
}

# Old versions = app total - current version size
$fileTotals += $fileTotals[0] - $fileTotals[1]

if ($fileTotals[2] + $fileTotals[3] + $fileTotals[4] -eq 0) {
# Simple app size output if no old versions, persisted data, cached downloads
$item.'Installed size' = filesize $fileTotals[1]
} else {
$fileSizes = [ordered] @{
'Current version: ' = $fileTotals[1]
'Old versions: ' = $fileTotals[4]
'Persisted data: ' = $fileTotals[2]
'Cached downloads: ' = $fileTotals[3]
'Total: ' = $fileTotals[0] + $fileTotals[2] + $fileTotals[3]
}

$fileSizeOutput = @()

# Don't output empty categories
$fileSizes.GetEnumerator() | ForEach-Object {
if ($_.Value -ne 0) {
$fileSizeOutput += $_.Key + (filesize $_.Value)
}
}

$item.'Installed size' = $fileSizeOutput -join "`n"
}
}
} else {
if ($verbose) {
# Get download size if app not installed
$totalPackage = 0
foreach ($url in @(url $manifest (default_architecture))) {
try {
if (Test-Path (fullpath (cache_path $app $manifest.version $url))) {
$cached = " (latest version is cached)"
} else {
$cached = $null
}

[int]$urlLength = (Invoke-WebRequest $url -Method Head).Headers.'Content-Length'[0]
$totalPackage += $urlLength
} catch [System.Management.Automation.RuntimeException] {
$totalPackage = 0
$packageError = "the server at $(([System.Uri]$url).Host) did not send a Content-Length header"
break
} catch {
$totalPackage = 0
$packageError = "the server at $(([System.Uri]$url).Host) is down"
break
}
}
if ($totalPackage -ne 0) {
$item.'Download size' = "$(filesize $totalPackage)$cached"
} else {
$item.'Download size' = "Unknown ($packageError)$cached"
}
}
}

$binaries = @(arch_specific 'bin' $manifest $install.architecture)
Expand Down