From 6dbf86f17ce4977cb9ffcd0d1bac0adf9a7e8910 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Thu, 4 Apr 2024 14:06:44 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=A9=B9=20[Patch]:=20Remove=20dynamic=20pa?= =?UTF-8?q?ram=20(#8)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Description ## Type of change - [ ] 📖 [Docs] - [ ] 🪲 [Fix] - [x] 🩹 [Patch] - [ ] ⚠️ [Security fix] - [ ] 🚀 [Feature] - [ ] 🌟 [Breaking change] ## Checklist - [x] I have performed a self-review of my own code - [x] I have commented my code, particularly in hard-to-understand areas --- src/private/Scope.ps1 | 4 ++ src/public/Get-NerdFont.ps1 | 14 +++-- src/public/Install-NerdFont.ps1 | 90 ++++++++++++++++++--------------- tests/NerdFonts.Tests.ps1 | 27 ++++++++-- 4 files changed, 84 insertions(+), 51 deletions(-) create mode 100644 src/private/Scope.ps1 diff --git a/src/private/Scope.ps1 b/src/private/Scope.ps1 new file mode 100644 index 0000000..3817570 --- /dev/null +++ b/src/private/Scope.ps1 @@ -0,0 +1,4 @@ +enum Scope { + CurrentUser + AllUsers +} diff --git a/src/public/Get-NerdFont.ps1 b/src/public/Get-NerdFont.ps1 index 14604ff..58c138e 100644 --- a/src/public/Get-NerdFont.ps1 +++ b/src/public/Get-NerdFont.ps1 @@ -12,9 +12,6 @@ .EXAMPLE Get-NerdFonts -Name '*Code' #> - [Diagnostics.CodeAnalysis.SuppressMessageAttribute( - 'PSReviewUnusedParameter', 'Name', Justification = 'PSScriptAnalyzer false positive' - )] [Alias('Get-NerdFonts')] [CmdletBinding()] param ( @@ -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" } } diff --git a/src/public/Install-NerdFont.ps1 b/src/public/Install-NerdFont.ps1 index a90343a..15c239d 100644 --- a/src/public/Install-NerdFont.ps1 +++ b/src/public/Install-NerdFont.ps1 @@ -1,5 +1,4 @@ -#Requires -Modules Fonts -#Requires -Modules Admin +#Requires -Modules Admin, Fonts, DynamicParams function Install-NerdFont { <# @@ -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 { @@ -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 } @@ -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 + } + } } diff --git a/tests/NerdFonts.Tests.ps1 b/tests/NerdFonts.Tests.ps1 index ebb95bb..b267ec8 100644 --- a/tests/NerdFonts.Tests.ps1 +++ b/tests/NerdFonts.Tests.ps1 @@ -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 @@ -18,7 +18,7 @@ Describe 'NerdFonts' { } } - Context 'Get-NerdFont' { + Context 'Function: Get-NerdFont' { It 'Function exists' { Get-Command Get-NerdFont | Should -Not -BeNullOrEmpty } @@ -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 + } + } }