Skip to content

Commit 7e4246e

Browse files
feat: psturtle.com revising splatting ( Fixes #164 )
1 parent 8eeeed0 commit 7e4246e

File tree

2 files changed

+32
-71
lines changed

2 files changed

+32
-71
lines changed

psturtle.com/buildPage.ps1

Lines changed: 2 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -169,23 +169,7 @@ $pagesByUrl = $site.PagesByUrl
169169
$title = $Page['title'] = $file.Name -replace '\..+?\.ps1$' -replace 'index'
170170

171171
#region Map File Parameters to Page and Site configuration
172-
$FileParameters = [Ordered]@{}
173-
:nextParameter foreach ($parameterName in $scriptCmd.Parameters.Keys) {
174-
$parameter = $scriptCmd.Parameters[$parameterName]
175-
$potentialType = $parameter.ParameterType
176-
foreach ($PotentialName in
177-
@($parameter.Name;$parameter.Aliases) -ne ''
178-
) {
179-
if ($page[$potentialName] -and $page[$potentialName] -as $potentialType) {
180-
$FileParameters[$potentialName] = $page[$potentialName]
181-
continue nextParameter
182-
}
183-
elseif ($site[$potentialName] -and $site[$potentialName] -as $potentialType) {
184-
$FileParameters[$potentialName] = $site[$potentialName]
185-
continue nextParameter
186-
}
187-
}
188-
}
172+
$FileParameters = $scriptCmd | splat $Site $Page
189173
try {
190174
$description = ''
191175
. $file @FileParameters
@@ -259,60 +243,7 @@ $pagesByUrl = $site.PagesByUrl
259243
# This is somewhat straightforward, and opens up a lot of doors.
260244
# By allowing information in page or site to pass down to any command,
261245
# we can easily extend sites with simple scripts, and seamlessly integrate them.
262-
$layoutParameters = $layoutAtPathParameters[$fileRoot] = [Ordered]@{}
263-
264-
# We have to walk over every parameter.
265-
# Since we want to exit the loop as soon as we have mapped a parameter,
266-
# we use a loop label, `:nextParameter`.
267-
:nextParameter foreach ($parameterName in $layoutCommand.Parameters.Keys) {
268-
$parameter = $layoutCommand.Parameters[$parameterName]
269-
$potentialType = $parameter.ParameterType
270-
# PowerShell parameters can have aliases, so lets find all potential names.
271-
$parameterNames = @($parameter.Name;$parameter.Aliases) -ne ''
272-
foreach ($PotentialName in $parameterNames) {
273-
# If the page has the parameter
274-
if ($page[$potentialName] -and
275-
# and it is the right type
276-
$page[$potentialName] -as $potentialType
277-
) {
278-
# We map the parameter from the page data.
279-
$layoutParameters[$potentialName] = $page[$potentialName]
280-
281-
# We want to merge any dictionaries in both `$site` and `$page`.
282-
if ($site[$potentialName] -and
283-
$site[$potentialName] -as $potentialType -and
284-
$site[$potentialName] -is [Collections.IDictionary] -and
285-
$page[$potentialName] -is [Collections.IDictionary]
286-
) {
287-
# We copy in site wide data first.
288-
# This will show site-wide entries
289-
$layoutParameters[$potentialName] = [Ordered]@{}
290-
foreach ($siteKey in $site[$potentialName].Keys) {
291-
$layoutParameters[$PotentialName][$siteKey] = $site[$potentialName][$SiteKey]
292-
}
293-
# before page-specific entries.
294-
# An entry in pages would overwrite an entry in the site.
295-
foreach ($pageKey in $page[$potentialName].Keys) {
296-
$layoutParameters[$potentialName][$pageKey] = $page[$potentialName][$pageKey]
297-
}
298-
}
299-
# Now that we've mapped this parameter, continue to the `nextParameter`.
300-
continue nextParameter
301-
}
302-
303-
304-
if (
305-
# If we have the layout parameter in the `$site`
306-
$site[$potentialName] -and
307-
# and it can be the right type
308-
$site[$potentialName] -as $potentialType
309-
) {
310-
# Map it, and continue to the next parameter.
311-
$layoutParameters[$potentialName] = $site[$potentialName]
312-
continue nextParameter
313-
}
314-
}
315-
}
246+
$layoutParameters = $layoutCommand | splat $site $page
316247
}
317248
#endregion Prepare Layout
318249

psturtle.com/filters.ps1

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,36 @@ filter RequireModule {
3131
Write-Host "Already loaded $($alreadyLoaded.Name) for $($file.FullName)"
3232
}
3333
}
34+
35+
filter Splat {
36+
$in = $_
37+
if ($in -isnot [Management.Automation.CommandInfo]) { return }
38+
$splat = [Ordered]@{}
39+
:nextParameter foreach ($parameterName in $in.Parameters.Keys) {
40+
$parameter = $in.Parameters[$parameterName]
41+
$potentialType = $parameter.ParameterType
42+
# PowerShell parameters can have aliases, so lets find all potential names.
43+
$parameterNames = @($parameter.Name;$parameter.Aliases) -ne ''
44+
foreach ($PotentialName in $parameterNames) {
45+
foreach ($arg in $args) {
46+
if ($arg -isnot [Collections.IDictionary]) { continue }
47+
if ($arg[$potentialName] -and $arg[$potentialName] -as $potentialType) {
48+
if ($potentialType -eq [Collections.IDictionary]) {
49+
if (-not $splat[$parameterName]) {
50+
$splat[$parameterName] = [Ordered]@{}
51+
}
52+
foreach ($key in $arg[$potentialName].Keys) {
53+
$splat[$parameterName][$key] = $arg[$potentialName][$key]
54+
}
55+
} else {
56+
$splat[$parameterName] = $arg[$potentialName]
57+
}
58+
}
59+
}
60+
}
61+
}
62+
return $splat
63+
}
3464
#endregion PowerShell Specific Filters
3565

3666
#region Special Filters

0 commit comments

Comments
 (0)