Skip to content

Commit 803bf24

Browse files
author
James Brundage
committed
Invoke-PipeScript: Trimming whitespace
1 parent 941fcc0 commit 803bf24

File tree

1 file changed

+66
-66
lines changed

1 file changed

+66
-66
lines changed

Invoke-PipeScript.ps1

Lines changed: 66 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
[Parameter(ValueFromPipeline)]
3030
[PSObject]
3131
$InputObject,
32-
32+
3333
# The Command that will be run.
3434
[ValidateScript({
3535
$PotentialCommand = $_
@@ -41,14 +41,14 @@
4141
$PotentialCommand.GetType().Name -in 'AttributeAST', 'TypeExpressionAST','InvokeMemberExpressionAst') {
4242
return $true
4343
}
44-
44+
4545
return $false
4646
})]
4747
[Alias('ScriptBlock','CommandName', 'CommandInfo', 'AttributeSyntaxTree','TypeConstraint')]
4848
[Parameter(Position=0)]
4949
[PSObject]
5050
$Command,
51-
51+
5252
# A collection of named parameters. These will be directly passed to the underlying script.
5353
[Alias('Parameters')]
5454
[Collections.IDictionary]
@@ -61,8 +61,8 @@
6161
$ArgumentList = @(),
6262

6363
# If this is not set, when a transpiler's parameters do not take a [ScriptBlock], ScriptBlock values will be evaluated.
64-
# This can be a very useful capability, because it can enable dynamic transpilation.
65-
# If this is set, will make ScriptBlockAst values will be run within data language, which significantly limits their capabilities.
64+
# This can be a very useful capability, because it can enable dynamic transpilation.
65+
# If this is set, will make ScriptBlockAst values will be run within data language, which significantly limits their capabilities.
6666
[switch]
6767
$SafeScriptBlockAttributeEvaluation
6868
)
@@ -73,7 +73,7 @@
7373
# If we didn't, do that now.
7474
$script:TypeAcceleratorsList = [PSObject].Assembly.GetType("System.Management.Automation.TypeAccelerators")::Get.Keys
7575
}
76-
76+
7777
function TypeConstraintToArguments (
7878
[Parameter(ValueFromPipeline)]
7979
$TypeName
@@ -85,7 +85,7 @@
8585
process {
8686

8787
if ($TypeName.IsGeneric) {
88-
$TypeNameParams[$typeName.Name] =
88+
$TypeNameParams[$typeName.Name] =
8989
$typeName.GenericArguments |
9090
TypeConstraintToArguments
9191
} elseif (-not $TypeName.IsArray) {
@@ -96,7 +96,7 @@
9696
[PSCustomObject]@{
9797
ArgumentList = $TypeNameArgs
9898
Parameter = $TypeNameParams
99-
}
99+
}
100100
}
101101
}
102102

@@ -143,8 +143,8 @@
143143
return
144144
}
145145

