|
| 1 | +// |
| 2 | +// Copyright (c) Microsoft Corporation. |
| 3 | +// |
| 4 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 5 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 6 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 7 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 8 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 9 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 10 | +// THE SOFTWARE. |
| 11 | +// |
| 12 | + |
| 13 | +using System; |
| 14 | +using System.Collections.Generic; |
| 15 | +using System.Management.Automation.Language; |
| 16 | +using System.Management.Automation; |
| 17 | +using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic; |
| 18 | +using System.ComponentModel.Composition; |
| 19 | +using System.Globalization; |
| 20 | + |
| 21 | +namespace Microsoft.Windows.Powershell.ScriptAnalyzer.BuiltinRules |
| 22 | +{ |
| 23 | + /// <summary> |
| 24 | + /// AvoidUsingDeprecatedManifestFields: Run Test Module Manifest to check that no deprecated fields are being used. |
| 25 | + /// </summary> |
| 26 | + [Export(typeof(IScriptRule))] |
| 27 | + public class AvoidUsingDeprecatedManifestFields : IScriptRule |
| 28 | + { |
| 29 | + /// <summary> |
| 30 | + /// AnalyzeScript: Run Test Module Manifest to check that no deprecated fields are being used. |
| 31 | + /// </summary> |
| 32 | + /// <param name="ast">The script's ast</param> |
| 33 | + /// <param name="fileName">The script's file name</param> |
| 34 | + /// <returns>A List of diagnostic results of this rule</returns> |
| 35 | + public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName) |
| 36 | + { |
| 37 | + if (ast == null) |
| 38 | + { |
| 39 | + throw new ArgumentNullException(Strings.NullAstErrorMessage); |
| 40 | + } |
| 41 | + |
| 42 | + if (String.Equals(System.IO.Path.GetExtension(fileName), ".psd1", StringComparison.OrdinalIgnoreCase)) |
| 43 | + { |
| 44 | + var ps = System.Management.Automation.PowerShell.Create(RunspaceMode.CurrentRunspace); |
| 45 | + IEnumerable<PSObject> result = null; |
| 46 | + try |
| 47 | + { |
| 48 | + ps.AddCommand("Test-ModuleManifest"); |
| 49 | + ps.AddParameter("Path", fileName); |
| 50 | + |
| 51 | + // Suppress warnings emitted during the execution of Test-ModuleManifest |
| 52 | + // ModuleManifest rule must catch any violations (warnings/errors) and generate DiagnosticRecord(s) |
| 53 | + ps.AddParameter("WarningAction", ActionPreference.SilentlyContinue); |
| 54 | + ps.AddParameter("WarningVariable", "Message"); |
| 55 | + ps.AddScript("$Message"); |
| 56 | + result = ps.Invoke(); |
| 57 | + |
| 58 | + } |
| 59 | + catch |
| 60 | + {} |
| 61 | + |
| 62 | + if (result != null) |
| 63 | + { |
| 64 | + foreach (var warning in result) |
| 65 | + { |
| 66 | + if (warning.BaseObject != null) |
| 67 | + { |
| 68 | + yield return |
| 69 | + new DiagnosticRecord( |
| 70 | + String.Format(CultureInfo.CurrentCulture, warning.BaseObject.ToString()), ast.Extent, |
| 71 | + GetName(), DiagnosticSeverity.Warning, fileName); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + } |
| 77 | + |
| 78 | + } |
| 79 | + |
| 80 | + /// <summary> |
| 81 | + /// GetName: Retrieves the name of this rule. |
| 82 | + /// </summary> |
| 83 | + /// <returns>The name of this rule</returns> |
| 84 | + public string GetName() |
| 85 | + { |
| 86 | + return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidUsingDeprecatedManifestFieldsName); |
| 87 | + } |
| 88 | + |
| 89 | + /// <summary> |
| 90 | + /// GetCommonName: Retrieves the common name of this rule. |
| 91 | + /// </summary> |
| 92 | + /// <returns>The common name of this rule</returns> |
| 93 | + public string GetCommonName() |
| 94 | + { |
| 95 | + return String.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingDeprecatedManifestFieldsCommonName); |
| 96 | + } |
| 97 | + |
| 98 | + /// <summary> |
| 99 | + /// GetDescription: Retrieves the description of this rule. |
| 100 | + /// </summary> |
| 101 | + /// <returns>The description of this rule</returns> |
| 102 | + public string GetDescription() |
| 103 | + { |
| 104 | + return String.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingDeprecatedManifestFieldsDescription); |
| 105 | + } |
| 106 | + |
| 107 | + /// <summary> |
| 108 | + /// Method: Retrieves the type of the rule: builtin, managed or module. |
| 109 | + /// </summary> |
| 110 | + public SourceType GetSourceType() |
| 111 | + { |
| 112 | + return SourceType.Builtin; |
| 113 | + } |
| 114 | + |
| 115 | + /// <summary> |
| 116 | + /// GetSeverity: Retrieves the severity of the rule: error, warning of information. |
| 117 | + /// </summary> |
| 118 | + /// <returns></returns> |
| 119 | + public RuleSeverity GetSeverity() |
| 120 | + { |
| 121 | + return RuleSeverity.Warning; |
| 122 | + } |
| 123 | + |
| 124 | + /// <summary> |
| 125 | + /// Method: Retrieves the module/assembly name the rule is from. |
| 126 | + /// </summary> |
| 127 | + public string GetSourceName() |
| 128 | + { |
| 129 | + return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments