Skip to content

Commit 066fc04

Browse files
🩹 [Patch]: Build root module from files before subfolders (#83)
## Description This pull request includes several changes to improve the organization and processing of module files in the PowerShell build scripts. The most important changes include modifying the order and method of adding content from subfolders, reorganizing the script regions, and ensuring consistent sorting of files and folders. Changes to file processing order and method: * [`README.md`](diffhunk://#diff-b335630551682c19a781afebcf4d07bf978fb1f8ac04c6bf87428ed5106870f5L47-R56): Changed the order of processing subfolder content and clarified that files are processed recursively in alphabetical order. * [`scripts/helpers/Build/Add-ContentFromItem.ps1`](diffhunk://#diff-36e9defd1dfaa43ba6fef9a2251761c86b9ac751eaf3177f344a0ec597e3c832L35-L44): Moved the processing of subfolders to occur after processing files in the current folder, ensuring a more logical and efficient order. [[1]](diffhunk://#diff-36e9defd1dfaa43ba6fef9a2251761c86b9ac751eaf3177f344a0ec597e3c832L35-L44) [[2]](diffhunk://#diff-36e9defd1dfaa43ba6fef9a2251761c86b9ac751eaf3177f344a0ec597e3c832L54-R64) Reorganization of script regions: * [`scripts/helpers/Build/Build-PSModuleRootModule.ps1`](diffhunk://#diff-1d337ff39f37506a54fda1c5d0487f1b2c2ef318f216a4d9a56c3e7248b69879R68): Added and reorganized region headers for better readability and structure, including "Class exporter," "Module post-header," "Data importer," and "Member exporter" sections. [[1]](diffhunk://#diff-1d337ff39f37506a54fda1c5d0487f1b2c2ef318f216a4d9a56c3e7248b69879R68) [[2]](diffhunk://#diff-1d337ff39f37506a54fda1c5d0487f1b2c2ef318f216a4d9a56c3e7248b69879R120) [[3]](diffhunk://#diff-1d337ff39f37506a54fda1c5d0487f1b2c2ef318f216a4d9a56c3e7248b69879R146-R162) [[4]](diffhunk://#diff-1d337ff39f37506a54fda1c5d0487f1b2c2ef318f216a4d9a56c3e7248b69879R232-R235) Consistency improvements: * [`scripts/helpers/Build/Build-PSModuleRootModule.ps1`](diffhunk://#diff-1d337ff39f37506a54fda1c5d0487f1b2c2ef318f216a4d9a56c3e7248b69879L204-R199): Ensured consistent sorting of files and folders by name before processing, improving the predictability of the build process. [[1]](diffhunk://#diff-1d337ff39f37506a54fda1c5d0487f1b2c2ef318f216a4d9a56c3e7248b69879L204-R199) [[2]](diffhunk://#diff-1d337ff39f37506a54fda1c5d0487f1b2c2ef318f216a4d9a56c3e7248b69879L213-R215) * [`scripts/helpers/Build/Get-PSModuleClassesToExport.ps1`](diffhunk://#diff-c51cdeca9e44fe598bc10e94c98fc0d72f8a6fb36e872193b2fc9b9b1348f1a9L28-R28): Sorted files by name before processing to maintain consistency. ## 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 8b38cc6 commit 066fc04

File tree

4 files changed

+33
-42
lines changed

4 files changed

+33
-42
lines changed

README.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,16 @@ following order:
4444
1. Adds a module header from `header.ps1` if it exists and removes the file from the module folder.
4545
1. Adds a data loader that loads files from the `data` folder as variables in the module scope, if the folder exists. The variables are available
4646
using the `$script:<filename>` syntax.
47-
1. Adds content from subfolders into the root module file and removes them from the module folder in the following order:
48-
- `init`
49-
- `classes/private`
50-
- `classes/public`
51-
- `functions/private`
52-
- `functions/public`
53-
- `variables/private`
54-
- `variables/public`
55-
- `*.ps1` on module root
47+
1. Adds content from the following folders into the root module file. The files on the root of a folder is added before recursivelfy going to the next
48+
folder in alphabetical order. Once the file is processed, it is removed from the module folder.
49+
1. `init`
50+
1. `classes/private`
51+
1. `classes/public`
52+
1. `functions/private`
53+
1. `functions/public`
54+
1. `variables/private`
55+
1. `variables/public`
56+
1. `*.ps1` on module root
5657
1. Adds a `class` and `enum` exporter that exports the ones from `classes/public` folder to the caller session, using [TypeAccelerators](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_classes?view=powershell-7.4#exporting-classes-with-type-accelerators).
5758
1. Adds the `Export-ModuleMember` function to the end of the file, to make sure that only the functions, cmdlets, variables and aliases that are
5859
defined in the `public` folders are exported.

scripts/helpers/Build/Add-ContentFromItem.ps1

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,10 @@
3232
$relativeFolderPath = $relativeFolderPath -Join ' - '
3333

3434
Add-Content -Path $RootModuleFilePath -Force -Value @"
35-
#region - From $relativeFolderPath
35+
#region $relativeFolderPath
3636
Write-Debug "[`$scriptName] - $relativeFolderPath - Processing folder"
37-
3837
"@
3938

40-
$subFolders = $Path | Get-ChildItem -Directory -Force | Sort-Object -Property Name
41-
foreach ($subFolder in $subFolders) {
42-
Add-ContentFromItem -Path $subFolder.FullName -RootModuleFilePath $RootModuleFilePath -RootPath $RootPath
43-
}
44-
4539
$files = $Path | Get-ChildItem -File -Force -Filter '*.ps1' | Sort-Object -Property FullName
4640
foreach ($file in $files) {
4741
$relativeFilePath = $file.FullName -Replace $RootPath, ''
@@ -51,21 +45,22 @@ Write-Debug "[`$scriptName] - $relativeFolderPath - Processing folder"
5145
$relativeFilePath = $relativeFilePath -Join ' - '
5246

5347
Add-Content -Path $RootModuleFilePath -Force -Value @"
54-
#region - From $relativeFilePath
48+
#region $relativeFilePath
5549
Write-Debug "[`$scriptName] - $relativeFilePath - Importing"
56-
5750
"@
5851
Get-Content -Path $file.FullName | Add-Content -Path $RootModuleFilePath -Force
5952
Add-Content -Path $RootModuleFilePath -Value @"
60-
6153
Write-Debug "[`$scriptName] - $relativeFilePath - Done"
62-
#endregion - From $relativeFilePath
54+
#endregion $relativeFilePath
6355
"@
6456
}
65-
Add-Content -Path $RootModuleFilePath -Force -Value @"
6657

58+
$subFolders = $Path | Get-ChildItem -Directory -Force | Sort-Object -Property Name
59+
foreach ($subFolder in $subFolders) {
60+
Add-ContentFromItem -Path $subFolder.FullName -RootModuleFilePath $RootModuleFilePath -RootPath $RootPath
61+
}
62+
Add-Content -Path $RootModuleFilePath -Force -Value @"
6763
Write-Debug "[`$scriptName] - $relativeFolderPath - Done"
68-
#endregion - From $relativeFolderPath
69-
64+
#endregion $relativeFolderPath
7065
"@
7166
}

scripts/helpers/Build/Build-PSModuleRootModule.ps1

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ function Build-PSModuleRootModule {
6565
$classes = Get-PSModuleClassesToExport -SourceFolderPath $classesFolder
6666
if ($classes.count -gt 0) {
6767
$classExports += @'
68+
#region Class exporter
6869
# Get the internal TypeAccelerators class to use its static methods.
6970
$TypeAcceleratorsClass = [psobject].Assembly.GetType(
7071
'System.Management.Automation.TypeAccelerators'
@@ -116,6 +117,7 @@ $MyInvocation.MyCommand.ScriptBlock.Module.OnRemove = {
116117
$TypeAcceleratorsClass::Remove($Type.FullName)
117118
}
118119
}.GetNewClosure()
120+
#endregion Class exporter
119121
'@
120122
}
121123
}
@@ -141,28 +143,23 @@ $MyInvocation.MyCommand.ScriptBlock.Module.OnRemove = {
141143
param()
142144
'@
143145
}
146+
#endregion - Module header
144147

145-
# Add a variable $script:PSModuleInfo to the root module, which contains the module manifest information.
148+
#region - Module post-header
146149
Add-Content -Path $rootModuleFile -Force -Value @'
147150
$baseName = [System.IO.Path]::GetFileNameWithoutExtension($PSCommandPath)
148151
$script:PSModuleInfo = Test-ModuleManifest -Path "$PSScriptRoot\$baseName.psd1"
149152
$script:PSModuleInfo | Format-List | Out-String -Stream | ForEach-Object { Write-Debug $_ }
153+
$scriptName = $script:PSModuleInfo.Name
154+
Write-Debug "[$scriptName] - Importing module"
150155
'@
151-
#endregion - Module header
152-
153-
#region - Module post-header
154-
Add-Content -Path $rootModuleFile -Force -Value @"
155-
`$scriptName = '$ModuleName'
156-
Write-Debug "[`$scriptName] - Importing module"
157-
158-
"@
159156
#endregion - Module post-header
160157

