@@ -131,11 +131,31 @@ function IncludeFileContents {
131
131
}
132
132
}
133
133
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
138
157
}
158
+ # We can include functions without any difficulty
139
159
elseif ($includingCommand -is [Management.Automation.FunctionInfo ]) {
140
160
if ($VariableAst -and $VariableAst.VariablePath -notmatch ' ^null$' ) {
141
161
# If we're including a function as a variable, define it as a ScriptBlock
@@ -156,7 +176,9 @@ if ($Passthru) {
156
176
})
157
177
"@ )
158
178
}
159
- } elseif ($includingCommand.ScriptBlock ) {
179
+ }
180
+ elseif ($includingCommand.ScriptBlock )
181
+ {
160
182
# If we're including a command with a ScriptBlock, assign it to a variable
161
183
[ScriptBlock ]::Create(@"
162
184
`$ {$ ( $includingCommand.Name ) } = {
@@ -167,15 +189,19 @@ if ($Passthru) { [Environment]::NewLine + "`${$($includingCommand.Name)}"}
167
189
"@ )
168
190
169
191
}
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 ) {
173
197
Write-Error " Could not transpile $ ( $includingCommand.Source ) "
174
198
return
175
199
}
176
- IncludeFileContents $transpiledFile .Fullname
200
+ IncludeFileContents $compiledFile .Fullname
177
201
}
178
- elseif ($includingCommand.Source -match ' \.ps$' ) {
202
+ elseif (
203
+ $includingCommand.Source -match ' \.ps$'
204
+ ) {
179
205
[ScriptBlock ]::Create(@"
180
206
`$ {$ ( $includingCommand.Name ) } = {
181
207
$ ( [ScriptBlock ]::Create([IO.File ]::ReadAllText($includingCommand.Source )) | .> PipeScript)
@@ -233,6 +259,7 @@ if ($Passthru) { [Environment]::NewLine + "`${$($includingCommand.Name)}"}
233
259
" '@" )
234
260
}
235
261
}
262
+ # endregion Generate the Include Script
236
263
237
264
if ($psCmdlet.ParameterSetName -eq ' ScriptBlock' -or
238
265
$VariableAst.VariablePath -match ' ^null$' ) {
0 commit comments