|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// |
| 3 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 4 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 5 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 6 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 7 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 8 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 9 | +// THE SOFTWARE. |
| 10 | + |
| 11 | +using System; |
| 12 | +using System.Collections.Generic; |
| 13 | +using System.Management.Automation.Language; |
| 14 | +using System.Reflection; |
| 15 | + |
| 16 | +namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic |
| 17 | +{ |
| 18 | + // This is still an experimental class. Use at your own risk! |
| 19 | + public abstract class ConfigurableRule : IScriptRule |
| 20 | + { |
| 21 | + /// <summary> |
| 22 | + /// Indicates if the rule is enabled or not. |
| 23 | + /// |
| 24 | + /// If the rule is enabled ScriptAnalyzer engine will run it |
| 25 | + /// otherwise it will not. |
| 26 | + /// |
| 27 | + /// Configurable rule properties should define a default value |
| 28 | + /// because if reading the configuration fails we use the |
| 29 | + /// property's default value |
| 30 | + /// </summary> |
| 31 | + [ConfigurableRuleProperty(defaultValue: false)] |
| 32 | + public bool Enable { get; protected set; } |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Initialize the configurable properties of a configurable rule. |
| 36 | + /// </summary> |
| 37 | + protected ConfigurableRule() |
| 38 | + { |
| 39 | + SetDefaultValues(); |
| 40 | + } |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// Sets the configurable properties of the rule. |
| 44 | + /// |
| 45 | + /// Properties having ConfigurableRuleProperty attribute are called configurable properties. |
| 46 | + /// </summary> |
| 47 | + /// <param name="paramValueMap">A dictionary that maps parameter name to it value. Must be non-null</param> |
| 48 | + public virtual void ConfigureRule(IDictionary<string, object> paramValueMap) |
| 49 | + { |
| 50 | + if (paramValueMap == null) |
| 51 | + { |
| 52 | + throw new ArgumentNullException(nameof(paramValueMap)); |
| 53 | + } |
| 54 | + |
| 55 | + try |
| 56 | + { |
| 57 | + foreach (var property in GetConfigurableProperties()) |
| 58 | + { |
| 59 | + if (paramValueMap.ContainsKey(property.Name)) |
| 60 | + { |
| 61 | + SetValue(property, paramValueMap[property.Name]); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + catch |
| 66 | + { |
| 67 | + // we do not know how to handle an exception in this case yet! |
| 68 | + // but we know that this should not crash the program |
| 69 | + // hence we revert the property values to their default |
| 70 | + SetDefaultValues(); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /// <summary> |
| 75 | + /// Analyzes the given abstract syntax tree (AST) and returns diagnostic records based on the analysis. |
| 76 | + /// </summary> |
| 77 | + /// <param name="ast">AST representing the file content</param> |
| 78 | + /// <param name="fileName">Path of the file corresponding to the AST</param> |
| 79 | + /// <returns>The results of the analysis</returns> |
| 80 | + public abstract IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName); |
| 81 | + |
| 82 | + /// <summary> |
| 83 | + /// Retrieves the Common name of the rule. |
| 84 | + /// </summary> |
| 85 | + /// <returns>The name of the rule.</returns> |
| 86 | + public abstract string GetCommonName(); |
| 87 | + |
| 88 | + /// <summary> |
| 89 | + /// Retrieves the description of the rule. |
| 90 | + /// </summary> |
| 91 | + /// <returns>The description of the rule.</returns> |
| 92 | + public abstract string GetDescription(); |
| 93 | + |
| 94 | + /// <summary> |
| 95 | + /// Retrieves the name of the rule. |
| 96 | + /// </summary> |
| 97 | + /// <returns>The name of the rule.</returns> |
| 98 | + public abstract string GetName(); |
| 99 | + |
| 100 | + /// <summary> |
| 101 | + /// Retrieves severity of the rule. |
| 102 | + /// </summary> |
| 103 | + /// <returns>The severity of the rule.</returns> |
| 104 | + public abstract RuleSeverity GetSeverity(); |
| 105 | + |
| 106 | + /// <summary> |
| 107 | + /// Retrieves the source name of the rule. |
| 108 | + /// </summary> |
| 109 | + /// <returns>The source name of the rule.</returns> |
| 110 | + public abstract string GetSourceName(); |
| 111 | + |
| 112 | + /// <summary> |
| 113 | + /// Retrieves the source type of the rule. |
| 114 | + /// </summary> |
| 115 | + /// <returns>The source type of the rule.</returns> |
| 116 | + public abstract SourceType GetSourceType(); |
| 117 | + |
| 118 | + private void SetDefaultValues() |
| 119 | + { |
| 120 | + foreach (var property in GetConfigurableProperties()) |
| 121 | + { |
| 122 | + SetValue(property, GetDefaultValue(property)); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + private void SetValue(PropertyInfo property, object value) |
| 127 | + { |
| 128 | + // TODO Check if type is convertible |
| 129 | + property.SetValue(this, Convert.ChangeType(value, property.PropertyType)); |
| 130 | + } |
| 131 | + |
| 132 | + private IEnumerable<PropertyInfo> GetConfigurableProperties() |
| 133 | + { |
| 134 | + foreach (var property in this.GetType().GetProperties()) |
| 135 | + { |
| 136 | + if (property.GetCustomAttribute(typeof(ConfigurableRulePropertyAttribute)) != null) |
| 137 | + { |
| 138 | + yield return property; |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + private Object GetDefaultValue(PropertyInfo property) |
| 144 | + { |
| 145 | + var attr = property.GetCustomAttribute(typeof(ConfigurableRulePropertyAttribute)); |
| 146 | + if (attr == null) |
| 147 | + { |
| 148 | + throw new ArgumentException( |
| 149 | + String.Format(Strings.ConfigurableScriptRulePropertyHasNotAttribute, property.Name), |
| 150 | + nameof(property)); |
| 151 | + } |
| 152 | + |
| 153 | + return ((ConfigurableRulePropertyAttribute)attr).DefaultValue; |
| 154 | + } |
| 155 | + } |
| 156 | +} |
0 commit comments