-
Notifications
You must be signed in to change notification settings - Fork 406
Expand file tree
/
Copy pathGetScriptAnalyzerRuleCommand.cs
More file actions
153 lines (134 loc) · 5.4 KB
/
GetScriptAnalyzerRuleCommand.cs
File metadata and controls
153 lines (134 loc) · 5.4 KB
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Management.Automation;
using System.Reflection;
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands
{
/// <summary>
/// GetScriptAnalyzerRuleCommand: Cmdlet to list all the analyzer rule names and descriptions.
/// </summary>
[Cmdlet(VerbsCommon.Get, "ScriptAnalyzerRule", HelpUri = "https://go.microsoft.com/fwlink/?LinkId=525913")]
[OutputType(typeof(RuleInfo))]
public class GetScriptAnalyzerRuleCommand : PSCmdlet, IOutputWriter
{
#region Parameters
/// <summary>
/// Path: Path to custom rules folder.
/// </summary>
[Parameter(Mandatory = false)]
[ValidateNotNullOrEmpty]
[SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
[Alias("CustomizedRulePath")]
public string[] CustomRulePath
{
get { return customRulePath; }
set { customRulePath = value; }
}
private string[] customRulePath;
/// <summary>
/// RecurseCustomRulePath: Find rules within subfolders under the path
/// </summary>
[Parameter(Mandatory = false)]
[SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
public SwitchParameter RecurseCustomRulePath
{
get { return recurseCustomRulePath; }
set { recurseCustomRulePath = value; }
}
private bool recurseCustomRulePath;
/// <summary>
/// Name: The name of a specific rule to list.
/// </summary>
[Parameter(Mandatory = false, Position = 1)]
[ValidateNotNullOrEmpty]
[SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
public string[] Name
{
get { return name; }
set { name = value; }
}
private string[] name;
/// <summary>
/// Severity: Array of the severity types to be enabled.
/// </summary>
/// </summary>
[ValidateSet("Warning", "Error", "Information", IgnoreCase = true)]
[Parameter(Mandatory = false)]
[SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
public string[] Severity
{
get { return severity; }
set { severity = value; }
}
private string[] severity;
#endregion Parameters
#region Overrides
/// <summary>
/// BeginProcessing : TBD
/// </summary>
protected override void BeginProcessing()
{
// Initialize helper
Helper.Instance = new Helper(
SessionState.InvokeCommand);
Helper.Instance.Initialize();
string[] rulePaths = Helper.ProcessCustomRulePaths(customRulePath,
this.SessionState, recurseCustomRulePath);
ScriptAnalyzer.Instance.Initialize(this, rulePaths, null, null, null, null == rulePaths);
}
/// <summary>
/// ProcessRecord : TBD
/// </summary>
protected override void ProcessRecord()
{
string[] modNames = ScriptAnalyzer.Instance.GetValidModulePaths();
IEnumerable<IRule> rules = ScriptAnalyzer.Instance.GetRule(modNames, name);
if (rules == null)
{
WriteObject(string.Format(CultureInfo.CurrentCulture, Strings.RulesNotFound));
}
else
{
if (severity != null)
{
var ruleSeverity = severity.Select(item => Enum.Parse(typeof (RuleSeverity), item, true));
rules = rules.Where(item => ruleSeverity.Contains(item.GetSeverity())).ToList();
}
foreach (IRule rule in rules)
{
IEnumerable<RuleOptionInfo> optionInfos = null;
if (rule is ConfigurableRule configurable)
{
var props = rule.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
var optList = new List<RuleOptionInfo>();
foreach (var p in props)
{
if (p.GetCustomAttribute<ConfigurableRulePropertyAttribute>(inherit: true) == null) {
continue;
}
optList.Add(new RuleOptionInfo
{
Name = p.Name,
OptionType = p.PropertyType,
DefaultValue = p.GetValue(rule)
});
}
if (optList.Count > 0)
{
optionInfos = optList;
}
}
WriteObject(new RuleInfo(rule.GetName(), rule.GetCommonName(), rule.GetDescription(),
rule.GetSourceType(), rule.GetSourceName(), rule.GetSeverity(), rule.GetType(), optionInfos));
}
}
}
#endregion
}
}