Skip to content

Commit 5756723

Browse files
Merge pull request #136 from StartAutomating/PipeScriptUpdates
Pipe script updates
2 parents 6f5bbac + 513034d commit 5756723

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+1291
-284
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.0.11:
2+
* Source Generators Now Support Parameters / Arguments (#75)
3+
* Invoke-PipeScript Terminating Build Errors (#135)
4+
---
5+
16
## 0.0.10:
27
* Improvements:
38
* REST transpiler

Invoke-PipeScript.ps1

Lines changed: 48 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -73,50 +73,7 @@
7373
# If we didn't, do that now.
7474
$script:TypeAcceleratorsList = [PSObject].Assembly.GetType("System.Management.Automation.TypeAccelerators")::Get.Keys
7575
}
76-
function TranspileOutput {
77-
param(
78-
[Parameter(Position=0)]
79-
$OriginalInputObject,
80-
81-
[Parameter(ValueFromPipeline)]
82-
[PSObject]
83-
$PipeScriptOutput
84-
)
85-
86-
begin {
87-
$astReplacements = [Ordered]@{}
88-
$textReplacements = [Ordered]@{}
89-
}
90-
91-
process {
92-
if ($PipeScriptOutput -is [Collections.IDictionary]) {
93-
$astReplacementCount = $astReplacements.Count
94-
$textReplacementCount = $textReplacements.Count
95-
foreach ($kv in $PipeScriptOutput.GetEnumerator()) {
96-
if ($kv.Key -is [Management.Automation.Language.Ast]) {
97-
$astReplacements[$kv.Key] = $kv.Value
98-
} elseif ($kv.Key -match '^\d,\d$') {
99-
$textReplacements["$($kv.Key)"] = $[kv.Value
100-
}
101-
}
102-
if ($astReplacementCount -eq $astReplacements.Count -and
103-
$textReplacementCount -eq $textReplacements.Count) {
104-
$PipeScriptOutput
105-
}
106-
} else {
107-
$PipeScriptOutput
108-
}
109-
}
110-
111-
end {
112-
if ($OriginalInputObject -is [scriptblock] -and $astReplacements.Count -or $textReplacements.Count) {
113-
Update-PipeScript -ScriptBlock $OriginalInputObject -AstReplacement $astReplacements -TextReplacement $textReplacements
114-
} elseif ($OriginalInputObject -is [string] -and $textReplacements.Count) {
115-
Update-PipeScript -Text $OriginalInputObject -TextReplacement $textReplacements
116-
}
117-
}
118-
}
119-
76+
12077
function TypeConstraintToArguments (
12178
[Parameter(ValueFromPipeline)]
12279
$TypeName
@@ -164,20 +121,24 @@
164121
}
165122

166123
$InvokePipeScriptParameters = [Ordered]@{} + $psBoundParameters
124+
$TranspilerErrors = @()
125+
$TranspilerWarnings = @()
126+
$ErrorsAndWarnings = @{ErrorVariable='TranspilerErrors';WarningVariable='TranspilerWarnings'}
127+
167128

168129
# If the command is a ```[ScriptBlock]```
169130
if ($Command -is [scriptblock])
170131
{
171132
# Attempt to transpile it.
172-
$transpilationErrors = @()
173-
$TranspiledScriptBlock = $Command | .>Pipescript -ErrorVariable TranspilationErrors
133+
$TranspiledScriptBlock = $Command | .>Pipescript @ErrorsAndWarnings
174134
if (-not $TranspiledScriptBlock) { # If we could not transpile it
175135
Write-Error "Command {$command} could not be transpiled" # error out.
176136

177137
$null =
178138
New-Event -SourceIdentifier 'PipeScript.Transpilation.Failed' -MessageData ([PSCustomObject][Ordered]@{
179139
Command = $command
180-
Error = $transpilationErrors
140+
Error = $TranspilerErrors
141+
Warning = $TranspilerWarnings
181142
})
182143
return
183144
}
@@ -226,7 +187,7 @@
226187
# If we could, recursively reinvoke.
227188
if ($command -is [ScriptBlock]) {
228189
$InvokePipeScriptParameters.Command = $command
229-
Invoke-PipeScript @InvokePipeScriptParameters
190+
Invoke-PipeScript @InvokePipeScriptParameters
230191
}
231192
}
232193
}
@@ -242,27 +203,31 @@
242203
if ($Command.Source -notmatch $IsSourceGenerator ) {
243204
# invoke it normally.
244205
if ($InputObject) {
245-
$InputObject | & $Command @Parameter @ArgumentList | TranspileOutput -OriginalInputObject $InputObject
206+
$InputObject | & $Command @Parameter @ArgumentList
246207
} else {
247-
& $Command @Parameter @ArgumentList | TranspileOutput -OriginalInputObject $InputObject
208+
& $Command @Parameter @ArgumentList
248209
}
249210
}
250211

251212
# If the command was a source generator
252-
else {
213+
else {
253214
# predetermine the output path
254215
$outputPath = $($Command.Source -replace $IsSourceGenerator, '.${ext}')
255216
# and attempt to find a transpiler.
256217
$foundTranspiler = Get-Transpiler -CouldPipe $Command -ValidateInput $Command -ErrorAction Ignore
257-
218+
219+
$ParamsAndArgs = [Ordered]@{Parameter=$Parameter;ArgumentList = $ArgumentList}
220+
$transpilerErrors = @()
221+
$transpilerWarnings = @()
222+
258223

259224
# Push into the location of the file, so the current working directory will be accurate for any inline scripts.
260225
Push-Location ($command.Source | Split-Path)
261226

262227
# Get the output from the source generator.
263228
$pipescriptOutput =
264229
if ($foundTranspiler) { # If we found transpilers
265-
foreach ($ft in $foundTranspiler) {
230+
foreach ($ft in $foundTranspiler) {
266231
# run them.
267232

268233
$null =
@@ -271,13 +236,16 @@
271236
SourcePath = $command.Source
272237
})
273238

274-
$transpilerOutput = $command | & $ft.ExtensionCommand
275-
239+
$transpilerOutput = $command |
240+
& $ft.ExtensionCommand @ErrorsAndWarnings @ParamsAndArgs
241+
276242
$null =
277243
New-Event -SourceIdentifier 'PipeScript.SourceGenerator.Stop' -MessageData ([PSCustomObject][Ordered]@{
278244
Transpiler = $ft.ExtensionCommand
279245
TranspilerOutput = $transpilerOutput
280246
SourcePath = $command.Source
247+
Errors = $TranspilerErrors
248+
Warnings = $TranspilerWarnings
281249
})
282250

283251
$transpilerOutput =
@@ -306,13 +274,13 @@
306274
$fileText = [IO.File]::ReadAllText($Command.Source)
307275
# and attempt to create a script block
308276
try {
309-
[scriptblock]::Create($fileText) | .>Pipescript
277+
[scriptblock]::Create($fileText) | .>Pipescript @ErrorsAndWarnings
310278
} catch {
311279
$ex = $_
312280
Write-Error "[CommandInfo] -Command could not be made into a [ScriptBlock]: $ex"
313281
}
314282
} else {
315-
$Command.ScriptBlock | .>Pipescript
283+
$Command.ScriptBlock | .>Pipescript @ErrorsAndWarnings
316284
}
317285

318286
if (-not $fileScriptBlock) { return }
@@ -327,6 +295,25 @@
327295
# Now that the source generator has finished running, we can Pop-Location.
328296
Pop-Location
329297

298+
if ($TranspilerErrors) {
299+
$failedMessage = @(
300+
"$($command.Source): " + "$($TranspilerErrors.Count) error(s)"
301+
if ($transpilerWarnings) {
302+
"$($TranspilerWarnings.Count) warning(s)"
303+
}
304+
) -join ','
305+
Write-Error $failedMessage -ErrorId Build.Failed -TargetObject (
306+
[PSCustomObject][ordered]@{
307+
Output = $pipescriptOutput
308+
Errors = $TranspilerErrors
309+
Warnings = $TranspilerWarnings
310+
Command = $Command
311+
Parameters = $InvokePipeScriptParameters
312+
}
313+
)
314+
return
315+
}
316+
330317
# If the source generator outputted a byte[]
331318
if ($pipeScriptOutput -as [byte[]]) {
332319
# Save the content to $OutputPath as bytes.
@@ -466,8 +453,7 @@
466453
$ArgumentList += $stringArguments
467454
if ($InputObject) {
468455
$inputObject |
469-
& $foundTranspiler @ArgumentList @Parameter |
470-
TranspileOutput -OriginalInputObject $InputObject
456+
& $foundTranspiler @ArgumentList @Parameter
471457
} else {
472458
& $foundTranspiler @ArgumentList @Parameter
473459
}
@@ -485,8 +471,7 @@
485471
}
486472
if ($canPipe) {
487473
$inputObject |
488-
& $realCommandExists @Parameter @ArgumentList |
489-
TranspileOutput -OriginalInputObject $InputObject
474+
& $realCommandExists @Parameter @ArgumentList
490475
} else {
491476
& $realCommandExists @Parameter @ArgumentList
492477
}
@@ -559,9 +544,9 @@
559544

560545
if ($foundTranspiler) {
561546
if ($InputObject) {
562-
$inputObject | & $foundTranspiler @ArgumentList @Parameter | TranspileOutput -OriginalInputObject $InputObject
547+
$inputObject | & $foundTranspiler @ArgumentList @Parameter
563548
} else {
564-
& $foundTranspiler @ArgumentList @Parameter | TranspileOutput -OriginalInputObject $InputObject
549+
& $foundTranspiler @ArgumentList @Parameter
565550
}
566551
} else {
567552
Write-Error "Could not find Transpiler '$TranspilerStepName'"

PipeScript.psd1

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
@{
2-
ModuleVersion = '0.0.10'
2+
ModuleVersion = '0.0.11'
33
Description = 'An Extensible Transpiler for PowerShell (and anything else)'
44
RootModule = 'PipeScript.psm1'
55
PowerShellVersion = '4.0'
@@ -17,6 +17,11 @@
1717

1818
Tags = 'PipeScript','PowerShell', 'Transpilation', 'Compiler'
1919
ReleaseNotes = @'
20+
## 0.0.11:
21+
* Source Generators Now Support Parameters / Arguments (#75)
22+
* Invoke-PipeScript Terminating Build Errors (#135)
23+
---
24+
2025
## 0.0.10:
2126
* Improvements:
2227
* REST transpiler

PipeScript.tests.ps1

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,12 @@ describe PipeScript {
2424
namespace TestProgram/*{Get-Random}*/ {
2525
public static class Program {
2626
public static string Hello() {
27-
string helloMessage = /*{
28-
'"hello"', '"hello world"', '"hey there"', '"howdy"' | Get-Random
27+
string helloMessage = /*{param($n)
28+
if ($n) {
29+
"`"hello $n`""
30+
} else {
31+
'"hello"', '"hello world"', '"hey there"', '"howdy"' | Get-Random
32+
}
2933
}*/ string.Empty;
3034
return helloMessage;
3135
}
@@ -40,6 +44,10 @@ describe PipeScript {
4044
$addedType = Add-Type -TypeDefinition (Get-Content $addedFile.FullName -Raw) -PassThru
4145
$addedType::Hello() | Should -belike 'H*'
4246

47+
$addedFile2 = .> .\HelloWorld.ps1.cs 1
48+
$addedType2 = Add-Type -TypeDefinition (Get-Content $addedFile.FullName -Raw) -PassThru
49+
$addedType2::Hello() | Should -be 'Hello 1'
50+
4351
Remove-Item .\HelloWorld.ps1.cs
4452
Remove-Item $AddedFile.FullName
4553
}

0 commit comments

Comments
 (0)