Skip to content

Commit fc4824c

Browse files
🩹 [Patch]: Use DynamicParams module (#9)
## Description - Use `DynamicParams` module. - Add tests. - Remove header.ps1 file. - Remove remnants from attempting to support Linux and macOS. ## 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
1 parent f246348 commit fc4824c

7 files changed

Lines changed: 98 additions & 50 deletions

File tree

.github/workflows/Process-PSModule.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,5 @@ jobs:
2626
Process-PSModule:
2727
uses: PSModule/Process-PSModule/.github/workflows/workflow.yml@v1
2828
secrets: inherit
29+
with:
30+
SkipTests: Linux, macOS

src/header.ps1

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/public/Add-EnvironmentPath.ps1

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ function Add-EnvironmentPath {
5757
)
5858

5959
begin {
60-
$separatorChar = [IO.Path]::DirectorySeparatorChar
60+
$separatorChar = [System.IO.Path]::DirectorySeparatorChar
6161

6262
$target = if ($Scope -eq 'CurrentUser') {
6363
[System.EnvironmentVariableTarget]::User
@@ -110,12 +110,10 @@ Please run the command again with elevated rights (Run as Administrator) or prov
110110
}
111111

112112
end {
113-
if ($IsWindows) {
114-
$pathSeparator = ';'
115-
} else {
116-
$pathSeparator = ':'
117-
}
113+
$pathSeparator = ';'
118114
$environmentPath = $environmentPath -join $pathSeparator
115+
$environmentPath = $environmentPath.Trim($pathSeparator)
116+
$environmentPath = $environmentPath + $pathSeparator
119117

120118
[System.Environment]::SetEnvironmentVariable('PATH', $environmentPath, [System.EnvironmentVariableTarget]::$target)
121119
Write-Verbose "Add PATH - [$target] - Done"

src/public/Get-EnvironmentPath.ps1

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,8 @@
4040
if (-not $AsArray) {
4141
return $environmentPath
4242
}
43-
if ([System.Environment]::OSVersion.Platform -eq 'Win32NT') {
44-
$pathSeparator = ';'
45-
} else {
46-
$pathSeparator = ':'
47-
}
43+
$pathSeparator = ';'
44+
$environmentPath = $environmentPath.Trim($pathSeparator)
4845
$environmentPath = $environmentPath.Split($pathSeparator)
4946
$environmentPath = $environmentPath | Sort-Object
5047

src/public/Remove-EnvironmentPath.ps1

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#Requires -Modules Admin
1+
#Requires -Modules Admin, DynamicParams
22

33
function Remove-EnvironmentPath {
44
<#
@@ -51,30 +51,26 @@ function Remove-EnvironmentPath {
5151
)
5252

5353
DynamicParam {
54-
$runtimeDefinedParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
55-
$attributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
56-
57-
$parameterName = 'Path'
58-
$parameterAttribute = New-Object System.Management.Automation.ParameterAttribute
59-
$parameterAttribute.Mandatory = $true
60-
$parameterAttribute.Position = 1
61-
$parameterAttribute.HelpMessage = 'Name of the font to uninstall.'
62-
$parameterAttribute.ValueFromPipeline = $true
63-
$parameterAttribute.ValueFromPipelineByPropertyName = $true
64-
$attributeCollection.Add($parameterAttribute)
65-
66-
$parameterValidateSet = Get-EnvironmentPath -Scope $Scope -AsArray
67-
$validateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($parameterValidateSet)
68-
$attributeCollection.Add($validateSetAttribute)
69-
70-
# Adding a parameter alias
71-
$parameterAlias = 'FullName'
72-
$aliasAttribute = New-Object System.Management.Automation.AliasAttribute -ArgumentList $parameterAlias
73-
$attributeCollection.Add($aliasAttribute)
74-
75-
$runtimeDefinedParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($parameterName, [string[]], $attributeCollection)
76-
$runtimeDefinedParameterDictionary.Add($parameterName, $runtimeDefinedParameter)
77-
return $runtimeDefinedParameterDictionary
54+
$DynamicParamDictionary = New-DynamicParamDictionary
55+
56+
$dynPath = @{
57+
Name = 'Path'
58+
Alias = 'FullName'
59+
Type = [string[]]
60+
Mandatory = $true
61+
HelpMessage = 'Name of the font to uninstall.'
62+
ValueFromPipeline = $true
63+
ValueFromPipelineByPropertyName = $true
64+
ValidateSet = if ([string]::IsNullOrEmpty($Scope)) {
65+
Get-EnvironmentPath -Scope 'CurrentUser' -AsArray
66+
} else {
67+
Get-EnvironmentPath -Scope $Scope -AsArray
68+
}
69+
DynamicParamDictionary = $DynamicParamDictionary
70+
}
71+
New-DynamicParam @dynPath
72+
73+
return $DynamicParamDictionary
7874
}
7975

8076
begin {
@@ -104,12 +100,11 @@ Please run the command again with elevated rights (Run as Administrator) or prov
104100
}
105101

106102
end {
107-
if ([System.Environment]::OSVersion.Platform -eq 'Win32NT') {
108-
$pathSeparator = ';'
109-
} else {
110-
$pathSeparator = ':'
111-
}
103+
$pathSeparator = ';'
112104
$environmentPath = $environmentPath -join $pathSeparator
105+
$environmentPath = $environmentPath.Trim($pathSeparator)
106+
$environmentPath = $environmentPath + $pathSeparator
107+
113108
if ($PSCmdlet.ShouldProcess($environmentPath, 'Remove')) {
114109
[System.Environment]::SetEnvironmentVariable('PATH', $environmentPath, [System.EnvironmentVariableTarget]::$target)
115110
}

src/public/Repair-EnvironmentPath.ps1

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ function Repair-EnvironmentPath {
4545
)
4646

4747
begin {
48-
$separatorChar = [IO.Path]::DirectorySeparatorChar
48+
$separatorChar = [System.IO.Path]::DirectorySeparatorChar
4949

5050
$target = if ($Scope -eq 'CurrentUser') {
5151
[System.EnvironmentVariableTarget]::User
@@ -98,13 +98,12 @@ Please run the command again with elevated rights (Run as Administrator) or prov
9898
}
9999

100100
end {
101-
if ([System.Environment]::OSVersion.Platform -eq 'Win32NT') {
102-
$pathSeparator = ';'
103-
} else {
104-
$pathSeparator = ':'
105-
}
101+
102+
$pathSeparator = ';'
106103
$repairedEnvironmentPaths = $repairedEnvironmentPaths | Sort-Object -Unique
107104
$repairedEnvironmentPaths = $repairedEnvironmentPaths -join $pathSeparator
105+
$repairedEnvironmentPaths = $repairedEnvironmentPaths.Trim($pathSeparator)
106+
$repairedEnvironmentPaths = $repairedEnvironmentPaths + $pathSeparator
108107

109108
[System.Environment]::SetEnvironmentVariable('PATH', $repairedEnvironmentPaths, [System.EnvironmentVariableTarget]::$target)
110109
Write-Verbose "Repair PATH - [$target] - Done"

tests/PATH.Tests.ps1

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,64 @@ Describe 'PATH' {
1717
{ Import-Module -Name 'PATH' -Verbose -RequiredVersion 999.0.0 -Force } | Should -Not -Throw
1818
}
1919
}
20+
21+
Context 'Function: Get-EnvironemntPath' {
22+
Context 'CurrentUser' {
23+
It 'Should not throw' {
24+
$result = Get-EnvironmentPath
25+
Write-Verbose ($result | Out-String) -Verbose
26+
$result | Should -BeOfType [System.String]
27+
}
28+
29+
It "Should not throw when using '-AsArray'" {
30+
$result = Get-EnvironmentPath -AsArray
31+
Write-Verbose ($result | Out-String) -Verbose
32+
Should -ActualValue $result -BeOfType [System.Object[]]
33+
}
34+
}
35+
36+
Context 'AllUsers' {
37+
It 'Should not throw' {
38+
$result = Get-EnvironmentPath -Scope 'AllUsers'
39+
Write-Verbose ($result | Out-String) -Verbose
40+
$result | Should -BeOfType [System.String]
41+
}
42+
43+
It "Should not throw when using '-AsArray'" {
44+
$result = Get-EnvironmentPath -Scope 'AllUsers' -AsArray
45+
Write-Verbose ($result | Out-String) -Verbose
46+
Should -ActualValue $result -BeOfType [System.Object[]]
47+
}
48+
}
49+
}
50+
51+
Context 'Function: Add-EnvironmentPath' {
52+
It 'Should not throw' {
53+
{
54+
Add-EnvironmentPath -Path $HOME -Verbose
55+
Write-Verbose (Get-EnvironmentPath | Out-String) -Verbose
56+
Write-Verbose (Get-EnvironmentPath -AsArray | Out-String) -Verbose
57+
} | Should -Not -Throw
58+
}
59+
}
60+
61+
Context 'Function: Repair-EnvironmentPath' {
62+
It 'Should not throw' {
63+
{
64+
Repair-EnvironmentPath -Verbose
65+
Write-Verbose (Get-EnvironmentPath | Out-String) -Verbose
66+
Write-Verbose (Get-EnvironmentPath -AsArray | Out-String) -Verbose
67+
} | Should -Not -Throw
68+
}
69+
}
70+
71+
Context 'Function: Remove-EnvironmentPath' {
72+
It 'Should not throw' {
73+
{
74+
Remove-EnvironmentPath -Path $HOME -Verbose
75+
Write-Verbose (Get-EnvironmentPath | Out-String) -Verbose
76+
Write-Verbose (Get-EnvironmentPath -AsArray | Out-String) -Verbose
77+
} | Should -Not -Throw
78+
}
79+
}
2080
}

0 commit comments

Comments
 (0)