146-
if ($TranspilerErrors) {
147-
$failedMessage = @(
146+
if ($TranspilerErrors) {
147+
$failedMessage = @(
148148
"$($command.Source): " + "$($TranspilerErrors.Count) error(s)"
149149
if ($transpilerWarnings) {
150150
"$($TranspilerWarnings.Count) warning(s)"
@@ -156,22 +156,22 @@
156156
Errors = $TranspilerErrors
157157
Warnings = $TranspilerWarnings
158158
Command = $Command
159-
Parameters = $InvokePipeScriptParameters
159+
Parameters = $InvokePipeScriptParameters
160160
}
161-
)
161+
)
162162
}
163163

164-
# If it could not be transpiled into a [ScriptBlock] or [ScriptBlock[]]
165-
if ($TranspilerErrors -or
164+
# If it could not be transpiled into a [ScriptBlock] or [ScriptBlock[]]
165+
if ($TranspilerErrors -or
166166
($transpiledScriptBlock -isnot [ScriptBlock] -and -not ($TranspiledScriptBlock -as [scriptblock[]]))) {
167167
# error out.
168168
Write-Error "Command {$command} could not be transpiled into [ScriptBlock]s"
169169
return
170170
}
171-
171+
172172
# Walk thru each resulting transpiled ScriptBlock
173173
foreach ($transpiledScript in $transpiledScriptBlock) {
174-
174+
175175
# If we had an input object
176176
if ($InputObject) {
177177
# pipe it to the transpiled script.
@@ -182,8 +182,8 @@
182182
}
183183
}
184184
}
185-
186-
185+
186+
187187
# If the command is a string,
188188
elseif ($Command -is [string])
189189
{
@@ -214,11 +214,11 @@
214214

215215

216216
# If the command is a ```[Management.Automation.CommandInfo]```
217-
elseif ($command -is [Management.Automation.CommandInfo])
217+
elseif ($command -is [Management.Automation.CommandInfo])
218218
{
219219
# Determine if the Command is a SourceGenerator.
220220
$IsSourceGenerator = '\.ps1{0,1}\.(?<ext>[^.]+$)' # if it matches the regex designating a SourceGenerator
221-
221+
222222
# If the command was not a source generator
223223
if ($Command.Source -notmatch $IsSourceGenerator ) {
224224
# invoke it normally.
@@ -230,12 +230,12 @@
230230
}
231231

232232
# If the command was a source generator
233-
else {
234-
# predetermine the output path
233+
else {
234+
# predetermine the output path
235235
$outputPath = $($Command.Source -replace $IsSourceGenerator, '.${ext}')
236236
# and attempt to find a transpiler.
237237
$foundTranspiler = Get-Transpiler -CouldPipe $Command -ValidateInput $Command -ErrorAction Ignore
238-
238+
239239
$ParamsAndArgs = [Ordered]@{Parameter=$Parameter;ArgumentList = $ArgumentList}
240240
$transpilerErrors = @()
241241
$transpilerWarnings = @()
@@ -256,19 +256,19 @@
256256
SourcePath = $command.Source
257257
})
258258

259-
$transpilerOutput = $command |
259+
$transpilerOutput = $command |
260260
& $ft.ExtensionCommand @ErrorsAndWarnings @ParamsAndArgs
261-
261+
262262
$null =
263263
New-Event -SourceIdentifier 'PipeScript.SourceGenerator.Stop' -MessageData ([PSCustomObject][Ordered]@{
264264
Transpiler = $ft.ExtensionCommand
265265
TranspilerOutput = $transpilerOutput
266266
SourcePath = $command.Source
267267
Errors = $TranspilerErrors
268-
Warnings = $TranspilerWarnings
268+
Warnings = $TranspilerWarnings
269269
})
270-
271-
$transpilerOutput =
270+
271+
$transpilerOutput =
272272
# If the transpiler returned a [ScriptBlock]
273273
if ($transpilerOutput -is [Scriptblock]) {
274274
# recursively invoke.
@@ -278,13 +278,13 @@
278278
# otherwise, return the output of the transpiler.
279279
$transpilerOutput
280280
}
281-
281+
282282
# If the transpiler had output,
283283
if ($transpilerOutput) {
284-
$transpilerOutput # use that output
284+
$transpilerOutput # use that output
285285
break # and stop processing additional transpilers.
286286
}
287-
}
287+
}
288288
} else {
289289
# If we did not find a transpiler, treat the source code as PipeScript/PowerShell.
290290
$fileScriptBlock =
@@ -297,9 +297,9 @@
297297
[scriptblock]::Create($fileText) | .>Pipescript @ErrorsAndWarnings
298298
} catch {
299299
$ex = $_
300-
Write-Error "[CommandInfo] -Command could not be made into a [ScriptBlock]: $ex"
300+
Write-Error "[CommandInfo] -Command could not be made into a [ScriptBlock]: $ex"
301301
}
302-
} else {
302+
} else {
303303
$Command.ScriptBlock | .>Pipescript @ErrorsAndWarnings
304304
}
305305

