|
| 1 | +[Cmdletbinding()] |
| 2 | +param() |
| 3 | + |
| 4 | +Write-Verbose 'Importing subcomponents' |
| 5 | +$Folders = 'init', 'classes', 'private', 'public' |
| 6 | +# Import everything in these folders |
| 7 | +Foreach ($Folder in $Folders) { |
| 8 | + $Root = Join-Path -Path $PSScriptRoot -ChildPath $Folder |
| 9 | + Write-Verbose "Processing folder: $Root" |
| 10 | + if (Test-Path -Path $Root) { |
| 11 | + Write-Verbose "Getting all files in $Root" |
| 12 | + $Files = $null |
| 13 | + $Files = Get-ChildItem -Path $Root -Include '*.ps1', '*.psm1' -Recurse |
| 14 | + # dot source each file |
| 15 | + foreach ($File in $Files) { |
| 16 | + Write-Verbose "Importing $($File)" |
| 17 | + Import-Module $File |
| 18 | + Write-Verbose "Importing $($File): Done" |
| 19 | + } |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +. "$PSScriptRoot\finally.ps1" |
| 24 | + |
| 25 | +# Define the types to export with type accelerators. |
| 26 | +$ExportableTypes = @( |
| 27 | + [Book] |
| 28 | + [BookList] |
| 29 | +) |
| 30 | + |
| 31 | +# Get the internal TypeAccelerators class to use its static methods. |
| 32 | +$TypeAcceleratorsClass = [psobject].Assembly.GetType( |
| 33 | + 'System.Management.Automation.TypeAccelerators' |
| 34 | +) |
| 35 | +# Ensure none of the types would clobber an existing type accelerator. |
| 36 | +# If a type accelerator with the same name exists, throw an exception. |
| 37 | +$ExistingTypeAccelerators = $TypeAcceleratorsClass::Get |
| 38 | +foreach ($Type in $ExportableTypes) { |
| 39 | + if ($Type.FullName -in $ExistingTypeAccelerators.Keys) { |
| 40 | + $Message = @( |
| 41 | + "Unable to register type accelerator '$($Type.FullName)'" |
| 42 | + 'Accelerator already exists.' |
| 43 | + ) -join ' - ' |
| 44 | + |
| 45 | + throw [System.Management.Automation.ErrorRecord]::new( |
| 46 | + [System.InvalidOperationException]::new($Message), |
| 47 | + 'TypeAcceleratorAlreadyExists', |
| 48 | + [System.Management.Automation.ErrorCategory]::InvalidOperation, |
| 49 | + $Type.FullName |
| 50 | + ) |
| 51 | + } |
| 52 | +} |
| 53 | +# Add type accelerators for every exportable type. |
| 54 | +foreach ($Type in $ExportableTypes) { |
| 55 | + $TypeAcceleratorsClass::Add($Type.FullName, $Type) |
| 56 | +} |
| 57 | +# Remove type accelerators when the module is removed. |
| 58 | +$MyInvocation.MyCommand.ScriptBlock.Module.OnRemove = { |
| 59 | + foreach ($Type in $ExportableTypes) { |
| 60 | + $TypeAcceleratorsClass::Remove($Type.FullName) |
| 61 | + } |
| 62 | +}.GetNewClosure() |
| 63 | + |
| 64 | +$Param = @{ |
| 65 | + Function = (Get-ChildItem -Path "$PSScriptRoot\public" -Include '*.ps1' -Recurse).BaseName |
| 66 | + Variable = '*' |
| 67 | + Cmdlet = '*' |
| 68 | + Alias = '*' |
| 69 | +} |
| 70 | + |
| 71 | +Write-Verbose 'Exporting module members' |
| 72 | + |
| 73 | +Export-ModuleMember @Param |
0 commit comments