|
1 | 1 | [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 | +)] |
2 | 10 | [CmdletBinding()] |
3 | 11 | param() |
4 | 12 |
|
5 | 13 | $scriptName = $MyInvocation.MyCommand.Name |
6 | 14 | Write-Debug "[$scriptName] Importing module" |
7 | 15 |
|
| 16 | +#region - IsWindows compatibility shim |
| 17 | +if ($PSEdition -eq 'Desktop') { |
| 18 | + $IsWindows = $true |
| 19 | +} |
| 20 | +#endregion - IsWindows compatibility shim |
| 21 | + |
8 | 22 | #region - Data import |
9 | 23 | Write-Debug "[$scriptName] - [data] - Processing folder" |
10 | 24 | $dataFolder = (Join-Path $PSScriptRoot 'data') |
@@ -377,6 +391,48 @@ Write-Verbose '------------------------------' |
377 | 391 | Write-Debug "[$scriptName] - [/finally.ps1] - Done" |
378 | 392 | #endregion - From /finally.ps1 |
379 | 393 |
|
| 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 | + |
380 | 436 | $exports = @{ |
381 | 437 | Cmdlet = '' |
382 | 438 | Alias = '*' |
|
0 commit comments