diff --git a/CLAUDE.md b/CLAUDE.md index faa27af9..b14fb016 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -13,6 +13,7 @@ - `pwsh -NoProfile -File ./tools/Invoke-IdleScriptAnalyzer.ps1` - **Generated references** under `docs/reference/` are never edited by hand. Regenerate them with `./tools/Generate-IdleCmdletReference.ps1` and `./tools/Generate-IdleStepReference.ps1` (see `CONTRIBUTING.md`). -- **Branch names** for issue work: `issues/-`. +- **Branch names** follow the branching model in `CONTRIBUTING.md` (`/-`, + e.g. `feature/292-...`). - On pull requests, CI restores `.claude/` and `CLAUDE.md` from the base branch. Changes to these files only take effect for reviews after they are merged. diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index d60173b9..54e64fdf 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -76,9 +76,17 @@ git clone https://github.com/blindzero/IdentityLifecycleEngine.git ### Branching Model - `main` → stable -- feature branches: - - `feature/` - - `fix/` +- work branches: `/-` (omit `-` if there is no issue) + +| Prefix | Use for | +| --- | --- | +| `feature/` | new functionality | +| `fix/` or `hotfix/` | bug fixes or urgent fixes for a released version | +| `ci/` | CI/CD workflows and build tooling | +| `docs/` | documentation only | +| `chore/` | maintenance (dependencies, cleanup, repo housekeeping) | + +Example: `feature/292-step-handler-registration-via-catalog` --- diff --git a/docs/extend/steps.md b/docs/extend/steps.md index 5f06b04b..4f80e4e4 100644 --- a/docs/extend/steps.md +++ b/docs/extend/steps.md @@ -95,9 +95,57 @@ Every step type registered with the engine **must** declare a data-only metadata - A key must not appear in both sets. - Metadata must be data-only — ScriptBlocks are rejected. -Step pack modules expose their catalog via `Get-IdleStepMetadataCatalog`, which loads from a -`StepMetadataCatalog.psd1` data file. Host-supplied step types may supplement (but not override) -the catalog via `Providers.StepMetadata`. +Host-supplied step types may supplement (but not override) the catalog via `Providers.StepMetadata`. + +### Step packs + +A step pack is a module that ships a `StepMetadataCatalog.psd1` data file and declares it in its +module manifest. The engine discovers every loaded module carrying this marker: + +```powershell +# Contoso.IdLE.Steps.Hr.psd1 +@{ + # ... + FunctionsToExport = @('Invoke-ContosoStepSyncManager') + PrivateData = @{ + IdLE = @{ + Kind = 'StepPack' + StepCatalog = 'StepMetadataCatalog.psd1' # relative to the module folder + } + } +} +``` + +Each catalog entry declares its handler with the `Handler` key. The engine registers it automatically, +so hosts do not need `Providers.StepRegistry` for step pack steps: + +```powershell +# Contoso.IdLE.Steps.Hr/StepMetadataCatalog.psd1 +@{ + 'Contoso.IdLE.Step.Hr.SyncManager' = @{ + Handler = 'Invoke-ContosoStepSyncManager' + RequiredCapabilities = @('Contoso.IdLE.Hr.Manager.Read') + WithSchema = @{ + RequiredKeys = @('IdentityKey') + OptionalKeys = @('Provider', 'AuthSessionName', 'AuthSessionOptions') + } + } +} +``` + +**Rules:** + +- `Handler` is a function name (string) exported by the step pack module. It is bound to the declaring + module (`Contoso.IdLE.Steps.Hr\Invoke-ContosoStepSyncManager`); a handler from another module is rejected. +- The module does not need to be imported globally; nested modules work. +- `StepCatalog` must point to a file inside the module folder. +- A step type may be defined by only one step pack. +- `Providers.StepRegistry` still takes precedence over catalog handlers (e.g. to replace a step in tests). +- `Providers.StepMetadata` must not declare `Handler`; host-defined steps register their handler via + `Providers.StepRegistry`. + +Modules named `IdLE.Steps.*` that export `Get-IdleStepMetadataCatalog` but carry no marker are still +discovered. This fallback is deprecated. Metadata exists to make steps: diff --git a/src/IdLE.Core/Private/ConvertTo-IdleWorkflowSteps.ps1 b/src/IdLE.Core/Private/ConvertTo-IdleWorkflowSteps.ps1 index a9d2b3cc..f0c8b05e 100644 --- a/src/IdLE.Core/Private/ConvertTo-IdleWorkflowSteps.ps1 +++ b/src/IdLE.Core/Private/ConvertTo-IdleWorkflowSteps.ps1 @@ -109,7 +109,7 @@ function ConvertTo-IdleWorkflowSteps { else { # Workflow references a Step.Type for which no StepMetadata entry is available - fail fast. $errorMessage = "MissingStepTypeMetadata: Workflow step '$stepName' references step type '$stepType' which has no metadata entry. " + ` - "To resolve this: (1) Import/load the step pack module (IdLE.Steps.*) that provides metadata for '$stepType' via Get-IdleStepMetadataCatalog, OR " + ` + "To resolve this: (1) Import/load the step pack module (manifest PrivateData.IdLE.Kind = 'StepPack') that provides metadata for '$stepType', OR " + ` "(2) For host-defined/custom step types only, provide Providers.StepMetadata['$stepType'] = @{ RequiredCapabilities = @(...) }." throw [System.InvalidOperationException]::new($errorMessage) } diff --git a/src/IdLE.Core/Private/Get-IdleStepRegistry.ps1 b/src/IdLE.Core/Private/Get-IdleStepRegistry.ps1 index 9bffc009..ade5de01 100644 --- a/src/IdLE.Core/Private/Get-IdleStepRegistry.ps1 +++ b/src/IdLE.Core/Private/Get-IdleStepRegistry.ps1 @@ -18,45 +18,6 @@ function Get-IdleStepRegistry { $registry = [hashtable]::new([System.StringComparer]::OrdinalIgnoreCase) - # Helper: Resolve a step handler name without requiring global command exports. - # - # Resolution order: - # 1) Global command discovery (host imported a module globally) -> "Invoke-IdleStepX" - # 2) Module-scoped discovery (nested/hidden module loaded) -> "ModuleName\Invoke-IdleStepX" - function Resolve-IdleStepHandlerName { - [CmdletBinding()] - param( - [Parameter(Mandatory)] - [ValidateNotNullOrEmpty()] - [string] $CommandName, - - [Parameter(Mandatory)] - [ValidateNotNullOrEmpty()] - [string] $ModuleName - ) - - # 1) Global discovery (optional; supports hosts that import step packs globally) - $cmd = Get-Command -Name $CommandName -ErrorAction SilentlyContinue - if ($null -ne $cmd) { - return $cmd.Name - } - - # 2) Module-scoped discovery (supports nested modules that are not globally exported) - $module = Get-Module -Name $ModuleName -All | Select-Object -First 1 - if ($null -eq $module) { - return $null - } - - if ($null -ne $module.ExportedCommands -and $module.ExportedCommands.ContainsKey($CommandName)) { - - # Use a module-qualified command name so the engine can invoke it without relying on - # global session exports. This keeps built-in steps available "within IdLE" only. - return "$($module.Name)\$CommandName" - } - - return $null - } - # 1) Copy host-provided StepRegistry (optional) # We support two shapes for compatibility: # - Providers.StepRegistry (hashtable) @@ -102,121 +63,21 @@ function Get-IdleStepRegistry { } } - # 2) Register built-in steps if available. + # 2) Register handlers declared by loaded step packs (StepMetadataCatalog 'Handler' key). # - # Built-in steps are first-party step packs (e.g. IdLE.Steps.Common). They may be loaded as nested - # modules by the IdLE meta module. In that case, the step commands are not necessarily exported - # globally. We therefore support module-qualified handler names. - if (-not $registry.ContainsKey('IdLE.Step.EmitEvent')) { - $handler = Resolve-IdleStepHandlerName -CommandName 'Invoke-IdleStepEmitEvent' -ModuleName 'IdLE.Steps.Common' - if (-not [string]::IsNullOrWhiteSpace($handler)) { - $registry['IdLE.Step.EmitEvent'] = $handler - } - } - - if (-not $registry.ContainsKey('IdLE.Step.EnsureAttributes')) { - $handler = Resolve-IdleStepHandlerName -CommandName 'Invoke-IdleStepEnsureAttributes' -ModuleName 'IdLE.Steps.Common' - if (-not [string]::IsNullOrWhiteSpace($handler)) { - $registry['IdLE.Step.EnsureAttributes'] = $handler - } - } - - if (-not $registry.ContainsKey('IdLE.Step.EnsureEntitlement')) { - $handler = Resolve-IdleStepHandlerName -CommandName 'Invoke-IdleStepEnsureEntitlement' -ModuleName 'IdLE.Steps.Common' - if (-not [string]::IsNullOrWhiteSpace($handler)) { - $registry['IdLE.Step.EnsureEntitlement'] = $handler - } - } - - if (-not $registry.ContainsKey('IdLE.Step.CreateIdentity')) { - $handler = Resolve-IdleStepHandlerName -CommandName 'Invoke-IdleStepCreateIdentity' -ModuleName 'IdLE.Steps.Common' - if (-not [string]::IsNullOrWhiteSpace($handler)) { - $registry['IdLE.Step.CreateIdentity'] = $handler - } - } - - if (-not $registry.ContainsKey('IdLE.Step.DisableIdentity')) { - $handler = Resolve-IdleStepHandlerName -CommandName 'Invoke-IdleStepDisableIdentity' -ModuleName 'IdLE.Steps.Common' - if (-not [string]::IsNullOrWhiteSpace($handler)) { - $registry['IdLE.Step.DisableIdentity'] = $handler - } - } - - if (-not $registry.ContainsKey('IdLE.Step.EnableIdentity')) { - $handler = Resolve-IdleStepHandlerName -CommandName 'Invoke-IdleStepEnableIdentity' -ModuleName 'IdLE.Steps.Common' - if (-not [string]::IsNullOrWhiteSpace($handler)) { - $registry['IdLE.Step.EnableIdentity'] = $handler - } - } - - if (-not $registry.ContainsKey('IdLE.Step.MoveIdentity')) { - $handler = Resolve-IdleStepHandlerName -CommandName 'Invoke-IdleStepMoveIdentity' -ModuleName 'IdLE.Steps.Common' - if (-not [string]::IsNullOrWhiteSpace($handler)) { - $registry['IdLE.Step.MoveIdentity'] = $handler - } - } - - if (-not $registry.ContainsKey('IdLE.Step.DeleteIdentity')) { - $handler = Resolve-IdleStepHandlerName -CommandName 'Invoke-IdleStepDeleteIdentity' -ModuleName 'IdLE.Steps.Common' - if (-not [string]::IsNullOrWhiteSpace($handler)) { - $registry['IdLE.Step.DeleteIdentity'] = $handler - } - } - - if (-not $registry.ContainsKey('IdLE.Step.RevokeIdentitySessions')) { - $handler = Resolve-IdleStepHandlerName -CommandName 'Invoke-IdleStepRevokeIdentitySessions' -ModuleName 'IdLE.Steps.Common' - if (-not [string]::IsNullOrWhiteSpace($handler)) { - $registry['IdLE.Step.RevokeIdentitySessions'] = $handler - } - } - - if (-not $registry.ContainsKey('IdLE.Step.PruneEntitlements')) { - $handler = Resolve-IdleStepHandlerName -CommandName 'Invoke-IdleStepPruneEntitlements' -ModuleName 'IdLE.Steps.Common' - if (-not [string]::IsNullOrWhiteSpace($handler)) { - $registry['IdLE.Step.PruneEntitlements'] = $handler - } - } + # First-party and third-party step packs declare their handlers the same way. Handler names are + # module-qualified, so step packs loaded as nested modules (e.g. by the IdLE meta module) work without + # exporting their commands globally. Host-provided entries from (1) take precedence. + $stepPackCatalog = Resolve-IdleStepMetadataCatalog -Providers $null - if (-not $registry.ContainsKey('IdLE.Step.PruneEntitlementsEnsureKeep')) { - $handler = Resolve-IdleStepHandlerName -CommandName 'Invoke-IdleStepPruneEntitlementsEnsureKeep' -ModuleName 'IdLE.Steps.Common' - if (-not [string]::IsNullOrWhiteSpace($handler)) { - $registry['IdLE.Step.PruneEntitlementsEnsureKeep'] = $handler + foreach ($stepType in $stepPackCatalog.Keys) { + if ($registry.ContainsKey($stepType)) { + continue } - } - - if (-not $registry.ContainsKey('IdLE.Step.TriggerDirectorySync')) { - $handler = Resolve-IdleStepHandlerName -CommandName 'Invoke-IdleStepTriggerDirectorySync' -ModuleName 'IdLE.Steps.DirectorySync' - if (-not [string]::IsNullOrWhiteSpace($handler)) { - $registry['IdLE.Step.TriggerDirectorySync'] = $handler - } - } - - # Mailbox steps (IdLE.Steps.Mailbox module) - if (-not $registry.ContainsKey('IdLE.Step.Mailbox.EnsureOutOfOffice')) { - $handler = Resolve-IdleStepHandlerName -CommandName 'Invoke-IdleStepMailboxOutOfOfficeEnsure' -ModuleName 'IdLE.Steps.Mailbox' - if (-not [string]::IsNullOrWhiteSpace($handler)) { - $registry['IdLE.Step.Mailbox.EnsureOutOfOffice'] = $handler - } - } - - if (-not $registry.ContainsKey('IdLE.Step.Mailbox.GetInfo')) { - $handler = Resolve-IdleStepHandlerName -CommandName 'Invoke-IdleStepMailboxGetInfo' -ModuleName 'IdLE.Steps.Mailbox' - if (-not [string]::IsNullOrWhiteSpace($handler)) { - $registry['IdLE.Step.Mailbox.GetInfo'] = $handler - } - } - - if (-not $registry.ContainsKey('IdLE.Step.Mailbox.EnsureType')) { - $handler = Resolve-IdleStepHandlerName -CommandName 'Invoke-IdleStepMailboxTypeEnsure' -ModuleName 'IdLE.Steps.Mailbox' - if (-not [string]::IsNullOrWhiteSpace($handler)) { - $registry['IdLE.Step.Mailbox.EnsureType'] = $handler - } - } - if (-not $registry.ContainsKey('IdLE.Step.Mailbox.EnsurePermissions')) { - $handler = Resolve-IdleStepHandlerName -CommandName 'Invoke-IdleStepMailboxPermissionsEnsure' -ModuleName 'IdLE.Steps.Mailbox' - if (-not [string]::IsNullOrWhiteSpace($handler)) { - $registry['IdLE.Step.Mailbox.EnsurePermissions'] = $handler + $metadata = $stepPackCatalog[$stepType] + if ($metadata.ContainsKey('Handler')) { + $registry[$stepType] = $metadata['Handler'] } } diff --git a/src/IdLE.Core/Private/Resolve-IdleStepMetadataCatalog.ps1 b/src/IdLE.Core/Private/Resolve-IdleStepMetadataCatalog.ps1 index d3ff130c..206bd435 100644 --- a/src/IdLE.Core/Private/Resolve-IdleStepMetadataCatalog.ps1 +++ b/src/IdLE.Core/Private/Resolve-IdleStepMetadataCatalog.ps1 @@ -261,19 +261,130 @@ function Resolve-IdleStepMetadataCatalog { return $null } - # Helper: Discover loaded step packs exporting Get-IdleStepMetadataCatalog. - function Get-IdleStepPackModules { + # Helper: Validate a step pack 'Handler' value and bind it to the declaring module. + function ConvertTo-IdleStepPackHandlerName { [CmdletBinding()] - param() + param( + [Parameter()] + [AllowNull()] + [object] $Value, + + [Parameter(Mandatory)] + [string] $StepType, + + [Parameter(Mandatory)] + [string] $SourceName + ) - $loadedModules = Get-Module -Name 'IdLE.Steps.*' -All - if ($null -eq $loadedModules) { - return @() + if ($Value -isnot [string] -or [string]::IsNullOrWhiteSpace($Value)) { + throw [System.ArgumentException]::new( + "$SourceName entry for step type '$StepType' has an invalid 'Handler'. Expected a non-empty function name (string) exported by '$SourceName', e.g. 'Invoke-MyStep' or '$SourceName\Invoke-MyStep'.", + 'Providers' + ) } + $handler = $Value.Trim() + + # Unqualified names are bound to the declaring module so that a same-named function from another + # module (or the global session) cannot take over the step type. + if (-not $handler.Contains('\')) { + return "$SourceName\$handler" + } + + $parts = $handler.Split('\', 2) + if ($parts[0] -ne $SourceName -or [string]::IsNullOrWhiteSpace($parts[1])) { + throw [System.ArgumentException]::new( + "$SourceName entry for step type '$StepType' declares Handler '$handler' outside of its own module. A step pack may only register its own commands, e.g. '$SourceName\Invoke-MyStep'.", + 'Providers' + ) + } + + return $handler + } + + # Helper: Return the step pack marker from a module manifest, or $null if the module is no step pack. + # Marker shape: PrivateData = @{ IdLE = @{ Kind = 'StepPack'; StepCatalog = 'StepMetadataCatalog.psd1' } } + function Get-IdleStepPackMarker { + [CmdletBinding()] + param( + [Parameter(Mandatory)] + [System.Management.Automation.PSModuleInfo] $Module + ) + + $privateData = $Module.PrivateData + if ($privateData -isnot [hashtable] -or -not $privateData.ContainsKey('IdLE')) { + return $null + } + + $marker = $privateData['IdLE'] + if ($marker -isnot [hashtable] -or $marker['Kind'] -ne 'StepPack') { + return $null + } + + return $marker + } + + # Helper: Load the catalog file declared by a step pack marker (data-only PSD1 inside the module). + function Import-IdleStepPackCatalogFile { + [CmdletBinding()] + param( + [Parameter(Mandatory)] + [System.Management.Automation.PSModuleInfo] $Module, + + [Parameter(Mandatory)] + [hashtable] $Marker + ) + + $relativePath = $Marker['StepCatalog'] + if ($relativePath -isnot [string] -or [string]::IsNullOrWhiteSpace($relativePath) -or [System.IO.Path]::IsPathRooted($relativePath)) { + throw [System.ArgumentException]::new( + "Step pack '$($Module.Name)' must declare PrivateData.IdLE.StepCatalog as a path relative to the module folder, e.g. 'StepMetadataCatalog.psd1'.", + 'Providers' + ) + } + + # The catalog must ship with the module; paths escaping the module folder are rejected. + $moduleBase = [System.IO.Path]::GetFullPath($Module.ModuleBase).TrimEnd([System.IO.Path]::DirectorySeparatorChar, [System.IO.Path]::AltDirectorySeparatorChar) + $catalogPath = [System.IO.Path]::GetFullPath((Join-Path -Path $moduleBase -ChildPath $relativePath)) + if (-not $catalogPath.StartsWith($moduleBase + [System.IO.Path]::DirectorySeparatorChar, [System.StringComparison]::OrdinalIgnoreCase)) { + throw [System.ArgumentException]::new( + "Step pack '$($Module.Name)' declares StepCatalog '$relativePath' outside of its module folder.", + 'Providers' + ) + } + + if (-not (Test-Path -LiteralPath $catalogPath -PathType Leaf)) { + throw [System.ArgumentException]::new( + "Step pack '$($Module.Name)' declares StepCatalog '$relativePath', but the file was not found at '$catalogPath'.", + 'Providers' + ) + } + + $rawData = Import-PowerShellDataFile -LiteralPath $catalogPath + + $stepPackCatalog = [hashtable]::new([System.StringComparer]::OrdinalIgnoreCase) + foreach ($key in $rawData.Keys) { + $stepPackCatalog[$key] = $rawData[$key] + } + + return $stepPackCatalog + } + + # Helper: Discover loaded step packs. + # - Manifest marker PrivateData.IdLE.Kind = 'StepPack' (preferred). + # - Legacy: modules named 'IdLE.Steps.*' exporting Get-IdleStepMetadataCatalog. + function Get-IdleStepPackModules { + [CmdletBinding()] + param() + $stepPackModules = @() - foreach ($m in @($loadedModules)) { - if ($null -ne $m.ExportedCommands -and $m.ExportedCommands.ContainsKey('Get-IdleStepMetadataCatalog')) { + foreach ($m in @(Get-Module -All)) { + if ($null -ne (Get-IdleStepPackMarker -Module $m)) { + $stepPackModules += $m + continue + } + + if ($m.Name -like 'IdLE.Steps.*' -and $null -ne $m.ExportedCommands -and $m.ExportedCommands.ContainsKey('Get-IdleStepMetadataCatalog')) { $stepPackModules += $m } } @@ -338,6 +449,12 @@ function Resolve-IdleStepMetadataCatalog { throw [System.InvalidOperationException]::new($errorMessage) } + if ($value.ContainsKey('Handler')) { + # Copy before normalizing so the step pack's own catalog object stays untouched. + $value = $value.Clone() + $value['Handler'] = ConvertTo-IdleStepPackHandlerName -Value $value['Handler'] -StepType $key -SourceName $SourceModuleName + } + # Register ownership and add to catalog $StepTypeOwners[[string]$key] = $SourceModuleName $Target[[string]$key] = $value @@ -349,12 +466,16 @@ function Resolve-IdleStepMetadataCatalog { $stepPackModules = Get-IdleStepPackModules foreach ($module in $stepPackModules) { - $catalogFunction = $module.ExportedCommands['Get-IdleStepMetadataCatalog'] - if ($null -ne $catalogFunction) { - $stepPackCatalog = & $catalogFunction - if ($null -ne $stepPackCatalog -and $stepPackCatalog -is [hashtable]) { - Merge-IdleStepPackCatalog -Target $catalog -Source $stepPackCatalog -SourceModuleName $module.Name -StepTypeOwners $stepTypeOwners - } + $marker = Get-IdleStepPackMarker -Module $module + if ($null -ne $marker) { + $stepPackCatalog = Import-IdleStepPackCatalogFile -Module $module -Marker $marker + } + else { + $stepPackCatalog = & $module.ExportedCommands['Get-IdleStepMetadataCatalog'] + } + + if ($null -ne $stepPackCatalog -and $stepPackCatalog -is [hashtable]) { + Merge-IdleStepPackCatalog -Target $catalog -Source $stepPackCatalog -SourceModuleName $module.Name -StepTypeOwners $stepTypeOwners } } @@ -404,6 +525,15 @@ function Resolve-IdleStepMetadataCatalog { $withSchemaValue = if ($value.ContainsKey('WithSchema')) { $value['WithSchema'] } else { $null } Test-IdleWithSchema -Value $withSchemaValue -StepType $key -SourceName 'Providers.StepMetadata' + # Host handlers are registered via Providers.StepRegistry only, so that execution does not depend on + # the metadata passed at plan time. + if ($value.ContainsKey('Handler')) { + throw [System.ArgumentException]::new( + "Providers.StepMetadata entry for step type '$key' must not declare 'Handler'. Register host step handlers via Providers.StepRegistry['$key'].", + 'Providers' + ) + } + # Add host supplement $catalog[[string]$key] = $value $stepTypeOwners[[string]$key] = 'Host' diff --git a/src/IdLE.Steps.Common/IdLE.Steps.Common.psd1 b/src/IdLE.Steps.Common/IdLE.Steps.Common.psd1 index 83c8ebcd..0a0fcdb2 100644 --- a/src/IdLE.Steps.Common/IdLE.Steps.Common.psd1 +++ b/src/IdLE.Steps.Common/IdLE.Steps.Common.psd1 @@ -26,6 +26,10 @@ ) PrivateData = @{ + IdLE = @{ + Kind = 'StepPack' + StepCatalog = 'StepMetadataCatalog.psd1' + } PSData = @{ Tags = @('IdentityLifecycleEngine', 'IdLE', 'Steps', 'Common', 'Builtin') LicenseUri = 'https://www.apache.org/licenses/LICENSE-2.0' diff --git a/src/IdLE.Steps.Common/Public/Get-IdleStepMetadataCatalog.ps1 b/src/IdLE.Steps.Common/Public/Get-IdleStepMetadataCatalog.ps1 index bd8040d3..4de4d1d3 100644 --- a/src/IdLE.Steps.Common/Public/Get-IdleStepMetadataCatalog.ps1 +++ b/src/IdLE.Steps.Common/Public/Get-IdleStepMetadataCatalog.ps1 @@ -8,6 +8,7 @@ function Get-IdleStepMetadataCatalog { The catalog is defined in StepMetadataCatalog.psd1 (data-only, no ScriptBlocks). Each metadata object contains: + Handler - handler function of this step pack (bound to this module by the engine) RequiredCapabilities - capability identifiers the step requires from providers WithSchema - the With key contract used for plan-time validation diff --git a/src/IdLE.Steps.Common/StepMetadataCatalog.psd1 b/src/IdLE.Steps.Common/StepMetadataCatalog.psd1 index d83902c4..ec95fadc 100644 --- a/src/IdLE.Steps.Common/StepMetadataCatalog.psd1 +++ b/src/IdLE.Steps.Common/StepMetadataCatalog.psd1 @@ -1,9 +1,11 @@ # StepMetadataCatalog.psd1 - IdLE.Steps.Common # # Data-only metadata catalog for all common built-in IdLE step types. -# This file is loaded by Get-IdleStepMetadataCatalog and must remain data-only (no ScriptBlocks). +# This file is loaded by the engine (manifest PrivateData.IdLE.StepCatalog) and by Get-IdleStepMetadataCatalog. +# It must remain data-only (no ScriptBlocks). # # Each entry maps a Step.Type to a metadata hashtable containing: +# Handler - handler function exported by this module (bound to this module by the engine) # RequiredCapabilities - capability identifiers the step requires from providers # WithSchema - declares the With key contract for plan-time validation: # RequiredKeys - keys that MUST be present in With @@ -14,6 +16,7 @@ # Provider/AuthSessionName/AuthSessionOptions are accepted as optional routing hints: # the execution engine reads these from any step's With to resolve Request.Context.Current.*. 'IdLE.Step.EmitEvent' = @{ + Handler = 'Invoke-IdleStepEmitEvent' RequiredCapabilities = @() WithSchema = @{ RequiredKeys = @() @@ -23,6 +26,7 @@ # IdLE.Step.CreateIdentity - provisions a new identity via the identity provider 'IdLE.Step.CreateIdentity' = @{ + Handler = 'Invoke-IdleStepCreateIdentity' RequiredCapabilities = @('IdLE.Identity.Create') WithSchema = @{ RequiredKeys = @('IdentityKey', 'Attributes') @@ -32,6 +36,7 @@ # IdLE.Step.DisableIdentity - disables an existing identity via the identity provider 'IdLE.Step.DisableIdentity' = @{ + Handler = 'Invoke-IdleStepDisableIdentity' RequiredCapabilities = @('IdLE.Identity.Disable') WithSchema = @{ RequiredKeys = @('IdentityKey') @@ -41,6 +46,7 @@ # IdLE.Step.EnableIdentity - re-enables a disabled identity via the identity provider 'IdLE.Step.EnableIdentity' = @{ + Handler = 'Invoke-IdleStepEnableIdentity' RequiredCapabilities = @('IdLE.Identity.Enable') WithSchema = @{ RequiredKeys = @('IdentityKey') @@ -50,6 +56,7 @@ # IdLE.Step.DeleteIdentity - permanently removes an identity via the identity provider 'IdLE.Step.DeleteIdentity' = @{ + Handler = 'Invoke-IdleStepDeleteIdentity' RequiredCapabilities = @('IdLE.Identity.Delete') WithSchema = @{ RequiredKeys = @('IdentityKey') @@ -59,6 +66,7 @@ # IdLE.Step.MoveIdentity - moves an identity to a target container/OU 'IdLE.Step.MoveIdentity' = @{ + Handler = 'Invoke-IdleStepMoveIdentity' RequiredCapabilities = @('IdLE.Identity.Move') WithSchema = @{ RequiredKeys = @('IdentityKey', 'TargetContainer') @@ -68,6 +76,7 @@ # IdLE.Step.EnsureAttributes - idempotently sets attributes on an identity 'IdLE.Step.EnsureAttributes' = @{ + Handler = 'Invoke-IdleStepEnsureAttributes' RequiredCapabilities = @('IdLE.Identity.Attribute.Ensure') WithSchema = @{ RequiredKeys = @('IdentityKey', 'Attributes') @@ -77,6 +86,7 @@ # IdLE.Step.EnsureEntitlement - idempotently grants or revokes a single entitlement 'IdLE.Step.EnsureEntitlement' = @{ + Handler = 'Invoke-IdleStepEnsureEntitlement' RequiredCapabilities = @('IdLE.Entitlement.List', 'IdLE.Entitlement.Grant', 'IdLE.Entitlement.Revoke') WithSchema = @{ RequiredKeys = @('IdentityKey', 'Entitlement', 'State') @@ -86,6 +96,7 @@ # IdLE.Step.RevokeIdentitySessions - revokes all active sessions for an identity 'IdLE.Step.RevokeIdentitySessions' = @{ + Handler = 'Invoke-IdleStepRevokeIdentitySessions' RequiredCapabilities = @('IdLE.Identity.RevokeSessions') WithSchema = @{ RequiredKeys = @('IdentityKey') @@ -96,6 +107,7 @@ # IdLE.Step.PruneEntitlements - remove-only: removes entitlements not in Keep/KeepPattern # Requires explicit prune opt-in capability plus list/revoke 'IdLE.Step.PruneEntitlements' = @{ + Handler = 'Invoke-IdleStepPruneEntitlements' RequiredCapabilities = @('IdLE.Entitlement.Prune', 'IdLE.Entitlement.List', 'IdLE.Entitlement.Revoke') WithSchema = @{ RequiredKeys = @('IdentityKey', 'Kind') @@ -106,6 +118,7 @@ # IdLE.Step.PruneEntitlementsEnsureKeep - remove + ensure keep present: prune + grant-back # KeepPattern is NOT in OptionalKeys because patterns cannot be granted (they are filter-only). 'IdLE.Step.PruneEntitlementsEnsureKeep' = @{ + Handler = 'Invoke-IdleStepPruneEntitlementsEnsureKeep' RequiredCapabilities = @('IdLE.Entitlement.Prune', 'IdLE.Entitlement.List', 'IdLE.Entitlement.Revoke', 'IdLE.Entitlement.Grant') WithSchema = @{ RequiredKeys = @('IdentityKey', 'Kind') diff --git a/src/IdLE.Steps.DirectorySync/IdLE.Steps.DirectorySync.psd1 b/src/IdLE.Steps.DirectorySync/IdLE.Steps.DirectorySync.psd1 index 8eac1dbf..0aea07d2 100644 --- a/src/IdLE.Steps.DirectorySync/IdLE.Steps.DirectorySync.psd1 +++ b/src/IdLE.Steps.DirectorySync/IdLE.Steps.DirectorySync.psd1 @@ -19,6 +19,10 @@ ) PrivateData = @{ + IdLE = @{ + Kind = 'StepPack' + StepCatalog = 'StepMetadataCatalog.psd1' + } PSData = @{ Tags = @('IdentityLifecycleEngine', 'IdLE', 'Steps', 'DirectorySync', 'Synchronization') LicenseUri = 'https://www.apache.org/licenses/LICENSE-2.0' diff --git a/src/IdLE.Steps.DirectorySync/Public/Get-IdleStepMetadataCatalog.ps1 b/src/IdLE.Steps.DirectorySync/Public/Get-IdleStepMetadataCatalog.ps1 index 2eb8134b..f5c67011 100644 --- a/src/IdLE.Steps.DirectorySync/Public/Get-IdleStepMetadataCatalog.ps1 +++ b/src/IdLE.Steps.DirectorySync/Public/Get-IdleStepMetadataCatalog.ps1 @@ -8,6 +8,7 @@ function Get-IdleStepMetadataCatalog { The catalog is defined in StepMetadataCatalog.psd1 (data-only, no ScriptBlocks). Each metadata object contains: + Handler - handler function of this step pack (bound to this module by the engine) RequiredCapabilities - capability identifiers the step requires from providers WithSchema - the With key contract used for plan-time validation diff --git a/src/IdLE.Steps.DirectorySync/StepMetadataCatalog.psd1 b/src/IdLE.Steps.DirectorySync/StepMetadataCatalog.psd1 index 54109194..5f6241fd 100644 --- a/src/IdLE.Steps.DirectorySync/StepMetadataCatalog.psd1 +++ b/src/IdLE.Steps.DirectorySync/StepMetadataCatalog.psd1 @@ -1,9 +1,11 @@ # StepMetadataCatalog.psd1 - IdLE.Steps.DirectorySync # # Data-only metadata catalog for directory sync step types. -# This file is loaded by Get-IdleStepMetadataCatalog and must remain data-only (no ScriptBlocks). +# This file is loaded by the engine (manifest PrivateData.IdLE.StepCatalog) and by Get-IdleStepMetadataCatalog. +# It must remain data-only (no ScriptBlocks). # # Each entry maps a Step.Type to a metadata hashtable containing: +# Handler - handler function exported by this module (bound to this module by the engine) # RequiredCapabilities - capability identifiers the step requires from providers # WithSchema - declares the With key contract for plan-time validation: # RequiredKeys - keys that MUST be present in With @@ -13,6 +15,7 @@ # IdLE.Step.TriggerDirectorySync - triggers a directory sync cycle and optionally waits for completion # Note: Even when With.Wait = $false, Status capability is advertised to keep planning deterministic. 'IdLE.Step.TriggerDirectorySync' = @{ + Handler = 'Invoke-IdleStepTriggerDirectorySync' RequiredCapabilities = @('IdLE.DirectorySync.Trigger', 'IdLE.DirectorySync.Status') WithSchema = @{ RequiredKeys = @() diff --git a/src/IdLE.Steps.Mailbox/IdLE.Steps.Mailbox.psd1 b/src/IdLE.Steps.Mailbox/IdLE.Steps.Mailbox.psd1 index d5f5944f..f5c1f481 100644 --- a/src/IdLE.Steps.Mailbox/IdLE.Steps.Mailbox.psd1 +++ b/src/IdLE.Steps.Mailbox/IdLE.Steps.Mailbox.psd1 @@ -19,6 +19,10 @@ ) PrivateData = @{ + IdLE = @{ + Kind = 'StepPack' + StepCatalog = 'StepMetadataCatalog.psd1' + } PSData = @{ Tags = @('IdentityLifecycleEngine', 'IdLE', 'Steps', 'Mailbox') LicenseUri = 'https://www.apache.org/licenses/LICENSE-2.0' diff --git a/src/IdLE.Steps.Mailbox/Public/Get-IdleStepMetadataCatalog.ps1 b/src/IdLE.Steps.Mailbox/Public/Get-IdleStepMetadataCatalog.ps1 index 7a3a5e04..d54cc72c 100644 --- a/src/IdLE.Steps.Mailbox/Public/Get-IdleStepMetadataCatalog.ps1 +++ b/src/IdLE.Steps.Mailbox/Public/Get-IdleStepMetadataCatalog.ps1 @@ -8,6 +8,7 @@ function Get-IdleStepMetadataCatalog { The catalog is defined in StepMetadataCatalog.psd1 (data-only, no ScriptBlocks). Each metadata object contains: + Handler - handler function of this step pack (bound to this module by the engine) RequiredCapabilities - capability identifiers the step requires from providers WithSchema - the With key contract used for plan-time validation diff --git a/src/IdLE.Steps.Mailbox/StepMetadataCatalog.psd1 b/src/IdLE.Steps.Mailbox/StepMetadataCatalog.psd1 index 60b6f454..b087ab77 100644 --- a/src/IdLE.Steps.Mailbox/StepMetadataCatalog.psd1 +++ b/src/IdLE.Steps.Mailbox/StepMetadataCatalog.psd1 @@ -1,9 +1,11 @@ # StepMetadataCatalog.psd1 - IdLE.Steps.Mailbox # # Data-only metadata catalog for mailbox step types. -# This file is loaded by Get-IdleStepMetadataCatalog and must remain data-only (no ScriptBlocks). +# This file is loaded by the engine (manifest PrivateData.IdLE.StepCatalog) and by Get-IdleStepMetadataCatalog. +# It must remain data-only (no ScriptBlocks). # # Each entry maps a Step.Type to a metadata hashtable containing: +# Handler - handler function exported by this module (bound to this module by the engine) # RequiredCapabilities - capability identifiers the step requires from providers # WithSchema - declares the With key contract for plan-time validation: # RequiredKeys - keys that MUST be present in With @@ -12,6 +14,7 @@ @{ # IdLE.Step.Mailbox.GetInfo - reads mailbox details for an identity 'IdLE.Step.Mailbox.GetInfo' = @{ + Handler = 'Invoke-IdleStepMailboxGetInfo' RequiredCapabilities = @('IdLE.Mailbox.Info.Read') WithSchema = @{ RequiredKeys = @('IdentityKey') @@ -22,6 +25,7 @@ # IdLE.Step.Mailbox.EnsureType - idempotently converts a mailbox to the specified type # MailboxType accepts: User, Shared, Room, Equipment 'IdLE.Step.Mailbox.EnsureType' = @{ + Handler = 'Invoke-IdleStepMailboxTypeEnsure' RequiredCapabilities = @('IdLE.Mailbox.Info.Read', 'IdLE.Mailbox.Type.Ensure') WithSchema = @{ RequiredKeys = @('IdentityKey', 'MailboxType') @@ -33,6 +37,7 @@ # Config accepts: Mode (required), Start/End (required when Mode=Scheduled), # InternalMessage, ExternalMessage, ExternalAudience, MessageFormat (optional) 'IdLE.Step.Mailbox.EnsureOutOfOffice' = @{ + Handler = 'Invoke-IdleStepMailboxOutOfOfficeEnsure' RequiredCapabilities = @('IdLE.Mailbox.Info.Read', 'IdLE.Mailbox.OutOfOffice.Ensure') WithSchema = @{ RequiredKeys = @('IdentityKey', 'Config') @@ -43,6 +48,7 @@ # IdLE.Step.Mailbox.EnsurePermissions - idempotently manages delegate permissions # Permissions is an array of hashtables with: AssignedUser, Right, Ensure 'IdLE.Step.Mailbox.EnsurePermissions' = @{ + Handler = 'Invoke-IdleStepMailboxPermissionsEnsure' RequiredCapabilities = @('IdLE.Mailbox.Info.Read', 'IdLE.Mailbox.Permissions.Ensure') WithSchema = @{ RequiredKeys = @('IdentityKey', 'Permissions') diff --git a/tests/Core/StepPackHandlerRegistration.Tests.ps1 b/tests/Core/StepPackHandlerRegistration.Tests.ps1 new file mode 100644 index 00000000..902e406d --- /dev/null +++ b/tests/Core/StepPackHandlerRegistration.Tests.ps1 @@ -0,0 +1,205 @@ +Set-StrictMode -Version Latest + +BeforeAll { + . (Join-Path (Split-Path -Path $PSScriptRoot -Parent) '_testHelpers.ps1') + Import-IdleTestModule + Import-IdleTestMailboxModule + + $script:PackName = 'Contoso.IdLE.Steps.Hr' + $script:StepType = 'Contoso.IdLE.Step.Hr.Hello' + + # Creates a step pack module on disk that is discovered via the manifest marker only + # (its name does not match 'IdLE.Steps.*' and it exports no Get-IdleStepMetadataCatalog). + function New-TestStepPack { + param( + [Parameter(Mandatory)] + [string] $CatalogContent, + + [Parameter()] + [string] $StepCatalog = 'StepMetadataCatalog.psd1' + ) + + $moduleDir = Join-Path -Path $TestDrive -ChildPath ([guid]::NewGuid().ToString('N')) | Join-Path -ChildPath $script:PackName + $null = New-Item -Path $moduleDir -ItemType Directory -Force + + Set-Content -Path (Join-Path $moduleDir "$($script:PackName).psm1") -Encoding UTF8 -Value @' +function Invoke-ContosoStepHello { + [CmdletBinding()] + param( + [Parameter(Mandatory)] [object] $Context, + [Parameter(Mandatory)] [object] $Step + ) + + $Context.EventSink.WriteEvent('Custom', 'Hello from step pack', $Step.Name, @{}) + + return [pscustomobject]@{ + PSTypeName = 'IdLE.StepResult' + Name = [string]$Step.Name + Type = [string]$Step.Type + Status = 'Completed' + Error = $null + } +} + +Export-ModuleMember -Function 'Invoke-ContosoStepHello' +'@ + + Set-Content -Path (Join-Path $moduleDir "$($script:PackName).psd1") -Encoding UTF8 -Value @" +@{ + RootModule = '$($script:PackName).psm1' + ModuleVersion = '1.0.0' + GUID = '$([guid]::NewGuid())' + FunctionsToExport = @('Invoke-ContosoStepHello') + PrivateData = @{ + IdLE = @{ + Kind = 'StepPack' + StepCatalog = '$StepCatalog' + } + } +} +"@ + + Set-Content -Path (Join-Path $moduleDir 'StepMetadataCatalog.psd1') -Encoding UTF8 -Value $CatalogContent + + Import-Module -Name (Join-Path $moduleDir "$($script:PackName).psd1") -Force -ErrorAction Stop + } + + function New-TestCatalogContent { + param( + [Parameter(Mandatory)] + [string] $HandlerLiteral + ) + + return @" +@{ + '$($script:StepType)' = @{ + Handler = $HandlerLiteral + RequiredCapabilities = @() + WithSchema = @{ + RequiredKeys = @() + OptionalKeys = @() + } + } +} +"@ + } + + function New-TestPlan { + $wfPath = New-IdleTestWorkflowFile -FileName 'step-pack-handler.psd1' -Content @" +@{ + Name = 'Step pack handler' + LifecycleEvent = 'Joiner' + Steps = @( + @{ Name = 'Hello'; Type = '$($script:StepType)' } + ) +} +"@ + $req = New-IdleTestRequest -LifecycleEvent 'Joiner' + return New-IdlePlan -WorkflowPath $wfPath -Request $req + } +} + +Describe 'Step pack handler registration (StepMetadataCatalog Handler)' { + AfterEach { + Remove-Module -Name $script:PackName -Force -ErrorAction SilentlyContinue + } + + Context 'Third-party step pack with manifest marker' { + It 'executes the catalog handler without Providers.StepRegistry' { + New-TestStepPack -CatalogContent (New-TestCatalogContent -HandlerLiteral "'Invoke-ContosoStepHello'") + + $result = Invoke-IdlePlan -Plan (New-TestPlan) -Providers @{} + + $result.Status | Should -Be 'Completed' + $result.Steps[0].Status | Should -Be 'Completed' + ($result.Events | Where-Object Message -eq 'Hello from step pack').Count | Should -Be 1 + } + + It 'binds an unqualified handler name to the declaring module' { + New-TestStepPack -CatalogContent (New-TestCatalogContent -HandlerLiteral "'Invoke-ContosoStepHello'") + + InModuleScope 'IdLE.Core' -Parameters @{ StepType = $script:StepType } { + param($StepType) + $registry = Get-IdleStepRegistry -Providers $null + $registry[$StepType] | Should -Be 'Contoso.IdLE.Steps.Hr\Invoke-ContosoStepHello' + } + } + + It 'lets Providers.StepRegistry override the catalog handler' { + New-TestStepPack -CatalogContent (New-TestCatalogContent -HandlerLiteral "'Invoke-ContosoStepHello'") + + InModuleScope 'IdLE.Core' -Parameters @{ StepType = $script:StepType } { + param($StepType) + $registry = Get-IdleStepRegistry -Providers @{ StepRegistry = @{ $StepType = 'Invoke-HostHello' } } + $registry[$StepType] | Should -Be 'Invoke-HostHello' + } + } + } + + Context 'Validation' { + It 'rejects a handler from another module' { + New-TestStepPack -CatalogContent (New-TestCatalogContent -HandlerLiteral "'IdLE.Steps.Common\Invoke-IdleStepEmitEvent'") + + { New-TestPlan } | Should -Throw '*outside of its own module*' + } + + It 'rejects a non-string handler' { + New-TestStepPack -CatalogContent (New-TestCatalogContent -HandlerLiteral "@('Invoke-ContosoStepHello')") + + { New-TestPlan } | Should -Throw "*invalid 'Handler'*" + } + + It 'rejects a StepCatalog path outside of the module folder' { + New-TestStepPack -CatalogContent (New-TestCatalogContent -HandlerLiteral "'Invoke-ContosoStepHello'") -StepCatalog '..\StepMetadataCatalog.psd1' + + { New-TestPlan } | Should -Throw '*outside of its module folder*' + } + + It 'rejects a missing StepCatalog file' { + New-TestStepPack -CatalogContent (New-TestCatalogContent -HandlerLiteral "'Invoke-ContosoStepHello'") -StepCatalog 'Missing.psd1' + + { New-TestPlan } | Should -Throw '*was not found*' + } + + It 'rejects Handler in host-provided Providers.StepMetadata' { + $wfPath = New-IdleTestWorkflowFile -FileName 'host-handler.psd1' -Content @' +@{ + Name = 'Host handler' + LifecycleEvent = 'Joiner' + Steps = @( + @{ Name = 'Hello'; Type = 'Test.IdLE.Step.Hello' } + ) +} +'@ + $req = New-IdleTestRequest -LifecycleEvent 'Joiner' + $providers = @{ + StepMetadata = @{ + 'Test.IdLE.Step.Hello' = @{ + Handler = 'Invoke-HostHello' + RequiredCapabilities = @() + WithSchema = @{ RequiredKeys = @(); OptionalKeys = @() } + } + } + } + + { New-IdlePlan -WorkflowPath $wfPath -Request $req -Providers $providers } | Should -Throw '*Providers.StepRegistry*' + } + } + + Context 'First-party step packs' { + It 'declares a resolvable handler for every built-in step type' { + InModuleScope 'IdLE.Core' { + $catalog = Resolve-IdleStepMetadataCatalog -Providers $null + $registry = Get-IdleStepRegistry -Providers $null + + $builtInTypes = @($catalog.Keys | Where-Object { $_ -like 'IdLE.Step.*' }) + $builtInTypes.Count | Should -Be 16 + + foreach ($stepType in $builtInTypes) { + $catalog[$stepType].Handler | Should -Match '^IdLE\.Steps\.(Common|DirectorySync|Mailbox)\\Invoke-IdleStep' -Because $stepType + Resolve-IdleStepHandler -StepType $stepType -StepRegistry $registry | Should -Not -BeNullOrEmpty -Because $stepType + } + } + } + } +} diff --git a/tools/Generate-IdleStepReference.ps1 b/tools/Generate-IdleStepReference.ps1 index 766e3ab8..ce26e66a 100644 --- a/tools/Generate-IdleStepReference.ps1 +++ b/tools/Generate-IdleStepReference.ps1 @@ -169,16 +169,19 @@ function ConvertTo-IdleMdxSafeText { function Get-IdleStepTypeFromCommandName { <# .SYNOPSIS - Resolves the canonical step type(s) for a command by loading and inverting the step registry. - + Resolves the canonical step type(s) for a command from its step pack's metadata catalog. + .DESCRIPTION - Instead of deriving step types from command names (which can be ambiguous), - this function loads the step registry script and inverts it to find the actual - registered step type(s). - + Step packs declare the handler of each step type via the 'Handler' key in their + StepMetadataCatalog.psd1. This function loads the catalog of the command's module and + inverts the Handler entries to find the registered step type(s). + .PARAMETER CommandName The step handler command name (e.g., 'Invoke-IdleStepMailboxOutOfOfficeEnsure'). - + + .PARAMETER ModuleName + The module that exports the command (e.g., 'IdLE.Steps.Mailbox'). + .OUTPUTS Array of step type strings registered for this command, or empty array if not found. #> @@ -186,40 +189,38 @@ function Get-IdleStepTypeFromCommandName { param( [Parameter(Mandatory)] [ValidateNotNullOrEmpty()] - [string] $CommandName + [string] $CommandName, + + [Parameter(Mandatory)] + [ValidateNotNullOrEmpty()] + [string] $ModuleName ) - # Load the step registry script to access its mappings - $registryPath = Join-Path $script:repoRoot 'src' 'IdLE.Core' 'Private' 'Get-IdleStepRegistry.ps1' - - if (-not (Test-Path $registryPath)) { - Write-Warning "Step registry not found at: $registryPath" + # Disambiguate in case multiple versions of the module are loaded + $module = Get-Module -Name $ModuleName -All -ErrorAction SilentlyContinue | Select-Object -First 1 + if ($null -eq $module) { return @() } - # Read the registry file and extract step type → handler mappings - $registryContent = Get-Content -Path $registryPath -Raw - - # Scan for command name and find nearby step type - $lines = $registryContent -split "`n" - $stepTypes = @() - - for ($i = 0; $i -lt $lines.Count; $i++) { - if ($lines[$i] -match "CommandName\s+'$([regex]::Escape($CommandName))'") { - # Look backwards for the step type in the ContainsKey check - for ($j = $i; $j -ge [Math]::Max(0, $i - 10); $j--) { - if ($lines[$j] -match "ContainsKey\('([^']+)'\)") { - $stepType = $matches[1] - if ($stepType -and $stepType -notlike '*$*') { - $stepTypes += $stepType - break - } - } - } + $catalog = & $module { Get-IdleStepMetadataCatalog -ErrorAction SilentlyContinue } + if ($catalog -isnot [System.Collections.IDictionary]) { + return @() + } + + $stepTypes = foreach ($stepType in $catalog.Keys) { + $metadata = $catalog[$stepType] + if ($metadata -isnot [System.Collections.IDictionary] -or -not $metadata.Contains('Handler')) { + continue + } + + # Catalog handlers may be unqualified or module-qualified ('Module\Command'). + $handler = [string]$metadata['Handler'] + if ($handler -eq $CommandName -or $handler -eq "$ModuleName\$CommandName") { + $stepType } } - - return $stepTypes | Select-Object -Unique + + return @($stepTypes | Sort-Object -Unique) } function Get-IdleHelpSafe { @@ -462,17 +463,16 @@ function New-IdleStepDocModel { ) $commandName = $CommandInfo.Name - $stepTypes = @(Get-IdleStepTypeFromCommandName -CommandName $commandName) - + $moduleName = Get-IdleCommandModuleName -CommandInfo $CommandInfo + $stepTypes = @(Get-IdleStepTypeFromCommandName -CommandName $commandName -ModuleName $moduleName) + if ($stepTypes.Count -eq 0) { - Write-Warning "No step types found in registry for command: $commandName" + Write-Warning "No step types found in the step pack catalog for command: $commandName" return $null } # Use the first (or only) step type as the primary $stepType = $stepTypes[0] - - $moduleName = Get-IdleCommandModuleName -CommandInfo $CommandInfo $help = Get-IdleHelpSafe -CommandName $commandName $synopsis = ''