|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | + Scala Template Transpiler. |
| 4 | +.DESCRIPTION |
| 5 | + Allows PipeScript to generate Scala. |
| 6 | +
|
| 7 | + Multiline comments with /*{}*/ will be treated as blocks of PipeScript. |
| 8 | +
|
| 9 | + Multiline comments can be preceeded or followed by 'empty' syntax, which will be ignored. |
| 10 | +
|
| 11 | + This for Inline PipeScript to be used with operators, and still be valid Scala syntax. |
| 12 | +
|
| 13 | + The Scala Template Transpiler will consider the following syntax to be empty: |
| 14 | +
|
| 15 | + * ```null``` |
| 16 | + * ```""``` |
| 17 | + * ```''``` |
| 18 | +#> |
| 19 | +[ValidatePattern('\.(?>scala|sc)$')] |
| 20 | +param( |
| 21 | +# The command information. This will include the path to the file. |
| 22 | +[Parameter(Mandatory,ValueFromPipeline,ParameterSetName='TemplateFile')] |
| 23 | +[Management.Automation.CommandInfo] |
| 24 | +$CommandInfo, |
| 25 | + |
| 26 | +# If set, will return the information required to dynamically apply this template to any text. |
| 27 | +[Parameter(Mandatory,ParameterSetName='TemplateObject')] |
| 28 | +[switch] |
| 29 | +$AsTemplateObject, |
| 30 | + |
| 31 | +# A dictionary of parameters. |
| 32 | +[Collections.IDictionary] |
| 33 | +$Parameter, |
| 34 | + |
| 35 | +# A list of arguments. |
| 36 | +[PSObject[]] |
| 37 | +$ArgumentList |
| 38 | +) |
| 39 | + |
| 40 | +begin { |
| 41 | + # We start off by declaring a number of regular expressions: |
| 42 | + $startComment = '/\*' # * Start Comments ```\*``` |
| 43 | + $endComment = '\*/' # * End Comments ```/*``` |
| 44 | + $Whitespace = '[\s\n\r]{0,}' |
| 45 | + # * IgnoredContext ```String.empty```, ```null```, blank strings and characters |
| 46 | + $IgnoredContext = "(?<ignore>(?>$("null", '""', "''" -join '|'))\s{0,}){0,1}" |
| 47 | + # * StartRegex ```$IgnoredContext + $StartComment + '{' + $Whitespace``` |
| 48 | + $startRegex = "(?<PSStart>${IgnoredContext}${startComment}\{$Whitespace)" |
| 49 | + # * EndRegex ```$whitespace + '}' + $EndComment + $ignoredContext``` |
| 50 | + $endRegex = "(?<PSEnd>$Whitespace\}${endComment}\s{0,}${IgnoredContext})" |
| 51 | + |
| 52 | + # Create a splat containing arguments to the core inline transpiler |
| 53 | + $Splat = [Ordered]@{ |
| 54 | + StartPattern = $startRegex |
| 55 | + EndPattern = $endRegex |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +process { |
| 60 | + # If we have been passed a command |
| 61 | + if ($CommandInfo) { |
| 62 | + # add parameters related to the file. |
| 63 | + $Splat.SourceFile = $commandInfo.Source -as [IO.FileInfo] |
| 64 | + $Splat.SourceText = [IO.File]::ReadAllText($commandInfo.Source) |
| 65 | + } |
| 66 | + |
| 67 | + if ($Parameter) { $splat.Parameter = $Parameter } |
| 68 | + if ($ArgumentList) { $splat.ArgumentList = $ArgumentList } |
| 69 | + |
| 70 | + # If we are being used within a keyword, |
| 71 | + if ($AsTemplateObject) { |
| 72 | + $splat # output the parameters we would use to evaluate this file. |
| 73 | + } else { |
| 74 | + # Otherwise, call the core template transpiler |
| 75 | + .>PipeScript.Template @Splat # and output the changed file. |
| 76 | + } |
| 77 | +} |
0 commit comments