Skip to content

Commit e16f8e5

Browse files
author
James Brundage
committed
feat: Templating Include ( Fixes #853 )
1 parent cfd12c2 commit e16f8e5

File tree

1 file changed

+37
-10
lines changed

1 file changed

+37
-10
lines changed

Transpilers/Include.psx.ps1

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,31 @@ function IncludeFileContents {
131131
}
132132
}
133133

134-
$includedScript =
135-
if ($includingCommand -is [Management.Automation.CmdletInfo]) {
136-
Write-Error "Cannot Include Cmdlets"
137-
return
134+
#region Generate the Include Script
135+
136+
$includeTemplate =
137+
if ($includingCommand.Include) {
138+
$includingCommand.Include
139+
} elseif ($includingCommand.Module.Include) {
140+
$includingCommand.Module.Include
141+
}
142+
143+
# We cannot include cmdlets
144+
if ($includingCommand -is [Management.Automation.CmdletInfo] -and -not $includeTemplate) {
145+
Write-Error "Cannot Include Cmdlets"
146+
return
147+
}
148+
149+
$includedScript =
150+
# If there was a include method
151+
if ($includeTemplate -is [Management.Automation.PSMethodInfo]) {
152+
# use that.
153+
$includeTemplate.Invoke(@($includingCommand))
154+
}
155+
elseif ($includeTemplate -is [scriptblock]) {
156+
& $includeTemplate $includingCommand
138157
}
158+
# We can include functions without any difficulty
139159
elseif ($includingCommand -is [Management.Automation.FunctionInfo]) {
140160
if ($VariableAst -and $VariableAst.VariablePath -notmatch '^null$') {
141161
# If we're including a function as a variable, define it as a ScriptBlock
@@ -156,7 +176,9 @@ if ($Passthru) {
156176
})
157177
"@)
158178
}
159-
} elseif ($includingCommand.ScriptBlock) {
179+
}
180+
elseif ($includingCommand.ScriptBlock)
181+
{
160182
# If we're including a command with a ScriptBlock, assign it to a variable
161183
[ScriptBlock]::Create(@"
162184
`${$($includingCommand.Name)} = {
@@ -167,15 +189,19 @@ if ($Passthru) { [Environment]::NewLine + "`${$($includingCommand.Name)}"}
167189
"@)
168190

169191
}
170-
elseif ($includingCommand.Source -match '\.ps1{0,}\.(?<ext>[^.]+$)') {
171-
$transpiledFile = Invoke-PipeScript -CommandInfo $includingCommand
172-
if (-not $transpiledFile) {
192+
elseif (
193+
$includingCommand.Source -match '\.ps1{0,}\.(?<ext>[^.]+$)'
194+
) {
195+
$compiledFile = Invoke-PipeScript -CommandInfo $includingCommand
196+
if (-not $compiledFile) {
173197
Write-Error "Could not transpile $($includingCommand.Source)"
174198
return
175199
}
176-
IncludeFileContents $transpiledFile.Fullname
200+
IncludeFileContents $compiledFile.Fullname
177201
}
178-
elseif ($includingCommand.Source -match '\.ps$') {
202+
elseif (
203+
$includingCommand.Source -match '\.ps$'
204+
) {
179205
[ScriptBlock]::Create(@"
180206
`${$($includingCommand.Name)} = {
181207
$([ScriptBlock]::Create([IO.File]::ReadAllText($includingCommand.Source)) | .>PipeScript)
@@ -233,6 +259,7 @@ if ($Passthru) { [Environment]::NewLine + "`${$($includingCommand.Name)}"}
233259
"'@")
234260
}
235261
}
262+
#endregion Generate the Include Script
236263

237264
if ($psCmdlet.ParameterSetName -eq 'ScriptBlock' -or
238265
$VariableAst.VariablePath -match '^null$') {

0 commit comments

Comments
 (0)