Skip to content

Commit

Permalink
🩹 [Patch]: Remove dynamic param (#8)
Browse files Browse the repository at this point in the history
## Description

<!-- Add your description here -->

## Type of change

<!-- Use the check-boxes [x] on the options that are relevant. -->

- [ ] 📖 [Docs]
- [ ] 🪲 [Fix]
- [x] 🩹 [Patch]
- [ ] ⚠️ [Security fix]
- [ ] 🚀 [Feature]
- [ ] 🌟 [Breaking change]

## Checklist

<!-- Use the check-boxes [x] on the options that are relevant. -->

- [x] I have performed a self-review of my own code
- [x] I have commented my code, particularly in hard-to-understand areas
  • Loading branch information
MariusStorhaug committed Apr 4, 2024
1 parent 5dbd5bd commit 6dbf86f
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 51 deletions.
4 changes: 4 additions & 0 deletions src/private/Scope.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
enum Scope {
CurrentUser
AllUsers
}
14 changes: 6 additions & 8 deletions src/public/Get-NerdFont.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
.EXAMPLE
Get-NerdFonts -Name '*Code'
#>
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
'PSReviewUnusedParameter', 'Name', Justification = 'PSScriptAnalyzer false positive'
)]
[Alias('Get-NerdFonts')]
[CmdletBinding()]
param (
Expand All @@ -26,12 +23,13 @@

$release = Invoke-RestMethod "$script:NerdFontsReleaseURL/latest" -Verbose:$false
$version = $release.tag_name
$assets = $release.assets.browser_download_url | Where-Object { $_ -like '*.zip' -and $_ -like "$Name" } | Sort-Object
foreach ($asset in $assets) {
Write-Verbose "Latest release: $version"
Write-Verbose "Selecting assets by name: '$Name'"
$release.assets.browser_download_url | Where-Object { $_ -like '*.zip' } | ForEach-Object {
[pscustomobject]@{
Name = $asset.Split('/')[-1].Split('.')[0]
Name = $_.Split('/')[-1].Split('.')[0]
Version = $version
URL = $asset
URL = $_
}
}
} | Where-Object { $_.Name -like "$Name" }
}
90 changes: 50 additions & 40 deletions src/public/Install-NerdFont.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#Requires -Modules Fonts
#Requires -Modules Admin
#Requires -Modules Admin, Fonts, DynamicParams

function Install-NerdFont {
<#
Expand All @@ -25,46 +24,38 @@ function Install-NerdFont {
Installs all Nerd Fonts to the current user.
#>
[CmdletBinding(
DefaultParameterSetName = 'Name'
DefaultParameterSetName = 'Name',
SupportsShouldProcess
)]
[Alias('Install-NerdFonts')]
param(
[Parameter(
Mandatory,
Position = 0,
ParameterSetName = 'All'
)]
# Specify to install all Nerd Font(s).
[Parameter(ParameterSetName = 'All', Mandatory)]
[switch] $All,

[Parameter(
Position = 1,
ParameterSetName = '__AllParameterSets'
)]
# Specify the scope of where to install the font(s).
[Parameter()]
[ValidateSet('CurrentUser', 'AllUsers')]
[string] $Scope = 'CurrentUser'
)

DynamicParam {
$runtimeDefinedParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
$attributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]

$parameterName = 'Name'
$parameterAttribute = New-Object System.Management.Automation.ParameterAttribute
$parameterAttribute.Mandatory = $true
$parameterAttribute.ParameterSetName = 'Name'
$parameterAttribute.Position = 0
$parameterAttribute.HelpMessage = 'Name of the font to uninstall.'
$parameterAttribute.ValueFromPipeline = $true
$parameterAttribute.ValueFromPipelineByPropertyName = $true
$attributeCollection.Add($parameterAttribute)

$parameterValidateSet = (Get-NerdFonts).Name
$validateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($parameterValidateSet)
$attributeCollection.Add($validateSetAttribute)

$runtimeDefinedParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($parameterName, [string[]], $attributeCollection)
$runtimeDefinedParameterDictionary.Add($parameterName, $runtimeDefinedParameter)
return $runtimeDefinedParameterDictionary
$DynamicParamDictionary = New-DynamicParamDictionary

$dynPath = @{
Name = 'Name'
Type = [string[]]
Mandatory = $true
ParameterSetName = 'Name'
HelpMessage = 'Name of the font to uninstall.'
ValueFromPipeline = $true
ValueFromPipelineByPropertyName = $true
ValidateSet = Get-NerdFonts | Select-Object -ExpandProperty Name
DynamicParamDictionary = $DynamicParamDictionary
}
New-DynamicParam @dynPath

return $DynamicParamDictionary
}

begin {
Expand All @@ -77,6 +68,14 @@ Please run the command again with elevated rights (Run as Administrator) or prov
}
$NerdFonts = Get-NerdFonts
$NerdFontsToInstall = @()

$tempPath = Join-Path -Path $HOME -ChildPath '.temp'
if (-not (Test-Path -Path $tempPath -PathType Container)) {
Write-Verbose "Create folder [$tempPath]"
$null = New-Item -Path $tempPath -ItemType Directory
$tempFolderCreated = $true
}

$Name = $PSBoundParameters.Name
}

