-
Notifications
You must be signed in to change notification settings - Fork 382
/
RuleMaker.psm1
485 lines (415 loc) · 13.1 KB
/
RuleMaker.psm1
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
Function New-RuleObject
{
param(
[string] $Name,
[string] $Severity,
[string] $CommonName,
[string] $Description,
[string] $Error)
New-Object -TypeName 'PSObject' -Property @{
Name = $Name
Severity = $Severity
CommonName = $CommonName
Description = $Description
Error = $Error
SourceFileName = $Name + ".cs"
}
}
Function Get-SolutionRoot
{
$PSModule = $ExecutionContext.SessionState.Module
$path = $PSModule.ModuleBase
$root = Split-Path -Path $path -Parent
$solutionFilename = 'PSScriptAnalyzer.sln'
if (-not (Test-Path (Join-Path $root $solutionFilename)))
{
return $null
}
$root
}
Function Get-RuleProjectRoot
{
$slnRoot = Get-SolutionRoot
if ($null -eq $slnRoot)
{
return $null
}
Join-Path $slnRoot "Rules"
}
Function Get-RuleProjectFile
{
$prjRoot = Get-RuleProjectRoot
if ($null -eq $prjRoot)
{
return $null
}
Join-Path $prjRoot "ScriptAnalyzerBuiltinRules.csproj"
}
Function Get-RuleSourcePath($Rule)
{
$ruleRoot = Get-RuleProjectRoot
Join-Path $ruleRoot $Rule.SourceFileName
}
Function Get-RuleTemplate
{
$ruleTemplate = @'
// Copyright (c) Microsoft Corporation.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Collections.Generic;
#if !CORECLR
using System.ComponentModel.Composition;
#endif
using System.Globalization;
using System.Linq;
using System.Management.Automation.Language;
using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic;
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
{{
/// <summary>
/// A class to walk an AST to check for [violation]
/// </summary>
#if !CORECLR
[Export(typeof(IScriptRule))]
#endif
public class {0} : IScriptRule
{{
/// <summary>
/// Analyzes the given ast to find the [violation]
/// </summary>
/// <param name="ast">AST to be analyzed. This should be non-null</param>
/// <param name="fileName">Name of file that corresponds to the input AST.</param>
/// <returns>A an enumerable type containing the violations</returns>
public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
{{
if (ast == null)
{{
throw new ArgumentNullException("ast");
}}
// your code goes here
yield return new DiagnosticRecord();
}}
/// <summary>
/// Retrieves the common name of this rule.
/// </summary>
public string GetCommonName()
{{
return string.Format(CultureInfo.CurrentCulture, Strings.{0}CommonName);
}}
/// <summary>
/// Retrieves the description of this rule.
/// </summary>
public string GetDescription()
{{
return string.Format(CultureInfo.CurrentCulture, Strings.{0}Description);
}}
/// <summary>
/// Retrieves the name of this rule.
/// </summary>
public string GetName()
{{
return string.Format(
CultureInfo.CurrentCulture,
Strings.NameSpaceFormat,
GetSourceName(),
Strings.{0}Name);
}}
/// <summary>
/// Retrieves the severity of the rule: error, warning or information.
/// </summary>
public RuleSeverity GetSeverity()
{{
return RuleSeverity.{1};
}}
/// <summary>
/// Gets the severity of the returned diagnostic record: error, warning, or information.
/// </summary>
/// <returns></returns>
public DiagnosticSeverity GetDiagnosticSeverity()
{{
return DiagnosticSeverity.{1};
}}
/// <summary>
/// Retrieves the name of the module/assembly the rule is from.
/// </summary>
public string GetSourceName()
{{
return string.Format(CultureInfo.CurrentCulture, Strings.SourceName);
}}
/// <summary>
/// Retrieves the type of the rule, Builtin, Managed or Module.
/// </summary>
public SourceType GetSourceType()
{{
return SourceType.Builtin;
}}
}}
}}
'@
$ruleTemplate
}
Function Get-RuleSource($Rule)
{
$source = (Get-RuleTemplate) -f $Rule.Name,$Rule.Severity
$source
}
Function Add-RuleSource($Rule)
{
$ruleSourceFilepath = Get-RuleSourcePath $Rule
$ruleSource = Get-RuleSource $Rule
New-Item -Path $ruleSourceFilepath -ItemType File
Set-Content -Path $ruleSourceFilepath -Value $ruleSource -Encoding UTF8
}
Function Remove-RuleSource($Rule)
{
$ruleSourceFilePath = Get-RuleSourcePath $Rule
if (Test-Path $ruleSourceFilePath)
{
Remove-Item $ruleSourceFilePath
}
}
Function Get-RuleDocumentationPath($Rule)
{
$root = Get-SolutionRoot
$ruleDocDir = Join-Path $root 'RuleDocumentation'
$ruleDocPath = Join-Path $ruleDocDir ($Rule.Name + ".md")
$ruleDocPath
}
Function Add-RuleDocumentation($Rule)
{
$ruleDocTemplate = @"
# {0}
**Severity Level: {1}**
## Description
## How to Fix
## Example
### Wrong:
``````PowerShell
``````
### Correct:
``````PowerShell
``````
"@
$ruleDoc = $ruleDocTemplate -f $Rule.Name,$Rule.Severity
$ruleDocPath = Get-RuleDocumentationPath $Rule
Set-Content -Path $ruleDocPath -Value $ruleDoc -Encoding UTF8
}
Function Remove-RuleDocumentation($Rule)
{
$ruleDocPath = Get-RuleDocumentationPath $Rule
Remove-Item $ruleDocPath
}
Function Get-RuleStringsPath
{
$ruleRoot = Get-RuleProjectRoot
$stringsFilepath = Join-Path $ruleRoot 'Strings.resx'
$stringsFilepath
}
Function Get-RuleStrings
{
$stringsFilepath = Get-RuleStringsPath
[xml]$stringsXml = New-Object xml
$stringsXml.Load($stringsFilepath)
$stringsXml
}
Function Set-RuleStrings
{
param([xml]$stringsXml)
$stringsFilepath = Get-RuleStringsPath
$stringsXml.Save($stringsFilepath)
}
Function Add-RuleStrings($Rule)
{
$stringsXml = Get-RuleStrings $Rule
Function Add-Node($nodeName, $nodeValue)
{
$dataNode = $stringsXml.CreateElement("data")
$nameAttr = $stringsXml.CreateAttribute("name")
$nameAttr.Value = $nodeName
$xmlspaceAttr = $stringsXml.CreateAttribute("xml:space")
$xmlspaceAttr.Value = "preserve"
$valueElem = $stringsXml.CreateElement("value")
$valueElem.InnerText = $nodeValue
$dataNode.Attributes.Append($nameAttr)
$dataNode.Attributes.Append($xmlspaceAttr)
$dataNode.AppendChild($valueElem)
$stringsXml.root.AppendChild($dataNode)
}
Add-Node ($Rule.Name + 'Name') $Rule.Name
Add-Node ($Rule.Name + 'CommonName') $Rule.CommonName
Add-Node ($Rule.Name + 'Description') $Rule.Description
Add-Node ($Rule.Name + 'Error') $Rule.Error
Set-RuleStrings $stringsXml
}
Function Remove-RuleStrings($Rule)
{
$stringsXml = Get-RuleStrings $Rule
$nodesToRemove = $stringsXml.root.GetElementsByTagName("data") | Where-Object { $_.name -match $Rule.Name }
$nodesToRemove | Foreach-Object { $stringsXml.root.RemoveChild($_) }
Set-RuleStrings $stringsXml
}
Function Get-RuleProjectXml
{
$ruleProject = Get-RuleProjectFile
$projectXml = New-Object -TypeName 'xml'
$projectXml.Load($ruleProject)
$projectXml
}
Function Set-RuleProjectXml($projectXml)
{
$ruleProjectFilepath = Get-RuleProjectFile
$projectXml.Save($ruleProjectFilepath)
}
Function Get-CompileTargetGroup($projectXml)
{
$projectXml.Project.ItemGroup | Where-Object { $_.Compile -ne $null }
}
Function Add-RuleToProject($Rule)
{
$projectXml = Get-RuleProjectXml
$compileItemgroup = Get-CompileTargetGroup $projectXml
$compileElement = $compileItemgroup.Compile.Item(0).Clone()
$compileElement.Include = $Rule.SourceFileName
$compileItemgroup.AppendChild($compileElement)
Set-RuleProjectXml $projectXml
}
Function Remove-RuleFromProject($Rule)
{
$projectXml = Get-RuleProjectXml
$compileItemgroup = Get-CompileTargetGroup $projectXml
$itemToRemove = $compileItemgroup.Compile | Where-Object { $_.Include -eq $Rule.SourceFileName }
$compileItemgroup.RemoveChild($itemToRemove)
Set-RuleProjectXml $projectXml
}
Function Get-RuleTestFilePath($Rule)
{
$testRoot = Join-Path (Get-SolutionRoot) 'Tests'
$ruleTestRoot = Join-Path $testRoot 'Rules'
$ruleTestFileName = $Rule.Name + ".tests.ps1"
$ruleTestFilePath = Join-Path $ruleTestRoot $ruleTestFileName
$ruleTestFilePath
}
Function Add-RuleTest($Rule)
{
$ruleTestFilePath = Get-RuleTestFilePath $Rule
New-Item -Path $ruleTestFilePath -ItemType File
$ruleTestTemplate = @'
$ruleName = "{0}"
Describe "{0}" {{
Context "" {{
It "" {{
}}
}}
}}
'@
$ruleTestSource = $ruleTestTemplate -f $Rule.Name
Set-Content -Path $ruleTestFilePath -Value $ruleTestSource -Encoding UTF8
}
Function Remove-RuleTest($Rule)
{
$ruleTestFilePath = Get-RuleTestFilePath $Rule
Remove-Item -Path $ruleTestFilePath
}
<#
.SYNOPSIS
Adds a C# based builtin rule to Rules project in PSScriptAnalyzer solution
.EXAMPLE
C:\PS> Add-Rule -Name UseCompatibleCmdlets -Severity Warning -CommonName 'Use Compatible Cmdlets' -Description 'Checks if a cmdlet is compatible with a given PowerShell version, edition and os combination' -Error '{0} command is not compatible with PowerShell version {1}, edition {2} and OS {3}'
This will result in the following.
- create {PScriptAnalyzerSolutionRoot}/Rules/UseCompatibleCmdlets.cs
- create {PScriptAnalyzerSolutionRoot}/Tests/Rules/UseCompatibleCmdlets.tests.ps1
- create {PScriptAnalyzerSolutionRoot}/RuleDocumentation/UseCompatibleCmdlets.md
- update {PScriptAnalyzerSolutionRoot}/Rules/Strings.resx
- update {PScriptAnalyzerSolutionRoot}/Rules/ScriptAnalyzerBuiltinRules.csproj
.PARAMETER Name
Rule name. An entry in Strings.resx is created for this value.
.PARAMETER Severity
Severity of the rule from on the following values: {Information, Warning, Error}
.PARAMETER CommonName
A somewhat verbose name of of the rule. An entry in Strings.resx is created for this value.
.PARAMETER Description
Rule description. An entry in Strings.resx is created for this value.
.PARAMETER Error
Error message. An entry in Strings.resx is created for this value.
.INPUTS
None
.OUTPUTS
None
#>
Function Add-Rule
{
param(
[Parameter(Mandatory=$true)]
[string] $Name,
[Parameter(Mandatory=$true)]
[ValidateSet("Error", "Warning", "Information")]
[string] $Severity,
[Parameter(Mandatory=$true)]
[string] $CommonName,
[Parameter(Mandatory=$true)]
[string] $Description,
[Parameter(Mandatory=$true)]
[string] $Error)
$rule = New-RuleObject -Name $Name -Severity $Severity -CommonName $CommonName -Description $Description -Error $Error
$undoStack = New-Object 'System.Collections.Stack'
$success = $false
try {
Add-RuleDocumentation $rule
$undoStack.Push((Get-Item -Path Function:\Remove-RuleDocumentation))
Add-RuleTest $rule
$undoStack.Push((Get-Item -Path Function:\Remove-RuleTest))
Add-RuleSource $rule
$undoStack.Push((Get-Item -Path Function:\Remove-RuleSource))
Add-RuleStrings $rule
$undoStack.Push((Get-Item -Path Function:\Remove-RuleStrings))
Add-RuleToProject $rule
$undoStack.Push((Get-Item -Path Function:\Remove-RuleFromProject))
$success = $true
}
finally {
if (-not $success)
{
while ($undoStack.Count -ne 0)
{
& ($undoStack.Pop()) $rule
}
}
}
}
<#
.SYNOPSIS
Removes a rule from builtin rules
.EXAMPLE
C:\PS> Remove-Rule -Name UseCompatibleCmdlets
This will result in the following.
- remove {PScriptAnalyzerSolutionRoot}/Rules/UseCompatibleCmdlets.cs
- remove {PScriptAnalyzerSolutionRoot}/Tests/Rules/UseCompatibleCmdlets.tests.ps1
- remove {PScriptAnalyzerSolutionRoot}/RuleDocumentation/UseCompatibleCmdlets.md
- remove UseCompatibleCmdlets entries from {PScriptAnalyzerSolutionRoot}/Rules/Strings.resx
- remove UseCompatibleCmdlets entries from {PScriptAnalyzerSolutionRoot}/Rules/ScriptAnalyzerBuiltinRules.csproj
.INPUTS
None
.OUTPUTS
None
#>
Function Remove-Rule
{
param(
[string] $Name
)
$rule = New-RuleObject -Name $Name
Remove-RuleFromProject $rule
Remove-RuleStrings $rule
Remove-RuleSource $rule
Remove-RuleTest $rule
Remove-RuleDocumentation $rule
}
Export-ModuleMember -Function Add-Rule
Export-ModuleMember -Function Remove-Rule