Skip to content

Commit d778e56

Browse files
🩹 [Patch]: Remove dependency on Utilities (#111)
## Description This pull request includes several changes to improve the handling of null or empty string checks in various PowerShell scripts. The updates primarily involve replacing custom `IsNullOrEmpty` function calls with the built-in `[string]::IsNullOrEmpty` method to enhance readability and maintainability. Improvements to null or empty string checks: * [`scripts/helpers/Build/Build-PSModuleManifest.ps1`](diffhunk://#diff-50cfb011f5c8aeef8145003927ec3e5edfdf26e5d417bcee6e441517c07454f9L63-R66): Replaced custom `IsNotNullOrEmpty` checks with `[string]::IsNullOrEmpty` for the `Author`, `CompanyName`, and `Description` properties in the module manifest. [[1]](diffhunk://#diff-50cfb011f5c8aeef8145003927ec3e5edfdf26e5d417bcee6e441517c07454f9L63-R66) [[2]](diffhunk://#diff-50cfb011f5c8aeef8145003927ec3e5edfdf26e5d417bcee6e441517c07454f9L76-R76) * [`scripts/helpers/Build/Get-PSModuleAliasesToExport.ps1`](diffhunk://#diff-ae18191466ffa02c1a8429365cf96d8768f5eae03331c4a35199f8cd961e76a7L33-R34): Updated the check for `AliasesToExport` to use `[string]::IsNullOrEmpty`. * [`scripts/helpers/Build/Get-PSModuleCmdletsToExport.ps1`](diffhunk://#diff-f542f4a75d778df60fee6e176c795e133be2ed109d8212c05aa545383bd26c46L33-R34): Updated the check for `CmdletsToExport` to use `[string]::IsNullOrEmpty`. * [`scripts/main.ps1`](diffhunk://#diff-dc2e5a659836b1b73abb03421c567f5018c2755677c4a0aa764cb26117b68011L8-L9): Removed unused module requirement and updated the check for `GITHUB_ACTION_INPUT_Name` to use `[string]::IsNullOrEmpty`. [[1]](diffhunk://#diff-dc2e5a659836b1b73abb03421c567f5018c2755677c4a0aa764cb26117b68011L8-L9) [[2]](diffhunk://#diff-dc2e5a659836b1b73abb03421c567f5018c2755677c4a0aa764cb26117b68011L19-R17) ## 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 1e3d612 commit d778e56

15 files changed

+744
-17
lines changed

.github/workflows/Action-Test.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
uses: actions/checkout@v4
2727

2828
- name: Initialize environment
29-
uses: PSModule/Initialize-PSModule@main
29+
uses: PSModule/Initialize-PSModule@v1
3030

3131
- name: Action-Test
3232
uses: ./
@@ -43,7 +43,7 @@ jobs:
4343
uses: actions/checkout@v4
4444

4545
- name: Initialize environment
46-
uses: PSModule/Initialize-PSModule@main
46+
uses: PSModule/Initialize-PSModule@v1
4747

4848
- name: Action-Test
4949
uses: ./
@@ -61,7 +61,7 @@ jobs:
6161
uses: actions/checkout@v4
6262

6363
- name: Initialize environment
64-
uses: PSModule/Initialize-PSModule@main
64+
uses: PSModule/Initialize-PSModule@v1
6565

6666
- name: Action-Test
6767
uses: ./

scripts/helpers/Build-PSModule.ps1

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#>
99
[CmdletBinding()]
1010
#Requires -Modules @{ ModuleName = 'GitHub'; ModuleVersion = '0.13.2' }
11-
#Requires -Modules @{ ModuleName = 'Utilities'; ModuleVersion = '0.3.0' }
1211
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
1312
'PSReviewUnusedParameter', '', Scope = 'Function',
1413
Justification = 'LogGroup - Scoping affects the variables line of sight.'
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
function Add-ModuleManifestData {
2+
<#
3+
.SYNOPSIS
4+
Add data to a module manifest file property
5+
6+
.DESCRIPTION
7+
This function adds data to a module manifest file property.
8+
If the property doesn't exist, it will be created.
9+
If it does exist, the new data will be appended to the existing data.
10+
11+
.EXAMPLE
12+
Add-ModuleManifestData -Path 'MyModule.psd1' -RequiredModules 'pester', 'platyPS'
13+
14+
Adds the modules 'pester' and 'platyPS' to the RequiredModules property of the module manifest file 'MyModule.psd1'.
15+
#>
16+
[CmdletBinding()]
17+
param(
18+
[Parameter(Mandatory)]
19+
[ValidateScript({ Test-Path -Path $_ -PathType Leaf })]
20+
[string]$Path,
21+
22+
# Modules that must be imported into the global environment prior to importing this module.
23+
[Parameter()]
24+
[Object[]] $RequiredModules,
25+
26+
# Compatible editions of PowerShell.
27+
[Parameter()]
28+
[string[]] $CompatiblePSEditions,
29+
30+
# Assemblies that must be loaded prior to importing this module.
31+
[Parameter()]
32+
[string[]] $RequiredAssemblies,
33+
34+
# Script files (.ps1) that are run in the caller's environment prior to importing this module.
35+
[Parameter()]
36+
[string[]] $ScriptsToProcess,
37+
38+
# Type files (.ps1xml) to be loaded when importing this module.
39+
[Parameter()]
40+
[string[]] $TypesToProcess,
41+
42+
# Format files (.ps1xml) to be loaded when importing this module.
43+
[Parameter()]
44+
[string[]] $FormatsToProcess,
45+
46+
# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess.
47+
[Parameter()]
48+
[Object[]] $NestedModules,
49+
50+
# Functions to export from this module, for best performance, do not use wildcards and do not
51+
# delete the entry, use an empty array if there are no functions to export.
52+
[Parameter()]
53+
[string[]] $FunctionsToExport,
54+
55+
# Cmdlets to export from this module, for best performance, do not use wildcards and do not
56+
# delete the entry, use an empty array if there are no cmdlets to export.
57+
[Parameter()]
58+
[string[]] $CmdletsToExport,
59+
60+
# Variables to export from this module.
61+
[Parameter()]
62+
[string[]] $VariablesToExport,
63+
64+
# Aliases to export from this module, for best performance, do not use wildcards and do not
65+
# delete the entry, use an empty array if there are no aliases to export.
66+
[Parameter()]
67+
[string[]] $AliasesToExport,
68+
69+
# DSC resources to export from this module.
70+
[Parameter()]
71+
[string[]] $DscResourcesToExport,
72+
73+
# List of all modules packaged with this module.
74+
[Parameter()]
75+
[Object[]] $ModuleList,
76+
77+
# List of all files packaged with this module.
78+
[Parameter()]
79+
[string[]] $FileList,
80+
81+
# Tags applied to this module. These help with module discovery in online galleries.
82+
[Parameter()]
83+
[string[]] $Tags,
84+
85+
# External dependent modules of this module.
86+
[Parameter()]
87+
[string[]] $ExternalModuleDependencies
88+
)
89+
90+
$moduleManifest = Get-ModuleManifest -Path $Path
91+
$changes = @{}
92+
93+
if ($RequiredModules) {
94+
$RequiredModules += $moduleManifest.RequiredModules
95+
$changes.RequiredModules = $RequiredModules
96+
}
97+
if ($RequiredAssemblies) {
98+
$RequiredAssemblies += $moduleManifest.RequiredAssemblies
99+
$changes.RequiredAssemblies = $RequiredAssemblies
100+
}
101+
if ($CompatiblePSEditions) {
102+
$CompatiblePSEditions += $moduleManifest.CompatiblePSEditions
103+
$changes.CompatiblePSEditions = $CompatiblePSEditions
104+
}
105+
if ($ScriptsToProcess) {
106+
$ScriptsToProcess += $moduleManifest.ScriptsToProcess
107+
$changes.ScriptsToProcess = $ScriptsToProcess
108+
}
109+
if ($TypesToProcess) {
110+
$TypesToProcess += $moduleManifest.TypesToProcess
111+
$changes.TypesToProcess = $TypesToProcess
112+
}
113+
if ($FormatsToProcess) {
114+
$FormatsToProcess += $moduleManifest.FormatsToProcess
115+
$changes.FormatsToProcess = $FormatsToProcess
116+
}
117+
if ($NestedModules) {
118+
$NestedModules += $moduleManifest.NestedModules
119+
$changes.NestedModules = $NestedModules
120+
}
121+
if ($FunctionsToExport) {
122+
$FunctionsToExport += $moduleManifest.FunctionsToExport
123+
$changes.FunctionsToExport = $FunctionsToExport
124+
}
125+
if ($CmdletsToExport) {
126+
$CmdletsToExport += $moduleManifest.CmdletsToExport
127+
$changes.CmdletsToExport = $CmdletsToExport
128+
}
129+
if ($VariablesToExport) {
130+
$VariablesToExport += $moduleManifest.VariablesToExport
131+
$changes.VariablesToExport = $VariablesToExport
132+
}
133+
if ($AliasesToExport) {
134+
$AliasesToExport += $moduleManifest.AliasesToExport
135+
$changes.AliasesToExport = $AliasesToExport
136+
}
137+
if ($DscResourcesToExport) {
138+
$DscResourcesToExport += $moduleManifest.DscResourcesToExport
139+
$changes.DscResourcesToExport = $DscResourcesToExport
140+
}
141+
if ($ModuleList) {
142+
$ModuleList += $moduleManifest.ModuleList
143+
$changes.ModuleList = $ModuleList
144+
}
145+
if ($FileList) {
146+
$FileList += $moduleManifest.FileList
147+
$changes.FileList = $FileList
148+
}
149+
if ($Tags) {
150+
$Tags += $moduleManifest.PrivateData.PSData.Tags
151+
$changes.Tags = $Tags
152+
}
153+
if ($ExternalModuleDependencies) {
154+
$ExternalModuleDependencies += $moduleManifest.PrivateData.PSData.ExternalModuleDependencies
155+
$changes.ExternalModuleDependencies = $ExternalModuleDependencies
156+
}
157+
158+
foreach ($key in $changes.GetEnumerator().Name) {
159+
$changes[$key] = $changes[$key] | Sort-Object -Unique | Where-Object { -not [string]::IsNullOrEmpty($_) }
160+
}
161+
162+
Set-ModuleManifest -Path $Path @changes
163+
164+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
function Add-PSModulePath {
2+
<#
3+
.SYNOPSIS
4+
Adds a path to the PSModulePath environment variable.
5+
6+
.DESCRIPTION
7+
Adds a path to the PSModulePath environment variable.
8+
For Linux and macOS, the path delimiter is ':' and for Windows it is ';'.
9+
10+
.EXAMPLE
11+
Add-PSModulePath -Path 'C:\Users\user\Documents\WindowsPowerShell\Modules'
12+
13+
Adds the path 'C:\Users\user\Documents\WindowsPowerShell\Modules' to the PSModulePath environment variable.
14+
#>
15+
[CmdletBinding()]
16+
param(
17+
# Path to the folder where the module source code is located.
18+
[Parameter(Mandatory)]
19+
[string] $Path
20+
)
21+
$PSModulePathSeparator = [System.IO.Path]::PathSeparator
22+
23+
$env:PSModulePath += "$PSModulePathSeparator$Path"
24+
25+
Write-Verbose 'PSModulePath:'
26+
$env:PSModulePath.Split($PSModulePathSeparator) | ForEach-Object {
27+
Write-Verbose " - [$_]"
28+
}
29+
}

scripts/helpers/Build/Build-PSModuleManifest.ps1

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#>
1313
[CmdletBinding()]
1414
#Requires -Modules @{ ModuleName = 'GitHub'; ModuleVersion = '0.13.2' }
15-
#Requires -Modules @{ ModuleName = 'Utilities'; ModuleVersion = '0.3.0' }
1615
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
1716
'PSAvoidLongLines', '', Scope = 'Function',
1817
Justification = 'Easier to read the multi ternery operators in a single line.'
@@ -60,10 +59,10 @@
6059
$manifest.ModuleVersion = '999.0.0'
6160
Write-Host "[ModuleVersion] - [$($manifest.ModuleVersion)]"
6261

63-
$manifest.Author = $manifest.Keys -contains 'Author' ? ($manifest.Author | IsNotNullOrEmpty) ? $manifest.Author : $env:GITHUB_REPOSITORY_OWNER : $env:GITHUB_REPOSITORY_OWNER
62+
$manifest.Author = $manifest.Keys -contains 'Author' ? -not [string]::IsNullOrEmpty($manifest.Author) ? $manifest.Author : $env:GITHUB_REPOSITORY_OWNER : $env:GITHUB_REPOSITORY_OWNER
6463
Write-Host "[Author] - [$($manifest.Author)]"
6564

66-
$manifest.CompanyName = $manifest.Keys -contains 'CompanyName' ? ($manifest.CompanyName | IsNotNullOrEmpty) ? $manifest.CompanyName : $env:GITHUB_REPOSITORY_OWNER : $env:GITHUB_REPOSITORY_OWNER
65+
$manifest.CompanyName = $manifest.Keys -contains 'CompanyName' ? -not [string]::IsNullOrEmpty($manifest.CompanyName) ? $manifest.CompanyName : $env:GITHUB_REPOSITORY_OWNER : $env:GITHUB_REPOSITORY_OWNER
6766
Write-Host "[CompanyName] - [$($manifest.CompanyName)]"
6867

6968
$year = Get-Date -Format 'yyyy'
@@ -73,7 +72,7 @@
7372
Write-Host "[Copyright] - [$($manifest.Copyright)]"
7473

7574
$repoDescription = gh repo view --json description | ConvertFrom-Json | Select-Object -ExpandProperty description
76-
$manifest.Description = $manifest.Keys -contains 'Description' ? ($manifest.Description | IsNotNullOrEmpty) ? $manifest.Description : $repoDescription : $repoDescription
75+
$manifest.Description = $manifest.Keys -contains 'Description' ? -not [string]::IsNullOrEmpty($manifest.Description) ? $manifest.Description : $repoDescription : $repoDescription
7776
Write-Host "[Description] - [$($manifest.Description)]"
7877

7978
$manifest.PowerShellHostName = $manifest.Keys -contains 'PowerShellHostName' ? -not [string]::IsNullOrEmpty($manifest.PowerShellHostName) ? $manifest.PowerShellHostName : $null : $null
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
function Export-PowerShellDataFile {
2+
<#
3+
.SYNOPSIS
4+
Export a hashtable to a .psd1 file.
5+
6+
.DESCRIPTION
7+
This function exports a hashtable to a .psd1 file. It also formats the .psd1 file using the Format-ModuleManifest cmdlet.
8+
9+
.EXAMPLE
10+
Export-PowerShellDataFile -Hashtable @{ Name = 'MyModule'; ModuleVersion = '1.0.0' } -Path 'MyModule.psd1'
11+
#>
12+
[CmdletBinding()]
13+
param (
14+
# The hashtable to export to a .psd1 file.
15+
[Parameter(Mandatory)]
16+
[object] $Hashtable,
17+
18+
# The path of the .psd1 file to export.
19+
[Parameter(Mandatory)]
20+
[string] $Path,
21+
22+
# Force the export, even if the file already exists.
23+
[Parameter()]
24+
[switch] $Force
25+
)
26+
27+
$content = Format-Hashtable -Hashtable $Hashtable
28+
$content | Out-File -FilePath $Path -Force:$Force
29+
Format-ModuleManifest -Path $Path
30+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
function Format-ModuleManifest {
2+
<#
3+
.SYNOPSIS
4+
Formats a module manifest file.
5+
6+
.DESCRIPTION
7+
This function formats a module manifest file, by removing comments and empty lines,
8+
and then formatting the file using the `Invoke-Formatter` function.
9+
10+
.EXAMPLE
11+
Format-ModuleManifest -Path 'C:\MyModule\MyModule.psd1'
12+
#>
13+
[CmdletBinding()]
14+
param(
15+
# Path to the module manifest file.
16+
[Parameter(Mandatory)]
17+
[string] $Path
18+
)
19+
20+
$Utf8BomEncoding = New-Object System.Text.UTF8Encoding $true
21+
22+
$manifestContent = Get-Content -Path $Path
23+
$manifestContent = $manifestContent | ForEach-Object { $_ -replace '#.*' }
24+
$manifestContent = $manifestContent | ForEach-Object { $_.TrimEnd() }
25+
$manifestContent = $manifestContent | Where-Object { -not [string]::IsNullOrEmpty($_) }
26+
[System.IO.File]::WriteAllLines($Path, $manifestContent, $Utf8BomEncoding)
27+
$manifestContent = Get-Content -Path $Path -Raw
28+
29+
$content = Invoke-Formatter -ScriptDefinition $manifestContent
30+
[System.IO.File]::WriteAllLines($Path, $content, $Utf8BomEncoding)
31+
32+
}

0 commit comments

Comments
 (0)