Skip to content

Commit 01d5160

Browse files
StartAutomatingStartAutomating
authored andcommitted
New-PipeScript: Trimming whitespace
1 parent b6f57b8 commit 01d5160

File tree

1 file changed

+43
-24
lines changed

1 file changed

+43
-24
lines changed

New-PipeScript.ps1

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ HTTP Accept indicates what content types the web request will accept as a respon
1919
# Defines one or more parameters for a ScriptBlock.
2020
# Parameters can be defined in a few ways:
2121
# * As a ```[Collections.Dictionary]``` of Parameters
22-
# * As the ```[string]``` name of an untyped parameter.
22+
# * As the ```[string]``` name of an untyped parameter.
2323
# * As a ```[ScriptBlock]``` containing only parameters.
2424
[Parameter(ValueFromPipelineByPropertyName)]
2525
[ValidateScript({
@@ -142,7 +142,17 @@ HTTP Accept indicates what content types the web request will accept as a respon
142142
$AutoParameterType = [PSObject],
143143
# If provided, will add inline help to parameters.
144144
[Collections.IDictionary]
145-
$ParameterHelp
145+
$ParameterHelp,
146+
<#
147+
If set, will weakly type parameters generated by reflection.
148+
1. Any parameter type implements IList should be made a [PSObject[]]
149+
2. Any parameter that implements IDictionary should be made an [Collections.IDictionary]
150+
3. Booleans should be made into [switch]es
151+
4. All other parameter types should be [PSObject]
152+
#>
153+
[Alias('WeakType', 'WeakParameters', 'WeaklyTypedParameters', 'WeakProperties', 'WeaklyTypedProperties')]
154+
[switch]
155+
$WeaklyTyped
146156
)
147157
begin {
148158
$ParametersToCreate = [Ordered]@{}
@@ -219,47 +229,45 @@ HTTP Accept indicates what content types the web request will accept as a respon
219229
$EachParameter.Value -join [Environment]::Newline
220230
}
221231
}
222-
223-
}
232+
}
224233
# If the parameter was a string
225-
elseif ($Parameter -is [string])
234+
elseif ($Parameter -is [string])
226235
{
227236
# treat it as parameter name
228-
$ParametersToCreate[$Parameter] =
237+
$ParametersToCreate[$Parameter] =
229238
@(
230239
if ($parameterHelp -and $parameterHelp[$Parameter]) {
231240
$parameterHelp[$Parameter] | embedParameterHelp
232241
}
233242
"[Parameter(ValueFromPipelineByPropertyName)]"
234243
"`$$Parameter"
235-
) -join [Environment]::NewLine
236-
}
244+
) -join [Environment]::NewLine
245+
}
237246
# If the parameter is a [ScriptBlock]
238-
elseif ($parameter -is [scriptblock])
247+
elseif ($parameter -is [scriptblock])
239248
{
240-
241249
# add it to a list of parameter script blocks.
242250
$parameterScriptBlocks +=
243-
if ($parameter.Ast.ParamBlock) {
251+
if ($parameter.Ast.ParamBlock) {
244252
$parameter
245-
}
253+
}
246254
}
247-
elseif ($parameter -is [Reflection.PropertyInfo] -or
248-
$parameter -as [Reflection.PropertyInfo[]] -or
249-
$parameter -is [Reflection.ParameterInfo] -or
255+
elseif ($parameter -is [Reflection.PropertyInfo] -or
256+
$parameter -as [Reflection.PropertyInfo[]] -or
257+
$parameter -is [Reflection.ParameterInfo] -or
250258
$parameter -as [Reflection.ParameterInfo[]] -or
251259
$parameter -is [Reflection.MethodInfo] -or
252260
$parameter -as [Reflection.MethodInfo[]]
253261
) {
254-
if ($parameter -is [Reflection.MethodInfo] -or
262+
if ($parameter -is [Reflection.MethodInfo] -or
255263
$parameter -as [Reflection.MethodInfo[]]) {
256264
$parameter = @(foreach ($methodInfo in $parameter) {
257265
$methodInfo.GetParameters()
258266
})
259267
}
260268
foreach ($prop in $Parameter) {
261269
if ($prop -is [Reflection.PropertyInfo] -and -not $prop.CanWrite) { continue }
262-
$paramType =
270+
$paramType =
263271
if ($prop.ParameterType) {
264272
$prop.ParameterType
265273
} elseif ($prop.PropertyType) {
@@ -276,28 +284,39 @@ HTTP Accept indicates what content types the web request will accept as a respon
276284
$parameterAttribute
277285
if ($paramType -eq [boolean]) {
278286
"[switch]"
279-
} else {
287+
} elseif ($WeaklyTyped) {
288+
if ($paramType.GetInterface([Collections.IDictionary])) {
289+
"[Collections.IDictionary]"
290+
}
291+
elseif ($paramType.GetInterface([Collections.IList])) {
292+
"[PSObject[]]"
293+
}
294+
else {
295+
"[PSObject]"
296+
}
297+
}
298+
else {
280299
"[$($paramType -replace '^System\.')]"
281300
}
282301
'$' + $prop.Name
283302
) -ne ''
284303
}
285-
}
304+
}
286305
}
287306
# If there is header content,
288-
if ($header) {
307+
if ($header) {
289308
$allHeaders += $Header
290309
}
291310
# dynamic parameters,
292-
if ($DynamicParameter) {
311+
if ($DynamicParameter) {
293312
$allDynamicParameters += $DynamicParameter
294313
}
295314
# begin,
296-
if ($Begin) {
315+
if ($Begin) {
297316
$allBeginBlocks += $begin
298317
}
299318
# process,
300-
if ($process) {
319+
if ($process) {
301320
$allProcessBlocks += $process
302321
}
303322
# or end blocks.
@@ -334,7 +353,7 @@ HTTP Accept indicates what content types the web request will accept as a respon
334353
[Environment]::NewLine +
335354
')'
336355
# If any parameters were passed in as ```[ScriptBlock]```s,
337-
if ($parameterScriptBlocks) {
356+
if ($parameterScriptBlocks) {
338357
$parameterScriptBlocks += [ScriptBlock]::Create($newParamBlock)
339358
# join them with the new parameter block.
340359
$newParamBlock = $parameterScriptBlocks | Join-PipeScript

0 commit comments

Comments
 (0)