@@ -309,14 +309,14 @@
309309
} else {
310310
$InvokePipeScriptParameters.Command = $fileScriptBlock
311311
Invoke-PipeScript @InvokePipeScriptParameters
312-
}
312+
}
313313
}
314314

315315
# Now that the source generator has finished running, we can Pop-Location.
316316
Pop-Location
317-
318-
if ($TranspilerErrors) {
319-
$failedMessage = @(
317+
318+
if ($TranspilerErrors) {
319+
$failedMessage = @(
320320
"$($command.Source): " + "$($TranspilerErrors.Count) error(s)"
321321
if ($transpilerWarnings) {
322322
"$($TranspilerWarnings.Count) warning(s)"
@@ -328,7 +328,7 @@
328328
Errors = $TranspilerErrors
329329
Warnings = $TranspilerWarnings
330330
Command = $Command
331-
Parameters = $InvokePipeScriptParameters
331+
Parameters = $InvokePipeScriptParameters
332332
}
333333
)
334334
return
@@ -361,23 +361,23 @@
361361
# If it was all files.
362362
if (-not ($allFiles -ne $true)) {
363363
$pipeScriptOutput # return them
364-
} else {
365-
# Otherwise, join the content by a space
366-
$pipescriptOutput -join ' ' |
364+
} else {
365+
# Otherwise, join the content by a space
366+
$pipescriptOutput -join ' ' |
367367
Set-Content -LiteralPath $outputPath # save it to the output path
368368
Get-Item -LiteralPath $outputPath # and return the file.
369369
}
370370
}
371371

372372
# If the source generator returned any other type
373373
else {
374-
374+
375375
$pipescriptOutput |
376376
Set-Content -LiteralPath $outputPath # save it to the output path
377377
Get-Item -LiteralPath $outputPath # and return the file.
378378
}
379379
return
380-
}
380+
}
381381
}
382382

383383
# If the -Command is an ```[Management.Automation.Language.AttributeAST]```
@@ -394,32 +394,32 @@
394394

395395
# Create a collection for stringified arguments.
396396
$stringArguments = @()
397-
397+
398398
# Get the name of the transpiler.
399-
$transpilerStepName =
399+
$transpilerStepName =
400400
if ($AttributeSyntaxTree.TypeName.IsGeneric) {
401401
$AttributeSyntaxTree.TypeName.TypeName.Name
402402
} else {
403403
$AttributeSyntaxTree.TypeName.Name
404404
}
405405

406406
# See if we could find a transpiler that fits.
407-
$foundTranspiler =
407+
$foundTranspiler =
408408
if ($InputObject) {
409-
Get-Transpiler -TranspilerName "$transpilerStepName" -CouldPipe $InputObject |
409+
Get-Transpiler -TranspilerName "$transpilerStepName" -CouldPipe $InputObject |
410410
Select-Object -ExpandProperty ExtensionCommand
411411
} else {
412412
Get-Transpiler -TranspilerName "$transpilerStepName"
413413
}
414-
414+
415415
# Collect all of the arguments of the attribute, in the order they were specified.
416416
$argsInOrder = @(
417417
@($AttributeSyntaxTree.PositionalArguments) + @($AttributeSyntaxTree.NamedArguments) | Sort-Object { $_.Extent.StartOffset})
418418

