|
| 1 | +using Microsoft.PowerShell.ScriptAnalyzer.Builder; |
| 2 | +using Microsoft.PowerShell.ScriptAnalyzer.Builtin.Rules; |
| 3 | +using Microsoft.PowerShell.ScriptAnalyzer.Configuration; |
| 4 | +using Microsoft.PowerShell.ScriptAnalyzer.Execution; |
| 5 | +using Microsoft.PowerShell.ScriptAnalyzer.Runtime; |
| 6 | +using Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules; |
| 7 | +using System; |
| 8 | +using System.Collections.Generic; |
| 9 | +using System.Management.Automation.Runspaces; |
| 10 | + |
| 11 | +namespace Microsoft.PowerShell.ScriptAnalyzer.Builtin |
| 12 | +{ |
| 13 | + public static class BuiltinRules |
| 14 | + { |
| 15 | + public static IReadOnlyList<Type> DefaultRules { get; } = new[] |
| 16 | + { |
| 17 | + typeof(AvoidEmptyCatchBlock), |
| 18 | + typeof(AvoidGlobalVars), |
| 19 | + typeof(AvoidPositionalParameters), |
| 20 | + typeof(AvoidUsingWMICmdlet), |
| 21 | + typeof(UseDeclaredVarsMoreThanAssignments), |
| 22 | + typeof(UseShouldProcessForStateChangingFunctions), |
| 23 | + }; |
| 24 | + } |
| 25 | + |
| 26 | + public static class Default |
| 27 | + { |
| 28 | + private static readonly Lazy<IRuleComponentProvider> s_ruleComponentProviderLazy = new Lazy<IRuleComponentProvider>(BuildRuleComponentProvider); |
| 29 | + |
| 30 | + public static IReadOnlyDictionary<string, IRuleConfiguration> RuleConfiguration { get; } = new Dictionary<string, IRuleConfiguration>(StringComparer.OrdinalIgnoreCase) |
| 31 | + { |
| 32 | + { "PS/AvoidUsingEmptyCatchBlock", null }, |
| 33 | + { "PS/AvoidGlobalVars", null }, |
| 34 | + { "PS/AvoidUsingPositionalParameters", null }, |
| 35 | + { "PS/AvoidUsingWMICmdlet", null }, |
| 36 | + { "PS/UseDeclaredVarsMoreThanAssignments", null }, |
| 37 | + { "PS/UseShouldProcessForStateChangingFunctions", null }, |
| 38 | + }; |
| 39 | + |
| 40 | + public static IRuleExecutorFactory RuleExecutorFactory { get; } = new ParallelLinqRuleExecutorFactory(); |
| 41 | + |
| 42 | + public static IRuleComponentProvider RuleComponentProvider => s_ruleComponentProviderLazy.Value; |
| 43 | + |
| 44 | + private static IRuleComponentProvider BuildRuleComponentProvider() |
| 45 | + { |
| 46 | + return new RuleComponentProviderBuilder() |
| 47 | + .AddSingleton(InstantiatePowerShellCommandDatabase()) |
| 48 | + .Build(); |
| 49 | + } |
| 50 | + |
| 51 | + private static IPowerShellCommandDatabase InstantiatePowerShellCommandDatabase() |
| 52 | + { |
| 53 | + using (Runspace runspace = RunspaceFactory.CreateRunspace()) |
| 54 | + { |
| 55 | + runspace.Open(); |
| 56 | + return SessionStateCommandDatabase.Create(runspace.SessionStateProxy.InvokeCommand); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | +} |
0 commit comments