Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 45 additions & 40 deletions src/functions/public/Install-NerdFont.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#Requires -Modules @{ ModuleName = 'Fonts'; RequiredVersion = '1.1.21' }
#Requires -Modules @{ ModuleName = 'Admin'; RequiredVersion = '1.1.6' }
#Requires -PSEdition Core
#Requires -Modules @{ ModuleName = 'Admin'; ModuleVersion = '1.1.6'; GUID = '70660c5c-30db-4787-861e-0a626ca8683c' }
#Requires -Modules @{ ModuleName = 'Fonts'; ModuleVersion = '1.1.21'; GUID = 'b6e7e61f-f8f5-4b0a-9fdd-f74a3311be0d' }

Set-StrictMode -Version 3.0

function Install-NerdFont {
<#
Expand Down Expand Up @@ -57,77 +60,79 @@ function Install-NerdFont {
ParameterSetName = 'All',
Mandatory
)]
[switch] $All,
[switch] ${All},

[Parameter()]
[ValidateSet('CurrentUser', 'AllUsers')]
[string] $Scope = 'CurrentUser',
[string] ${Scope} = 'CurrentUser',

# Force will overwrite existing fonts
[Parameter()]
[switch] $Force
[switch] ${Force}
)

begin {
if ($Scope -eq 'AllUsers' -and -not (IsAdmin)) {
$errorMessage = @'
Administrator rights are required to install fonts.
Please run the command again with elevated rights (Run as Administrator) or provide '-Scope CurrentUser' to your command."
if (${Scope} -eq 'AllUsers' -and -not (IsAdmin)) {
${errorMessage} = @'
Administrator privileges are required to install system-wide fonts.

Please run this command again with elevated permissions ("Run as Administrator") or append '-Scope CurrentUser' to it for a user-level install.
'@
throw $errorMessage
throw ${errorMessage}
}
$nerdFontsToInstall = @()
${nerdFontsToInstall} = @()

$guid = (New-Guid).Guid
$tempPath = Join-Path -Path $HOME -ChildPath "NerdFonts-$guid"
if (-not (Test-Path -Path $tempPath -PathType Container)) {
Write-Verbose "Create folder [$tempPath]"
$null = New-Item -Path $tempPath -ItemType Directory
${tempPath} = Join-Path -Path ${Env:TEMP} -ChildPath "NerdFonts-$([Convert]::ToString((Get-Random 16777216),16).PadLeft(6,'0'))"
if (-not (Test-Path -Path ${tempPath} -PathType Container)) {
Write-Verbose "Creating temporary download directory [${tempPath}]…"
${null} = New-Item -Path ${tempPath} -ItemType Directory
}
}

process {
if ($All) {
$nerdFontsToInstall = $script:NerdFonts
if (${All}) {
${nerdFontsToInstall} = ${script:NerdFonts}
} else {
foreach ($fontName in $Name) {
$nerdFontsToInstall += $script:NerdFonts | Where-Object { $_.Name -like $fontName }
foreach (${fontName} in ${Name}) {
${nerdFontsToInstall} += ${script:NerdFonts} | Where-Object {
$_.Name -like ${fontName}
}
}
}

Write-Verbose "[$Scope] - Installing [$($nerdFontsToInstall.count)] fonts"
Write-Verbose "[${Scope}] - Installing [$(${nerdFontsToInstall}.count)] fonts"

foreach ($nerdFont in $nerdFontsToInstall) {
$URL = $nerdFont.URL
$fontName = $nerdFont.Name
$downloadFileName = Split-Path -Path $URL -Leaf
$downloadPath = Join-Path -Path $tempPath -ChildPath $downloadFileName
foreach (${nerdFont} in ${nerdFontsToInstall}) {
${URL} = ${nerdFont}.URL
${fontName} = ${nerdFont}.Name
${downloadFileName} = Split-Path -Path ${URL} -Leaf
${downloadPath} = Join-Path -Path ${tempPath} -ChildPath ${downloadFileName}

Write-Verbose "[$fontName] - Downloading to [$downloadPath]"
if ($PSCmdlet.ShouldProcess("[$fontName] to [$downloadPath]", 'Download')) {
Invoke-WebRequest -Uri $URL -OutFile $downloadPath -RetryIntervalSec 5 -MaximumRetryCount 5
Write-Verbose "[${fontName}] - Downloading to [${downloadPath}]…"
if (${PSCmdlet}.ShouldProcess("[${fontName}] to [${downloadPath}]", 'Download')) {
Invoke-WebRequest -Uri ${URL} -OutFile ${downloadPath} -RetryIntervalSec 5 -MaximumRetryCount 5
}

$extractPath = Join-Path -Path $tempPath -ChildPath $fontName
Write-Verbose "[$fontName] - Extract to [$extractPath]"
if ($PSCmdlet.ShouldProcess("[$fontName] to [$extractPath]", 'Extract')) {
Expand-Archive -Path $downloadPath -DestinationPath $extractPath -Force
Remove-Item -Path $downloadPath -Force
${extractPath} = Join-Path -Path ${tempPath} -ChildPath ${fontName}
Write-Verbose "[${fontName}] - Extracting archive to [${extractPath}]…"
if (${PSCmdlet}.ShouldProcess("[${fontName}] to [${extractPath}]", 'Extract')) {
Expand-Archive -Path ${downloadPath} -DestinationPath ${extractPath} -Force
Remove-Item -Path ${downloadPath} -Force
}

Write-Verbose "[$fontName] - Install to [$Scope]"
if ($PSCmdlet.ShouldProcess("[$fontName] to [$Scope]", 'Install font')) {
Install-Font -Path $extractPath -Scope $Scope -Force:$Force
Remove-Item -Path $extractPath -Force -Recurse
Write-Verbose "[${fontName}] - Installing for [${Scope}]…"
if (${PSCmdlet}.ShouldProcess("[${fontName}] to [${Scope}]", 'Install font')) {
Install-Font -Path ${extractPath} -Scope ${Scope} -Force:${Force}
Remove-Item -Path ${extractPath} -Force -Recurse
}
}
}

end {
Write-Verbose "Remove folder [$tempPath]"
Write-Verbose "Removing temporary download directory [${tempPath}]…"
}

clean {
Remove-Item -Path $tempPath -Force
Remove-Item -Path ${tempPath} -Force
}
}