forked from szymonos/vagrant-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nerd_fonts_install.ps1
65 lines (55 loc) · 1.73 KB
/
nerd_fonts_install.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#Requires -RunAsAdministrator
<#
.SYNOPSIS
Install specified nerd font from ryanoasis/nerd-fonts GitHub repo.
.LINK
https://github.com/ryanoasis/nerd-fonts
.PARAMETER Font
Name of the nerd font to be installed.
.PARAMETER FontExt
Specified font extension.
.EXAMPLE
$Font = 'FiraCode'
.assets/scripts/nerd_fonts_install.ps1 $Font
# :check installed fonts
[Drawing.Text.InstalledFontCollection]::new().Families | Select-String $Font
#>
[CmdletBinding()]
param (
[Parameter(Mandatory, Position = 0)]
[string]$Font,
[ValidateSet('ttf', 'otf')]
[string]$FontExt = 'otf'
)
begin {
$ErrorActionPreference = 'Stop'
$tmp = New-Item "tmp.$(Get-Random)" -ItemType Directory
$fontArchive = [IO.Path]::Combine($tmp, "${Font}.zip")
}
process {
# download font
try {
$uri = "https://github.com/ryanoasis/nerd-fonts/releases/latest/download/${Font}.zip"
[Net.WebClient]::new().DownloadFile($uri, $fontArchive)
} catch {
Write-Warning "Font not found ($Font)."
exit 1
}
Expand-Archive $fontArchive -DestinationPath $tmp
# filter fonts by type
if (-not $($fontFiles = Get-ChildItem $tmp -Filter "*.$FontExt" -File -Recurse)) {
$otherFontExt = $('ttf', 'otf') -ne $FontExt
$fontFiles = Get-ChildItem $tmp -Filter "*.$otherFontExt" -File -Recurse
}
# use only Windows Compatible fonts if available
if ($fontFiles.BaseName -match 'Windows Compatible$') {
$fontFiles = $fontFiles | Where-Object BaseName -Match 'Windows Compatible$'
}
# install fonts
$shellApp = New-Object -ComObject shell.application
$fonts = $shellApp.NameSpace(0x14)
$fontFiles.ForEach({ $fonts.CopyHere($_.FullName) })
}
end {
Remove-Item $tmp -Recurse -Force
}