Skip to content

Commit fc669bf

Browse files
author
James Brundage
committed
feat: Templating Inherit ( Fixes #852 )
1 parent e16f8e5 commit fc669bf

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

Transpilers/Inherit.psx.ps1

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ $Override,
7070
[switch]
7171
$Dynamic,
7272

73+
# If set, will not generate a dynamic parameter block. This is primarily present so Abstract inheritance has a small change footprint.
74+
[switch]
75+
$NoDynamic,
76+
7377
# If set, will always inherit commands as proxy commands.
7478
# This is implied by -Dynamic.
7579
[switch]
@@ -163,6 +167,20 @@ process {
163167
return
164168
}
165169

170+
$InheritTemplate =
171+
if ($resolvedCommand.Inherit) {
172+
$resolvedCommand.Inherit
173+
} elseif ($resolvedCommand.Module.Inherit) {
174+
$resolvedCommand.Module.Inherit
175+
}
176+
177+
if ($InheritTemplate -is [Management.Automation.PSMethodInfo]) {
178+
return $InheritTemplate.Invoke(@([Ordered]@{} + $PSBoundParameters))
179+
}
180+
elseif ($InheritTemplate -is [scriptblock]) {
181+
return (& $InheritTemplate @([Ordered]@{} + $PSBoundParameters) )
182+
}
183+
166184
# Prepare parameters for Join-ScriptBlock
167185
$joinSplat = @{}
168186
foreach ($key in 'IncludeBlockType', 'ExcludeBlockType') {
@@ -263,7 +281,8 @@ $(
263281
# If we do not have a resolved command,
264282
if (-not $resolvedCommand) {
265283
{} # the first script block is empty.
266-
} else {
284+
}
285+
else {
267286
# If we have a resolvedCommand, fully qualify it.
268287
$fullyQualifiedCommand =
269288
if ($resolvedCommand.Module) {
@@ -273,9 +292,13 @@ $(
273292
}
274293

275294
# Then create a dynamicParam block that will set `$baseCommand,
276-
# as well as a `$script: scoped variable for the command name.
277-
278-
[scriptblock]::create(@"
295+
# as well as a `$script: scoped variable for the command name.
296+
if ($NotDynamic) # unless, of course -NotDynamic is passed
297+
{
298+
{}
299+
} else
300+
{
301+
[scriptblock]::create(@"
279302
dynamicParam {
280303
`$baseCommand =
281304
if (-not `$script:$commandVariable) {
@@ -302,6 +325,7 @@ dynamicParam {
302325
})
303326
}
304327
"@)
328+
}
305329
}
306330
),
307331
# Next is our [ScriptBlock]. This will come before almost everything else.

0 commit comments

Comments
 (0)