Skip to content

Commit 7d4c803

Browse files
Address review threads: ShouldProcess-safe downloads, retry+cache-force fixes, cleanup and test hardening
1 parent 76689b9 commit 7d4c803

3 files changed

Lines changed: 51 additions & 19 deletions

File tree

scripts/Measure-InstallPerformance.ps1

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,19 @@ function Measure-Scenario {
5050
$err = $null
5151
try { & $Action | Out-Null } catch { $err = $_.Exception.Message }
5252
$sw.Stop()
53+
$module = Get-Module GoogleFonts | Select-Object -First 1
54+
$resultsDirectory = Split-Path -Path $script:ResultsPath -Parent
55+
if ($resultsDirectory -and -not (Test-Path -Path $resultsDirectory -PathType Container)) {
56+
$null = New-Item -Path $resultsDirectory -ItemType Directory -Force
57+
}
5358
$obj = [pscustomobject]@{
5459
Iteration = $Iteration
5560
Scenario = $Name
5661
DurationMs = [int]$sw.Elapsed.TotalMilliseconds
5762
DurationS = [math]::Round($sw.Elapsed.TotalSeconds, 2)
5863
Timestamp = (Get-Date).ToString('o')
5964
Error = $err
60-
Module = (Get-Module GoogleFonts | Select-Object -First 1 -ExpandProperty Version).ToString()
65+
Module = if ($module) { $module.Version.ToString() } else { $null }
6166
}
6267
($obj | ConvertTo-Json -Compress) | Add-Content -LiteralPath $script:ResultsPath
6368
Write-Host "[$Iteration] Result : $Name -> $($obj.DurationS)s"

src/functions/public/Install-GoogleFont.ps1

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ Please run the command again with elevated rights (Run as Administrator) or prov
7878
throw $errorMessage
7979
}
8080
$previousProgressPreference = $ProgressPreference
81-
$ProgressPreference = 'SilentlyContinue'
8281
$googleFontsToInstall = [System.Collections.Generic.List[object]]::new()
8382
$seenUrls = [System.Collections.Generic.HashSet[string]]::new([System.StringComparer]::OrdinalIgnoreCase)
8483

@@ -124,7 +123,11 @@ Please run the command again with elevated rights (Run as Administrator) or prov
124123
foreach ($family in $installedFamilies) {
125124
if ($family -like "$fontName*") { $skip = $true; break }
126125
}
127-
if (-not $skip) { $toProcess.Add($googleFont) }
126+
if ($skip) {
127+
Write-Verbose "[$fontName] - Already installed, skipping"
128+
continue
129+
}
130+
$toProcess.Add($googleFont)
128131
}
129132
$googleFontsToInstall = $toProcess
130133
}
@@ -144,11 +147,16 @@ Please run the command again with elevated rights (Run as Administrator) or prov
144147
$httpClient = [System.Net.Http.HttpClient]::new()
145148
$httpClient.Timeout = [TimeSpan]::FromMinutes(5)
146149
$throttle = [Environment]::ProcessorCount
150+
$maxRetryCount = 5
151+
$retryDelaySeconds = 5
147152
try {
148153
$pending = [System.Collections.Generic.List[object]]::new()
149154
foreach ($googleFont in $googleFontsToInstall) {
150155
$URL = $googleFont.URL
151156
$fontName = $googleFont.Name
157+
if (-not $PSCmdlet.ShouldProcess("[$fontName] to [$Scope]", 'Install font')) {
158+
continue
159+
}
152160
$fontVariant = $googleFont.Variant
153161
$fileExtension = $URL.Split('.')[-1]
154162
$downloadFileName = "$fontName-$fontVariant.$fileExtension"
@@ -161,7 +169,7 @@ Please run the command again with elevated rights (Run as Administrator) or prov
161169
URL = $URL
162170
DownloadPath = $downloadPath
163171
CachePath = $cachePath
164-
FromCache = (Test-Path -LiteralPath $cachePath)
172+
FromCache = (-not $Force) -and (Test-Path -LiteralPath $cachePath)
165173
})
166174
}
167175

