-
Notifications
You must be signed in to change notification settings - Fork 31
/
format-json.ps1
216 lines (176 loc) · 7.49 KB
/
format-json.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
param (
[string]$Path = ".",
[switch]$Fix = $false
)
$newline = [Environment]::NewLine
$windowsNewline = "`r`n"
$charA = [System.Convert]::ToChar(0xA)
$charD = [System.Convert]::ToChar(0xD)
function Format-Json {
Param(
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]
[string]$Json,
[int]$Indentation = 4
)
$indent = 0
$regexUnlessQuoted = '(?=([^"]*"[^"]*")*[^"]*$)'
$result = $Json -split $newline |
ForEach-Object {
# If the line contains a ] or } character,
# we need to decrement the indentation level, unless:
# - it is inside quotes, AND
# - it does not contain a [ or {
if (($_ -match "[}\]]$regexUnlessQuoted") -and ($_ -notmatch "[\{\[]$regexUnlessQuoted")) {
$indent = [Math]::Max($indent - $Indentation, 0)
}
# Replace all colon-space combinations by ": " unless it is inside quotes.
$line = (' ' * $indent) + ($_.TrimStart() -replace ":\s+$regexUnlessQuoted", ': ')
# If the line contains a [ or { character,
# we need to increment the indentation level, unless:
# - it is inside quotes, AND
# - it does not contain a ] or }
if (($_ -match "[\{\[]$regexUnlessQuoted") -and ($_ -notmatch "[}\]]$regexUnlessQuoted")) {
$indent += $Indentation
}
# Powershell 5.10 doesn't handle some chars well
# Replace escapped "\u0027" with "'"
# Replace escapped "\u0026" with "&"
$line = $line -replace "\\u0027", "'"
$line = $line -replace "\\u0026", "&"
$line
}
$res = ($result -Join $windowsNewline) -replace $charA, $windowsNewline
return $res
}
function Compare-Json {
param (
[System.Collections.Specialized.OrderedDictionary]$Base,
[System.Collections.Specialized.OrderedDictionary]$Translation
)
$missingKeys = @()
$superfluousKeys = @()
foreach ($key in $Base.Keys) {
if (-not $Translation.Contains($key)) {
$missingKeys += $key
}
}
foreach ($key in $Translation.Keys) {
if (-not $Base.Contains($key)) {
$superfluousKeys += $key
}
}
return [pscustomobject]@{
MissingKeys = $missingKeys
SuperfluousKeys = $superfluousKeys
}
}
function ConvertTo-OrderedDictionary {
param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[pscustomobject]$object
)
$ordered = [ordered]@{}
foreach ($property in $object.PSObject.Properties) {
$ordered[$property.Name] = $property.Value
}
return $ordered
}
function ConvertTo-OrderedDictionaryFromArray {
param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[object[]]$object
)
$ordered = [ordered]@{}
foreach ($item in $object) {
$ordered[$item.Name] = $item.Value
}
return $ordered
}
$fullPath = $Path | Resolve-Path
Write-Output "Scanning $fullPath"
Write-Output "$newline"
$baseFiles = Get-ChildItem -Path $Path -Filter "*.json" -Recurse | Where-Object { $_.Name -notmatch "\.[a-z]{2}(-[a-z]{2})?\.json$" }
$exitCode = 0
$problem = ""
foreach ($baseFile in $baseFiles) {
$baseJson = [IO.File]::ReadAllText($baseFile.FullName, [System.Text.Encoding]::UTF8)
$baseDictionary = $baseJson | ConvertFrom-Json | ConvertTo-OrderedDictionary
$translationFiles = $translationFiles = Get-ChildItem -Path $baseFile.DirectoryName -Filter "$($baseFile.BaseName).*.json"
foreach ($translationFile in $translationFiles) {
$translationJson =[IO.File]::ReadAllText($translationFile.FullName, [System.Text.Encoding]::UTF8);
$translation = $translationJson | ConvertFrom-Json | ConvertTo-OrderedDictionary
$comparison = Compare-Json -Base $baseDictionary -Translation $translation
if ($comparison.MissingKeys.Count -gt 0 -or $comparison.SuperfluousKeys.Count -gt 0) {
$exitCode = -1
Write-Output "File: $($translationFile.FullName)"
Write-Output "Missing Keys: $($comparison.MissingKeys -join ', ')"
Write-Output "Superfluous Keys: $($comparison.SuperfluousKeys -join ', ')"
if ($Fix) {
foreach ($key in $comparison.MissingKeys) {
$translation[$key] = $baseDictionary[$key]
}
foreach ($key in $comparison.SuperfluousKeys) {
$translation.Remove($key)
}
$formattedJson = ConvertTo-OrderedDictionaryFromArray($translation.GetEnumerator() | Sort-Object -Property Name ) | ConvertTo-Json -Depth 100 | Format-Json -Indentation 2
if ($formattedJson -ne $translationJson) {
Write-Output "Fixing translationFile"
[IO.File]::WriteAllText($translationFile.FullName, $formattedJson, [System.Text.Encoding]::UTF8)
}
}
}
else {
$formattedTranslationJson = ConvertTo-OrderedDictionaryFromArray($translation.GetEnumerator() | Sort-Object -Property Name) | ConvertTo-Json -Depth 100 | Format-Json -Indentation 2
if ($formattedTranslationJson -ne $translationJson) {
$exitCode = -1
$problem += "Formatting for [$translationFile] is wrong" + $newline
$length = [math]::Min($formattedTranslationJson.Length, $translationJson.Length)
for ($i = 0; $i -lt $length; $i++) {
if ($formattedTranslationJson[$i] -ne $translationJson[$i]) {
$hex1 = [System.Convert]::ToString([System.Convert]::ToInt32($formattedTranslationJson[$i]), 16)
$hex2 = [System.Convert]::ToString([System.Convert]::ToInt32($translationJson[$i]), 16)
Write-Output "Difference at position {$i}: (0x$hex1) vs (0x$hex2)"
break;
}
}
if ($Fix) {
Write-Output "Formatting [$translationFile]"
[IO.File]::WriteAllText( $translationFile.FullName, $formattedTranslationJson, [System.Text.Encoding]::UTF8)
}
}
}
}
$formattedBaseJson = ConvertTo-OrderedDictionaryFromArray( $baseDictionary.GetEnumerator() | Sort-Object -Property Name) | ConvertTo-Json -Depth 100 | Format-Json -Indentation 2
if ($formattedBaseJson -ne $baseJson) {
$exitCode = -1;
$problem += "Formatting for [$baseFile] is wrong" + $newline
$length = [math]::Min($formattedBaseJson.Length, $baseJson.Length)
for ($i = 0; $i -lt $length; $i++) {
if ($formattedBaseJson[$i] -ne $baseJson[$i]) {
$hex1 = [System.Convert]::ToString([System.Convert]::ToInt32($formattedBaseJson[$i]), 16)
$hex2 = [System.Convert]::ToString([System.Convert]::ToInt32($baseJson[$i]), 16)
Write-Output "Difference at position {$i}: (0x$hex1) vs (0x$hex2)"
break;
}
}
if ($Fix) {
Write-Output "Formatting [$baseFile]"
[IO.File]::WriteAllText($baseFile.FullName, $formattedBaseJson, [System.Text.Encoding]::UTF8)
}
}
}
Write-Output "$newline"
if ($Fix -and ($exitCode -eq -1)) {
$exitCode = 0
}
elseif ( $exitCode -eq -1) {
Write-Output "Problems found!"
if (-Not [String]::IsNullOrEmpty($problem) ) {
Write-Output $problem
}
}
# Success (0)
else {
Write-Output "No problem found!"
}
exit $exitCode