|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | + Batch PipeScript Transpiler. |
| 4 | +.DESCRIPTION |
| 5 | + Transpiles Windows Batch with Inline PipeScript into Batch Scripts. |
| 6 | +
|
| 7 | + Because Batch Scripts only allow single-line comments, this is done using a pair of comment markers. |
| 8 | + |
| 9 | +
|
| 10 | + ```batch |
| 11 | + :: { |
| 12 | +
|
| 13 | + Uncommented lines between these two points will be ignored |
| 14 | +
|
| 15 | + :: # Commented lines will become PipeScript / PowerShell. |
| 16 | + :: param($message = 'hello world') |
| 17 | + :: "echo $message" |
| 18 | +
|
| 19 | + :: } |
| 20 | + ``` |
| 21 | +.EXAMPLE |
| 22 | + Invoke-PipeScript { |
| 23 | + $batchScript = ' |
| 24 | + :: { |
| 25 | +
|
| 26 | + Uncommented lines between these two points will be ignored |
| 27 | +
|
| 28 | + :: # Commented lines will become PipeScript / PowerShell. |
| 29 | + :: param($message = "hello world") |
| 30 | + :: "echo $message" |
| 31 | +
|
| 32 | + :: } |
| 33 | + ' |
| 34 | + |
| 35 | + [OutputFile('.\HelloWorld.ps1.cmd')]$batchScript |
| 36 | + } |
| 37 | +
|
| 38 | + Invoke-PipeScript .\HelloWorld.ps1.cmd |
| 39 | +#> |
| 40 | +[ValidateScript({ |
| 41 | + $cmdInfo = $_ |
| 42 | + if ($cmdInfo.Source -match '\.cmd$') { |
| 43 | + return $true |
| 44 | + } |
| 45 | + return $false |
| 46 | +})] |
| 47 | +param( |
| 48 | +# The command information. This will include the path to the file. |
| 49 | +[Parameter(Mandatory,ValueFromPipeline)] |
| 50 | +[Management.Automation.CommandInfo] |
| 51 | +$CommandInfo, |
| 52 | + |
| 53 | +# A dictionary of parameters. |
| 54 | +[Collections.IDictionary] |
| 55 | +$Parameter, |
| 56 | + |
| 57 | +# A list of arguments. |
| 58 | +[PSObject[]] |
| 59 | +$ArgumentList |
| 60 | +) |
| 61 | + |
| 62 | +begin { |
| 63 | + # We start off by declaring a number of regular expressions: |
| 64 | + $startComment = '(?>(?>\:\:|rem)\s{0,}(?:PipeScript)?\s{0,}\{)' |
| 65 | + $endComment = '(?>(?>\:\:|rem)\s{0,}(?:PipeScript)?\s{0,}\})' |
| 66 | + # * StartRegex ```$StartComment + '{' + $Whitespace``` |
| 67 | + $startRegex = "(?<PSStart>${startComment})" |
| 68 | + # * EndRegex ```$whitespace + '}' + $EndComment``` |
| 69 | + $endRegex = "(?<PSEnd>${endComment})" |
| 70 | + |
| 71 | + # Create a splat containing arguments to the core inline transpiler |
| 72 | + $Splat = [Ordered]@{ |
| 73 | + StartPattern = $startRegex |
| 74 | + EndPattern = $endRegex |
| 75 | + LinePattern = "^\s{0,}(?>\:\:|rem)\s{0,}" |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +process { |
| 80 | + # Add parameters related to the file |
| 81 | + $Splat.SourceFile = $commandInfo.Source -as [IO.FileInfo] |
| 82 | + $Splat.SourceText = [IO.File]::ReadAllText($commandInfo.Source) |
| 83 | + if ($Parameter) { $splat.Parameter = $Parameter } |
| 84 | + if ($ArgumentList) { $splat.ArgumentList = $ArgumentList } |
| 85 | + |
| 86 | + # Call the core inline transpiler. |
| 87 | + .>PipeScript.Inline @Splat |
| 88 | +} |
0 commit comments