Skip to content

Commit c467345

Browse files
🩹 [Patch]: Make verbose nicer for module loader (#48)
## Description - Make verbose nicer for module loader - Split enum and class loader and make verbose output resemble that of PowerShell module loader. - Add example data to test source files. ## 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 776e27b commit c467345

File tree

5 files changed

+69
-14
lines changed

5 files changed

+69
-14
lines changed

scripts/helpers/Build/Add-ContentFromItem.ps1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
$pathSeparator = [System.IO.Path]::DirectorySeparatorChar
2727

2828
$relativeFolderPath = $Path -Replace $RootPath, ''
29+
$relativeFolderPath = $relativeFolderPath -Replace $file.Extension, ''
2930
$relativeFolderPath = $relativeFolderPath.TrimStart($pathSeparator)
3031
$relativeFolderPath = $relativeFolderPath -Split $pathSeparator | ForEach-Object { "[$_]" }
3132
$relativeFolderPath = $relativeFolderPath -Join ' - '
@@ -44,6 +45,7 @@ Write-Verbose "[`$scriptName] - $relativeFolderPath - Processing folder"
4445
$files = $Path | Get-ChildItem -File -Force -Filter '*.ps1' | Sort-Object -Property FullName
4546
foreach ($file in $files) {
4647
$relativeFilePath = $file.FullName -Replace $RootPath, ''
48+
$relativeFilePath = $relativeFilePath -Replace $file.Extension, ''
4749
$relativeFilePath = $relativeFilePath.TrimStart($pathSeparator)
4850
$relativeFilePath = $relativeFilePath -Split $pathSeparator | ForEach-Object { "[$_]" }
4951
$relativeFilePath = $relativeFilePath -Join ' - '

scripts/helpers/Build/Build-PSModuleRootModule.ps1

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,20 @@ function Build-PSModuleRootModule {
5757
if ($classes.count -gt 0) {
5858
$classExports = @'
5959
# Define the types to export with type accelerators.
60-
$ExportableTypes = @(
60+
$ExportableClasses = @(
6161
6262
'@
63-
$classes | ForEach-Object {
64-
$classExports += " [$_]`n"
63+
$classes | Where-Object Type -EQ 'class' | ForEach-Object {
64+
$classExports += " [$($_.Name)]`n"
65+
}
66+
67+
$classExports += @'
68+
)
69+
$ExportableEnums = @(
70+
71+
'@
72+
$classes | Where-Object Type -EQ 'enum' | ForEach-Object {
73+
$classExports += " [$($_.Name)]`n"
6574
}
6675

6776
$classExports += @'
@@ -73,17 +82,27 @@ $TypeAcceleratorsClass = [psobject].Assembly.GetType(
7382
# Ensure none of the types would clobber an existing type accelerator.
7483
# If a type accelerator with the same name exists, throw an exception.
7584
$ExistingTypeAccelerators = $TypeAcceleratorsClass::Get
76-
foreach ($Type in $ExportableTypes) {
85+
foreach ($Type in $ExportableEnums) {
86+
if ($Type.FullName -in $ExistingTypeAccelerators.Keys) {
87+
Write-Warning "Enum already exists [$($Type.FullName)]. Skipping."
88+
} else {
89+
$TypeAcceleratorsClass::Add($Type.FullName, $Type)
90+
Write-Verbose "Exporting enum '$Type'."
91+
}
92+
}
93+
foreach ($Type in $ExportableClasses) {
7794
if ($Type.FullName -in $ExistingTypeAccelerators.Keys) {
78-
Write-Debug "Accelerator already exists [$($Type.FullName)]"
95+
Write-Warning "Class already exists [$($Type.FullName)]. Skipping."
7996
} else {
8097
$TypeAcceleratorsClass::Add($Type.FullName, $Type)
98+
Write-Verbose "Exporting class '$Type'."
8199
}
82100
}
83101
102+
84103
# Remove type accelerators when the module is removed.
85104
$MyInvocation.MyCommand.ScriptBlock.Module.OnRemove = {
86-
foreach ($Type in $ExportableTypes) {
105+
foreach ($Type in ($ExportableEnums + $ExportableClasses)) {
87106
$TypeAcceleratorsClass::Remove($Type.FullName)
88107
}
89108
}.GetNewClosure()
@@ -115,11 +134,11 @@ param()
115134
#endregion - Module header
116135

117136
#region - Module post-header
118-
Add-Content -Path $rootModuleFile -Force -Value @'
119-
$scriptName = $MyInvocation.MyCommand.Name
120-
Write-Verbose "[$scriptName] Importing module"
137+
Add-Content -Path $rootModuleFile -Force -Value @"
138+
`$scriptName = '$ModuleName'
139+
Write-Verbose "[`$scriptName] - Importing module"
121140
122-
'@
141+
"@
123142
#endregion - Module post-header
124143

125144
#region - Data and variables
@@ -131,9 +150,9 @@ Write-Verbose "[$scriptName] - [data] - Processing folder"
131150
$dataFolder = (Join-Path $PSScriptRoot 'data')
132151
Write-Verbose "[$scriptName] - [data] - [$dataFolder]"
133152
Get-ChildItem -Path "$dataFolder" -Recurse -Force -Include '*.psd1' -ErrorAction SilentlyContinue | ForEach-Object {
134-
Write-Verbose "[$scriptName] - [data] - [$($_.Name)] - Importing"
153+
Write-Verbose "[$scriptName] - [data] - [$($_.BaseName)] - Importing"
135154
New-Variable -Name $_.BaseName -Value (Import-PowerShellDataFile -Path $_.FullName) -Force
136-
Write-Verbose "[$scriptName] - [data] - [$($_.Name)] - Done"
155+
Write-Verbose "[$scriptName] - [data] - [$($_.BaseName)] - Done"
137156
}
138157
139158
Write-Verbose "[$scriptName] - [data] - Done"
@@ -165,6 +184,7 @@ Write-Verbose "[$scriptName] - [data] - Done"
165184
$files = $ModuleOutputFolder | Get-ChildItem -File -Force -Filter '*.ps1'
166185
foreach ($file in $files) {
167186
$relativePath = $file.FullName -Replace $ModuleOutputFolder, ''
187+
$relativePath = $relativePath -Replace $file.Extension, ''
168188
$relativePath = $relativePath.TrimStart($pathSeparator)
169189
$relativePath = $relativePath -Split $pathSeparator | ForEach-Object { "[$_]" }
170190
$relativePath = $relativePath -Join ' - '

scripts/helpers/Build/Get-PSModuleClassesToExport.ps1

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,12 @@
2929

3030
foreach ($file in $files) {
3131
$content = Get-Content -Path $file.FullName -Raw
32-
$stringMatches = [Regex]::Matches($content, '(?i)^(?:class|enum)\s+([^\s{]+)', 'Multiline')
32+
$stringMatches = [Regex]::Matches($content, '(?i)^(class|enum)\s+([^\s{]+)', 'Multiline')
3333
foreach ($match in $stringMatches) {
34-
$match.Groups[1].Value
34+
[pscustomobject]@{
35+
Type = $match.Groups[1].Value
36+
Name = $match.Groups[2].Value
37+
}
3538
}
3639
}
3740
}

tests/src/classes/Book.ps1

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,18 @@ class BookList {
130130
}
131131
}
132132
}
133+
134+
enum Binding {
135+
Hardcover
136+
Paperback
137+
EBook
138+
}
139+
140+
enum Genre {
141+
Mystery
142+
Thriller
143+
Romance
144+
ScienceFiction
145+
Fantasy
146+
Horror
147+
}

tests/srcWithManifest/classes/Book.ps1

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,18 @@ class BookList {
130130
}
131131
}
132132
}
133+
134+
enum Binding {
135+
Hardcover
136+
Paperback
137+
EBook
138+
}
139+
140+
enum Genre {
141+
Mystery
142+
Thriller
143+
Romance
144+
ScienceFiction
145+
Fantasy
146+
Horror
147+
}

0 commit comments

Comments
 (0)