Skip to content

Commit 729b884

Browse files
Remove empty try/finally wrapper in Install-GoogleFont per review feedback
1 parent 18785dc commit 729b884

1 file changed

Lines changed: 117 additions & 119 deletions

File tree

src/functions/public/Install-GoogleFont.ps1

Lines changed: 117 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -147,118 +147,80 @@ Please run the command again with elevated rights (Run as Administrator) or prov
147147
$maxRetryCount = 5
148148
$retryDelaySeconds = 5
149149
$downloadFailures = [System.Collections.Generic.List[string]]::new()
150-
try {
151-
$pending = [System.Collections.Generic.List[object]]::new()
152-
foreach ($googleFont in $googleFontsToInstall) {
153-
$URL = $googleFont.URL
154-
$fontName = $googleFont.Name
155-
if (-not $PSCmdlet.ShouldProcess("[$fontName] to [$Scope]", 'Install font')) {
156-
continue
157-
}
158-
$fontVariant = $googleFont.Variant
159-
$fileExtension = $URL.Split('.')[-1]
160-
$downloadFileName = "$fontName-$fontVariant.$fileExtension"
161-
$downloadPath = Join-Path -Path $tempPath -ChildPath $downloadFileName
162-
$sha256 = [System.Security.Cryptography.SHA256]::Create()
163-
try {
164-
$hashBytes = $sha256.ComputeHash([System.Text.Encoding]::UTF8.GetBytes($URL))
165-
} finally {
166-
$sha256.Dispose()
167-
}
168-
$urlHash = ([System.BitConverter]::ToString($hashBytes)).Replace('-', '').ToLowerInvariant().Substring(0, 16)
169-
$safeDownloadFileName = ($downloadFileName -replace '[^a-zA-Z0-9._-]', '_')
170-
$cachePath = Join-Path -Path $cacheRoot -ChildPath "$urlHash-$safeDownloadFileName"
171-
172-
$pending.Add([pscustomobject]@{
173-
Name = $fontName
174-
URL = $URL
175-
DownloadPath = $downloadPath
176-
CachePath = $cachePath
177-
FromCache = (-not $Force) -and (Test-Path -LiteralPath $cachePath)
178-
})
179-
}
180-
181-
if ($pending.Count -eq 0) {
182-
return
183-
}
184150

185-
if (-not (Test-Path -Path $tempPath -PathType Container)) {
186-
Write-Verbose "Create folder [$tempPath]"
187-
$null = New-Item -Path $tempPath -ItemType Directory
188-
}
189-
if (-not (Test-Path -Path $cacheRoot -PathType Container)) {
190-
$null = New-Item -Path $cacheRoot -ItemType Directory -Force
151+
$pending = [System.Collections.Generic.List[object]]::new()
152+
foreach ($googleFont in $googleFontsToInstall) {
153+
$URL = $googleFont.URL
154+
$fontName = $googleFont.Name
155+
if (-not $PSCmdlet.ShouldProcess("[$fontName] to [$Scope]", 'Install font')) {
156+
continue
191157
}
192-
193-
foreach ($item in $pending) {
194-
if ($item.FromCache) {
195-
try {
196-
Write-Verbose "[$($item.Name)] - Cache hit, copying from [$($item.CachePath)]"
197-
Copy-Item -LiteralPath $item.CachePath -Destination $item.DownloadPath -Force
198-
} catch {
199-
Write-Verbose "[$($item.Name)] - Cache copy failed, will download instead: $($_.Exception.Message)"
200-
$item.FromCache = $false
201-
}
202-
}
158+
$fontVariant = $googleFont.Variant
159+
$fileExtension = $URL.Split('.')[-1]
160+
$downloadFileName = "$fontName-$fontVariant.$fileExtension"
161+
$downloadPath = Join-Path -Path $tempPath -ChildPath $downloadFileName
162+
$sha256 = [System.Security.Cryptography.SHA256]::Create()
163+
try {
164+
$hashBytes = $sha256.ComputeHash([System.Text.Encoding]::UTF8.GetBytes($URL))
165+
} finally {
166+
$sha256.Dispose()
203167
}
168+
$urlHash = ([System.BitConverter]::ToString($hashBytes)).Replace('-', '').ToLowerInvariant().Substring(0, 16)
169+
$safeDownloadFileName = ($downloadFileName -replace '[^a-zA-Z0-9._-]', '_')
170+
$cachePath = Join-Path -Path $cacheRoot -ChildPath "$urlHash-$safeDownloadFileName"
171+
172+
$pending.Add([pscustomobject]@{
173+
Name = $fontName
174+
URL = $URL
175+
DownloadPath = $downloadPath
176+
CachePath = $cachePath
177+
FromCache = (-not $Force) -and (Test-Path -LiteralPath $cachePath)
178+
})
179+
}
204180

205-
$toDownload = @($pending | Where-Object { -not $_.FromCache })
206-
if ($toDownload.Count -gt 0) {
207-
$disableParallelDownloads = (
208-
$script:DisableParallelDownloadsForTests -eq $true -or
209-
$env:PSMODULE_GOOGLEFONTS_DISABLE_PARALLEL -eq '1'
210-
)
211-
$useParallelDownloads = (
212-
$PSVersionTable.PSVersion.Major -ge 7 -and
213-
-not $disableParallelDownloads
214-
)
181+
if ($pending.Count -eq 0) {
182+
return
183+
}
215184

216-
if ($useParallelDownloads) {
217-
$downloadResults = @(
218-
$toDownload | ForEach-Object -Parallel {
219-
$item = $_
220-
$downloadSucceeded = $false
221-
$lastError = $null
185+
if (-not (Test-Path -Path $tempPath -PathType Container)) {
186+
Write-Verbose "Create folder [$tempPath]"
187+
$null = New-Item -Path $tempPath -ItemType Directory
188+
}
189+
if (-not (Test-Path -Path $cacheRoot -PathType Container)) {
190+
$null = New-Item -Path $cacheRoot -ItemType Directory -Force
191+
}
222192

223-
for ($attempt = 1; $attempt -le $using:maxRetryCount -and -not $downloadSucceeded; $attempt++) {
224-
try {
225-
$currentProgressPreference = $ProgressPreference
226-
$ProgressPreference = 'SilentlyContinue'
227-
try {
228-
Invoke-WebRequest -Uri $item.URL -OutFile $item.DownloadPath
229-
} finally {
230-
$ProgressPreference = $currentProgressPreference
231-
}
232-
233-
try {
234-
Copy-Item -LiteralPath $item.DownloadPath -Destination $item.CachePath -Force
235-
} catch {
236-
Write-Verbose "[$($item.Name)] - Cache write failed: $($_.Exception.Message)"
237-
}
238-
239-
$downloadSucceeded = $true
240-
} catch {
241-
$lastError = $_.Exception.Message
242-
if ($attempt -lt $using:maxRetryCount) {
243-
Start-Sleep -Seconds $using:retryDelaySeconds
244-
}
245-
}
246-
}
193+
foreach ($item in $pending) {
194+
if ($item.FromCache) {
195+
try {
196+
Write-Verbose "[$($item.Name)] - Cache hit, copying from [$($item.CachePath)]"
197+
Copy-Item -LiteralPath $item.CachePath -Destination $item.DownloadPath -Force
198+
} catch {
199+
Write-Verbose "[$($item.Name)] - Cache copy failed, will download instead: $($_.Exception.Message)"
200+
$item.FromCache = $false
201+
}
202+
}
203+
}
247204

248-
[pscustomobject]@{
249-
Name = $item.Name
250-
URL = $item.URL
251-
Success = $downloadSucceeded
252-
Error = $lastError
253-
}
254-
} -ThrottleLimit $throttle
255-
)
256-
} else {
257-
$downloadResults = foreach ($item in $toDownload) {
205+
$toDownload = @($pending | Where-Object { -not $_.FromCache })
206+
if ($toDownload.Count -gt 0) {
207+
$disableParallelDownloads = (
208+
$script:DisableParallelDownloadsForTests -eq $true -or
209+
$env:PSMODULE_GOOGLEFONTS_DISABLE_PARALLEL -eq '1'
210+
)
211+
$useParallelDownloads = (
212+
$PSVersionTable.PSVersion.Major -ge 7 -and
213+
-not $disableParallelDownloads
214+
)
215+
216+
if ($useParallelDownloads) {
217+
$downloadResults = @(
218+
$toDownload | ForEach-Object -Parallel {
219+
$item = $_
258220
$downloadSucceeded = $false
259221
$lastError = $null
260222

261-
for ($attempt = 1; $attempt -le $maxRetryCount -and -not $downloadSucceeded; $attempt++) {
223+
for ($attempt = 1; $attempt -le $using:maxRetryCount -and -not $downloadSucceeded; $attempt++) {
262224
try {
263225
$currentProgressPreference = $ProgressPreference
264226
$ProgressPreference = 'SilentlyContinue'
@@ -277,8 +239,8 @@ Please run the command again with elevated rights (Run as Administrator) or prov
277239
$downloadSucceeded = $true
278240
} catch {
279241
$lastError = $_.Exception.Message
280-
if ($attempt -lt $maxRetryCount) {
281-
Start-Sleep -Seconds $retryDelaySeconds
242+
if ($attempt -lt $using:maxRetryCount) {
243+
Start-Sleep -Seconds $using:retryDelaySeconds
282244
}
283245
}
284246
}
@@ -289,29 +251,65 @@ Please run the command again with elevated rights (Run as Administrator) or prov
289251
Success = $downloadSucceeded
290252
Error = $lastError
291253
}
254+
} -ThrottleLimit $throttle
255+
)
256+
} else {
257+
$downloadResults = foreach ($item in $toDownload) {
258+
$downloadSucceeded = $false
259+
$lastError = $null
260+
261+
for ($attempt = 1; $attempt -le $maxRetryCount -and -not $downloadSucceeded; $attempt++) {
262+
try {
263+
$currentProgressPreference = $ProgressPreference
264+
$ProgressPreference = 'SilentlyContinue'
265+
try {
266+
Invoke-WebRequest -Uri $item.URL -OutFile $item.DownloadPath
267+
} finally {
268+
$ProgressPreference = $currentProgressPreference
269+
}
270+
271+
try {
272+
Copy-Item -LiteralPath $item.DownloadPath -Destination $item.CachePath -Force
273+
} catch {
274+
Write-Verbose "[$($item.Name)] - Cache write failed: $($_.Exception.Message)"
275+
}
276+
277+
$downloadSucceeded = $true
278+
} catch {
279+
$lastError = $_.Exception.Message
280+
if ($attempt -lt $maxRetryCount) {
281+
Start-Sleep -Seconds $retryDelaySeconds
282+
}
283+
}
292284
}
293-
}
294285

295-
foreach ($result in $downloadResults) {
296-
if (-not $result.Success) {
297-
$downloadFailures.Add("$($result.Name): $($result.Error)")
298-
Write-Warning "[$($result.Name)] - Download failed after $maxRetryCount attempts: $($result.Error)"
286+
[pscustomobject]@{
287+
Name = $item.Name
288+
URL = $item.URL
289+
Success = $downloadSucceeded
290+
Error = $lastError
299291
}
300292
}
301293
}
302294

303-
foreach ($item in $pending) {
304-
if (-not (Test-Path -LiteralPath $item.DownloadPath)) { continue }
305-
Write-Verbose "[$($item.Name)] - Install to [$Scope]"
306-
Install-Font -Path $item.DownloadPath -Scope $Scope -Force:$Force
307-
Remove-Item -Path $item.DownloadPath -Force -ErrorAction SilentlyContinue
295+
foreach ($result in $downloadResults) {
296+
if (-not $result.Success) {
297+
$downloadFailures.Add("$($result.Name): $($result.Error)")
298+
Write-Warning "[$($result.Name)] - Download failed after $maxRetryCount attempts: $($result.Error)"
299+
}
308300
}
301+
}
309302

310-
if ($downloadFailures.Count -gt 0) {
311-
$failureSummary = $downloadFailures -join '; '
312-
throw "One or more font downloads failed: $failureSummary"
313-
}
314-
} finally {
303+
foreach ($item in $pending) {
304+
if (-not (Test-Path -LiteralPath $item.DownloadPath)) { continue }
305+
Write-Verbose "[$($item.Name)] - Install to [$Scope]"
306+
Install-Font -Path $item.DownloadPath -Scope $Scope -Force:$Force
307+
Remove-Item -Path $item.DownloadPath -Force -ErrorAction SilentlyContinue
308+
}
309+
310+
if ($downloadFailures.Count -gt 0) {
311+
$failureSummary = $downloadFailures -join '; '
312+
throw "One or more font downloads failed: $failureSummary"
315313
}
316314
}
317315

0 commit comments

Comments
 (0)