@@ -176,31 +184,44 @@ Please run the command again with elevated rights (Run as Administrator) or prov
176184
for ($i = 0; $i -lt $toDownload.Count; $i += $throttle) {
177185
$chunk = $toDownload[$i..([math]::Min($i + $throttle - 1, $toDownload.Count - 1))]
178186
$tasks = foreach ($item in $chunk) {
179-
Write-Verbose "[$($item.Name)] - Downloading to [$($item.DownloadPath)]"
180-
[pscustomobject]@{ Item = $item; Task = $httpClient.GetByteArrayAsync($item.URL) }
187+
[pscustomobject]@{ Item = $item }
181188
}
182189
foreach ($t in $tasks) {
183-
try {
184-
$bytes = $t.Task.GetAwaiter().GetResult()
185-
[System.IO.File]::WriteAllBytes($t.Item.DownloadPath, $bytes)
190+
$downloadSucceeded = $false
191+
for ($attempt = 1; $attempt -le $maxRetryCount -and -not $downloadSucceeded; $attempt++) {
186192
try {
187-
[System.IO.File]::WriteAllBytes($t.Item.CachePath, $bytes)
193+
Write-Verbose "[$($t.Item.Name)] - Downloading to [$($t.Item.DownloadPath)] (attempt $attempt/$maxRetryCount)"
194+
$currentProgressPreference = $ProgressPreference
195+
$ProgressPreference = 'SilentlyContinue'
196+
try {
197+
$bytes = $httpClient.GetByteArrayAsync($t.Item.URL).GetAwaiter().GetResult()
198+
} finally {
199+
$ProgressPreference = $currentProgressPreference
200+
}
201+
[System.IO.File]::WriteAllBytes($t.Item.DownloadPath, $bytes)
202+
try {
203+
[System.IO.File]::WriteAllBytes($t.Item.CachePath, $bytes)
204+
} catch {
205+
Write-Verbose "Cache write failed: $($_.Exception.Message)"
206+
}
207+
$downloadSucceeded = $true
188208
} catch {
189-
Write-Verbose "Cache write failed: $($_.Exception.Message)"
209+
if ($attempt -lt $maxRetryCount) {
210+
Write-Verbose "[$($t.Item.Name)] - Download attempt $attempt failed, retrying in $retryDelaySeconds seconds: $($_.Exception.Message)"
211+
Start-Sleep -Seconds $retryDelaySeconds
212+
continue
213+
}
214+
Write-Warning "[$($t.Item.Name)] - Download failed after $maxRetryCount attempts: $($_.Exception.Message)"
190215
}
191-
} catch {
192-
Write-Warning "[$($t.Item.Name)] - Download failed: $($_.Exception.Message)"
193216
}
194217
}
195218
}
196219

197220
foreach ($item in $pending) {
198221
if (-not (Test-Path -LiteralPath $item.DownloadPath)) { continue }
199222
Write-Verbose "[$($item.Name)] - Install to [$Scope]"
200-
if ($PSCmdlet.ShouldProcess("[$($item.Name)] to [$Scope]", 'Install font')) {
201-
Install-Font -Path $item.DownloadPath -Scope $Scope -Force:$Force
202-
Remove-Item -Path $item.DownloadPath -Force -Recurse
203-
}
223+
Install-Font -Path $item.DownloadPath -Scope $Scope -Force:$Force
224+
Remove-Item -Path $item.DownloadPath -Force -ErrorAction SilentlyContinue
204225
}
205226
} finally {
206227
$httpClient.Dispose()
@@ -210,8 +231,11 @@ Please run the command again with elevated rights (Run as Administrator) or prov
210231
}
211232

212233
clean {
213-
Remove-Item -Path $tempPath -Force
214-
if ($previousProgressPreference) {
234+
try {
235+
if ($tempPath -and (Test-Path -Path $tempPath -PathType Container)) {
236+
Remove-Item -Path $tempPath -Force -Recurse -ErrorAction SilentlyContinue
237+
}
238+
} finally {
215239
$ProgressPreference = $previousProgressPreference
216240
}
217241
}

tests/GoogleFonts.Tests.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@
2121

2222
It '[Install-GoogleFont] - Skips an already-installed font' {
2323
{ Install-GoogleFont -Name 'Akshar' } | Should -Not -Throw
24+
$verboseOutput = Install-GoogleFont -Name 'Akshar' -Verbose 4>&1
25+
($verboseOutput | Out-String) | Should -Match 'Installing \[0\] fonts'
2426
}
2527

2628
It '[Install-GoogleFont] - Supports wildcard names' {
2729
{ Install-GoogleFont -Name 'ABee*' } | Should -Not -Throw
30+
Get-Font -Name 'ABee*' | Should -Not -BeNullOrEmpty
2831
}
2932

3033
# It '[Install-GoogleFont] - Installs all fonts' {

0 commit comments

Comments
 (0)