@@ -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 }
0 commit comments