-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetIllegals.ps1
291 lines (273 loc) · 9.44 KB
/
GetIllegals.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
param([string]$path, [string]$all, [string]$mod)
$ErrorMessagePrefix = "[Error]"
$WarningMessagePrefix = "[Warn]"
# Получить изменения добавленные в файл в текущей ветке Git.
function Get-AddedCode ($file)
{
$firstcommit = git rev-list --min-parents=2 --max-count=1 HEAD
$diff = git diff $firstcommit HEAD $file
$addedcode = " "
foreach ($line in $diff)
{
if ($line -like "+*")
{
$addedcode = $addedcode + "`n" + $line
}
}
return $addedcode
}
# Поиск вызова Create|Delete|Get|GetAll вне серверных функций
function Get-FilesCreateDeleteGetIllegal ($file, $mod)
{
$result = 0
if ($mod -eq "Pipeline")
{
$diff = Get-AddedCode $file
$illegal = $diff | Select-String -Pattern '.*(.(Create|Delete|Get|GetAll))\(.*' -AllMatches -CaseSensitive
}
else
{
$illegal = $file | Select-String -Pattern '^.*\b(.(Create|Delete|Get|GetAll))\b\(.*$' -AllMatches -CaseSensitive
}
if ($illegal.Count -gt 0 -or $illegal.Matches.Count -gt 0)
{
foreach ($match in $illegal.Matches)
{
Write-Host "$ErrorMessagePrefix" $file.FullName "`t" $match.Value.TrimStart(" ")
$result = 1
}
}
return $result
}
# Поиск вызова Remote-функций|Get|GetAll в событиях CanExecute
# Технически могут находиться дубли при работе в режиме Pipeline
function Get-FilesCanExecuteIllegal ($file, $mod)
{
$result = 0
Get-Content $file.FullName | ForEach-Object { $contentstring += $_ } #Собрать содержимое файла в строку
# Регулярка будет находить первое значение, последующие в выборку попадать не будут. Последующие нелегалы будут найдены после пуша исправлений предыдущих.
$illegal = $contentstring | Select-String -Pattern 'CanExecute.*?(Remote|Get\(|GetAll\(|public)' -AllMatches -CaseSensitive
if ($illegal.Count -ne 0)
{
foreach ($match in $illegal.Matches)
{
if ($match.Value -ne $null -and ($match.Value.Contains("Remote") -or $match.Value.Contains("Get(") -or $match.Value.Contains("GetAll(")))
{
if ($mod -eq "Pipeline")
{
$diff = Get-AddedCode $file
$str = $match.Value.Trim()
# Написано методом подбора.
if ($str -match '(\S+[.]|\s)\b(Remote|Get|GetAll|public)')
{
$str = $matches[0]
if ("$diff" -like "*$str*")
{
Write-Host "$ErrorMessagePrefix" $file.FullName "`t`t" $match.Value.TrimStart(" ")
$result = 1
}
}
}
else
{
Write-Host "$ErrorMessagePrefix" $file.FullName "`t`t" $match.Value.TrimStart(" ")
$result = 1
}
}
}
}
return $result
}
# Поиск вызова Remote-функций|Get|GetAll в событии Refresh
function Get-FilesRefreshIllegal ($file, $mod)
{
$result = 0
Get-Content $file.FullName | ForEach-Object { $contentstring += $_ } #Собрать содержимое файла в строку
# Регулярка будет находить первое значение, последующие в выборку попадать не будут. Последующие нелегалы будут найдены после пуша исправлений предыдущих.
$illegal = $contentstring | Select-String -Pattern ' Refresh\(.*?(Remote|Get\(|GetAll\(|public)' -AllMatches -CaseSensitive
if ($illegal.Count -ne 0)
{
foreach ($match in $illegal.Matches)
{
if ($match.Value -ne $null -and ($match.Value.Contains("Remote") -or $match.Value.Contains("Get(") -or $match.Value.Contains("GetAll(")))
{
$diff = Get-AddedCode $file
$str = $match.Value.Trim()
if ($mod -eq "Pipeline" -and "$diff" -like "*$str*")
{
Write-Host "$ErrorMessagePrefix" $file.FullName "`t`t" $match.Value.TrimStart(" ")
$result = 1
}
if ($mod -ne "Pipeline")
{
Write-Host "$ErrorMessagePrefix" $file.FullName "`t`t" $match.Value.TrimStart(" ")
$result = 1
}
}
}
}
return $result
}
# Поиск использования Анонимных типов и Создания объектов с помощью new (кроме коллекции List)
function Get-FilesAnonymousIllegal ($file, $mod)
{
if ($mod -eq "Pipeline")
{
$diff = Get-AddedCode $file
$illegal = $diff | Select-String -Pattern '\s*new\s+(?!.*List)' -AllMatches -CaseSensitive
}
else
{
$illegal = $file | Select-String -Pattern '\s*new\s+(?!.*List)' -AllMatches -CaseSensitive
}
if ($illegal.Count -gt 0 -or $illegal.Matches.Count -gt 0)
{
foreach ($match in $illegal.Matches)
{
Write-Host "$WarningMessagePrefix" $file.FullName "`t`t" $match.Value.TrimStart(" ")
}
}
}
# Поиск Приведения к типу через is, as
function Get-FilesIsAsIllegal ($file, $mod)
{
if ($mod -eq "Pipeline")
{
$diff = Get-AddedCode $file
$illegal = $diff | Select-String -Pattern '\s(is|as)\s' -AllMatches -CaseSensitive
}
else
{
$illegal = $file | Select-String -Pattern '\s(is|as)\s' -AllMatches -CaseSensitive
}
if ($illegal.Count -gt 0 -or $illegal.Matches.Count -gt 0)
{
foreach ($match in $illegal.Matches)
{
Write-Host "$WarningMessagePrefix" $file.FullName "`t`t" $match.Value.TrimStart(" ")
}
}
}
# Использование Запрещенных классов из .Net
function Get-FilesNetClassIllegal ($file, $mod)
{
if ($mod -eq "Pipeline")
{
$diff = Get-AddedCode $file
$illegal = $diff | Select-String -Pattern '.*(Tuple|CultureInfo|Convert.To|System.(Windows|Reflection|Xml|Threading|Data))' -AllMatches -CaseSensitive
}
else
{
$illegal = $file | Select-String -Pattern '.*(Tuple|CultureInfo|Convert.To|System.(Windows|Reflection|Xml|Threading|Data))' -AllMatches -CaseSensitive
}
if ($illegal.Count -gt 0 -or $illegal.Matches.Count -gt 0)
{
foreach ($match in $illegal.Matches)
{
Write-Host "$WarningMessagePrefix" $file.FullName "`t`t" $match.Value.TrimStart(" ")
}
}
}
if ($path -ne $null)
{
Set-Location $path
}
if ($mod -eq "git")
{
$modifiedFilesPath = git diff --cached --name-only --diff-filter=ACM --line-prefix="${PWD}\"
$modifiedFiles = [System.Collections.ArrayList]::new()
foreach ($filePath in $modifiedFilesPath)
{
$file = Get-Item $filePath
$modifiedFiles.Add($file) | Out-Null
}
$allFiles = $modifiedFiles
$withoutServerFiles = $modifiedFiles
$refreshFiles = $modifiedFiles
$canExecuteFiles = $modifiedFiles
}
if ($mod -eq "Pipeline")
{
$firstcommit = git rev-list --min-parents=2 --max-count=1 HEAD
$modifiedFilesPath = git diff $firstcommit HEAD --name-only '*.cs' ':!*.xaml.cs' ':!*g.i.cs' ':!*.g.cs'
$modifiedFiles = [System.Collections.ArrayList]::new()
$withoutServerFiles = [System.Collections.ArrayList]::new()
$refreshFiles = [System.Collections.ArrayList]::new()
$canExecuteFiles = [System.Collections.ArrayList]::new()
foreach ($filePath in $modifiedFilesPath)
{
$file = Get-Item $filePath
if ($file -notmatch '\w*.Server')
{
$withoutServerFiles.Add($file) | Out-Null
}
if ($file -match '\w*.ClientBase.*(Handlers)')
{
$refreshFiles.Add($file) | Out-Null
}
if ($file -match '\w*.ClientBase.*Actions')
{
$canExecuteFiles.Add($file) | Out-Null
}
$modifiedFiles.Add($file) | Out-Null
}
$allFiles = $modifiedFiles
}
$exitcode = 0
if ($all -eq "all")
{
if ($mod -ne "git" -and $mod -ne "Pipeline")
{
$allFiles = Get-ChildItem -Include *.cs -Exclude *.xaml.cs, *.g.cs, *g.i.cs -Recurse | Where-Object { $_.FullName -notmatch '\w*.(Debug|AssemblyInfo)' }
$withoutServerFiles = Get-ChildItem -Include *.cs -Exclude *.g.cs, *g.i.cs -Recurse | Where-Object { $_.FullName -notmatch '\w*.Server' }
}
Write-Host "`n"
Write-Host "###################################"
Write-Host "# Rule -> Использование new, as is или запрещенных библиотек .NET." -ForegroundColor Yellow
foreach ($file in $allFiles)
{
Get-FilesAnonymousIllegal $file $mod
Get-FilesIsAsIllegal $file $mod
Get-FilesNetClassIllegal $file $mod
}
Write-Host "`n"
Write-Host "###################################"
Write-Host "# Rule -> Создание, удаление сущностей или использование Get, GetAll вне серверного кода." -ForegroundColor Red
foreach ($file in $withoutServerFiles)
{
$result = Get-FilesCreateDeleteGetIllegal $file $mod
if ($result -eq 1)
{
$exitcode = 1
}
}
}
if ($mod -ne "git" -and $mod -ne "Pipeline")
{
$refreshFiles = Get-ChildItem -Include *.cs -Exclude *.g.cs, *g.i.cs -Recurse | Where-Object { $_.FullName -match '\w*.ClientBase.*(Handlers)' }
$canExecuteFiles = Get-ChildItem -Include *.cs -Exclude *.g.cs, *g.i.cs -Recurse | Where-Object { $_.FullName -match '\w*.ClientBase.*Actions' }
}
Write-Host "`n"
Write-Host "###################################"
Write-Host "# Rule -> Вызов Get, GetAll или серверных функций в Возможности выполнения действий (CanExecute)." -ForegroundColor Red
foreach ($file in $canExecuteFiles)
{
$result = Get-FilesCanExecuteIllegal $file $mod
if ($result -eq 1)
{
$exitcode = 1
}
}
Write-Host "`n"
Write-Host "###################################"
Write-Host "# Rule -> Вызов серверных функций в Обновлении формы (Refresh)." -ForegroundColor Red
foreach ($file in $refreshFiles)
{
$result = Get-FilesRefreshIllegal $file $mod
if ($result -eq 1)
{
$exitcode = 1
}
}
Write-Host "###################################"
exit($exitcode)