Skip to content

Commit 62233a5

Browse files
Handle partial download failures in Install-NerdFont
1 parent ed569aa commit 62233a5

4 files changed

Lines changed: 54 additions & 10 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ Get-Font -Name 'FiraCode*' -Scope AllUsers
8585

8686
If the command returns results, the font is installed. If it returns nothing, the font is not installed in that scope.
8787

88-
When you run `Install-NerdFont` again without `-Force`, fonts that are already installed in the requested scope are skipped. Downloaded archives are also cached per Nerd Fonts release so retries and repeated installs do not need to fetch the same zip again.
88+
When you run `Install-NerdFont` again without `-Force`, fonts that are already installed in the requested scope are skipped. Downloaded archives are also cached per Nerd Fonts release so retries and repeated installs do not need to fetch the same ZIP again.
8989

9090
### Update an installed NerdFont
9191

@@ -103,7 +103,7 @@ Install-NerdFont -Name 'FiraCode' -Force -Scope AllUsers
103103
```
104104

105105
This re-downloads and installs the font version bundled with your installed NerdFonts module, overwriting any existing
106-
files. `-Force` also bypasses the local archive cache so the font zip is fetched again before reinstalling. To pick up newer font releases, update the NerdFonts module first (`Update-PSResource -Name NerdFonts` if you
106+
files. `-Force` also bypasses the local archive cache so the font ZIP is fetched again before reinstalling. To pick up newer font releases, update the NerdFonts module first (`Update-PSResource -Name NerdFonts` if you
107107
installed via PSResourceGet, or `Update-Module -Name NerdFonts` if you installed via PowerShellGet).
108108

109109
### Uninstall a NerdFont

scripts/Measure-InstallPerformance.ps1

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ param(
4646
$ErrorActionPreference = 'Stop'
4747

4848
function Invoke-Uninstall {
49+
<#
50+
.SYNOPSIS
51+
Removes matching Nerd Font families for the current user.
52+
#>
4953
param([string[]]$Names)
5054
foreach ($n in $Names) {
5155
# Nerd Fonts archives expand to multiple family names that all start
@@ -62,11 +66,19 @@ function Invoke-Uninstall {
6266
}
6367

6468
function Invoke-UninstallAll {
69+
<#
70+
.SYNOPSIS
71+
Removes all Nerd Fonts known to the current module.
72+
#>
6573
$names = (Get-NerdFont).Name
6674
Invoke-Uninstall -Names $names
6775
}
6876

6977
function Measure-Scenario {
78+
<#
79+
.SYNOPSIS
80+
Runs one setup/action performance scenario and records the result.
81+
#>
7082
param(
7183
[string]$Name,
7284
[scriptblock]$Setup,

src/functions/public/Install-NerdFont.ps1

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ Please run the command again with elevated rights (Run as Administrator) or prov
149149
$httpClient = [System.Net.Http.HttpClient]::new()
150150
$httpClient.Timeout = [TimeSpan]::FromMinutes(5)
151151
$pending = [System.Collections.Generic.List[object]]::new()
152+
$readyToInstall = [System.Collections.Generic.List[object]]::new()
152153
$throttle = 8
153154

154155
try {
@@ -169,14 +170,16 @@ Please run the command again with elevated rights (Run as Administrator) or prov
169170
if ((Test-Path -LiteralPath $cachedFile) -and -not $Force) {
170171
Write-Verbose "[$fontName] - Cache hit at [$cachedFile]"
171172
Copy-Item -LiteralPath $cachedFile -Destination $downloadPath -Force
172-
$pending.Add([pscustomobject]@{
173+
$item = [pscustomobject]@{
173174
Name = $fontName
174175
URL = $URL
175176
DownloadPath = $downloadPath
176177
CachedFile = $cachedFile
177178
CacheTagDir = $cacheTagDir
178179
FromCache = $true
179-
})
180+
}
181+
$pending.Add($item)
182+
$readyToInstall.Add($item)
180183
} else {
181184
Write-Verbose "[$fontName] - Queue download to [$downloadPath]"
182185
$pending.Add([pscustomobject]@{
@@ -199,19 +202,24 @@ Please run the command again with elevated rights (Run as Administrator) or prov
199202
$tasks += [pscustomobject]@{ Q = $q; Task = $httpClient.GetByteArrayAsync($q.URL) }
200203
}
201204
foreach ($t in $tasks) {
202-
$bytes = $t.Task.GetAwaiter().GetResult()
203-
[System.IO.File]::WriteAllBytes($t.Q.DownloadPath, $bytes)
204-
if (-not (Test-Path -LiteralPath $t.Q.CacheTagDir)) {
205-
$null = New-Item -ItemType Directory -Path $t.Q.CacheTagDir -Force
205+
try {
206+
$bytes = $t.Task.GetAwaiter().GetResult()
207+
[System.IO.File]::WriteAllBytes($t.Q.DownloadPath, $bytes)
208+
if (-not (Test-Path -LiteralPath $t.Q.CacheTagDir)) {
209+
$null = New-Item -ItemType Directory -Path $t.Q.CacheTagDir -Force
210+
}
211+
[System.IO.File]::WriteAllBytes($t.Q.CachedFile, $bytes)
212+
$readyToInstall.Add($t.Q)
213+
} catch {
214+
Write-Error "[$($t.Q.Name)] - Download failed: $($_.Exception.Message)"
206215
}
207-
[System.IO.File]::WriteAllBytes($t.Q.CachedFile, $bytes)
208216
}
209217
}
210218
} finally {
211219
$httpClient.Dispose()
212220
}
213221

214-
foreach ($p in $pending) {
222+
foreach ($p in $readyToInstall) {
215223
$fontName = $p.Name
216224
$downloadPath = $p.DownloadPath
217225
$extractPath = Join-Path -Path $tempPath -ChildPath $fontName

tests/NerdFonts.Tests.ps1

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,30 @@ Describe 'Module' {
4040
Get-Font -Name 'Tinos*' | Should -Not -BeNullOrEmpty
4141
}
4242

43+
It 'Install-NerdFont - Continues when one queued download fails' {
44+
. (Join-Path -Path $PSScriptRoot -ChildPath '..\src\functions\public\Install-NerdFont.ps1')
45+
46+
$originalFonts = $script:NerdFonts
47+
$loadedFonts = Get-Content -Path (Join-Path -Path $PSScriptRoot -ChildPath '..\src\FontsData.json') | ConvertFrom-Json
48+
$goodFont = $loadedFonts | Where-Object Name -eq 'Tinos' | Select-Object -First 1
49+
50+
$script:NerdFonts = @(
51+
[pscustomobject]@{
52+
Name = 'BrokenDownloadTest'
53+
URL = 'https://github.com/ryanoasis/nerd-fonts/releases/download/v3.4.0/does-not-exist.zip'
54+
},
55+
$goodFont
56+
)
57+
58+
try {
59+
Mock Install-Font {}
60+
{ Install-NerdFont -Name @('BrokenDownloadTest', 'Tinos') -Force -ErrorAction SilentlyContinue } | Should -Not -Throw
61+
Should -Invoke Install-Font -Times 1 -Exactly
62+
} finally {
63+
$script:NerdFonts = $originalFonts
64+
}
65+
}
66+
4367
It 'Install-NerdFont - Installs a font with -Variant Mono' {
4468
{ Install-NerdFont -Name 'Hack' -Variant Mono -Force } | Should -Not -Throw
4569
Get-Font -Name 'Hack*' | Should -Not -BeNullOrEmpty

0 commit comments

Comments
 (0)