Skip to content

Commit

Permalink
🩹 [Patch]: Make verbose nicer for module loader (#48)
Browse files Browse the repository at this point in the history
## 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
  • Loading branch information
MariusStorhaug committed Apr 2, 2024
1 parent 776e27b commit c467345
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 14 deletions.
2 changes: 2 additions & 0 deletions scripts/helpers/Build/Add-ContentFromItem.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
$pathSeparator = [System.IO.Path]::DirectorySeparatorChar

$relativeFolderPath = $Path -Replace $RootPath, ''
$relativeFolderPath = $relativeFolderPath -Replace $file.Extension, ''
$relativeFolderPath = $relativeFolderPath.TrimStart($pathSeparator)
$relativeFolderPath = $relativeFolderPath -Split $pathSeparator | ForEach-Object { "[$_]" }
$relativeFolderPath = $relativeFolderPath -Join ' - '
Expand All @@ -44,6 +45,7 @@ Write-Verbose "[`$scriptName] - $relativeFolderPath - Processing folder"
$files = $Path | Get-ChildItem -File -Force -Filter '*.ps1' | Sort-Object -Property FullName
foreach ($file in $files) {
$relativeFilePath = $file.FullName -Replace $RootPath, ''
$relativeFilePath = $relativeFilePath -Replace $file.Extension, ''
$relativeFilePath = $relativeFilePath.TrimStart($pathSeparator)
$relativeFilePath = $relativeFilePath -Split $pathSeparator | ForEach-Object { "[$_]" }
$relativeFilePath = $relativeFilePath -Join ' - '
Expand Down
44 changes: 32 additions & 12 deletions scripts/helpers/Build/Build-PSModuleRootModule.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,20 @@ function Build-PSModuleRootModule {
if ($classes.count -gt 0) {
$classExports = @'
# Define the types to export with type accelerators.
$ExportableTypes = @(
$ExportableClasses = @(
'@
$classes | ForEach-Object {
$classExports += " [$_]`n"
$classes | Where-Object Type -EQ 'class' | ForEach-Object {
$classExports += " [$($_.Name)]`n"
}

$classExports += @'
)
$ExportableEnums = @(
'@
$classes | Where-Object Type -EQ 'enum' | ForEach-Object {
$classExports += " [$($_.Name)]`n"
}

$classExports += @'
Expand All @@ -73,17 +82,27 @@ $TypeAcceleratorsClass = [psobject].Assembly.GetType(
# Ensure none of the types would clobber an existing type accelerator.
# If a type accelerator with the same name exists, throw an exception.
$ExistingTypeAccelerators = $TypeAcceleratorsClass::Get
foreach ($Type in $ExportableTypes) {
foreach ($Type in $ExportableEnums) {
if ($Type.FullName -in $ExistingTypeAccelerators.Keys) {
Write-Warning "Enum already exists [$($Type.FullName)]. Skipping."
} else {
$TypeAcceleratorsClass::Add($Type.FullName, $Type)
Write-Verbose "Exporting enum '$Type'."
}
}
foreach ($Type in $ExportableClasses) {
if ($Type.FullName -in $ExistingTypeAccelerators.Keys) {
Write-Debug "Accelerator already exists [$($Type.FullName)]"
Write-Warning "Class already exists [$($Type.FullName)]. Skipping."
} else {
$TypeAcceleratorsClass::Add($Type.FullName, $Type)
Write-Verbose "Exporting class '$Type'."
}
}
# Remove type accelerators when the module is removed.
$MyInvocation.MyCommand.ScriptBlock.Module.OnRemove = {
foreach ($Type in $ExportableTypes) {
foreach ($Type in ($ExportableEnums + $ExportableClasses)) {
$TypeAcceleratorsClass::Remove($Type.FullName)
}
}.GetNewClosure()
Expand Down Expand Up @@ -115,11 +134,11 @@ param()
#endregion - Module header

#region - Module post-header
Add-Content -Path $rootModuleFile -Force -Value @'
$scriptName = $MyInvocation.MyCommand.Name
Write-Verbose "[$scriptName] Importing module"
Add-Content -Path $rootModuleFile -Force -Value @"
`$scriptName = '$ModuleName'
Write-Verbose "[`$scriptName] - Importing module"
'@
"@
#endregion - Module post-header

#region - Data and variables
Expand All @@ -131,9 +150,9 @@ Write-Verbose "[$scriptName] - [data] - Processing folder"
$dataFolder = (Join-Path $PSScriptRoot 'data')
Write-Verbose "[$scriptName] - [data] - [$dataFolder]"
Get-ChildItem -Path "$dataFolder" -Recurse -Force -Include '*.psd1' -ErrorAction SilentlyContinue | ForEach-Object {
Write-Verbose "[$scriptName] - [data] - [$($_.Name)] - Importing"
Write-Verbose "[$scriptName] - [data] - [$($_.BaseName)] - Importing"
New-Variable -Name $_.BaseName -Value (Import-PowerShellDataFile -Path $_.FullName) -Force
Write-Verbose "[$scriptName] - [data] - [$($_.Name)] - Done"
Write-Verbose "[$scriptName] - [data] - [$($_.BaseName)] - Done"
}
Write-Verbose "[$scriptName] - [data] - Done"
Expand Down Expand Up @@ -165,6 +184,7 @@ Write-Verbose "[$scriptName] - [data] - Done"
$files = $ModuleOutputFolder | Get-ChildItem -File -Force -Filter '*.ps1'
foreach ($file in $files) {
$relativePath = $file.FullName -Replace $ModuleOutputFolder, ''
$relativePath = $relativePath -Replace $file.Extension, ''
$relativePath = $relativePath.TrimStart($pathSeparator)
$relativePath = $relativePath -Split $pathSeparator | ForEach-Object { "[$_]" }
$relativePath = $relativePath -Join ' - '
Expand Down
7 changes: 5 additions & 2 deletions scripts/helpers/Build/Get-PSModuleClassesToExport.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@

foreach ($file in $files) {
$content = Get-Content -Path $file.FullName -Raw
$stringMatches = [Regex]::Matches($content, '(?i)^(?:class|enum)\s+([^\s{]+)', 'Multiline')
$stringMatches = [Regex]::Matches($content, '(?i)^(class|enum)\s+([^\s{]+)', 'Multiline')
foreach ($match in $stringMatches) {
$match.Groups[1].Value
[pscustomobject]@{
Type = $match.Groups[1].Value
Name = $match.Groups[2].Value
}
}
}
}
15 changes: 15 additions & 0 deletions tests/src/classes/Book.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,18 @@ class BookList {
}
}
}

enum Binding {
Hardcover
Paperback
EBook
}

enum Genre {
Mystery
Thriller
Romance
ScienceFiction
Fantasy
Horror
}
15 changes: 15 additions & 0 deletions tests/srcWithManifest/classes/Book.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,18 @@ class BookList {
}
}
}

enum Binding {
Hardcover
Paperback
EBook
}

enum Genre {
Mystery
Thriller
Romance
ScienceFiction
Fantasy
Horror
}

0 comments on commit c467345

Please sign in to comment.