Skip to content

Commit 399643f

Browse files
committed
refactor(scripts): add support for ShouldProcess
1 parent b89cfcb commit 399643f

1 file changed

Lines changed: 22 additions & 12 deletions

File tree

src/automation/Install-FontsFromBlob.ps1

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Using module ..\common\Blob.psm1
2626
.\Install-FontsFromBlob.ps1 -StorageBlobUrl 'https://myaccount.blob.core.windows.net/fonts' -StorageBlobSasToken 'sv=2021-06-08&ss=b&srt=co&sp=rl&se=...'
2727
#>
2828

29-
[CmdletBinding()]
29+
[CmdletBinding(SupportsShouldProcess)]
3030
param(
3131
[Parameter(Mandatory)]
3232
[ValidateNotNullOrEmpty()]
@@ -207,18 +207,25 @@ function Get-FontDisplayName {
207207
}
208208

209209
function Install-Font {
210-
[CmdletBinding()]
210+
[CmdletBinding(SupportsShouldProcess)]
211+
[OutputType([Boolean])]
211212
param(
212213
[Parameter(Mandatory)]
213214
[System.IO.FileInfo]$FontFile
214215
)
215216

216217
begin { Enter-Scope; }
217-
end { Exit-Scope; }
218+
end { Exit-Scope -ReturnValue $Local:Installed; }
218219

219220
process {
220221
[String]$Local:FileName = $FontFile.Name;
221222
[String]$Local:DestinationPath = $Script:FontsFolder | Join-Path -ChildPath $Local:FileName;
223+
[String]$Local:DisplayName = Get-FontDisplayName -FontFile:$FontFile;
224+
[Boolean]$Local:Installed = $false;
225+
226+
if (-not $PSCmdlet.ShouldProcess($Local:DisplayName, 'Install font')) {
227+
return $false;
228+
}
222229

223230
Invoke-Info "Installing font: $Local:FileName";
224231

@@ -229,9 +236,6 @@ function Install-Font {
229236
Invoke-FailedExit -ErrorRecord $_ -ExitCode $Script:ERROR_FONT_COPY_FAILED -FormatArgs @($Local:FileName);
230237
}
231238

232-
# Get display name for registry
233-
[String]$Local:DisplayName = Get-FontDisplayName -FontFile:$FontFile;
234-
235239
# Register in registry
236240
try {
237241
Set-RegistryKey -Path $Script:FontsRegistryPath -Key $Local:DisplayName -Value $Local:FileName -Kind String;
@@ -247,6 +251,8 @@ function Install-Font {
247251
}
248252

249253
Invoke-Info "Installed font: $Local:DisplayName";
254+
$Local:Installed = $true;
255+
return $true;
250256
}
251257
}
252258

@@ -269,7 +275,10 @@ function Send-FontChangeNotification {
269275
}
270276

271277
Invoke-RunMain $PSCmdlet {
272-
Invoke-EnsureAdministrator;
278+
# Only require admin when actually installing (not in WhatIf mode)
279+
if (-not $WhatIfPreference) {
280+
Invoke-EnsureAdministrator;
281+
}
273282

274283
# Register exit codes
275284
$Script:ERROR_BLOB_DOWNLOAD_FAILED = Register-ExitCode -Description 'Failed to download blob {0}';
@@ -320,13 +329,14 @@ Invoke-RunMain $PSCmdlet {
320329
# Get font file (from cache or download)
321330
[System.IO.FileInfo]$Local:FontFile = Get-FontFile -BlobName:$Local:BlobName -ContainerUrl:$StorageBlobUrl -SasQueryString:$Local:SasQueryString;
322331

323-
# Install the font
324-
Install-Font -FontFile:$Local:FontFile;
325-
$Local:InstalledCount++;
332+
# Install the font (returns $false if WhatIf)
333+
if (Install-Font -FontFile:$Local:FontFile) {
334+
$Local:InstalledCount++;
335+
}
326336
}
327337

328-
# Broadcast font change if any fonts were installed
329-
if ($Local:InstalledCount -gt 0) {
338+
# Broadcast font change if any fonts were installed (skip in WhatIf mode)
339+
if ($Local:InstalledCount -gt 0 -and -not $WhatIfPreference) {
330340
Send-FontChangeNotification;
331341
}
332342

0 commit comments

Comments
 (0)