-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtag-checker.ps1
212 lines (205 loc) · 4.55 KB
/
tag-checker.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
# 5eTools Tag Checker
# ===================
#
# This script will scan your file, or every JSON file in a directory recursively, for `@tag`s. If any found `@tag`
# does not match against its list of known 5eTools tags, it will compile an error object for manual correction.
# Note that, although you can use the `-LogErrors` switch to be informed of errors as they're found, the script
# doesn't actually return anything until all files have been tested.
#
# To write the errors to a file, you should first convert the output to JSON (see examples below).
#
#
# Usage:
# & <path to script> [-Path] <path to file/directory> [-LogErrors]
#
# Parameters:
# -Path (string; required): absolute or relative path to a file or directory
# -LogErrors (switch): outputs error-containing files to the console early
#
# Examples:
# & .\tag-checker.ps1 -Path '.\homebrew'
# & .\tag-checker.ps1 -Path '.\homebrew\creature\badooga; Better Greatwyrms.json'
# & .\tag-checker.ps1 -Path '.\homebrew' -LogErrors
# & .\tag-checker.ps1 -Path '.\homebrew' | ConvertTo-Json | Out-File '.\badFiles.json' -Encoding utf8
#
#
# Spappz 2023
#
# MIT License
PARAM (
# A path to a file or directory of files to test
[Parameter(Mandatory, Position = 0)]
[String]$Path,
# Log erroneous files to the console early (useful if testing a large directory)
[switch]$LogErrors
)
$keywords = @(
'b'
'bold'
'i'
'italic'
's'
'strike'
'u'
'underline'
'sup'
'sub'
'kbd'
'code'
'style'
'font'
'comic'
'comicH1'
'comicH2'
'comicH3'
'comicH4'
'comicNote'
'note'
'unit'
'h'
'm'
'atk'
'hitYourSpellAttack'
'dc'
'dcYourSpellSave'
'chance'
'd20'
'damage'
'dice'
'autodice'
'hit'
'recharge'
'ability'
'savingThrow'
'skillCheck'
'scaledice'
'scaledamage'
'coinflip'
'5etools'
'adventure'
'book'
'filter'
'footnote'
'link'
'loader'
'color'
'highlight'
'help'
'quickref'
'area'
'action'
'background'
'boon'
'charoption'
'class'
'condition'
'creature'
'cult'
'deck'
'disease'
'feat'
'hazard'
'item'
'itemMastery'
'language'
'legroup'
'object'
'optfeature'
'psionic'
'race'
'recipe'
'reward'
'vehicle'
'vehupgrade'
'sense'
'skill'
'spell'
'status'
'table'
'trap'
'variantrule'
'card'
'deity'
'classFeature'
'subclassFeature'
'homebrew'
'itemEntry'
'cite'
)
$target = Get-Item (Resolve-Path $Path -ErrorAction Stop)
switch ($target.Attributes) {
Archive {
if ($target.Extension -eq '.json') {
$files = @($target)
} else {
throw "Not a JSON file: $($target.FullName)"
}
break
}
Directory {
$baseFiles = Get-ChildItem $target -File
$i = $baseFiles.Name.IndexOf('.gitignore')
if ($i -ne -1) {
$gitignore = Get-Content $baseFiles[$i] -Encoding utf8 |
Where-Object { $_ -and $_ -notmatch '^#' } |
ForEach-Object {
# Escape .
# Convert * to any character
# Convert ? to any single character
# dir/ matches sub-paths
# /xyz matches only within root
# [!...] becomes [^...]
# Make path-separators neutral
$_ -replace '\.', '\.' `
-replace '\*', '.*' `
-replace '\?', '.' `
-replace '/$', '/.+' `
-replace '^/', [Regex]::Escape($target) `
-replace '\[!([^]]+)\]', '[^$1]' `
-replace '/', '[/|\\]'
}
$gitignore += 'package\.json$'
$gitignore += 'package-lock\.json$'
$files = @(
Get-ChildItem $target -File -Recurse | Where-Object {
$_.Extension -eq '.json' -and -not (Select-String -Quiet -Pattern $gitignore -InputObject $_.FullName)
}
)
} else {
$files = @(Get-ChildItem $target -File -Recurse | Where-Object { $_.Extension -eq '.json' })
}
if (-not $files.Count) {
throw "No JSON files found in $($target.FullName)"
exit 1
}
break
}
Default {
throw "Not a file or directory: $($target.FullName)"
exit 1
}
}
$fileCount = $files.Count
$errors = [System.Collections.Generic.List[psobject]]::new()
for ($i = 0; $i -lt $fileCount; $i++) {
Write-Progress -Activity 'Checking files...' -Id 1 -Status $files[$i].FullName -PercentComplete (100 * ($i / $fileCount))
$tags = [regex]::Matches((Get-Content $files[$i] -Encoding utf8NoBOM), '\{@(?<tag>[^|}\s]+)\b')
$badTags = $tags.groups.Where({ $_.Name -eq 'tag' -and $_.Value -cnotin $keywords })
if ($badTags.Count) {
$errorLog = [PSCustomObject]@{
path = $files[$i].FullName
badTags = @($badTags.Value | Select-Object -Unique)
}
if ($LogErrors) {
if (-not $errors.Count) {
Write-Warning 'Bad tags found in:'
}
Write-Host ("`t" + $errorLog.path)
}
$errors.Add($errorLog)
}
}
if ($LogErrors -and -not $errors.Count) {
Write-Host 'No errors found.'
}
return $errors