|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | + XAML Template Transpiler. |
| 4 | +.DESCRIPTION |
| 5 | + Allows PipeScript to generate XAML. |
| 6 | +
|
| 7 | + Multiline comments blocks like this ```<!--{}-->``` will be treated as blocks of PipeScript. |
| 8 | +
|
| 9 | + Executed output will be converted to XAML |
| 10 | +#> |
| 11 | +[ValidatePattern('\.xaml$')] |
| 12 | +param( |
| 13 | +# The command information. This will include the path to the file. |
| 14 | +[Parameter(Mandatory,ValueFromPipeline,ParameterSetName='TemplateFile')] |
| 15 | +[Management.Automation.CommandInfo] |
| 16 | +$CommandInfo, |
| 17 | + |
| 18 | +# If set, will return the information required to dynamically apply this template to any text. |
| 19 | +[Parameter(Mandatory,ParameterSetName='TemplateObject')] |
| 20 | +[switch] |
| 21 | +$AsTemplateObject, |
| 22 | + |
| 23 | +# A dictionary of parameters. |
| 24 | +[Collections.IDictionary] |
| 25 | +$Parameter, |
| 26 | + |
| 27 | +# A list of arguments. |
| 28 | +[PSObject[]] |
| 29 | +$ArgumentList |
| 30 | +) |
| 31 | + |
| 32 | +begin { |
| 33 | + # We start off by declaring a number of regular expressions: |
| 34 | + $startComment = '<\!--' # * Start Comments ```<!--``` |
| 35 | + $endComment = '-->' # * End Comments ```-->``` |
| 36 | + $Whitespace = '[\s\n\r]{0,}' |
| 37 | + # * StartRegex ```$StartComment + '{' + $Whitespace``` |
| 38 | + $startRegex = "(?<PSStart>${startComment}\{$Whitespace)" |
| 39 | + # * EndRegex ```$whitespace + '}' + $EndComment``` |
| 40 | + $endRegex = "(?<PSEnd>$Whitespace\}${endComment}\s{0,})" |
| 41 | + |
| 42 | + # Create a splat containing arguments to the core inline transpiler |
| 43 | + $Splat = [Ordered]@{ |
| 44 | + StartPattern = $startRegex |
| 45 | + EndPattern = $endRegex |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +process { |
| 50 | + # If we have been passed a command |
| 51 | + if ($CommandInfo) { |
| 52 | + # add parameters related to the file. |
| 53 | + $Splat.SourceFile = $commandInfo.Source -as [IO.FileInfo] |
| 54 | + $Splat.SourceText = [IO.File]::ReadAllText($commandInfo.Source) |
| 55 | + } |
| 56 | + |
| 57 | + if ($Parameter) { $splat.Parameter = $Parameter } |
| 58 | + if ($ArgumentList) { $splat.ArgumentList = $ArgumentList } |
| 59 | + |
| 60 | + $splat.ForeachObject = { |
| 61 | + $in = $_ |
| 62 | + if (($in -is [string]) -or |
| 63 | + ($in.GetType -and $in.GetType().IsPrimitive)) { |
| 64 | + $in |
| 65 | + } elseif ($in.ChildNodes) { |
| 66 | + foreach ($inChildNode in $in.ChildNodes) { |
| 67 | + if ($inChildNode.NodeType -ne 'XmlDeclaration') { |
| 68 | + $inChildNode.OuterXml |
| 69 | + } |
| 70 | + } |
| 71 | + } else { |
| 72 | + $inXml = (ConvertTo-Xml -Depth 100 -InputObject $in) |
| 73 | + foreach ($inChildNode in $inXml.ChildNodes) { |
| 74 | + if ($inChildNode.NodeType -ne 'XmlDeclaration') { |
| 75 | + $inChildNode.OuterXml |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + # If we are being used within a keyword, |
| 82 | + if ($AsTemplateObject) { |
| 83 | + $splat # output the parameters we would use to evaluate this file. |
| 84 | + } else { |
| 85 | + # Otherwise, call the core template transpiler |
| 86 | + .>PipeScript.Template @Splat # and output the changed file. |
| 87 | + } |
| 88 | +} |
0 commit comments