@@ -12,9 +12,8 @@ function New-PipeScript {
1212 # Defines one or more parameters for a ScriptBlock.
1313 # Parameters can be defined in a few ways:
1414 # * As a ```[Collections.Dictionary]``` of Parameters
15- # * As the ```[string]``` name of an untyped parameter.
16- # * As an ```[Object[]]```.
17- # * As a ```[ScriptBlock]```
15+ # * As the ```[string]``` name of an untyped parameter.
16+ # * As a ```[ScriptBlock]``` containing only parameters.
1817 [Parameter (ValueFromPipelineByPropertyName )]
1918 [ValidateScript ({
2019 if ($_ -isnot [ScriptBlock ]) { return $true }
@@ -179,80 +178,73 @@ function New-PipeScript {
179178 )
180179 }
181180 }
181+ # If the value was an array
182182 elseif ($EachParameter.Value -is [Object []]) {
183- $ParametersToCreate [$EachParameter.Key ] =
183+ $ParametersToCreate [$EachParameter.Key ] = # join it's elements by newlines
184184 $EachParameter.Value -join [Environment ]::Newline
185185 }
186186 }
187- } elseif ($Parameter -is [string ]) {
188- $ParametersToCreate [$Parameter ] = @ (
187+
188+ }
189+ # If the parameter was a string
190+ elseif ($Parameter -is [string ])
191+ {
192+ # treat it as parameter name
193+ $ParametersToCreate [$Parameter ] =
194+ @ (
189195 " [Parameter(ValueFromPipelineByPropertyName)]"
190196 " `$ $Parameter "
191- )
192- } elseif ($parameter -is [scriptblock ]) {
197+ ) -join [Environment ]::NewLine
198+ }
199+ # If the parameter is a [ScriptBlock]
200+ elseif ($parameter -is [scriptblock ])
201+ {
202+
203+ # add it to a list of parameter script blocks.
193204 $parameterScriptBlocks +=
194- if ($parameter.Ast.ParamBlock ) {
195- # embed the parameter block (except for the param keyword)
205+ if ($parameter.Ast.ParamBlock ) {
196206 $parameter
197- } else {
198- }
199- } elseif ($Parameter -is [Object []]) {
200- $currentParam = @ ()
201- $currentParamName = ' '
202- foreach ($EachParameter in $Parameter ) {
203- if ($EachParameter -is [string ] -and -not $EachParameter.Contains (' ' )) {
204- if ($currentParam ) {
205- $ParametersToCreate [$currentParamName ] = $currentParam
206- $currentParam = @ ()
207- $currentParamName = ' '
208- }
209- $currentParam += " `$ $EachParameter "
210- $currentParamName = $EachParameter
211- } elseif ($EachParameter -is [string ] -and $EachParameter.Contains (' ' )) {
212- $currentParam = @ (
213- if ($EachParameter.Contains (" `n " )) {
214- " <#" + [Environment ]::newLine + $EachParameter + [Environment ]::newLine + ' #>'
215- } else {
216- " # $EachParameter "
217- }
218- ) + $currentParam
219- } elseif ($EachParameter -is [type ]) {
220- $currentParam += " [$ ( $EachParameter.Fullname ) ]"
221- }
222- }
223- if ($currentParamName ) {
224- $ParametersToCreate [$currentParamName ] = $currentParam
225- }
207+ }
226208 }
227209 }
228- if ($header ) {
210+ # If there is header content,
211+ if ($header ) {
229212 $allHeaders += $Header
230213 }
231- if ($DynamicParameter ) {
214+ # dynamic parameters,
215+ if ($DynamicParameter ) {
232216 $allDynamicParameters += $DynamicParameter
233217 }
234- if ($Begin ) {
218+ # begin,
219+ if ($Begin ) {
235220 $allBeginBlocks += $begin
236221 }
237- if ($process ) {
222+ # process,
223+ if ($process ) {
238224 $allProcessBlocks += $process
239225 }
226+ # or end blocks.
240227 if ($end ) {
228+ # accumulate them.
241229 $allEndBlocks += $end
242230 }
243231 }
244232 end {
233+ # Take all of the accumulated parameters and create a parameter block
245234 $newParamBlock =
246235 " param(" + [Environment ]::newLine +
247236 $ (@ (foreach ($toCreate in $ParametersToCreate.GetEnumerator ()) {
248237 $toCreate.Value -join [Environment ]::NewLine
249238 }) -join (' ,' + [Environment ]::NewLine)) +
250239 [Environment ]::NewLine +
251240 ' )'
252- if ($parameterScriptBlocks ) {
253- $parameterScriptBlocks += [ScriptBlock ]::new($newParamBlock )
241+ # If any parameters were passed in as ```[ScriptBlock]```s,
242+ if ($parameterScriptBlocks ) {
243+ $parameterScriptBlocks += [ScriptBlock ]::Create($newParamBlock )
244+ # join them with the new parameter block.
254245 $newParamBlock = $parameterScriptBlocks | Join-PipeScript
255246 }
247+ # Create the script block by combining together the provided parts.
256248 $createdScriptBlock = [scriptblock ]::Create("
257249$ ( $allHeaders -join [Environment ]::Newline)
258250$newParamBlock
@@ -271,7 +263,8 @@ $(if ($allEndBlocks -and -not $allBeginBlocks -and -not $allProcessBlocks) {
271263 @ (@ (" end {" ) + $allEndBlocks + ' }' ) -join [Environment ]::Newline
272264})
273265" )
274- $createdScriptBlock
266+ # return the created script block.
267+ return $createdScriptBlock
275268 }
276269}
277270
0 commit comments