Skip to content

Commit d68d3d9

Browse files
author
James Brundage
committed
Adding Kusto Template Support (Fixes #416)
1 parent 1229db6 commit d68d3d9

File tree

1 file changed

+86
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)