DrawManagement.psm1

#Requires -Version 7.0
# ============================================================
# DrawManagement.psm1 — Root Module
# Classes are dot-sourced here (one definition, in the module) and
# published to the whole session as type accelerators (section 1b),
# so [DrawFile], [DrawPage], [DrawCell] ... resolve wherever the
# module was imported from: a script, a function, a Pester block.
# [FIX-40] 1.3.1: replaces ScriptsToProcess, see section 1b.
# ============================================================

Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'

# ── 1. Classes ───────────────────────────────────────────────────────
# [FIX-31] Join-Path, not "$PSScriptRoot\Classes". A backslash is only a
# directory separator on Windows; on Linux and macOS it is an ordinary
# filename character, so these three paths named directories that cannot
# exist and Get-ChildItem failed. With $ErrorActionPreference = 'Stop' set
# four lines above, that is a terminating error during import — the module
# did not load at all on non-Windows, reporting
# Cannot find path '/.../DrawManagement\Classes' because it does not exist.
# The manifest declares CompatiblePSEditions = Core with no OS constraint
# and Install.ps1 documents the Linux and macOS module paths, so this was
# advertised as supported.
$classFiles = Get-ChildItem -Path (Join-Path $PSScriptRoot 'Classes') -Filter '*.ps1' -Recurse
foreach ($f in $classFiles) { . $f.FullName }

# ── 1b. Publish the classes and enums as type accelerators ───────────
# [FIX-40] Up to 1.3.0 the manifest ran Classes/DrawClasses.ps1 through
# ScriptsToProcess, i.e. in the IMPORTER's scope, and the public functions
# resolved [DrawFile] / [DrawPage] / [DrawCell] there. That only worked when
# Import-Module ran at script or global scope. Imported from inside a
# function (a wrapper such as PsReport.Core's Import-PsReportModule), the
# types vanished when that function returned: New-DrawFile returned nothing
# and every cmdlet failed with "Unable to find type [DrawPage]". Importing
# the .psm1 directly failed the same way.
# A type accelerator is session-wide, so the names resolve from any scope.
# This is the pattern documented in about_Classes ("Exporting classes with
# type accelerators"). Removed again when the module is removed.
$script:ExportedTypes = @(
    [DrawEdgeStyle], [DrawArrowType], [DrawShapeCategory], [DrawFontStyle],
    [DrawCell], [DrawEdge], [DrawPage], [DrawStencil], [DrawFile]
)
$script:TypeAccelerators = [psobject].Assembly.GetType('System.Management.Automation.TypeAccelerators')
$existingAccelerators = $script:TypeAccelerators::Get
foreach ($t in $script:ExportedTypes) {
    if ($existingAccelerators.ContainsKey($t.FullName)) {
        $prior = $existingAccelerators[$t.FullName]
        # An earlier import of this module (Import-Module -Force, a second
        # version): its class lives in a dynamic PowerShell class assembly.
        # Anything else is another product's type with the same name.
        if (-not $prior.Assembly.IsDynamic) {
            throw "DrawManagement: cannot register type [$($t.FullName)]: the name is already used by $($prior.AssemblyQualifiedName)."
        }
        $null = $script:TypeAccelerators::Remove($t.FullName)
    }
    $script:TypeAccelerators::Add($t.FullName, $t)
}
$ExecutionContext.SessionState.Module.OnRemove = {
    foreach ($t in $script:ExportedTypes) {
        # Only remove what is still ours (a newer import may have replaced it).
        if ($script:TypeAccelerators::Get[$t.FullName] -eq $t) { $null = $script:TypeAccelerators::Remove($t.FullName) }
    }
}.GetNewClosure()

# ── 2. Private helpers ───────────────────────────────────────────────
$privateFiles = Get-ChildItem -Path (Join-Path $PSScriptRoot 'Private') -Filter '*.ps1' -Recurse
foreach ($f in $privateFiles) {
    . $f.FullName
}

# ── 3. Public functions ──────────────────────────────────────────────
$publicFiles = Get-ChildItem -Path (Join-Path $PSScriptRoot 'Public') -Filter '*.ps1' -Recurse
foreach ($f in $publicFiles) {
    . $f.FullName
}

# ── Module-scoped state (accessible inside the module) ───────────────
# Read the version from the manifest rather than duplicating it — the
# hard-coded copy here sat at '1.0.0' for two releases.
$script:ModuleRoot    = $PSScriptRoot
$script:ModuleVersion = try {
    (Import-PowerShellDataFile -Path (Join-Path $PSScriptRoot 'DrawManagement.psd1')).ModuleVersion
} catch { 'unknown' }

# ── On-load message ──────────────────────────────────────────────────
Write-Verbose "DrawManagement v$script:ModuleVersion loaded. Run 'Get-DrawShapeTypes' to see available shapes."