Bsn.SystemManagement.psm1
|
#Requires -Version 5.1 <# Module loader for Bsn.SystemManagement: imports the shared Bsn.Common helpers and dot-sources every function file under Public/ and Private/. The tests/ and scripts/ folders are skipped. Function files start with 'using module Bsn.Common', which resolves the module through PSModulePath at parse time; the probe below guarantees resolvability (sibling repo module during development, gallery copy for end users) BEFORE the function files are dot-sourced. #> Set-StrictMode -Version 'Latest' $script:ModuleRoot = $PSScriptRoot $script:SuspendActionScriptPath = Join-Path -Path $script:ModuleRoot -ChildPath 'scripts/Suspend-Action.ps1' if (-not (Get-Module -Name 'Bsn.Common' -ErrorAction 'SilentlyContinue')) { # Prefer the repo sibling module during development; fall back to the gallery copy. $commonManifestPath = Join-Path -Path (Split-Path -Parent -Path $script:ModuleRoot) -ChildPath 'Bsn.Common/Bsn.Common.psd1' if (Test-Path -LiteralPath $commonManifestPath) { Import-Module -Name $commonManifestPath -ErrorAction 'Stop' } else { Import-Module -Name 'Bsn.Common' -ErrorAction 'Stop' } } # 'using module Bsn.Common' in the function files resolves by name through # PSModulePath at parse time - even when the module is already loaded - so make # the repo sibling discoverable before those files are dot-sourced. $repoSourceDirectory = Split-Path -Parent -Path $script:ModuleRoot if ((Test-Path -LiteralPath (Join-Path -Path $repoSourceDirectory -ChildPath 'Bsn.Common/Bsn.Common.psd1')) -and (($env:PSModulePath -split [System.IO.Path]::PathSeparator) -notcontains $repoSourceDirectory)) { $env:PSModulePath = '{0}{1}{2}' -f $repoSourceDirectory, [System.IO.Path]::PathSeparator, $env:PSModulePath } $functionSearchPaths = @( (Join-Path -Path $script:ModuleRoot -ChildPath 'Public'), (Join-Path -Path $script:ModuleRoot -ChildPath 'Private') ) | Where-Object -FilterScript { Test-Path -LiteralPath $_ } $functionFiles = Get-ChildItem -Path $functionSearchPaths -Filter '*.ps1' -Recurse -File -ErrorAction 'SilentlyContinue' foreach ($functionFile in $functionFiles) { . $functionFile.FullName } # Delegate the interop assembly load to Bsn.Common's private helper (issue # #15): reimporting the SAME release reuses what the session already hosts, # while importing a NEWER release after Update-Module stops with explicit # guidance, because one process cannot host one assembly under two versions. # The helper is invoked through the newest loaded Bsn.Common instance (old and # new copies coexist after an update; a bare array would stringify into a # bogus command). Interop types bind at call time, so the late placement is # equivalent to the manifest's RequiredAssemblies timing. $systemInteropHost = Get-Module -Name 'Bsn.Common' | Sort-Object -Property Version -Descending | Select-Object -First 1 & $systemInteropHost { param($moduleDirectory, $assemblySimpleName) Import-BsnInteropAssembly -ModuleRoot $moduleDirectory -AssemblyName $assemblySimpleName } $script:ModuleRoot 'Bsn.SystemManagement' |