Private/Install-Font.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
function Install-Font {
  <#
  .SYNOPSIS
    Download a font from a GitHub release.
  .DESCRIPTION
    Without the requirement of Git installed:, this function can be leveraged to:
      1. Download a font from a GitHub release to a temporary directory
      2. Extract the font to a temporary directory
      3. Install the font
      4. Clean up the temporary directories
  .LINK
    https://github.com/powerline/fonts/blob/master/install.ps1
    https://gist.github.com/f3l3gy/0e89dde158dde024959e36e915abf6bd
  .PARAMETER FontName
    Name of the font to install (ex: 'CascadiaCodePL')
  .EXAMPLE
    Install-Font -FontName 'CascadiaCodePL'
  #>

  [CmdletBinding()]
  param (
    [System.IO.FileInfo]$FontName = 'CascadiaCodePL',
    [string]$Repo = 'microsoft/cascadia-code'
  )
  $font_file = "$env:windir\fonts\$FontName.ttf"
  $release = "https://github.com/$Repo/releases/latest"

  if (Test-Path $font_file) {
    return
  }

  try {
    $tags = (Invoke-WebRequest -Uri $release -UseBasicParsing)
    $release_path = ($tags.Links -match "/releases/download/").href
    $uri = "https://github.com$release_path"
    Invoke-WebRequest $uri -OutFile "$env:TEMP/cascadia-code.zip"
  }
  catch {
    throw "Failed to download [$FontName] from [$download]: $_"
  }

  try {
    Expand-Archive "$env:TEMP/cascadia-code.zip"  -DestinationPath "$env:TEMP/cascadia-code-unzipped" -Force
  }
  catch {
    throw "Failed to unzip [$FontName]: $_"
  }

  Write-Verbose "Adding $FontName to Windows fonts"
  $fonts = (New-Object -ComObject shell.application).NameSpace(0x14)
  $fonts.CopyHere($font_file)

  Remove-Item @("$env:TEMP/cascadia-code.zip", "$env:TEMP/cascadia-code-unzipped") -Recurse -Force -ErrorAction SilentlyContinue
}