From d97240f10cb43651011e21c18dc0539e2e0344d4 Mon Sep 17 00:00:00 2001 From: Ilya Date: Tue, 22 Oct 2019 13:55:32 +0500 Subject: [PATCH 01/11] Use nameof() --- .../engine/InitialSessionState.cs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/System.Management.Automation/engine/InitialSessionState.cs b/src/System.Management.Automation/engine/InitialSessionState.cs index 3abb53b0b80..a7748c8751d 100644 --- a/src/System.Management.Automation/engine/InitialSessionState.cs +++ b/src/System.Management.Automation/engine/InitialSessionState.cs @@ -188,7 +188,7 @@ public SessionStateTypeEntry(string fileName) { if (string.IsNullOrWhiteSpace(fileName)) { - throw PSTraceSource.NewArgumentException("fileName"); + throw PSTraceSource.NewArgumentException(nameof(fileName)); } FileName = fileName.Trim(); @@ -203,7 +203,7 @@ public SessionStateTypeEntry(TypeTable typeTable) { if (typeTable == null) { - throw PSTraceSource.NewArgumentNullException("typeTable"); + throw PSTraceSource.NewArgumentException(nameof(typeTable)); } TypeTable = typeTable; @@ -219,7 +219,7 @@ public SessionStateTypeEntry(TypeData typeData, bool isRemove) { if (typeData == null) { - throw PSTraceSource.NewArgumentNullException("typeData"); + throw PSTraceSource.NewArgumentException(nameof(typeData)); } TypeData = typeData; @@ -295,7 +295,7 @@ public SessionStateFormatEntry(string fileName) { if (string.IsNullOrWhiteSpace(fileName)) { - throw PSTraceSource.NewArgumentException("fileName"); + throw PSTraceSource.NewArgumentException(nameof(fileName)); } FileName = fileName.Trim(); @@ -310,7 +310,7 @@ public SessionStateFormatEntry(FormatTable formattable) { if (formattable == null) { - throw PSTraceSource.NewArgumentNullException("formattable"); + throw PSTraceSource.NewArgumentException(nameof(formattable)); } Formattable = formattable; @@ -325,7 +325,7 @@ public SessionStateFormatEntry(ExtendedTypeDefinition typeDefinition) { if (typeDefinition == null) { - throw PSTraceSource.NewArgumentNullException("typeDefinition"); + throw PSTraceSource.NewArgumentException(nameof(typeDefinition)); } FormatData = typeDefinition; @@ -3652,7 +3652,7 @@ public PSSnapInInfo ImportPSSnapIn(string name, out PSSnapInException warning) { if (string.IsNullOrEmpty(name)) { - PSTraceSource.NewArgumentNullException("name"); + PSTraceSource.NewArgumentException(nameof(name)); } // Check whether the mshsnapin is present in the registry. From f5c2954a642f8973909cbcb089cd50afbf11a9a8 Mon Sep 17 00:00:00 2001 From: Ilya Date: Tue, 22 Oct 2019 14:01:15 +0500 Subject: [PATCH 02/11] Fix closing parenthesis --- .../engine/InitialSessionState.cs | 44 +++++++------------ 1 file changed, 15 insertions(+), 29 deletions(-) diff --git a/src/System.Management.Automation/engine/InitialSessionState.cs b/src/System.Management.Automation/engine/InitialSessionState.cs index a7748c8751d..aa6ccae31cd 100644 --- a/src/System.Management.Automation/engine/InitialSessionState.cs +++ b/src/System.Management.Automation/engine/InitialSessionState.cs @@ -4315,97 +4315,83 @@ .ForwardHelpCategory Cmdlet Utils.utf8NoBom, RunspaceInit.OutputEncodingDescription, ScopedItemOptions.None, - new ArgumentTypeConverterAttribute(typeof(System.Text.Encoding)) - ), + new ArgumentTypeConverterAttribute(typeof(System.Text.Encoding))), // Preferences - + // // NTRAID#Windows Out Of Band Releases-931461-2006/03/13 // ArgumentTypeConverterAttribute is applied to these variables, // but this only reaches the global variable. If these are // redefined in script scope etc, the type conversion // is not applicable. - + // // Variables typed to ActionPreference new SessionStateVariableEntry( SpecialVariables.ConfirmPreference, defaultConfirmPreference, RunspaceInit.ConfirmPreferenceDescription, ScopedItemOptions.None, - new ArgumentTypeConverterAttribute(typeof(ConfirmImpact)) - ), + new ArgumentTypeConverterAttribute(typeof(ConfirmImpact))), new SessionStateVariableEntry( SpecialVariables.DebugPreference, defaultDebugPreference, RunspaceInit.DebugPreferenceDescription, ScopedItemOptions.None, - new ArgumentTypeConverterAttribute(typeof(ActionPreference)) - ), + new ArgumentTypeConverterAttribute(typeof(ActionPreference))), new SessionStateVariableEntry( SpecialVariables.ErrorActionPreference, defaultErrorActionPreference, RunspaceInit.ErrorActionPreferenceDescription, ScopedItemOptions.None, - new ArgumentTypeConverterAttribute(typeof(ActionPreference)) - ), + new ArgumentTypeConverterAttribute(typeof(ActionPreference))), new SessionStateVariableEntry( SpecialVariables.ProgressPreference, defaultProgressPreference, RunspaceInit.ProgressPreferenceDescription, ScopedItemOptions.None, - new ArgumentTypeConverterAttribute(typeof(ActionPreference)) - ), + new ArgumentTypeConverterAttribute(typeof(ActionPreference))), new SessionStateVariableEntry( SpecialVariables.VerbosePreference, defaultVerbosePreference, RunspaceInit.VerbosePreferenceDescription, ScopedItemOptions.None, - new ArgumentTypeConverterAttribute(typeof(ActionPreference)) - ), + new ArgumentTypeConverterAttribute(typeof(ActionPreference))), new SessionStateVariableEntry( SpecialVariables.WarningPreference, defaultWarningPreference, RunspaceInit.WarningPreferenceDescription, ScopedItemOptions.None, - new ArgumentTypeConverterAttribute(typeof(ActionPreference)) - ), + new ArgumentTypeConverterAttribute(typeof(ActionPreference))), new SessionStateVariableEntry( SpecialVariables.InformationPreference, defaultInformationPreference, RunspaceInit.InformationPreferenceDescription, ScopedItemOptions.None, - new ArgumentTypeConverterAttribute(typeof(ActionPreference)) - ), + new ArgumentTypeConverterAttribute(typeof(ActionPreference))), new SessionStateVariableEntry( SpecialVariables.ErrorView, ExperimentalFeature.IsEnabled("PSErrorView") ? ErrorView.ConciseView : defaultErrorView, RunspaceInit.ErrorViewDescription, ScopedItemOptions.None, - new ArgumentTypeConverterAttribute(typeof(ErrorView)) - ), + new ArgumentTypeConverterAttribute(typeof(ErrorView))), new SessionStateVariableEntry( SpecialVariables.NestedPromptLevel, 0, - RunspaceInit.NestedPromptLevelDescription - ), - + RunspaceInit.NestedPromptLevelDescription), new SessionStateVariableEntry( SpecialVariables.WhatIfPreference, defaultWhatIfPreference, - RunspaceInit.WhatIfPreferenceDescription - ), + RunspaceInit.WhatIfPreferenceDescription), new SessionStateVariableEntry( FormatEnumerationLimit, DefaultFormatEnumerationLimit, - RunspaceInit.FormatEnumerationLimitDescription - ), + RunspaceInit.FormatEnumerationLimitDescription), // variable for PSEmailServer new SessionStateVariableEntry( SpecialVariables.PSEmailServer, string.Empty, - RunspaceInit.PSEmailServerDescription - ), + RunspaceInit.PSEmailServerDescription), // Start: Variables which control remoting behavior new SessionStateVariableEntry( From 19bc7d1e7b3f57ac6979bdf61728aeb577a12b79 Mon Sep 17 00:00:00 2001 From: Ilya Date: Tue, 22 Oct 2019 14:49:48 +0500 Subject: [PATCH 03/11] Parameters --- .../engine/InitialSessionState.cs | 184 ++++++++++-------- 1 file changed, 98 insertions(+), 86 deletions(-) diff --git a/src/System.Management.Automation/engine/InitialSessionState.cs b/src/System.Management.Automation/engine/InitialSessionState.cs index aa6ccae31cd..8d861a8934a 100644 --- a/src/System.Management.Automation/engine/InitialSessionState.cs +++ b/src/System.Management.Automation/engine/InitialSessionState.cs @@ -619,8 +619,7 @@ public SessionStateAliasEntry(string name, string definition, string description /// A description of the purpose of the alias. /// Options defining the scope visibility, readonly and constant. /// - internal SessionStateAliasEntry(string name, string definition, string description, - ScopedItemOptions options, SessionStateEntryVisibility visibility) + internal SessionStateAliasEntry(string name, string definition, string description, ScopedItemOptions options, SessionStateEntryVisibility visibility) : base(name, visibility) { Definition = definition; @@ -769,14 +768,12 @@ internal static SessionStateFunctionEntry GetDelayParsedFunctionEntry(string nam internal static SessionStateFunctionEntry GetDelayParsedFunctionEntry(string name, string definition, bool isProductCode) { var sb = ScriptBlock.CreateDelayParsedScriptBlock(definition, isProductCode); - return new SessionStateFunctionEntry(name, definition, ScopedItemOptions.None, - SessionStateEntryVisibility.Public, sb, null); + return new SessionStateFunctionEntry(name, definition, ScopedItemOptions.None, SessionStateEntryVisibility.Public, sb, null); } internal static SessionStateFunctionEntry GetDelayParsedFunctionEntry(string name, string definition, ScriptBlock sb) { - return new SessionStateFunctionEntry(name, definition, ScopedItemOptions.None, - SessionStateEntryVisibility.Public, sb, null); + return new SessionStateFunctionEntry(name, definition, ScopedItemOptions.None, SessionStateEntryVisibility.Public, sb, null); } /// @@ -868,8 +865,7 @@ public SessionStateVariableEntry(string name, object value, string description, /// A descriptive string to attach to the variable. /// Options like readonly, constant, allscope, etc. /// A list of attributes to attach to the variable. - public SessionStateVariableEntry(string name, object value, string description, - ScopedItemOptions options, Collection attributes) + public SessionStateVariableEntry(string name, object value, string description, ScopedItemOptions options, Collection attributes) : base(name, SessionStateEntryVisibility.Public) { Value = value; @@ -889,8 +885,7 @@ public SessionStateVariableEntry(string name, object value, string description, /// A descriptive string to attach to the variable. /// Options like readonly, constant, allscope, etc. /// A single attribute to attach to the variable. - public SessionStateVariableEntry(string name, object value, string description, - ScopedItemOptions options, Attribute attribute) + public SessionStateVariableEntry(string name, object value, string description, ScopedItemOptions options, Attribute attribute) : base(name, SessionStateEntryVisibility.Public) { Value = value; @@ -912,8 +907,7 @@ public SessionStateVariableEntry(string name, object value, string description, /// Options like readonly, constant, allscope, etc. /// A single attribute to attach to the variable. /// - internal SessionStateVariableEntry(string name, object value, string description, - ScopedItemOptions options, Collection attributes, SessionStateEntryVisibility visibility) + internal SessionStateVariableEntry(string name, object value, string description, ScopedItemOptions options, Collection attributes, SessionStateEntryVisibility visibility) : base(name, visibility) { Value = value; @@ -1367,10 +1361,7 @@ private static InitialSessionState CreateRestrictedForRemoteServer() iss.ImportPSSnapIn(si, out warning); } - // // restrict what gets exposed - // - List allowedCommands = new List(); // required by implicit remoting and interactive remoting @@ -1412,9 +1403,7 @@ private static InitialSessionState CreateRestrictedForRemoteServer() // Add built-in variables. iss.Variables.Add(BuiltInVariables); - // // wrap some commands in a proxy function to restrict their parameters - // foreach (KeyValuePair proxyFunction in CommandMetadata.GetRestrictedCommands(SessionCapabilities.RemoteServer)) { string commandName = proxyFunction.Key; @@ -2338,7 +2327,8 @@ private void Bind_LoadAssemblies(ExecutionContext context) // throw the exception instead of writing it out... if ((!string.IsNullOrEmpty(context.ModuleBeingProcessed) && Path.GetExtension(context.ModuleBeingProcessed) - .Equals(StringLiterals.PowerShellDataFileExtension, + .Equals( + StringLiterals.PowerShellDataFileExtension, StringComparison.OrdinalIgnoreCase)) || ThrowOnRunspaceOpenError) { @@ -2374,7 +2364,9 @@ HashSet GetPublicCommands() publicCommands = new HashSet(); foreach (CommandInfo sessionCommand in initializedRunspace.ExecutionContext.SessionState.InvokeCommand.GetCommands( - "*", CommandTypes.Alias | CommandTypes.Function | CommandTypes.Filter | CommandTypes.Cmdlet, nameIsPattern: true)) + name: "*", + CommandTypes.Alias | CommandTypes.Function | CommandTypes.Filter | CommandTypes.Cmdlet, + nameIsPattern: true)) { if (sessionCommand.Visibility == SessionStateEntryVisibility.Public) { @@ -2405,7 +2397,8 @@ HashSet GetPublicCommands() if (moduleImportException != null) { runspaceInitTracer.WriteLine( - "Runspace open failed while loading module: First error {1}", moduleImportException); + "Runspace open failed while loading module: First error {1}", + moduleImportException); return moduleImportException; } } @@ -2440,7 +2433,8 @@ HashSet GetPublicCommands() if (userDriveException != null) { runspaceInitTracer.WriteLine( - "Runspace open failed while processing user drive with error {1}", userDriveException); + "Runspace open failed while processing user drive with error {1}", + userDriveException); Exception result = PSTraceSource.NewInvalidOperationException(userDriveException, RemotingErrorIdStrings.UserDriveProcessingThrewTerminatingError, userDriveException.Message); return result; @@ -2454,7 +2448,8 @@ HashSet GetPublicCommands() if (startupScriptException != null) { runspaceInitTracer.WriteLine( - "Runspace open failed while running startup script: First error {1}", startupScriptException); + "Runspace open failed while running startup script: First error {1}", + startupScriptException); Exception result = PSTraceSource.NewInvalidOperationException(startupScriptException, RemotingErrorIdStrings.StartupScriptThrewTerminatingError, startupScriptException.Message); return result; @@ -2889,7 +2884,8 @@ private RunspaceOpenModuleLoadException ProcessModulesToImport( version = moduleSpecification.MaximumVersion; } - string message = StringUtil.Format(global::Modules.RequiredModuleNotFoundWrongGuidVersion, + string message = StringUtil.Format( + global::Modules.RequiredModuleNotFoundWrongGuidVersion, moduleSpecification.Name, moduleSpecification.Guid, version); @@ -2923,8 +2919,7 @@ private RunspaceOpenModuleLoadException ProcessModulesToImport( // Special case for wild card lookups. // "Import-Module" or "ipmo" cannot be visible when exposing commands via VisibleCmdlets, etc. if ((cmd.Name.Equals("Import-Module", StringComparison.OrdinalIgnoreCase) && - (!string.IsNullOrEmpty(cmd.ModuleName) && cmd.ModuleName.Equals("Microsoft.PowerShell.Core", StringComparison.OrdinalIgnoreCase)) - ) || + (!string.IsNullOrEmpty(cmd.ModuleName) && cmd.ModuleName.Equals("Microsoft.PowerShell.Core", StringComparison.OrdinalIgnoreCase))) || cmd.Name.Equals("ipmo", StringComparison.OrdinalIgnoreCase) ) { @@ -2936,8 +2931,10 @@ private RunspaceOpenModuleLoadException ProcessModulesToImport( publicCommands.Add(cmd); } } - // Some CommandInfo derivations throw on the Visibility setter. - catch (PSNotImplementedException) { } + catch (PSNotImplementedException) + { + // Some CommandInfo derivations throw on the Visibility setter. + } } if (found && !WildcardPattern.ContainsWildcardCharacters(commandToMakeVisible)) @@ -3065,7 +3062,9 @@ private RunspaceOpenModuleLoadException ProcessOneModule(Runspace initializedRun if (this.DefaultCommandVisibility != SessionStateEntryVisibility.Public) { foreach (CommandInfo importedCommand in initializedRunspace.ExecutionContext.SessionState.InvokeCommand.GetCommands( - "*", CommandTypes.Alias | CommandTypes.Function | CommandTypes.Filter | CommandTypes.Cmdlet, true)) + name: "*", + CommandTypes.Alias | CommandTypes.Function | CommandTypes.Filter | CommandTypes.Cmdlet, + true)) { try { @@ -3076,8 +3075,10 @@ private RunspaceOpenModuleLoadException ProcessOneModule(Runspace initializedRun importedCommand.Visibility = this.DefaultCommandVisibility; } } - // Some CommandInfo derivations throw on the Visibility setter. - catch (PSNotImplementedException) { } + catch (PSNotImplementedException) + { + // Some CommandInfo derivations throw on the Visibility setter. + } } } @@ -3140,8 +3141,11 @@ internal void ResetRunspaceState(ExecutionContext context) // Add the built-in variables foreach (SessionStateVariableEntry e in InitialSessionState.BuiltInVariables) { - PSVariable v = new PSVariable(e.Name, e.Value, - e.Options, e.Attributes, e.Description) + PSVariable v = new PSVariable( + e.Name, + e.Value, + e.Options, e.Attributes, + e.Description) { Visibility = e.Visibility }; ss.GlobalScope.SetVariable(e.Name, v, false, true, ss, fastPath: true); } @@ -3151,8 +3155,12 @@ internal void ResetRunspaceState(ExecutionContext context) // Then re-initialize it with variables to session state... foreach (SessionStateVariableEntry e in Variables) { - PSVariable v = new PSVariable(e.Name, e.Value, - e.Options, e.Attributes, e.Description) + PSVariable v = new PSVariable( + e.Name, + e.Value, + e.Options, + e.Attributes, + e.Description) { Visibility = e.Visibility }; ss.GlobalScope.SetVariable(e.Name, v, false, true, ss, fastPath: true); } @@ -3455,9 +3463,15 @@ internal void UpdateTypes(ExecutionContext context, bool updateOnly) // Use at most 3 locks (we don't expect contention on that many cores anyways, // and typically we'll be processing just 2 or 3 files anyway, hence capacity=3. ConcurrentDictionary filesProcessed - = new ConcurrentDictionary(/*concurrencyLevel*/3, /*capacity*/3, StringComparer.OrdinalIgnoreCase); - Parallel.ForEach(Types, sste => - { + = new ConcurrentDictionary( + concurrencyLevel: 3, + capacity: 3, + StringComparer.OrdinalIgnoreCase); + Parallel.ForEach( + Types, + sste => + { + // foreach (var sste in Types) if (sste.FileName != null) { if (filesProcessed.TryAdd(sste.FileName, null)) @@ -3469,8 +3483,7 @@ ConcurrentDictionary filesProcessed } bool unused; - context.TypeTable.Update(moduleName, sste.FileName, errors, - context.AuthorizationManager, context.EngineHostInterface, out unused); + context.TypeTable.Update(moduleName, sste.FileName, errors, context.AuthorizationManager, context.EngineHostInterface, out unused); } } else if (sste.TypeTable != null) @@ -3664,10 +3677,11 @@ public PSSnapInInfo ImportPSSnapIn(string name, out PSSnapInException warning) { s_PSSnapInTracer.TraceError("MshSnapin {0} and current monad engine's versions don't match.", name); - throw PSTraceSource.NewArgumentException("mshSnapInID", - ConsoleInfoErrorStrings.AddPSSnapInBadMonadVersion, - newPSSnapIn.PSVersion.ToString(), - "2.0"); + throw PSTraceSource.NewArgumentException( + "mshSnapInID", + ConsoleInfoErrorStrings.AddPSSnapInBadMonadVersion, + newPSSnapIn.PSVersion.ToString(), + "2.0"); } // Now actually load the snapin... @@ -3794,8 +3808,7 @@ internal PSSnapInInfo ImportPSSnapIn(PSSnapInInfo psSnapInInfo, out PSSnapInExce this.Formats.Add(formatEntry); } - SessionStateAssemblyEntry assemblyEntry = - new SessionStateAssemblyEntry(psSnapInInfo.AssemblyName, psSnapInInfo.AbsoluteModulePath); + SessionStateAssemblyEntry assemblyEntry = new SessionStateAssemblyEntry(psSnapInInfo.AssemblyName, psSnapInInfo.AbsoluteModulePath); assemblyEntry.SetPSSnapIn(psSnapInInfo); @@ -3926,8 +3939,7 @@ internal void ImportCmdletsFromAssembly(Assembly assembly, PSModuleInfo module) // since it can't be loaded by path or name if (!string.IsNullOrEmpty(assembly.Location)) { - SessionStateAssemblyEntry assemblyEntry = - new SessionStateAssemblyEntry(assembly.FullName, assemblyPath); + SessionStateAssemblyEntry assemblyEntry = new SessionStateAssemblyEntry(assembly.FullName, assemblyPath); this.Assemblies.Add(assemblyEntry); } @@ -3959,9 +3971,7 @@ internal void ImportCmdletsFromAssembly(Assembly assembly, PSModuleInfo module) } } - // // Now define a bunch of functions that describe the rest of the default session state... - // internal const string FormatEnumerationLimit = "FormatEnumerationLimit"; internal const int DefaultFormatEnumerationLimit = 4; @@ -4711,51 +4721,51 @@ internal static void RemoveAllDrivesForProvider(ProviderInfo pi, SessionStateInt // The list of engine modules to create warnings when you try to remove them internal static HashSet EngineModules = new HashSet(StringComparer.OrdinalIgnoreCase) - { - "Microsoft.PowerShell.Utility", - "Microsoft.PowerShell.Management", - "Microsoft.PowerShell.Diagnostics", - "Microsoft.PowerShell.Host", - "Microsoft.PowerShell.Security", - "Microsoft.WSMan.Management" - }; + { + "Microsoft.PowerShell.Utility", + "Microsoft.PowerShell.Management", + "Microsoft.PowerShell.Diagnostics", + "Microsoft.PowerShell.Host", + "Microsoft.PowerShell.Security", + "Microsoft.WSMan.Management" + }; internal static HashSet NestedEngineModules = new HashSet(StringComparer.OrdinalIgnoreCase) - { - "Microsoft.PowerShell.Commands.Utility", - "Microsoft.PowerShell.Commands.Management", - "Microsoft.PowerShell.Commands.Diagnostics", - "Microsoft.PowerShell.ConsoleHost" - }; + { + "Microsoft.PowerShell.Commands.Utility", + "Microsoft.PowerShell.Commands.Management", + "Microsoft.PowerShell.Commands.Diagnostics", + "Microsoft.PowerShell.ConsoleHost" + }; internal static Dictionary EngineModuleNestedModuleMapping = new Dictionary(StringComparer.OrdinalIgnoreCase) - { - { "Microsoft.PowerShell.Utility", "Microsoft.PowerShell.Commands.Utility"}, - { "Microsoft.PowerShell.Management", "Microsoft.PowerShell.Commands.Management"}, - { "Microsoft.PowerShell.Diagnostics", "Microsoft.PowerShell.Commands.Diagnostics"}, - { "Microsoft.PowerShell.Host", "Microsoft.PowerShell.ConsoleHost"}, - }; + { + { "Microsoft.PowerShell.Utility", "Microsoft.PowerShell.Commands.Utility"}, + { "Microsoft.PowerShell.Management", "Microsoft.PowerShell.Commands.Management"}, + { "Microsoft.PowerShell.Diagnostics", "Microsoft.PowerShell.Commands.Diagnostics"}, + { "Microsoft.PowerShell.Host", "Microsoft.PowerShell.ConsoleHost"}, + }; internal static Dictionary NestedModuleEngineModuleMapping = new Dictionary(StringComparer.OrdinalIgnoreCase) - { - { "Microsoft.PowerShell.Commands.Utility", "Microsoft.PowerShell.Utility"}, - { "Microsoft.PowerShell.Commands.Management", "Microsoft.PowerShell.Management"}, - { "Microsoft.PowerShell.Commands.Diagnostics", "Microsoft.PowerShell.Diagnostics"}, - { "Microsoft.PowerShell.ConsoleHost", "Microsoft.PowerShell.Host"}, - { "Microsoft.PowerShell.Security", "Microsoft.PowerShell.Security"}, - { "Microsoft.WSMan.Management", "Microsoft.WSMan.Management"}, - }; + { + { "Microsoft.PowerShell.Commands.Utility", "Microsoft.PowerShell.Utility"}, + { "Microsoft.PowerShell.Commands.Management", "Microsoft.PowerShell.Management"}, + { "Microsoft.PowerShell.Commands.Diagnostics", "Microsoft.PowerShell.Diagnostics"}, + { "Microsoft.PowerShell.ConsoleHost", "Microsoft.PowerShell.Host"}, + { "Microsoft.PowerShell.Security", "Microsoft.PowerShell.Security"}, + { "Microsoft.WSMan.Management", "Microsoft.WSMan.Management"}, + }; // The list of engine modules that we will not allow users to remove internal static HashSet ConstantEngineModules = new HashSet(StringComparer.OrdinalIgnoreCase) - { - CoreModule, - }; + { + CoreModule, + }; // The list of nested engine modules that we will not allow users to remove internal static HashSet ConstantEngineNestedModules = new HashSet(StringComparer.OrdinalIgnoreCase) - { - "System.Management.Automation", - }; + { + "System.Management.Automation", + }; internal static string GetNestedModuleDllName(string moduleName) { @@ -5293,12 +5303,14 @@ internal static IEnumerable GetAssemblyTypes(Assembly assembly, string nam } // cmdletCache holds the list of cmdlets along with its aliases per each assembly. - private static Lazy>>>> s_cmdletCache = + private static readonly Lazy>>>> s_cmdletCache = new Lazy>>>>(); - private static Lazy>> s_providerCache = + + private static readonly Lazy>> s_providerCache = new Lazy>>(); + // Using a ConcurrentDictionary for this so that we can avoid having a private lock variable. We use only the keys for checking. - private static Lazy> s_assembliesWithModuleInitializerCache = new Lazy>(); + private static readonly Lazy> s_assembliesWithModuleInitializerCache = new Lazy>(); [MethodImpl(MethodImplOptions.AggressiveInlining)] private static bool IsCmdletClass(Type type) From c0647adedf095ba4bb9255c9c393e214dab3f452 Mon Sep 17 00:00:00 2001 From: Ilya Date: Tue, 22 Oct 2019 14:52:23 +0500 Subject: [PATCH 04/11] Use nameof() 2 --- .../engine/InitialSessionState.cs | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/System.Management.Automation/engine/InitialSessionState.cs b/src/System.Management.Automation/engine/InitialSessionState.cs index 8d861a8934a..eee1b4cc968 100644 --- a/src/System.Management.Automation/engine/InitialSessionState.cs +++ b/src/System.Management.Automation/engine/InitialSessionState.cs @@ -976,7 +976,7 @@ public InitialSessionStateEntryCollection(IEnumerable items) { if (items == null) { - throw new ArgumentNullException("items"); + throw new ArgumentNullException(nameof(items)); } _internalCollection = new Collection(); @@ -1078,7 +1078,7 @@ public Collection this[string name] /// internal Collection LookUpByName(string name) { - if (name == null) { throw new PSArgumentNullException("name"); } + if (name == null) { throw new PSArgumentNullException(nameof(name)); } Collection result = new Collection(); WildcardPattern namePattern = WildcardPattern.Get(name, WildcardOptions.IgnoreCase); @@ -1146,7 +1146,7 @@ public void Clear() public void Remove(string name, object type) { if (name == null) - throw new ArgumentNullException("name"); + throw new ArgumentNullException(nameof(name)); lock (_syncObject) { @@ -1179,7 +1179,7 @@ public void Remove(string name, object type) public void Add(T item) { if (item == null) - throw new ArgumentNullException("item"); + throw new ArgumentNullException(nameof(item)); lock (_syncObject) { @@ -1194,7 +1194,7 @@ public void Add(T item) public void Add(IEnumerable items) { if (items == null) - throw new ArgumentNullException("items"); + throw new ArgumentNullException(nameof(items)); lock (_syncObject) { @@ -1821,7 +1821,7 @@ public void ImportPSModule(params string[] name) { if (name == null) { - throw new ArgumentNullException("name"); + throw new ArgumentNullException(nameof(name)); } foreach (string n in name) @@ -1849,7 +1849,7 @@ public void ImportPSModule(IEnumerable modules) { if (modules == null) { - throw new ArgumentNullException("modules"); + throw new ArgumentNullException(nameof(modules)); } foreach (var moduleSpecification in modules) @@ -1879,7 +1879,7 @@ public void ImportPSModulesFromPath(string path) internal void ImportPSCoreModule(string[] name) { if (name == null) - throw new ArgumentNullException("name"); + throw new ArgumentNullException(nameof(name)); foreach (string n in name) { CoreModulesToImport.Add(n); @@ -3748,7 +3748,7 @@ internal PSSnapInInfo ImportPSSnapIn(PSSnapInInfo psSnapInInfo, out PSSnapInExce if (psSnapInInfo == null) { - ArgumentNullException e = new ArgumentNullException("psSnapInInfo"); + ArgumentNullException e = new ArgumentNullException(nameof(psSnapInInfo)); throw e; } @@ -3924,7 +3924,7 @@ internal void ImportCmdletsFromAssembly(Assembly assembly, PSModuleInfo module) { if (assembly == null) { - ArgumentNullException e = new ArgumentNullException("assembly"); + ArgumentNullException e = new ArgumentNullException(nameof(assembly)); throw e; } From 4dcf4dd275a6accf4572c265ad814605f9eed69e Mon Sep 17 00:00:00 2001 From: Ilya Date: Tue, 22 Oct 2019 15:42:01 +0500 Subject: [PATCH 05/11] Statement must be wrapped in opening and closing curly brackets --- .../engine/InitialSessionState.cs | 203 +++++++++++++++--- 1 file changed, 172 insertions(+), 31 deletions(-) diff --git a/src/System.Management.Automation/engine/InitialSessionState.cs b/src/System.Management.Automation/engine/InitialSessionState.cs index eee1b4cc968..99c1154b807 100644 --- a/src/System.Management.Automation/engine/InitialSessionState.cs +++ b/src/System.Management.Automation/engine/InitialSessionState.cs @@ -925,7 +925,10 @@ public override InitialSessionStateEntry Clone() // Copy the attribute collection if necessary... Collection attrs = null; if (_attributes != null && _attributes.Count > 0) + { attrs = new Collection(_attributes); + } + return new SessionStateVariableEntry(Name, Value, Description, Options, attrs, Visibility); } @@ -1078,7 +1081,10 @@ public Collection this[string name] /// internal Collection LookUpByName(string name) { - if (name == null) { throw new PSArgumentNullException(nameof(name)); } + if (name == null) + { + throw new PSArgumentNullException(nameof(name)); + } Collection result = new Collection(); WildcardPattern namePattern = WildcardPattern.Get(name, WildcardOptions.IgnoreCase); @@ -1146,7 +1152,9 @@ public void Clear() public void Remove(string name, object type) { if (name == null) + { throw new ArgumentNullException(nameof(name)); + } lock (_syncObject) { @@ -1162,7 +1170,10 @@ public void Remove(string name, object type) { T element = _internalCollection[i]; if (element == null) + { continue; + } + if ((objType == null || element.GetType() == objType) && string.Equals(element.Name, name, StringComparison.OrdinalIgnoreCase)) { @@ -1179,7 +1190,9 @@ public void Remove(string name, object type) public void Add(T item) { if (item == null) + { throw new ArgumentNullException(nameof(item)); + } lock (_syncObject) { @@ -1194,7 +1207,9 @@ public void Add(T item) public void Add(IEnumerable items) { if (items == null) + { throw new ArgumentNullException(nameof(items)); + } lock (_syncObject) { @@ -1762,7 +1777,10 @@ internal bool EnforceInputParameterValidation /// public Microsoft.PowerShell.ExecutionPolicy ExecutionPolicy { - get { return _executionPolicy; } + get + { + return _executionPolicy; + } set { @@ -1879,7 +1897,10 @@ public void ImportPSModulesFromPath(string path) internal void ImportPSCoreModule(string[] name) { if (name == null) + { throw new ArgumentNullException(nameof(name)); + } + foreach (string n in name) { CoreModulesToImport.Add(n); @@ -1911,7 +1932,10 @@ public virtual InitialSessionStateEntryCollection Ass get { if (_assemblies == null) + { Interlocked.CompareExchange(ref _assemblies, new InitialSessionStateEntryCollection(), null); + } + return _assemblies; } } @@ -1926,7 +1950,10 @@ public virtual InitialSessionStateEntryCollection Types get { if (_types == null) + { Interlocked.CompareExchange(ref _types, new InitialSessionStateEntryCollection(), null); + } + return _types; } } @@ -1940,7 +1967,10 @@ public virtual InitialSessionStateEntryCollection Forma get { if (_formats == null) + { Interlocked.CompareExchange(ref _formats, new InitialSessionStateEntryCollection(), null); + } + return _formats; } } @@ -1962,7 +1992,10 @@ public virtual InitialSessionStateEntryCollection Pro get { if (_providers == null) + { Interlocked.CompareExchange(ref _providers, new InitialSessionStateEntryCollection(), null); + } + return _providers; } } @@ -1977,7 +2010,10 @@ public virtual InitialSessionStateEntryCollection Comm get { if (_commands == null) + { Interlocked.CompareExchange(ref _commands, new InitialSessionStateEntryCollection(), null); + } + return _commands; } } @@ -1991,7 +2027,10 @@ internal HashSet UnresolvedCommandsToExpose get { if (_unresolvedCommandsToExpose == null) + { Interlocked.CompareExchange(ref _unresolvedCommandsToExpose, new HashSet(StringComparer.OrdinalIgnoreCase), null); + } + return _unresolvedCommandsToExpose; } } @@ -2003,7 +2042,10 @@ internal Dictionary CommandModifications get { if (_commandModifications == null) + { Interlocked.CompareExchange(ref _commandModifications, new Dictionary(StringComparer.OrdinalIgnoreCase), null); + } + return _commandModifications; } } @@ -2015,7 +2057,10 @@ internal List DynamicVariablesToDefine get { if (_dynamicVariablesToDefine == null) + { Interlocked.CompareExchange(ref _dynamicVariablesToDefine, new List(), null); + } + return _dynamicVariablesToDefine; } } @@ -2029,7 +2074,10 @@ public virtual InitialSessionStateEntryCollection Var get { if (_variables == null) + { Interlocked.CompareExchange(ref _variables, new InitialSessionStateEntryCollection(), null); + } + return _variables; } } @@ -2043,7 +2091,10 @@ public virtual InitialSessionStateEntryCollection Env get { if (_environmentVariables == null) + { Interlocked.CompareExchange(ref _environmentVariables, new InitialSessionStateEntryCollection(), null); + } + return _environmentVariables; } } @@ -2057,7 +2108,10 @@ public virtual HashSet StartupScripts get { if (_startupScripts == null) + { Interlocked.CompareExchange(ref _startupScripts, new HashSet(), null); + } + return _startupScripts; } } @@ -2175,7 +2229,10 @@ internal void Bind(ExecutionContext context, bool updateOnly, PSModuleInfo modul private void Bind_SetVariables(SessionStateInternal ss) { bool etwEnabled = RunspaceEventSource.Log.IsEnabled(); - if (etwEnabled) RunspaceEventSource.Log.LoadVariablesStart(); + if (etwEnabled) + { + RunspaceEventSource.Log.LoadVariablesStart(); + } // Add all of the variables to session state... foreach (SessionStateVariableEntry var in Variables) @@ -2183,64 +2240,105 @@ private void Bind_SetVariables(SessionStateInternal ss) ss.AddSessionStateEntry(var); } - if (etwEnabled) RunspaceEventSource.Log.LoadVariablesStop(); + if (etwEnabled) + { + RunspaceEventSource.Log.LoadVariablesStop(); + } } private void Bind_SetEnvironment() { bool etwEnabled = RunspaceEventSource.Log.IsEnabled(); - if (etwEnabled) RunspaceEventSource.Log.LoadEnvironmentVariablesStart(); + if (etwEnabled) + { + RunspaceEventSource.Log.LoadEnvironmentVariablesStart(); + } foreach (SessionStateVariableEntry var in EnvironmentVariables) { Environment.SetEnvironmentVariable(var.Name, var.Value.ToString()); } - if (etwEnabled) RunspaceEventSource.Log.LoadEnvironmentVariablesStop(); + if (etwEnabled) + { + RunspaceEventSource.Log.LoadEnvironmentVariablesStop(); + } } private void Bind_UpdateTypes(ExecutionContext context, bool updateOnly) { bool etwEnabled = RunspaceEventSource.Log.IsEnabled(); - if (etwEnabled) RunspaceEventSource.Log.UpdateTypeTableStart(); + if (etwEnabled) + { + RunspaceEventSource.Log.UpdateTypeTableStart(); + } + this.UpdateTypes(context, updateOnly); - if (etwEnabled) RunspaceEventSource.Log.UpdateTypeTableStop(); + if (etwEnabled) + { + RunspaceEventSource.Log.UpdateTypeTableStop(); + } } private void Bind_UpdateFormats(ExecutionContext context, bool updateOnly) { bool etwEnabled = RunspaceEventSource.Log.IsEnabled(); - if (etwEnabled) RunspaceEventSource.Log.UpdateFormatTableStart(); + if (etwEnabled) + { + RunspaceEventSource.Log.UpdateFormatTableStart(); + } this.UpdateFormats(context, updateOnly); - if (etwEnabled) RunspaceEventSource.Log.UpdateFormatTableStop(); + if (etwEnabled) + { + RunspaceEventSource.Log.UpdateFormatTableStop(); + } } private void Bind_LoadProviders(SessionStateInternal ss) { bool etwEnabled = RunspaceEventSource.Log.IsEnabled(); - if (etwEnabled) RunspaceEventSource.Log.LoadProvidersStart(); + if (etwEnabled) + { + RunspaceEventSource.Log.LoadProvidersStart(); + } // Add all of the providers to session state... foreach (SessionStateProviderEntry provider in Providers) { - if (etwEnabled) RunspaceEventSource.Log.LoadProviderStart(provider.Name); + if (etwEnabled) + { + RunspaceEventSource.Log.LoadProviderStart(provider.Name); + } + ss.AddSessionStateEntry(provider); - if (etwEnabled) RunspaceEventSource.Log.LoadProviderStop(provider.Name); + if (etwEnabled) + { + RunspaceEventSource.Log.LoadProviderStop(provider.Name); + } } - if (etwEnabled) RunspaceEventSource.Log.LoadProvidersStop(); + if (etwEnabled) + { + RunspaceEventSource.Log.LoadProvidersStop(); + } } private void Bind_BindCommands(PSModuleInfo module, bool noClobber, bool local, SessionStateInternal ss) { bool etwEnabled = RunspaceEventSource.Log.IsEnabled(); - if (etwEnabled) RunspaceEventSource.Log.LoadCommandsStart(); + if (etwEnabled) + { + RunspaceEventSource.Log.LoadCommandsStart(); + } foreach (SessionStateCommandEntry cmd in Commands) { - if (etwEnabled) RunspaceEventSource.Log.LoadCommandStart(cmd.Name); + if (etwEnabled) + { + RunspaceEventSource.Log.LoadCommandStart(cmd.Name); + } SessionStateCmdletEntry ssce = cmd as SessionStateCmdletEntry; if (ssce != null) @@ -2295,16 +2393,25 @@ private void Bind_BindCommands(PSModuleInfo module, bool noClobber, bool local, continue; } - if (etwEnabled) RunspaceEventSource.Log.LoadCommandStop(cmd.Name); + if (etwEnabled) + { + RunspaceEventSource.Log.LoadCommandStop(cmd.Name); + } } - if (etwEnabled) RunspaceEventSource.Log.LoadCommandsStop(); + if (etwEnabled) + { + RunspaceEventSource.Log.LoadCommandsStop(); + } } private void Bind_LoadAssemblies(ExecutionContext context) { bool etwEnabled = RunspaceEventSource.Log.IsEnabled(); - if (etwEnabled) RunspaceEventSource.Log.LoadAssembliesStart(); + if (etwEnabled) + { + RunspaceEventSource.Log.LoadAssembliesStart(); + } // Load the assemblies and initialize the assembly cache... foreach (SessionStateAssemblyEntry ssae in Assemblies) @@ -2340,10 +2447,16 @@ private void Bind_LoadAssemblies(ExecutionContext context) } } - if (etwEnabled) RunspaceEventSource.Log.LoadAssemblyStop(ssae.Name, ssae.FileName); + if (etwEnabled) + { + RunspaceEventSource.Log.LoadAssemblyStop(ssae.Name, ssae.FileName); + } } - if (etwEnabled) RunspaceEventSource.Log.LoadAssembliesStop(); + if (etwEnabled) + { + RunspaceEventSource.Log.LoadAssembliesStop(); + } } internal Exception BindRunspace(Runspace initializedRunspace, PSTraceSource runspaceInitTracer) @@ -2912,7 +3025,10 @@ private RunspaceOpenModuleLoadException ProcessModulesToImport( foreach (CommandInfo cmd in LookupCommands(commandToMakeVisible, moduleName, initializedRunspace.ExecutionContext)) { - if (!found) { found = true; } + if (!found) + { + found = true; + } try { @@ -2965,7 +3081,7 @@ private IEnumerable LookupCommands( ExecutionContext context) { bool isWildCardPattern = WildcardPattern.ContainsWildcardCharacters(commandPattern); - var searchOptions = (isWildCardPattern) ? + var searchOptions = isWildCardPattern ? SearchResolutionOptions.CommandNameIsPattern | SearchResolutionOptions.ResolveFunctionPatterns | SearchResolutionOptions.SearchAllScopes : SearchResolutionOptions.ResolveFunctionPatterns | SearchResolutionOptions.SearchAllScopes; @@ -2984,12 +3100,18 @@ private IEnumerable LookupCommands( continue; } - if (!found) { found = true; } + if (!found) + { + found = true; + } yield return commandInfo; // Return first match unless a wild card pattern is submitted. - if (!isWildCardPattern) { break; } + if (!isWildCardPattern) + { + break; + } } if (found || (cmdOrigin == CommandOrigin.Internal)) @@ -5081,7 +5203,10 @@ private static void AnalyzeModuleAssemblyWithReflection( foreach (Type type in assemblyTypes) { - if (!HasDefaultConstructor(type)) { continue; } + if (!HasDefaultConstructor(type)) + { + continue; + } // Check for cmdlets if (IsCmdletClass(type) && TryGetCustomAttribute(type, out CmdletAttribute cmdletAttribute)) @@ -5102,9 +5227,15 @@ private static void AnalyzeModuleAssemblyWithReflection( } SessionStateCmdletEntry cmdlet = new SessionStateCmdletEntry(cmdletName, type, helpFile); - if (psSnapInInfo != null) { cmdlet.SetPSSnapIn(psSnapInInfo); } + if (psSnapInInfo != null) + { + cmdlet.SetPSSnapIn(psSnapInInfo); + } - if (moduleInfo != null) { cmdlet.SetModule(moduleInfo); } + if (moduleInfo != null) + { + cmdlet.SetModule(moduleInfo); + } cmdlets = cmdlets ?? new Dictionary(StringComparer.OrdinalIgnoreCase); cmdlets.Add(cmdletName, cmdlet); @@ -5122,7 +5253,10 @@ private static void AnalyzeModuleAssemblyWithReflection( var aliasEntry = new SessionStateAliasEntry(alias, cmdletName, description: string.Empty, ScopedItemOptions.None); if (psSnapInInfo != null) { aliasEntry.SetPSSnapIn(psSnapInInfo); } - if (moduleInfo != null) { aliasEntry.SetModule(moduleInfo); } + if (moduleInfo != null) + { + aliasEntry.SetModule(moduleInfo); + } aliasList.Add(aliasEntry); } @@ -5151,9 +5285,15 @@ private static void AnalyzeModuleAssemblyWithReflection( } SessionStateProviderEntry provider = new SessionStateProviderEntry(providerName, type, helpFile); - if (psSnapInInfo != null) { provider.SetPSSnapIn(psSnapInInfo); } + if (psSnapInInfo != null) + { + provider.SetPSSnapIn(psSnapInInfo); + } - if (moduleInfo != null) { provider.SetModule(moduleInfo); } + if (moduleInfo != null) + { + provider.SetModule(moduleInfo); + } providers = providers ?? new Dictionary(StringComparer.OrdinalIgnoreCase); providers.Add(providerName, provider); @@ -5163,6 +5303,7 @@ private static void AnalyzeModuleAssemblyWithReflection( } } + [SuppressMessage("StyleCop.CSharp.SpacingRules", "SA1001:CommasMustBeSpacedCorrectly", Justification = "Reviewed.")] private static void InitializeCoreCmdletsAndProviders( PSSnapInInfo psSnapInInfo, out Dictionary cmdlets, From 21e8aa5563f35309871af73dd29fffd75b337af1 Mon Sep 17 00:00:00 2001 From: Ilya Date: Tue, 22 Oct 2019 15:45:54 +0500 Subject: [PATCH 06/11] Consts --- .../engine/InitialSessionState.cs | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/System.Management.Automation/engine/InitialSessionState.cs b/src/System.Management.Automation/engine/InitialSessionState.cs index 99c1154b807..7ee3ef550a9 100644 --- a/src/System.Management.Automation/engine/InitialSessionState.cs +++ b/src/System.Management.Automation/engine/InitialSessionState.cs @@ -4420,16 +4420,16 @@ .ForwardHelpCategory Cmdlet "; } - internal const ActionPreference defaultDebugPreference = ActionPreference.SilentlyContinue; - internal const ActionPreference defaultErrorActionPreference = ActionPreference.Continue; - internal const ActionPreference defaultProgressPreference = ActionPreference.Continue; - internal const ActionPreference defaultVerbosePreference = ActionPreference.SilentlyContinue; - internal const ActionPreference defaultWarningPreference = ActionPreference.Continue; - internal const ActionPreference defaultInformationPreference = ActionPreference.SilentlyContinue; + internal const ActionPreference DefaultDebugPreference = ActionPreference.SilentlyContinue; + internal const ActionPreference DefaultErrorActionPreference = ActionPreference.Continue; + internal const ActionPreference DefaultProgressPreference = ActionPreference.Continue; + internal const ActionPreference DefaultVerbosePreference = ActionPreference.SilentlyContinue; + internal const ActionPreference DefaultWarningPreference = ActionPreference.Continue; + internal const ActionPreference DefaultInformationPreference = ActionPreference.SilentlyContinue; - internal const ErrorView defaultErrorView = ErrorView.NormalView; - internal const bool defaultWhatIfPreference = false; - internal const ConfirmImpact defaultConfirmPreference = ConfirmImpact.High; + internal const ErrorView DefaultErrorView = ErrorView.NormalView; + internal const bool DefaultWhatIfPreference = false; + internal const ConfirmImpact DefaultConfirmPreference = ConfirmImpact.High; internal static SessionStateVariableEntry[] BuiltInVariables = new SessionStateVariableEntry[] { @@ -4460,49 +4460,49 @@ .ForwardHelpCategory Cmdlet // Variables typed to ActionPreference new SessionStateVariableEntry( SpecialVariables.ConfirmPreference, - defaultConfirmPreference, + DefaultConfirmPreference, RunspaceInit.ConfirmPreferenceDescription, ScopedItemOptions.None, new ArgumentTypeConverterAttribute(typeof(ConfirmImpact))), new SessionStateVariableEntry( SpecialVariables.DebugPreference, - defaultDebugPreference, + DefaultDebugPreference, RunspaceInit.DebugPreferenceDescription, ScopedItemOptions.None, new ArgumentTypeConverterAttribute(typeof(ActionPreference))), new SessionStateVariableEntry( SpecialVariables.ErrorActionPreference, - defaultErrorActionPreference, + DefaultErrorActionPreference, RunspaceInit.ErrorActionPreferenceDescription, ScopedItemOptions.None, new ArgumentTypeConverterAttribute(typeof(ActionPreference))), new SessionStateVariableEntry( SpecialVariables.ProgressPreference, - defaultProgressPreference, + DefaultProgressPreference, RunspaceInit.ProgressPreferenceDescription, ScopedItemOptions.None, new ArgumentTypeConverterAttribute(typeof(ActionPreference))), new SessionStateVariableEntry( SpecialVariables.VerbosePreference, - defaultVerbosePreference, + DefaultVerbosePreference, RunspaceInit.VerbosePreferenceDescription, ScopedItemOptions.None, new ArgumentTypeConverterAttribute(typeof(ActionPreference))), new SessionStateVariableEntry( SpecialVariables.WarningPreference, - defaultWarningPreference, + DefaultWarningPreference, RunspaceInit.WarningPreferenceDescription, ScopedItemOptions.None, new ArgumentTypeConverterAttribute(typeof(ActionPreference))), new SessionStateVariableEntry( SpecialVariables.InformationPreference, - defaultInformationPreference, + DefaultInformationPreference, RunspaceInit.InformationPreferenceDescription, ScopedItemOptions.None, new ArgumentTypeConverterAttribute(typeof(ActionPreference))), new SessionStateVariableEntry( SpecialVariables.ErrorView, - ExperimentalFeature.IsEnabled("PSErrorView") ? ErrorView.ConciseView : defaultErrorView, + ExperimentalFeature.IsEnabled("PSErrorView") ? ErrorView.ConciseView : DefaultErrorView, RunspaceInit.ErrorViewDescription, ScopedItemOptions.None, new ArgumentTypeConverterAttribute(typeof(ErrorView))), @@ -4512,7 +4512,7 @@ .ForwardHelpCategory Cmdlet RunspaceInit.NestedPromptLevelDescription), new SessionStateVariableEntry( SpecialVariables.WhatIfPreference, - defaultWhatIfPreference, + DefaultWhatIfPreference, RunspaceInit.WhatIfPreferenceDescription), new SessionStateVariableEntry( FormatEnumerationLimit, From 3ae1bdc5d5bb2fa5522811ba911a094c2cc6d88d Mon Sep 17 00:00:00 2001 From: Ilya Date: Thu, 7 Nov 2019 09:58:34 +0500 Subject: [PATCH 07/11] Address feedback --- .../engine/InitialSessionState.cs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/System.Management.Automation/engine/InitialSessionState.cs b/src/System.Management.Automation/engine/InitialSessionState.cs index 7ee3ef550a9..ee649239f55 100644 --- a/src/System.Management.Automation/engine/InitialSessionState.cs +++ b/src/System.Management.Automation/engine/InitialSessionState.cs @@ -203,7 +203,7 @@ public SessionStateTypeEntry(TypeTable typeTable) { if (typeTable == null) { - throw PSTraceSource.NewArgumentException(nameof(typeTable)); + throw PSTraceSource.NewArgumentNullException(nameof(typeTable)); } TypeTable = typeTable; @@ -219,7 +219,7 @@ public SessionStateTypeEntry(TypeData typeData, bool isRemove) { if (typeData == null) { - throw PSTraceSource.NewArgumentException(nameof(typeData)); + throw PSTraceSource.NewArgumentNullException(nameof(typeData)); } TypeData = typeData; @@ -3584,8 +3584,7 @@ internal void UpdateTypes(ExecutionContext context, bool updateOnly) ConcurrentBag errors = new ConcurrentBag(); // Use at most 3 locks (we don't expect contention on that many cores anyways, // and typically we'll be processing just 2 or 3 files anyway, hence capacity=3. - ConcurrentDictionary filesProcessed - = new ConcurrentDictionary( + ConcurrentDictionary filesProcessed = new ConcurrentDictionary( concurrencyLevel: 3, capacity: 3, StringComparer.OrdinalIgnoreCase); From 720e54c86749acf0c79c36f36970a3e3a6050d73 Mon Sep 17 00:00:00 2001 From: Ilya Date: Thu, 7 Nov 2019 09:59:59 +0500 Subject: [PATCH 08/11] Fix CodeFactor issue --- src/System.Management.Automation/engine/InitialSessionState.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/System.Management.Automation/engine/InitialSessionState.cs b/src/System.Management.Automation/engine/InitialSessionState.cs index ee649239f55..f09da64d59d 100644 --- a/src/System.Management.Automation/engine/InitialSessionState.cs +++ b/src/System.Management.Automation/engine/InitialSessionState.cs @@ -3592,7 +3592,7 @@ internal void UpdateTypes(ExecutionContext context, bool updateOnly) Types, sste => { - // foreach (var sste in Types) + // foreach (var sste in Types) if (sste.FileName != null) { if (filesProcessed.TryAdd(sste.FileName, null)) From fbe3333db27c143c1ded2129bc7e7e31b82be276 Mon Sep 17 00:00:00 2001 From: Ilya Date: Thu, 7 Nov 2019 10:07:03 +0500 Subject: [PATCH 09/11] Fix typos --- .../engine/ExecutionContext.cs | 12 ++++++------ .../engine/MshCommandRuntime.cs | 16 ++++++++-------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/System.Management.Automation/engine/ExecutionContext.cs b/src/System.Management.Automation/engine/ExecutionContext.cs index 4e4afe0f721..118ff82a899 100644 --- a/src/System.Management.Automation/engine/ExecutionContext.cs +++ b/src/System.Management.Automation/engine/ExecutionContext.cs @@ -1056,7 +1056,7 @@ internal ActionPreference DebugPreferenceVariable bool defaultUsed = false; return this.GetEnumPreference( SpecialVariables.DebugPreferenceVarPath, - InitialSessionState.defaultDebugPreference, + InitialSessionState.DefaultDebugPreference, out defaultUsed); } @@ -1077,7 +1077,7 @@ internal ActionPreference VerbosePreferenceVariable bool defaultUsed = false; return this.GetEnumPreference( SpecialVariables.VerbosePreferenceVarPath, - InitialSessionState.defaultVerbosePreference, + InitialSessionState.DefaultVerbosePreference, out defaultUsed); } @@ -1098,7 +1098,7 @@ internal ActionPreference ErrorActionPreferenceVariable bool defaultUsed = false; return this.GetEnumPreference( SpecialVariables.ErrorActionPreferenceVarPath, - InitialSessionState.defaultErrorActionPreference, + InitialSessionState.DefaultErrorActionPreference, out defaultUsed); } @@ -1119,7 +1119,7 @@ internal ActionPreference WarningActionPreferenceVariable bool defaultUsed = false; return this.GetEnumPreference( SpecialVariables.WarningPreferenceVarPath, - InitialSessionState.defaultWarningPreference, + InitialSessionState.DefaultWarningPreference, out defaultUsed); } @@ -1140,7 +1140,7 @@ internal ActionPreference InformationActionPreferenceVariable bool defaultUsed = false; return this.GetEnumPreference( SpecialVariables.InformationPreferenceVarPath, - InitialSessionState.defaultInformationPreference, + InitialSessionState.DefaultInformationPreference, out defaultUsed); } @@ -1186,7 +1186,7 @@ internal ConfirmImpact ConfirmPreferenceVariable bool defaultUsed = false; return this.GetEnumPreference( SpecialVariables.ConfirmPreferenceVarPath, - InitialSessionState.defaultConfirmPreference, + InitialSessionState.DefaultConfirmPreference, out defaultUsed); } diff --git a/src/System.Management.Automation/engine/MshCommandRuntime.cs b/src/System.Management.Automation/engine/MshCommandRuntime.cs index cf1260f1fc9..19c328785d8 100644 --- a/src/System.Management.Automation/engine/MshCommandRuntime.cs +++ b/src/System.Management.Automation/engine/MshCommandRuntime.cs @@ -2927,7 +2927,7 @@ internal void _WriteErrorSkipAllowCheck(ErrorRecord errorRecord, ActionPreferenc // See "User Feedback Mechanisms - Note.doc" for details. private bool _isConfirmPreferenceCached = false; - private ConfirmImpact _confirmPreference = InitialSessionState.defaultConfirmPreference; + private ConfirmImpact _confirmPreference = InitialSessionState.DefaultConfirmPreference; /// /// Preference setting controlling behavior of ShouldProcess() /// @@ -2967,7 +2967,7 @@ internal ConfirmImpact ConfirmPreference } private bool _isDebugPreferenceSet = false; - private ActionPreference _debugPreference = InitialSessionState.defaultDebugPreference; + private ActionPreference _debugPreference = InitialSessionState.DefaultDebugPreference; private bool _isDebugPreferenceCached = false; /// /// Preference setting. @@ -3021,7 +3021,7 @@ internal ActionPreference DebugPreference } private bool _isVerbosePreferenceCached = false; - private ActionPreference _verbosePreference = InitialSessionState.defaultVerbosePreference; + private ActionPreference _verbosePreference = InitialSessionState.DefaultVerbosePreference; /// /// Preference setting. /// @@ -3070,7 +3070,7 @@ internal ActionPreference VerbosePreference internal bool IsWarningActionSet { get; private set; } = false; private bool _isWarningPreferenceCached = false; - private ActionPreference _warningPreference = InitialSessionState.defaultWarningPreference; + private ActionPreference _warningPreference = InitialSessionState.DefaultWarningPreference; /// /// Preference setting. /// @@ -3209,7 +3209,7 @@ internal bool Debug internal bool IsDebugFlagSet { get; private set; } = false; - private bool _whatIfFlag = InitialSessionState.defaultWhatIfPreference; + private bool _whatIfFlag = InitialSessionState.DefaultWhatIfPreference; private bool _isWhatIfPreferenceCached /* = false */; /// /// WhatIf indicates that the command should not @@ -3241,7 +3241,7 @@ internal SwitchParameter WhatIf internal bool IsWhatIfFlagSet { get; private set; } - private ActionPreference _errorAction = InitialSessionState.defaultErrorActionPreference; + private ActionPreference _errorAction = InitialSessionState.DefaultErrorActionPreference; private bool _isErrorActionPreferenceCached = false; /// /// ErrorAction tells the command what to do when an error occurs. @@ -3317,7 +3317,7 @@ internal ActionPreference ProgressPreference } } - private ActionPreference _progressPreference = InitialSessionState.defaultProgressPreference; + private ActionPreference _progressPreference = InitialSessionState.DefaultProgressPreference; private bool _isProgressPreferenceSet = false; private bool _isProgressPreferenceCached = false; @@ -3354,7 +3354,7 @@ internal ActionPreference InformationPreference } } - private ActionPreference _informationPreference = InitialSessionState.defaultInformationPreference; + private ActionPreference _informationPreference = InitialSessionState.DefaultInformationPreference; internal bool IsInformationActionSet { get; private set; } = false; From ce30a62370af1baa58a7edd186270a9a147fb8c1 Mon Sep 17 00:00:00 2001 From: Ilya Date: Fri, 8 Nov 2019 08:22:49 +0500 Subject: [PATCH 10/11] Update src/System.Management.Automation/engine/InitialSessionState.cs Co-Authored-By: Steve Lee --- src/System.Management.Automation/engine/InitialSessionState.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/System.Management.Automation/engine/InitialSessionState.cs b/src/System.Management.Automation/engine/InitialSessionState.cs index f09da64d59d..6207d6e727c 100644 --- a/src/System.Management.Automation/engine/InitialSessionState.cs +++ b/src/System.Management.Automation/engine/InitialSessionState.cs @@ -310,7 +310,7 @@ public SessionStateFormatEntry(FormatTable formattable) { if (formattable == null) { - throw PSTraceSource.NewArgumentException(nameof(formattable)); + throw PSTraceSource.NewArgumentNullException(nameof(formattable)); } Formattable = formattable; From 2506310f16ddc5925f0e736585f0c566e2c42180 Mon Sep 17 00:00:00 2001 From: Ilya Date: Fri, 8 Nov 2019 08:23:00 +0500 Subject: [PATCH 11/11] Update src/System.Management.Automation/engine/InitialSessionState.cs Co-Authored-By: Steve Lee --- src/System.Management.Automation/engine/InitialSessionState.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/System.Management.Automation/engine/InitialSessionState.cs b/src/System.Management.Automation/engine/InitialSessionState.cs index 6207d6e727c..101461be407 100644 --- a/src/System.Management.Automation/engine/InitialSessionState.cs +++ b/src/System.Management.Automation/engine/InitialSessionState.cs @@ -325,7 +325,7 @@ public SessionStateFormatEntry(ExtendedTypeDefinition typeDefinition) { if (typeDefinition == null) { - throw PSTraceSource.NewArgumentException(nameof(typeDefinition)); + throw PSTraceSource.NewArgumentNullException(nameof(typeDefinition)); } FormatData = typeDefinition;