From 974bff8766e557bdd773efbb297bdac5fd717954 Mon Sep 17 00:00:00 2001 From: James Brundage <@github.com> Date: Thu, 18 Apr 2024 16:19:32 -0700 Subject: [PATCH] feat: Serve.Module - Framing Results ( Fixes #1034 ) --- Commands/Services/Serve-Module.ps.ps1 | 93 +++++++++++++++++---------- 1 file changed, 59 insertions(+), 34 deletions(-) diff --git a/Commands/Services/Serve-Module.ps.ps1 b/Commands/Services/Serve-Module.ps.ps1 index a616dc9db..0180d969e 100644 --- a/Commands/Services/Serve-Module.ps.ps1 +++ b/Commands/Services/Serve-Module.ps.ps1 @@ -27,48 +27,73 @@ Serve function Module { ) process { - $nowServingModule = $Module + $nowServingModule = $Module $NowServingModuleRoutes = $NowServingModule.Route + $UrlSegments = @($Request.Url.Segments -replace '/$' -ne '') $MyParameters = [Ordered]@{} + $PSBoundParameters - - $ServedARoute = $false - :ServicingRoutes foreach ($nowServingRoute in $NowServingModuleRoutes) { - $ShouldServeThisUrl = $nowServingRoute.ForUrl($request.Url) - if ($ShouldServeThisUrl) { - $ServedARoute = $true - } - if ($ShouldServeThisUrl -is [ScriptBlock]) { - . $ShouldServeThisUrl - } elseif ($ShouldServeThisUrl -is [Management.Automation.CommandInfo]) { - . $ShouldServeThisUrl - } elseif ($ShouldServeThisUrl.pstypenames -contains 'PipeScript.Module.Service') { - foreach ($serviceParameterSet in $ShouldServeThisUrl.GetServiceParameters()) { - $serviceCommand = $serviceParameterSet.Command - if ($serviceCommand) { - $serviceParameterCopy = [Ordered]@{} + $serviceParameterSet - foreach ($parameterName in $MyParameters.Keys) { - if ($serviceCommand.Parameters[$parameterName] -and - -not $serviceParameterCopy[$parameterName] - ) { - $serviceParameterCopy[$parameterName] = $MyParameters[$parameterName] - } - } - . $serviceCommand @serviceParameterCopy + $AdditionalContext = @() + + $NowServingModuleOutput = . { + $ServedARoute = $false + :ServicingRoutes foreach ($nowServingRoute in $NowServingModuleRoutes) { + $ShouldServeThisUrl = $nowServingRoute.ForUrl($request.Url) + if ($ShouldServeThisUrl) { + $ServedARoute = $true + } + if ($ShouldServeThisUrl -is [ScriptBlock]) { + . $ShouldServeThisUrl + } elseif ($ShouldServeThisUrl -is [Management.Automation.CommandInfo]) { + . $ShouldServeThisUrl + } elseif ($ShouldServeThisUrl.pstypenames -contains 'PipeScript.Module.Service') { + foreach ($serviceParameterSet in $ShouldServeThisUrl.GetServiceParameters()) { + $serviceCommand = $serviceParameterSet.Command + if ($serviceCommand) { + $AdditionalContext += $serviceParameterSet + $serviceParameterCopy = [Ordered]@{} + $serviceParameterSet + foreach ($parameterName in $MyParameters.Keys) { + if ($serviceCommand.Parameters[$parameterName] -and + -not $serviceParameterCopy[$parameterName] + ) { + $serviceParameterCopy[$parameterName] = $MyParameters[$parameterName] + } + } + . $serviceCommand @serviceParameterCopy + } } } } - } - - return if $ServedARoute + + return if $ServedARoute + $potentialTopicName = $Request.Url.Segments -replace '^/' -ne '' -join ' ' + if ($potentialTopicName) { + $module.Topics.Get($potentialTopicName) + } else { + $module.Topics.Get($module.Name) + } + } + + $nowServingSiteInfo = $nowServingModule.Site - $potentialTopicName = $Request.Url.Segments -replace '^/' -ne '' -join ' ' - if ($potentialTopicName) { - $module.Topics.Get($potentialTopicName) + if ($nowServingSiteInfo) { + $nowServingSite = foreach ($siteInfo in $nowServingSiteInfo) { + if ($request.Url -match "^$([Regex]::Escape($siteInfo.Url))") { + $siteInfo + break + } + } + if (-not $nowServingSite) { + $nowServingModuleOutput + } + else { + $nowServingSite.UseLayout( + $nowServingSite.GetDefaultLayout(), + (@($NowServingSite) + $AdditionalContext) + ) + } } else { - $module.Topics.Get($module.Name) - } - + $NowServingModuleOutput + } } } \ No newline at end of file