161158
#region - Data loader
162159
if (Test-Path -Path (Join-Path -Path $ModuleOutputFolder -ChildPath 'data')) {
163160

164161
Add-Content -Path $rootModuleFile.FullName -Force -Value @'
165-
#region - Data import
162+
#region Data importer
166163
Write-Debug "[$scriptName] - [data] - Processing folder"
167164
$dataFolder = (Join-Path $PSScriptRoot 'data')
168165
Write-Debug "[$scriptName] - [data] - [$dataFolder]"
@@ -171,10 +168,8 @@ Get-ChildItem -Path "$dataFolder" -Recurse -Force -Include '*.psd1' -ErrorAction
171168
New-Variable -Name $_.BaseName -Value (Import-PowerShellDataFile -Path $_.FullName) -Force
172169
Write-Debug "[$scriptName] - [data] - [$($_.BaseName)] - Done"
173170
}
174-
175171
Write-Debug "[$scriptName] - [data] - Done"
176-
#endregion - Data import
177-
172+
#endregion Data importer
178173
'@
179174
}
180175
#endregion - Data loader
@@ -201,7 +196,7 @@ Write-Debug "[$scriptName] - [data] - Done"
201196
#endregion - Add content from subfolders
202197

203198
#region - Add content from *.ps1 files on module root
204-
$files = $ModuleOutputFolder | Get-ChildItem -File -Force -Filter '*.ps1'
199+
$files = $ModuleOutputFolder | Get-ChildItem -File -Force -Filter '*.ps1' | Sort-Object -Property FullName
205200
foreach ($file in $files) {
206201
$relativePath = $file.FullName -Replace $ModuleOutputFolder, ''
207202
$relativePath = $relativePath -Replace $file.Extension, ''
@@ -210,16 +205,14 @@ Write-Debug "[$scriptName] - [data] - Done"
210205
$relativePath = $relativePath -Join ' - '
211206

212207
Add-Content -Path $rootModuleFile -Force -Value @"
213-
#region - From $relativePath
208+
#region $relativePath
214209
Write-Debug "[`$scriptName] - $relativePath - Importing"
215-
216210
"@
217211
Get-Content -Path $file.FullName | Add-Content -Path $rootModuleFile -Force
218212

219213
Add-Content -Path $rootModuleFile -Force -Value @"
220214
Write-Debug "[`$scriptName] - $relativePath - Done"
221-
#endregion - From $relativePath
222-
215+
#endregion $relativePath
223216
"@
224217
$file | Remove-Item -Force
225218
}
@@ -236,8 +229,10 @@ Write-Debug "[`$scriptName] - $relativePath - Done"
236229
Path = $rootModuleFile
237230
Force = $true
238231
Value = @"
232+
#region Member exporter
239233
`$exports = $exportsString
240234
Export-ModuleMember @exports
235+
#endregion Member exporter
241236
"@
242237
}
243238
Add-Content @params

scripts/helpers/Build/Get-PSModuleClassesToExport.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
[string] $SourceFolderPath
2626
)
2727

28-
$files = Get-ChildItem -Path $SourceFolderPath -Recurse -Include '*.ps1'
28+
$files = Get-ChildItem -Path $SourceFolderPath -Recurse -Include '*.ps1' | Sort-Object -Property FullName
2929

3030
foreach ($file in $files) {
3131
$content = Get-Content -Path $file.FullName -Raw

0 commit comments

Comments
 (0)