Skip to content

Commit 1021f29

Browse files
test: add class exporter and IsWindows shim boilerplate to test module
1 parent 17f9456 commit 1021f29

1 file changed

Lines changed: 56 additions & 0 deletions

File tree

tests/outputTestRepo/outputs/module/PSModuleTest/PSModuleTest.psm1

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
11
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidLongLines', '', Justification = 'Contains long links.')]
2+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
3+
'PSAvoidAssignmentToAutomaticVariable', 'IsWindows',
4+
Justification = 'IsWindows does not exist in PS5.1'
5+
)]
6+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
7+
'PSUseDeclaredVarsMoreThanAssignments', 'IsWindows',
8+
Justification = 'IsWindows does not exist in PS5.1'
9+
)]
210
[CmdletBinding()]
311
param()
412

513
$scriptName = $MyInvocation.MyCommand.Name
614
Write-Debug "[$scriptName] Importing module"
715

16+
#region - IsWindows compatibility shim
17+
if ($PSEdition -eq 'Desktop') {
18+
$IsWindows = $true
19+
}
20+
#endregion - IsWindows compatibility shim
21+
822
#region - Data import
923
Write-Debug "[$scriptName] - [data] - Processing folder"
1024
$dataFolder = (Join-Path $PSScriptRoot 'data')
@@ -377,6 +391,48 @@ Write-Verbose '------------------------------'
377391
Write-Debug "[$scriptName] - [/finally.ps1] - Done"
378392
#endregion - From /finally.ps1
379393

394+
#region Class exporter
395+
# Get the internal TypeAccelerators class to use its static methods.
396+
$TypeAcceleratorsClass = [psobject].Assembly.GetType(
397+
'System.Management.Automation.TypeAccelerators'
398+
)
399+
# Ensure none of the types would clobber an existing type accelerator.
400+
# If a type accelerator with the same name exists, throw an exception.
401+
$ExistingTypeAccelerators = $TypeAcceleratorsClass::Get
402+
# Define the types to export with type accelerators.
403+
$ExportableEnums = @(
404+
)
405+
$ExportableEnums | Foreach-Object { Write-Verbose "Exporting enum '$($_.FullName)'." }
406+
foreach ($Type in $ExportableEnums) {
407+
if ($Type.FullName -in $ExistingTypeAccelerators.Keys) {
408+
Write-Verbose "Enum already exists [$($Type.FullName)]. Skipping."
409+
} else {
410+
Write-Verbose "Importing enum '$Type'."
411+
$TypeAcceleratorsClass::Add($Type.FullName, $Type)
412+
}
413+
}
414+
$ExportableClasses = @(
415+
[Book]
416+
[BookList]
417+
)
418+
$ExportableClasses | Foreach-Object { Write-Verbose "Exporting class '$($_.FullName)'." }
419+
foreach ($Type in $ExportableClasses) {
420+
if ($Type.FullName -in $ExistingTypeAccelerators.Keys) {
421+
Write-Verbose "Class already exists [$($Type.FullName)]. Skipping."
422+
} else {
423+
Write-Verbose "Importing class '$Type'."
424+
$TypeAcceleratorsClass::Add($Type.FullName, $Type)
425+
}
426+
}
427+
428+
# Remove type accelerators when the module is removed.
429+
$MyInvocation.MyCommand.ScriptBlock.Module.OnRemove = {
430+
foreach ($Type in ($ExportableEnums + $ExportableClasses)) {
431+
$null = $TypeAcceleratorsClass::Remove($Type.FullName)
432+
}
433+
}.GetNewClosure()
434+
#endregion Class exporter
435+
380436
$exports = @{
381437
Cmdlet = ''
382438
Alias = '*'

0 commit comments

Comments
 (0)