|
73 | 73 | # If we didn't, do that now. |
74 | 74 | $script:TypeAcceleratorsList = [PSObject].Assembly.GetType("System.Management.Automation.TypeAccelerators")::Get.Keys |
75 | 75 | } |
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 | + |
120 | 77 | function TypeConstraintToArguments ( |
121 | 78 | [Parameter(ValueFromPipeline)] |
122 | 79 | $TypeName |
|
164 | 121 | } |
165 | 122 |
|
166 | 123 | $InvokePipeScriptParameters = [Ordered]@{} + $psBoundParameters |
| 124 | + $TranspilerErrors = @() |
| 125 | + $TranspilerWarnings = @() |
| 126 | + $ErrorsAndWarnings = @{ErrorVariable='TranspilerErrors';WarningVariable='TranspilerWarnings'} |
| 127 | + |
167 | 128 |
|
168 | 129 | # If the command is a ```[ScriptBlock]``` |
169 | 130 | if ($Command -is [scriptblock]) |
170 | 131 | { |
171 | 132 | # Attempt to transpile it. |
172 | | - $transpilationErrors = @() |
173 | | - $TranspiledScriptBlock = $Command | .>Pipescript -ErrorVariable TranspilationErrors |
| 133 | + $TranspiledScriptBlock = $Command | .>Pipescript @ErrorsAndWarnings |
174 | 134 | if (-not $TranspiledScriptBlock) { # If we could not transpile it |
175 | 135 | Write-Error "Command {$command} could not be transpiled" # error out. |
176 | 136 |
|
177 | 137 | $null = |
178 | 138 | New-Event -SourceIdentifier 'PipeScript.Transpilation.Failed' -MessageData ([PSCustomObject][Ordered]@{ |
179 | 139 | Command = $command |
180 | | - Error = $transpilationErrors |
| 140 | + Error = $TranspilerErrors |
| 141 | + Warning = $TranspilerWarnings |
181 | 142 | }) |
182 | 143 | return |
183 | 144 | } |
|
226 | 187 | # If we could, recursively reinvoke. |
227 | 188 | if ($command -is [ScriptBlock]) { |
228 | 189 | $InvokePipeScriptParameters.Command = $command |
229 | | - Invoke-PipeScript @InvokePipeScriptParameters |
| 190 | + Invoke-PipeScript @InvokePipeScriptParameters |
230 | 191 | } |
231 | 192 | } |
232 | 193 | } |
|
242 | 203 | if ($Command.Source -notmatch $IsSourceGenerator ) { |
243 | 204 | # invoke it normally. |
244 | 205 | if ($InputObject) { |
245 | | - $InputObject | & $Command @Parameter @ArgumentList | TranspileOutput -OriginalInputObject $InputObject |
| 206 | + $InputObject | & $Command @Parameter @ArgumentList |
246 | 207 | } else { |
247 | | - & $Command @Parameter @ArgumentList | TranspileOutput -OriginalInputObject $InputObject |
| 208 | + & $Command @Parameter @ArgumentList |
248 | 209 | } |
249 | 210 | } |
250 | 211 |
|
251 | 212 | # If the command was a source generator |
252 | | - else { |
| 213 | + else { |
253 | 214 | # predetermine the output path |
254 | 215 | $outputPath = $($Command.Source -replace $IsSourceGenerator, '.${ext}') |
255 | 216 | # and attempt to find a transpiler. |
256 | 217 | $foundTranspiler = Get-Transpiler -CouldPipe $Command -ValidateInput $Command -ErrorAction Ignore |
257 | | - |
| 218 | + |
| 219 | + $ParamsAndArgs = [Ordered]@{Parameter=$Parameter;ArgumentList = $ArgumentList} |
| 220 | + $transpilerErrors = @() |
| 221 | + $transpilerWarnings = @() |
| 222 | + |
258 | 223 |
|
259 | 224 | # Push into the location of the file, so the current working directory will be accurate for any inline scripts. |
260 | 225 | Push-Location ($command.Source | Split-Path) |
261 | 226 |
|
262 | 227 | # Get the output from the source generator. |
263 | 228 | $pipescriptOutput = |
264 | 229 | if ($foundTranspiler) { # If we found transpilers |
265 | | - foreach ($ft in $foundTranspiler) { |
| 230 | + foreach ($ft in $foundTranspiler) { |
266 | 231 | # run them. |
267 | 232 |
|
268 | 233 | $null = |
|
271 | 236 | SourcePath = $command.Source |
272 | 237 | }) |
273 | 238 |
|
274 | | - $transpilerOutput = $command | & $ft.ExtensionCommand |
275 | | - |
| 239 | + $transpilerOutput = $command | |
| 240 | + & $ft.ExtensionCommand @ErrorsAndWarnings @ParamsAndArgs |
| 241 | + |
276 | 242 | $null = |
277 | 243 | New-Event -SourceIdentifier 'PipeScript.SourceGenerator.Stop' -MessageData ([PSCustomObject][Ordered]@{ |
278 | 244 | Transpiler = $ft.ExtensionCommand |
279 | 245 | TranspilerOutput = $transpilerOutput |
280 | 246 | SourcePath = $command.Source |
| 247 | + Errors = $TranspilerErrors |
| 248 | + Warnings = $TranspilerWarnings |
281 | 249 | }) |
282 | 250 |
|
283 | 251 | $transpilerOutput = |
|
306 | 274 | $fileText = [IO.File]::ReadAllText($Command.Source) |
307 | 275 | # and attempt to create a script block |
308 | 276 | try { |
309 | | - [scriptblock]::Create($fileText) | .>Pipescript |
| 277 | + [scriptblock]::Create($fileText) | .>Pipescript @ErrorsAndWarnings |
310 | 278 | } catch { |
311 | 279 | $ex = $_ |
312 | 280 | Write-Error "[CommandInfo] -Command could not be made into a [ScriptBlock]: $ex" |
313 | 281 | } |
314 | 282 | } else { |
315 | | - $Command.ScriptBlock | .>Pipescript |
| 283 | + $Command.ScriptBlock | .>Pipescript @ErrorsAndWarnings |
316 | 284 | } |
317 | 285 |
|
318 | 286 | if (-not $fileScriptBlock) { return } |
|
327 | 295 | # Now that the source generator has finished running, we can Pop-Location. |
328 | 296 | Pop-Location |
329 | 297 |
|
| 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 | + |
330 | 317 | # If the source generator outputted a byte[] |
331 | 318 | if ($pipeScriptOutput -as [byte[]]) { |
332 | 319 | # Save the content to $OutputPath as bytes. |
|
466 | 453 | $ArgumentList += $stringArguments |
467 | 454 | if ($InputObject) { |
468 | 455 | $inputObject | |
469 | | - & $foundTranspiler @ArgumentList @Parameter | |
470 | | - TranspileOutput -OriginalInputObject $InputObject |
| 456 | + & $foundTranspiler @ArgumentList @Parameter |
471 | 457 | } else { |
472 | 458 | & $foundTranspiler @ArgumentList @Parameter |
473 | 459 | } |
|
485 | 471 | } |
486 | 472 | if ($canPipe) { |
487 | 473 | $inputObject | |
488 | | - & $realCommandExists @Parameter @ArgumentList | |
489 | | - TranspileOutput -OriginalInputObject $InputObject |
| 474 | + & $realCommandExists @Parameter @ArgumentList |
490 | 475 | } else { |
491 | 476 | & $realCommandExists @Parameter @ArgumentList |
492 | 477 | } |
|
559 | 544 |
|
560 | 545 | if ($foundTranspiler) { |
561 | 546 | if ($InputObject) { |
562 | | - $inputObject | & $foundTranspiler @ArgumentList @Parameter | TranspileOutput -OriginalInputObject $InputObject |
| 547 | + $inputObject | & $foundTranspiler @ArgumentList @Parameter |
563 | 548 | } else { |
564 | | - & $foundTranspiler @ArgumentList @Parameter | TranspileOutput -OriginalInputObject $InputObject |
| 549 | + & $foundTranspiler @ArgumentList @Parameter |
565 | 550 | } |
566 | 551 | } else { |
567 | 552 | Write-Error "Could not find Transpiler '$TranspilerStepName'" |
|
0 commit comments