419-
419+
420420
# Now we need to map each of those arguments into either named or positional arguments.
421421
foreach ($attributeArg in $argsInOrder) {
422-
# Named arguments are fairly straightforward:
422+
# Named arguments are fairly straightforward:
423423
if ($attributeArg -is [Management.Automation.Language.NamedAttributeArgumentAst]) {
424424
$argName = $attributeArg.ArgumentName
425425
$argAst = $attributeArg.Argument
@@ -432,7 +432,7 @@
432432
# Turn it into a [ScriptBlock]
433433
$argScriptBlock = [ScriptBlock]::Create($argAst.Extent.ToString() -replace '^\{' -replace '\}$')
434434
# If the Transpiler had a parameter that took a [ScriptBlock] or [ScriptBlock[]]
435-
if ($foundTranspiler.parameters.$argName.ParameterType -eq [ScriptBlock] -or
435+
if ($foundTranspiler.parameters.$argName.ParameterType -eq [ScriptBlock] -or
436436
$foundTranspiler.parameters.$argName.ParameterType -eq [ScriptBlock[]]) {
437437
$argScriptBlock # pass the [ScriptBlock] directly.
438438
}
@@ -449,10 +449,10 @@
449449
}
450450
elseif ($argAst.Value) {
451451
$argAst.Value.ToString()
452-
}
452+
}
453453
else {
454454
$argAst.Extent.ToString()
455-
}
455+
}
456456
} else {
457457
# If we are a positional parameter, for the moment:
458458
if ($parameter.Count) {
@@ -466,7 +466,7 @@
466466
# We _should_ get more intelligent over time here.
467467
# See [the GitHub Issue](https://github.com/StartAutomating/PipeScript/issues/70) for more details.
468468
}
469-
}
469+
}
470470

471471
# If we have found a transpiler, run it.
472472
if ($foundTranspiler) {
@@ -480,7 +480,7 @@
480480
}
481481
elseif ($(
482482
$realCommandExists = $ExecutionContext.SessionState.InvokeCommand.GetCommand(($transpilerStepName -replace '_','-'), 'All')
483-
$realCommandExists
483+
$realCommandExists
484484
)) {
485485
if ($inputObject) {
486486
$canPipe = foreach ($param in $realCommandExists.Parameters.Values) {
@@ -497,7 +497,7 @@
497497
}
498498
} else {
499499
& $realCommandExists @Parameter @ArgumentList
500-
}
500+
}
501501
}
502502
elseif ($script:TypeAcceleratorsList -notcontains $transpilerStepName -and $transpilerStepName -notin 'Ordered') {
503503
$psCmdlet.WriteError(
@@ -508,7 +508,7 @@
508508
$command
509509
)
510510
)
511-
511+
512512
return
513513
}
514514

@@ -525,19 +525,19 @@
525525
}
526526
# Next, make sure it's not ```[ordered]``` (return it if is)
527527
if ($command.TypeName.Name -eq 'ordered') { return}
528-
528+
529529
# Determine the name of the transpiler step.
530-
$transpilerStepName =
530+
$transpilerStepName =
531531
# If the typename is generic, the generic arguments will be treated as positional arguments
532-
if ($command.TypeName.IsGeneric) {
532+
if ($command.TypeName.IsGeneric) {
533533
$command.TypeName.TypeName.Name # and the name will the root typename.
534534
} else {
535535
$command.TypeName.Name # Otherwise, the step will have no positional arguments, and it's name is the typename.
536536
}
537-
538-
537+
538+
539539
# Attempt to find the transpiler
540-
$foundTranspiler =
540+
$foundTranspiler =
541541
if ($InputObject) {
542542
# If we had an -InputObject, be sure we can pipe it in.
543543
Get-Transpiler -TranspilerName "$transpilerStepName" | Where-Object { $_ | Get-Transpiler -CouldPipe $InputObject }
@@ -548,10 +548,10 @@
548548

549549
# If the TypeName was generic, treat the generic parameters as arguments
550550
# ```[t[n,a]]``` would pass two positional parameters, n and a.
551-
# ```[t[a[b,c],d[e]]]``` would pass two named parameters @{a='b,'c';d='e'}
551+
# ```[t[a[b,c],d[e]]]``` would pass two named parameters @{a='b,'c';d='e'}
552552
if ($TypeConstraint.TypeName.IsGeneric) {
553-
$TypeConstraint.TypeName.GenericArguments |
554-
TypeConstraintToArguments |
553+
$TypeConstraint.TypeName.GenericArguments |
554+
TypeConstraintToArguments |
555555
ForEach-Object {
556556
if ($_.ArgumentList) {
557557
$ArgumentList += $_.ArgumentList

0 commit comments

Comments
 (0)