Expand All @@ -94,24 +93,35 @@ Please run the command again with elevated rights (Run as Administrator) or prov
foreach ($NerdFont in $NerdFontsToInstall) {
$URL = $NerdFont.URL
$FontName = $NerdFont.Name
$downloadPath = "$env:TEMP\$FontName.zip"
$extractPath = "$env:TEMP\$FontName"
$downloadPath = Join-Path -Path $tempPath -ChildPath "$FontName.zip"
$extractPath = Join-Path -Path $tempPath -ChildPath "$FontName"

Write-Verbose "[$FontName] - Downloading to [$downloadPath]"
$storedProgressPreference = $ProgressPreference
$ProgressPreference = 'SilentlyContinue' # Suppress progress bar
Invoke-WebRequest -Uri $URL -OutFile $downloadPath -Verbose:$false
if ($PSCmdlet.ShouldProcess($FontName, "Download $FontName")) {
Invoke-WebRequest -Uri $URL -OutFile $downloadPath -Verbose:$false
}
$ProgressPreference = $storedProgressPreference

Write-Verbose "[$FontName] - Unpack to [$extractPath]"
Expand-Archive -Path $downloadPath -DestinationPath $extractPath -Force
Remove-Item -Path $downloadPath -Force
if ($PSCmdlet.ShouldProcess($FontName, 'Extract archive')) {
Expand-Archive -Path $downloadPath -DestinationPath $extractPath -Force
Remove-Item -Path $downloadPath -Force
}

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

end {}
end {
if ($tempFolderCreated) {
Write-Verbose "Remove folder [$tempPath]"
Remove-Item -Path $tempPath -Force
}
}
}
27 changes: 24 additions & 3 deletions tests/NerdFonts.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ Param(

Write-Verbose "Path to the module: [$Path]" -Verbose

Describe 'NerdFonts' {
Context 'Module' {
Describe 'Module' {
Context 'NerdFonts' {
It 'The module should be available' {
Get-Module -Name 'NerdFonts' -ListAvailable | Should -Not -BeNullOrEmpty
Write-Verbose (Get-Module -Name 'NerdFonts' -ListAvailable | Out-String) -Verbose
Expand All @@ -18,7 +18,7 @@ Describe 'NerdFonts' {
}
}

Context 'Get-NerdFont' {
Context 'Function: Get-NerdFont' {
It 'Function exists' {
Get-Command Get-NerdFont | Should -Not -BeNullOrEmpty
}
Expand All @@ -28,6 +28,27 @@ Describe 'NerdFonts' {
Write-Verbose ($fonts | Out-String) -Verbose
$fonts | Should -Not -BeNullOrEmpty
}

It 'Returns a specific font' {
$font = Get-NerdFont -Name 'Tinos'
Write-Verbose ($font | Out-String) -Verbose
$font | Should -Not -BeNullOrEmpty
}
}

Context 'Function: Install-NerdFont' {
It '[Install-NerdFont] - Exists' {
Get-Command -Name Install-NerdFont | Should -Not -BeNullOrEmpty
}

It '[Install-NerdFont] - Installs a font' {
{ Install-NerdFont -Name 'Tinos' } | Should -Not -Throw
Get-Font -Name 'Tinos' | Should -Not -BeNullOrEmpty
}

It '[Install-NerdFont] - Installs all fonts' {
{ Install-NerdFont -All -Verbose } | Should -Not -Throw
Get-Font -Name 'VictorMono' | Should -Not -BeNullOrEmpty
}
}
}

0 comments on commit 6dbf86f

Please sign in to comment.