forked from PowerShell/EditorSyntax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-grammar.ps1
35 lines (31 loc) · 1.65 KB
/
build-grammar.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# convert the PowerShell grammar from PLIST to JSON format
try {
# create the output folder if it does not exist
if (-not (Test-Path grammars -PathType Container)) {
New-Item grammars -ItemType Directory | Out-Null
}
# read in the PList grammar
$grammar_plist = [xml](Get-Content PowerShellSyntax.tmLanguage) | ConvertFrom-Plist
# create the JSON grammar heading, including the commit version
$grammar_json = [ordered]@{
information_for_contributors =
'This file has been converted from source and may not be in line with the official build.',
'The current master branch for this syntax can be found here: https://github.com/PowerShell/EditorSyntax',
'If you want to provide a fix or improvement, please create a pull request against the original repository.'
version = git rev-parse HEAD
}
# move select items from the PList to the JSON grammar in a specific order, if they exist in the PList
foreach ($key in 'name', 'scopeName', 'comment', 'injections', 'patterns', 'repository') {
if ($grammar_plist.$key) {
$grammar_json[$key] = $grammar_plist.$key
}
}
# output the final JSON grammar (changing the leading double spaces to tabs)
# Note: on PS <= 5.1, ConvertTo-JSON uses 4 spaces or alignment with the parent property,
# PS >= 6.0 uses 2, so the `-replace` only works correctly with PS >= 6.0
($grammar_json | ConvertTo-Json -Depth 100) -replace "(?m) (?<=^(?: )*)", "`t" |
Set-Content grammars\powershell.tmLanguage.json -Encoding UTF8
}
catch {
throw # error occured, give it forward to the user
}