Skip to content

Commit 6980dea

Browse files
author
James Brundage
committed
Adding Piped Assignment Transpiler (#88)
1 parent dd5f29e commit 6980dea

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<#
2+
.SYNOPSIS
3+
Piped Assignment Transpiler
4+
.DESCRIPTION
5+
Enables Piped Assignment (```|=|```).
6+
7+
Piped Assignment allows for an expression to be reassigned based off of the pipeline input.
8+
.EXAMPLE
9+
{
10+
$Collection |=| Where-Object Name -match $Pattern
11+
} | .>PipeScript
12+
13+
# This will become:
14+
15+
$Collection = $Collection | Where-Object Name -match $pattern
16+
.EXAMPLE
17+
{
18+
$Collection |=| Where-Object Name -match $Pattern | Foreach-Object { $_.Name }
19+
} | .>PipeScript
20+
#>
21+
[ValidateScript({
22+
$ast = $_
23+
if ($ast.PipelineElements.Count -ge 3 -and
24+
$ast.PipelineElements[1].CommandElements -and
25+
$ast.PipelineElements[1].CommandElements[0].Value -eq '=') {
26+
return $true
27+
}
28+
return $false
29+
})]
30+
param(
31+
[Parameter(Mandatory,ValueFromPipeline)]
32+
[Management.Automation.Language.PipelineAst]
33+
$PipelineAst
34+
)
35+
36+
process {
37+
$null = $PipelineAst.PipelineElements[0]
38+
[ScriptBlock]::Create(@"
39+
$($PipelineAst.PipelineElements[0]) = $($PipelineAst.PipelineElements[0]) | $(
40+
$(
41+
$(if ($PipelineAst.PipelineElements.Count -gt 3) {
42+
[Environment]::NewLine + (' ' * 4)
43+
} else {
44+
''
45+
}) +
46+
(@($PipelineAst.PipelineElements[2..$PipelineAst.PipelineElements.Count]) -join (
47+
' |' + [Environment]::NewLine + (' ' * 4)
48+
))
49+
)
50+
)
51+
"@)
52+
}

0 commit comments

Comments
 (0)