|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | + Core Protocol Transpiler |
| 4 | +.DESCRIPTION |
| 5 | + Enables the transpilation of protocols. |
| 6 | +
|
| 7 | + ```https://api.github.com/repos/StartAutomating/PipeScript/issues``` is a valid command. |
| 8 | +
|
| 9 | + So is ```get https://api.github.com/repos/StartAutomating/PipeScript/issues```. |
| 10 | + |
| 11 | + So is ```MyCustomProtocol:// -Parameter value```. |
| 12 | +
|
| 13 | + This transpiler enables commands in protocol format to be transpiled. |
| 14 | +.NOTES |
| 15 | + This transpiler will match any command whose first or second element contains ```://``` |
| 16 | +.EXAMPLE |
| 17 | + .> -ScriptBlock { |
| 18 | + https://api.github.com/users/StartAutomating |
| 19 | + } |
| 20 | +.EXAMPLE |
| 21 | + .> -ScriptBlock { |
| 22 | + $userName = 'StartAutomating' |
| 23 | + https://$GitHubApi/users/$UserName |
| 24 | + } |
| 25 | +.EXAMPLE |
| 26 | + .> -ScriptBlock { |
| 27 | + $env:GitUserName = 'StartAutomating' |
| 28 | + https://api.github.com/users/$env:GitUserName |
| 29 | + } |
| 30 | +#> |
| 31 | +[ValidateScript({ |
| 32 | + $commandAst = $_ |
| 33 | + if ($commandAst.CommandElements -and |
| 34 | + $commandAst.CommandElements[0] -match '://') { |
| 35 | + return $true |
| 36 | + } |
| 37 | + if ($commandAst.CommandElements.Count -ge 2 -and |
| 38 | + $commandAst.CommandElements[1] -match '://') { |
| 39 | + return $true |
| 40 | + } |
| 41 | + return $false |
| 42 | +})] |
| 43 | +param( |
| 44 | +# The Command Abstract Syntax Tree. |
| 45 | +[Parameter(Mandatory,ValueFromPipeline)] |
| 46 | +[Management.Automation.Language.CommandAst] |
| 47 | +$CommandAst |
| 48 | +) |
| 49 | + |
| 50 | +process { |
| 51 | + [string]$CommandMethod = '' |
| 52 | + [string]$commandName = |
| 53 | + if ($CommandAst.CommandElements[0].Value -match '://') { |
| 54 | + $CommandAst.CommandElements[0].Value |
| 55 | + } |
| 56 | + else |
| 57 | + { |
| 58 | + $commandMethod = $CommandAst.CommandElements[0].Value |
| 59 | + $CommandAst.CommandElements[1].Value |
| 60 | + } |
| 61 | + |
| 62 | + $commandUri = |
| 63 | + if ($commandName -as [uri]) { |
| 64 | + $commandName -as [uri] |
| 65 | + } |
| 66 | + else { |
| 67 | + $commandName -replace '\$(.+)\:','$1__' -replace '\$','__' -as [uri] |
| 68 | + } |
| 69 | + |
| 70 | + $commandAstSplat = @{ |
| 71 | + CommandAST = $commandAst |
| 72 | + } |
| 73 | + |
| 74 | + if ($commandMethod) { |
| 75 | + $commandAstSplat.Method = $commandMethod |
| 76 | + } |
| 77 | + |
| 78 | + $foundTranspiler = |
| 79 | + Get-Transpiler -CouldPipe $commandUri -ValidateInput $CommandAst -CouldRun -Parameter $commandAstSplat |
| 80 | + |
| 81 | + if (-not $foundTranspiler) { |
| 82 | + Write-Error "Could not find a transpiler for $commandAst" |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + foreach ($found in $foundTranspiler) { |
| 87 | + $params = $found.ExtensionParameter |
| 88 | + $transpilerOutput = & $found.ExtensionCommand @params |
| 89 | + if ($transpilerOutput) { $transpilerOutput; break } |
| 90 | + } |
| 91 | +} |
0 commit comments