From 6177d28410cd6c41eeae67d2c3dc621d20668e1b Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Wed, 18 Oct 2017 09:12:23 -0700 Subject: [PATCH] removed RunspaceConfiguration support (#4942) For PSCore 6, we are only supporting InitialSessionState. The RunspaceConfiguration APIs were already made internal. This PR removes all the code related to RunspaceConfiguration. This also means that some public APIs have changed. Was deciding between leaving the RunspaceConfiguration parameters and throwing Unsupported, but thought it was better to have it a compile-time error. This should simplify the code base. --- .../commands/utility/Update-TypeData.cs | 129 +-- .../host/msh/ConsoleHost.cs | 147 +-- .../host/msh/ConsoleShell.cs | 44 +- .../host/msh/ManagedEntrance.cs | 48 +- .../engine/AutomationEngine.cs | 53 +- .../engine/CommandDiscovery.cs | 200 +--- .../engine/CommandSearcher.cs | 110 +- .../engine/ExecutionContext.cs | 156 +-- .../engine/InitialSessionState.cs | 23 +- .../engine/Modules/ModuleCmdletBase.cs | 178 +--- .../engine/ProviderNames.cs | 83 -- .../RunspaceConfigurationEntry.cs | 41 + .../engine/SessionState.cs | 30 +- .../engine/SessionStateProviderAPIs.cs | 105 +- .../engine/SpecialVariables.cs | 3 - .../engine/TypeTable.cs | 2 - .../engine/hostifaces/Connection.cs | 155 ++- .../engine/hostifaces/ConnectionBase.cs | 24 +- .../engine/hostifaces/ConnectionFactory.cs | 123 +-- .../engine/hostifaces/LocalConnection.cs | 46 +- .../engine/hostifaces/RunspaceInit.cs | 18 - .../engine/hostifaces/RunspaceInvoke.cs | 59 +- .../engine/hostifaces/RunspacePool.cs | 10 +- .../engine/hostifaces/RunspacePoolInternal.cs | 39 +- .../engine/remoting/client/remoterunspace.cs | 16 - .../help/HelpProvider.cs | 11 - .../help/HelpProviderWithFullCache.cs | 10 +- .../help/HelpSystem.cs | 18 - .../help/MUIFileSearcher.cs | 5 +- .../minishell/api/RunspaceConfiguration.cs | 791 --------------- ...RunspaceConfigurationAttributeException.cs | 194 ---- .../RunspaceConfigurationEntryCollection.cs | 434 -------- .../api/RunspaceConfigurationHelper.cs | 29 - .../api/RunspaceConfigurationTypeAttribute.cs | 31 - .../api/RunspaceConfigurationTypeException.cs | 181 ---- .../resources/MiniShellErrors.resx | 12 - .../singleshell/Commands/ConsoleCommands.cs | 602 ----------- .../singleshell/Commands/MshSnapinCommands.cs | 850 ---------------- .../singleshell/config/MshConsoleInfo.cs | 959 ------------------ .../config/MshConsoleLoadException.cs | 92 +- .../config/RegistryStringResourceIndirect.cs | 611 ----------- .../config/RunspaceConfigForSingleShell.cs | 754 -------------- .../singleshell/installer/CustomMshSnapin.cs | 122 --- .../singleshell/installer/MshCoreMshSnapin.cs | 113 --- .../singleshell/installer/MshInstaller.cs | 220 ---- .../singleshell/installer/MshSnapin.cs | 88 -- .../installer/MshSnapinInstaller.cs | 159 --- .../api => utils}/FormatAndTypeDataHelper.cs | 84 +- .../Update-FormatData.Tests.ps1 | 14 + .../engine/Api/BasicEngine.Tests.ps1 | 2 +- 50 files changed, 301 insertions(+), 7927 deletions(-) rename src/System.Management.Automation/{minishell/api => engine}/RunspaceConfigurationEntry.cs (95%) delete mode 100644 src/System.Management.Automation/minishell/api/RunspaceConfiguration.cs delete mode 100644 src/System.Management.Automation/minishell/api/RunspaceConfigurationAttributeException.cs delete mode 100644 src/System.Management.Automation/minishell/api/RunspaceConfigurationEntryCollection.cs delete mode 100644 src/System.Management.Automation/minishell/api/RunspaceConfigurationHelper.cs delete mode 100644 src/System.Management.Automation/minishell/api/RunspaceConfigurationTypeAttribute.cs delete mode 100644 src/System.Management.Automation/minishell/api/RunspaceConfigurationTypeException.cs delete mode 100644 src/System.Management.Automation/singleshell/Commands/ConsoleCommands.cs delete mode 100644 src/System.Management.Automation/singleshell/Commands/MshSnapinCommands.cs delete mode 100644 src/System.Management.Automation/singleshell/config/MshConsoleInfo.cs delete mode 100644 src/System.Management.Automation/singleshell/config/RegistryStringResourceIndirect.cs delete mode 100644 src/System.Management.Automation/singleshell/config/RunspaceConfigForSingleShell.cs delete mode 100644 src/System.Management.Automation/singleshell/installer/CustomMshSnapin.cs delete mode 100644 src/System.Management.Automation/singleshell/installer/MshCoreMshSnapin.cs delete mode 100644 src/System.Management.Automation/singleshell/installer/MshInstaller.cs delete mode 100644 src/System.Management.Automation/singleshell/installer/MshSnapin.cs delete mode 100644 src/System.Management.Automation/singleshell/installer/MshSnapinInstaller.cs rename src/System.Management.Automation/{minishell/api => utils}/FormatAndTypeDataHelper.cs (63%) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Update-TypeData.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Update-TypeData.cs index daef516bbd..53ec744bce 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Update-TypeData.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Update-TypeData.cs @@ -366,17 +366,13 @@ namespace Microsoft.PowerShell.Commands else { // Update successfully, we add the TypeData into cache - if (Context.RunspaceConfiguration != null) - { - Context.RunspaceConfiguration.Types.Append(new TypeConfigurationEntry(type, false)); - } - else if (Context.InitialSessionState != null) + if (Context.InitialSessionState != null) { Context.InitialSessionState.Types.Add(new SessionStateTypeEntry(type, false)); } else { - Dbg.Assert(false, "Either RunspaceConfiguration or InitialSessionState must be non-null for Update-Typedata to work"); + Dbg.Assert(false, "InitialSessionState must be non-null for Update-Typedata to work"); } } } @@ -484,17 +480,13 @@ namespace Microsoft.PowerShell.Commands else { // Update successfully, we add the TypeData into cache - if (Context.RunspaceConfiguration != null) - { - Context.RunspaceConfiguration.Types.Append(new TypeConfigurationEntry(type, false)); - } - else if (Context.InitialSessionState != null) + if (Context.InitialSessionState != null) { Context.InitialSessionState.Types.Add(new SessionStateTypeEntry(type, false)); } else { - Dbg.Assert(false, "Either RunspaceConfiguration or InitialSessionState must be non-null for Update-Typedata to work"); + Dbg.Assert(false, "InitialSessionState must be non-null for Update-Typedata to work"); } } } @@ -757,38 +749,7 @@ namespace Microsoft.PowerShell.Commands // filename is available string target = UpdateDataStrings.UpdateTarget; - if (Context.RunspaceConfiguration != null) - { - for (int i = prependPathTotal.Count - 1; i >= 0; i--) - { - string formattedTarget = string.Format(CultureInfo.InvariantCulture, target, prependPathTotal[i]); - - if (ShouldProcess(formattedTarget, action)) - { - this.Context.RunspaceConfiguration.Types.Prepend(new TypeConfigurationEntry(prependPathTotal[i])); - } - } - - foreach (string appendPathTotalItem in appendPathTotal) - { - string formattedTarget = string.Format(CultureInfo.InvariantCulture, target, appendPathTotalItem); - - if (ShouldProcess(formattedTarget, action)) - { - this.Context.RunspaceConfiguration.Types.Append(new TypeConfigurationEntry(appendPathTotalItem)); - } - } - - try - { - this.Context.CurrentRunspace.RunspaceConfiguration.Types.Update(true); - } - catch (RuntimeException e) - { - this.WriteError(new ErrorRecord(e, "TypesXmlUpdateException", ErrorCategory.InvalidOperation, null)); - } - } - else if (Context.InitialSessionState != null) + if (Context.InitialSessionState != null) { // This hashSet is to detect if there are duplicate type files var fullFileNameHash = new HashSet(StringComparer.CurrentCultureIgnoreCase); @@ -887,7 +848,7 @@ namespace Microsoft.PowerShell.Commands } else { - Dbg.Assert(false, "Either RunspaceConfiguration or InitialSessionState must be non-null for Update-Typedata to work"); + Dbg.Assert(false, "InitialSessionState must be non-null for Update-Typedata to work"); } } @@ -931,38 +892,7 @@ namespace Microsoft.PowerShell.Commands // filename is available string target = UpdateDataStrings.UpdateTarget; - if (Context.RunspaceConfiguration != null) - { - for (int i = prependPathTotal.Count - 1; i >= 0; i--) - { - string formattedTarget = string.Format(CultureInfo.CurrentCulture, target, prependPathTotal[i]); - - if (ShouldProcess(formattedTarget, action)) - { - this.Context.RunspaceConfiguration.Formats.Prepend(new FormatConfigurationEntry(prependPathTotal[i])); - } - } - - foreach (string appendPathTotalItem in appendPathTotal) - { - string formattedTarget = string.Format(CultureInfo.CurrentCulture, target, appendPathTotalItem); - - if (ShouldProcess(formattedTarget, action)) - { - this.Context.RunspaceConfiguration.Formats.Append(new FormatConfigurationEntry(appendPathTotalItem)); - } - } - - try - { - this.Context.CurrentRunspace.RunspaceConfiguration.Formats.Update(true); - } - catch (RuntimeException e) - { - this.WriteError(new ErrorRecord(e, "FormatXmlUpdateException", ErrorCategory.InvalidOperation, null)); - } - } - else if (Context.InitialSessionState != null) + if (Context.InitialSessionState != null) { if (Context.InitialSessionState.DisableFormatUpdates) { @@ -1055,12 +985,11 @@ namespace Microsoft.PowerShell.Commands if (entries.Count > 0) { Context.FormatDBManager.UpdateDataBase(entries, this.Context.AuthorizationManager, this.Context.EngineHostInterface, false); - FormatAndTypeDataHelper.ThrowExceptionOnError( - "ErrorsUpdatingFormats", - null, - entries, - RunspaceConfigurationCategory.Formats); - } + FormatAndTypeDataHelper.ThrowExceptionOnError( "ErrorsUpdatingFormats", + null, + entries, + RunspaceConfigurationCategory.Formats); + } } catch (RuntimeException e) { @@ -1069,7 +998,7 @@ namespace Microsoft.PowerShell.Commands } else { - Dbg.Assert(false, "Either RunspaceConfiguration or InitialSessionState must be non-null for Update-FormatData to work"); + Dbg.Assert(false, "InitialSessionState must be non-null for Update-FormatData to work"); } } } @@ -1154,17 +1083,7 @@ namespace Microsoft.PowerShell.Commands Dictionary> fileToIndexMap = new Dictionary>(StringComparer.OrdinalIgnoreCase); List indicesToRemove = new List(); - if (Context.RunspaceConfiguration != null) - { - for (int index = 0; index < Context.RunspaceConfiguration.Types.Count; index++) - { - string fileName = Context.RunspaceConfiguration.Types[index].FileName; - if (fileName == null) { continue; } - - ConstructFileToIndexMap(fileName, index, fileToIndexMap); - } - } - else if (Context.InitialSessionState != null) + if (Context.InitialSessionState != null) { for (int index = 0; index < Context.InitialSessionState.Types.Count; index++) { @@ -1200,11 +1119,7 @@ namespace Microsoft.PowerShell.Commands indicesToRemove.Sort(); for (int i = indicesToRemove.Count - 1; i >= 0; i--) { - if (Context.RunspaceConfiguration != null) - { - Context.RunspaceConfiguration.Types.RemoveItem(indicesToRemove[i]); - } - else if (Context.InitialSessionState != null) + if (Context.InitialSessionState != null) { Context.InitialSessionState.Types.RemoveItem(indicesToRemove[i]); } @@ -1212,11 +1127,7 @@ namespace Microsoft.PowerShell.Commands try { - if (Context.RunspaceConfiguration != null) - { - Context.RunspaceConfiguration.Types.Update(); - } - else if (Context.InitialSessionState != null) + if (Context.InitialSessionState != null) { bool oldRefreshTypeFormatSetting = Context.InitialSessionState.RefreshTypeAndFormatSetting; try @@ -1279,17 +1190,13 @@ namespace Microsoft.PowerShell.Commands else { // Type is removed successfully, add it into the cache - if (Context.RunspaceConfiguration != null) - { - Context.RunspaceConfiguration.Types.Append(new TypeConfigurationEntry(type, true)); - } - else if (Context.InitialSessionState != null) + if (Context.InitialSessionState != null) { Context.InitialSessionState.Types.Add(new SessionStateTypeEntry(type, true)); } else { - Dbg.Assert(false, "Either RunspaceConfiguration or InitialSessionState must be non-null for Remove-Typedata to work"); + Dbg.Assert(false, "InitialSessionState must be non-null for Remove-Typedata to work"); } } } diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs index bc7e761372..2064dee6b3 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs @@ -66,10 +66,6 @@ namespace Microsoft.PowerShell /// /// /// - /// - /// Configuration information to use for creating runspace. - /// - /// /// /// Banner text to be displayed by ConsoleHost /// @@ -78,12 +74,6 @@ namespace Microsoft.PowerShell /// Help text for minishell. This is displayed on 'minishell -?'. /// /// - /// - /// - /// Warning occurred prior to this point, for example, a snap-in fails to load beforehand. - /// This string will be printed out. - /// - /// /// /// /// Command line parameters to pwsh.exe @@ -118,10 +108,8 @@ namespace Microsoft.PowerShell /// /// internal static int Start( - RunspaceConfiguration configuration, string bannerText, string helpText, - string preStartWarning, string[] args) { #if DEBUG @@ -178,7 +166,7 @@ namespace Microsoft.PowerShell HostException hostException = null; try { - s_theConsoleHost = ConsoleHost.CreateSingletonInstance(configuration); + s_theConsoleHost = ConsoleHost.CreateSingletonInstance(); } catch (HostException e) { @@ -257,11 +245,6 @@ namespace Microsoft.PowerShell s_theConsoleHost.BindBreakHandler(); PSHost.IsStdOutputRedirected = Console.IsOutputRedirected; - if (!string.IsNullOrEmpty(preStartWarning)) - { - s_theConsoleHost.UI.WriteWarningLine(preStartWarning); - } - // Send startup telemetry for ConsoleHost startup ApplicationInsightsTelemetry.SendPSCoreStartupTelemetry(); @@ -269,7 +252,7 @@ namespace Microsoft.PowerShell s_theConsoleHost.LoadPSReadline() ? "StartupProfileData-Interactive" : "StartupProfileData-NonInteractive"); - exitCode = s_theConsoleHost.Run(s_cpp, !string.IsNullOrEmpty(preStartWarning)); + exitCode = s_theConsoleHost.Run(s_cpp, false); } } finally @@ -415,10 +398,6 @@ namespace Microsoft.PowerShell lock (host.hostGlobalLock) { - if (host._isCtrlCDisabled) - { - return; - } bht = host._breakHandlerThread; if (!host.ShouldEndSession && shouldEndSession) { @@ -517,10 +496,10 @@ namespace Microsoft.PowerShell /// /// Create single instance of ConsoleHost. /// - internal static ConsoleHost CreateSingletonInstance(RunspaceConfiguration configuration) + internal static ConsoleHost CreateSingletonInstance() { Dbg.Assert(s_theConsoleHost == null, "CreateSingletonInstance should not be called multiple times"); - s_theConsoleHost = new ConsoleHost(configuration); + s_theConsoleHost = new ConsoleHost(); return s_theConsoleHost; } @@ -1080,7 +1059,7 @@ namespace Microsoft.PowerShell /// Constructs a new instance /// /// - internal ConsoleHost(RunspaceConfiguration configuration) + internal ConsoleHost() { #if !UNIX try @@ -1106,7 +1085,6 @@ namespace Microsoft.PowerShell InDebugMode = false; _displayDebuggerBanner = true; - _configuration = configuration; this.ui = new ConsoleHostUserInterface(this); _consoleWriter = new ConsoleTextWriter(ui); @@ -1590,10 +1568,7 @@ namespace Microsoft.PowerShell private void DoCreateRunspace(string initialCommand, bool skipProfiles, bool staMode, string configurationName, Collection initialCommandArgs) { Dbg.Assert(_runspaceRef == null, "runspace should be null"); -#if !DEBUG - Dbg.Assert(_configuration != null, "configuration should be set"); -#endif - + Dbg.Assert(DefaultInitialSessionState != null, "DefaultInitialSessionState should not be null"); s_runspaceInitTracer.WriteLine("Calling RunspaceFactory.CreateRunspace"); try @@ -1601,50 +1576,41 @@ namespace Microsoft.PowerShell Runspace consoleRunspace = null; bool psReadlineFailed = false; - // Use InitialSessionState if available. - if (DefaultInitialSessionState != null) + // Load PSReadline by default unless there is no use: + // - we're running a command/file and just exiting + // - stdin is redirected by a parent process + // - we're not interactive + // - we're explicitly reading from stdin (the '-' argument) + // It's also important to have a scenario where PSReadline is not loaded so it can be updated, e.g. + // powershell -command "Update-Module PSReadline" + // This should work just fine as long as no other instances of PowerShell are running. + ReadOnlyCollection defaultImportModulesList = null; + if (LoadPSReadline()) { - // Load PSReadline by default unless there is no use: - // - we're running a command/file and just exiting - // - stdin is redirected by a parent process - // - we're not interactive - // - we're explicitly reading from stdin (the '-' argument) - // It's also important to have a scenario where PSReadline is not loaded so it can be updated, e.g. - // powershell -command "Update-Module PSReadline" - // This should work just fine as long as no other instances of PowerShell are running. - ReadOnlyCollection defaultImportModulesList = null; - if (LoadPSReadline()) + // Create and open Runspace with PSReadline. + defaultImportModulesList = DefaultInitialSessionState.Modules; + DefaultInitialSessionState.ImportPSModule(new[] { "PSReadLine" }); + consoleRunspace = RunspaceFactory.CreateRunspace(this, DefaultInitialSessionState); + try { - // Create and open Runspace with PSReadline. - defaultImportModulesList = DefaultInitialSessionState.Modules; - DefaultInitialSessionState.ImportPSModule(new[] { "PSReadLine" }); - consoleRunspace = RunspaceFactory.CreateRunspace(this, DefaultInitialSessionState); - try - { - OpenConsoleRunspace(consoleRunspace, staMode); - } - catch (Exception) - { - consoleRunspace = null; - psReadlineFailed = true; - } - } - - if (consoleRunspace == null) - { - if (psReadlineFailed) - { - // Try again but without importing the PSReadline module. - DefaultInitialSessionState.ClearPSModules(); - DefaultInitialSessionState.ImportPSModule(defaultImportModulesList); - } - consoleRunspace = RunspaceFactory.CreateRunspace(this, DefaultInitialSessionState); OpenConsoleRunspace(consoleRunspace, staMode); } + catch (Exception) + { + consoleRunspace = null; + psReadlineFailed = true; + } } - else + + if (consoleRunspace == null) { - consoleRunspace = RunspaceFactory.CreateRunspace(this, _configuration); + if (psReadlineFailed) + { + // Try again but without importing the PSReadline module. + DefaultInitialSessionState.ClearPSModules(); + DefaultInitialSessionState.ImportPSModule(defaultImportModulesList); + } + consoleRunspace = RunspaceFactory.CreateRunspace(this, DefaultInitialSessionState); OpenConsoleRunspace(consoleRunspace, staMode); } @@ -1728,46 +1694,7 @@ namespace Microsoft.PowerShell } else { - // Run the built-in scripts - RunspaceConfigurationEntryCollection scripts = new RunspaceConfigurationEntryCollection(); - if (_configuration != null) - scripts = _configuration.InitializationScripts; - - if ((scripts == null) || (scripts.Count == 0)) - { - s_runspaceInitTracer.WriteLine("There are no built-in scripts to run"); - } - else - { - foreach (ScriptConfigurationEntry s in scripts) - { - s_runspaceInitTracer.WriteLine("Running script: '{0}'", s.Name); - - // spec claims that Ctrl-C is not supposed to stop these. - - try - { - _isCtrlCDisabled = true; - Exception e = InitializeRunspaceHelper(s.Definition, exec, Executor.ExecutionOptions.AddOutputter); - if (e != null) - { - throw new ConsoleHostStartupException(ConsoleHostStrings.InitScriptFailed, e); - } - } - finally - { - _isCtrlCDisabled = false; - } - } - } - - // If -iss has been specified, then there won't be a runspace - // configuration to get the shell ID from, so we'll use the default... - string shellId = null; - if (_configuration != null) - shellId = _configuration.ShellId; - else - shellId = "Microsoft.PowerShell"; // TODO: what will happen for custom shells built using Make-Shell.exe + string shellId = "Microsoft.PowerShell"; // If the system lockdown policy says "Enforce", do so. Do this after types / formatting, default functions, etc // are loaded so that they are trusted. (Validation of their signatures is done in F&O) @@ -2928,11 +2855,9 @@ namespace Microsoft.PowerShell private Version _ver = PSVersionInfo.PSVersion; private int _exitCodeFromRunspace; private bool _noExit = true; - private bool _isCtrlCDisabled; private bool _setShouldExitCalled; private bool _isRunningPromptLoop; private bool _wasInitialCommandEncoded; - private RunspaceConfiguration _configuration; // hostGlobalLock is used to sync public method calls (in case multiple threads call into the host) and access to // state that persists across method calls, like progress data. It's internal because the ui object also diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleShell.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleShell.cs index 801b3dbed5..064ff33d7a 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleShell.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleShell.cs @@ -21,55 +21,13 @@ namespace Microsoft.PowerShell /// Commandline parameters specified by user. /// An integer value which should be used as exit code for the process. public static int Start(string bannerText, string helpText, string[] args) - { - return Start(null, bannerText, helpText, null, args); - } - - /// - /// - /// Entry point in to ConsoleShell. This method is called from the native or managed exe host application - /// - /// - /// - /// Configuration information which is used to create Runspace. - /// - /// - /// - /// Banner text to be displayed by ConsoleHost - /// - /// - /// - /// Help text for minishell. This is displayed on 'minishell -?'. - /// - /// - /// - /// Warning occurred prior to this point, for example, a snap-in fails to load beforehand. - /// This string will be printed out. - /// - /// - /// - /// Commandline parameters specified by user. - /// - /// - /// - /// An integer value which should be used as exit code for the - /// process. - /// - - internal static - int - Start(RunspaceConfiguration configuration, - string bannerText, - string helpText, - string preStartWarning, - string[] args) { if (args == null) { throw PSTraceSource.NewArgumentNullException("args"); } - return ConsoleHost.Start(configuration, bannerText, helpText, preStartWarning, args); + return ConsoleHost.Start(bannerText, helpText, args); } } } diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ManagedEntrance.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ManagedEntrance.cs index 380fa7e0d4..4e78975644 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ManagedEntrance.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ManagedEntrance.cs @@ -23,7 +23,7 @@ namespace Microsoft.PowerShell /// Starts managed MSH /// /// - /// Console file used to create a runspace configuration to start MSH + /// Deprecated: Console file used to create a runspace configuration to start MSH /// /// /// Command line arguments to the managed MSH @@ -55,54 +55,26 @@ namespace Microsoft.PowerShell Thread.CurrentThread.CurrentUICulture = NativeCultureResolver.UICulture; Thread.CurrentThread.CurrentCulture = NativeCultureResolver.Culture; - RunspaceConfigForSingleShell configuration = null; - PSConsoleLoadException warning = null; - // PSSnapInException will cause the control to return back to the native code - // and stuff the EXCEPINFO field with the message of the exception. - // The native code will print this out and exit the process. - if (string.IsNullOrEmpty(consoleFilePath)) - { #if DEBUG -// Special switches for debug mode to allow self-hosting on InitialSessionState instead -// of runspace configuration... - if (args.Length > 0 && !String.IsNullOrEmpty(args[0]) && args[0].Equals("-iss", StringComparison.OrdinalIgnoreCase)) - { - ConsoleHost.DefaultInitialSessionState = InitialSessionState.CreateDefault2(); - configuration = null; - } - else if (args.Length > 0 && !String.IsNullOrEmpty(args[0]) && args[0].Equals("-isswait", StringComparison.OrdinalIgnoreCase)) - { - Console.WriteLine("Attach the debugger and hit enter to continue:"); - Console.ReadLine(); - ConsoleHost.DefaultInitialSessionState = InitialSessionState.CreateDefault2(); - configuration = null; - } - else - { - ConsoleHost.DefaultInitialSessionState = InitialSessionState.CreateDefault2(); - configuration = null; - } -#else - ConsoleHost.DefaultInitialSessionState = InitialSessionState.CreateDefault2(); - configuration = null; -#endif - } - else + if (args.Length > 0 && !String.IsNullOrEmpty(args[0]) && args[0].Equals("-isswait", StringComparison.OrdinalIgnoreCase)) { - //TODO : Deprecate RunspaceConfiguration and use InitialSessionState - configuration = - RunspaceConfigForSingleShell.Create(consoleFilePath, out warning); + Console.WriteLine("Attach the debugger to continue..."); + while (!System.Diagnostics.Debugger.IsAttached) { + Thread.Sleep(100); + } + System.Diagnostics.Debugger.Break(); } +#endif + ConsoleHost.DefaultInitialSessionState = InitialSessionState.CreateDefault2(); + int exitCode = 0; try { var banner = ManagedEntranceStrings.ShellBannerNonWindowsPowerShell; var formattedBanner = string.Format(CultureInfo.InvariantCulture, banner, PSVersionInfo.GitCommitId); exitCode = Microsoft.PowerShell.ConsoleShell.Start( - configuration, formattedBanner, ManagedEntranceStrings.ShellHelp, - warning == null ? null : warning.Message, args); } catch (System.Management.Automation.Host.HostException e) diff --git a/src/System.Management.Automation/engine/AutomationEngine.cs b/src/System.Management.Automation/engine/AutomationEngine.cs index fde88bea9a..a088121d03 100644 --- a/src/System.Management.Automation/engine/AutomationEngine.cs +++ b/src/System.Management.Automation/engine/AutomationEngine.cs @@ -37,60 +37,15 @@ namespace System.Management.Automation /// an instance of the automation engine. It allows you to pass in an /// instance of PSHost that provides the host-specific I/O routines, etc. /// - internal AutomationEngine(PSHost hostInterface, RunspaceConfiguration runspaceConfiguration, InitialSessionState iss) + internal AutomationEngine(PSHost hostInterface, InitialSessionState iss) { -#if !CORECLR// There is no control panel items in CSS - // Update the env variable PathEXT to contain .CPL - var pathext = Environment.GetEnvironmentVariable("PathEXT"); - pathext = pathext ?? string.Empty; - bool cplExist = false; - if (pathext != string.Empty) - { - string[] entries = pathext.Split(Utils.Separators.Semicolon); - foreach (string entry in entries) - { - string ext = entry.Trim(); - if (ext.Equals(".CPL", StringComparison.OrdinalIgnoreCase)) - { - cplExist = true; - break; - } - } - } - if (!cplExist) - { - pathext = (pathext == string.Empty) ? ".CPL" : - pathext.EndsWith(";", StringComparison.OrdinalIgnoreCase) - ? (pathext + ".CPL") : (pathext + ";.CPL"); - Environment.SetEnvironmentVariable("PathEXT", pathext); - } -#endif - if (runspaceConfiguration != null) - { - Context = new ExecutionContext(this, hostInterface, runspaceConfiguration); - } - else - { - Context = new ExecutionContext(this, hostInterface, iss); - } + Context = new ExecutionContext(this, hostInterface, iss); EngineParser = new Language.Parser(); CommandDiscovery = new CommandDiscovery(Context); - // Initialize providers before loading types so that any ScriptBlocks in the - // types.ps1xml file can be parsed. - - // Bind the execution context with RunspaceConfiguration. - // This has the side effect of initializing cmdlet cache and providers from runspace configuration. - if (runspaceConfiguration != null) - { - runspaceConfiguration.Bind(Context); - } - else - { - // Load the iss, resetting everything to it's defaults... - iss.Bind(Context, /*updateOnly*/ false); - } + // Load the iss, resetting everything to it's defaults... + iss.Bind(Context, /*updateOnly*/ false); InitialSessionState.SetSessionStateDrive(Context, true); } diff --git a/src/System.Management.Automation/engine/CommandDiscovery.cs b/src/System.Management.Automation/engine/CommandDiscovery.cs index 5ee174bc14..d8d4a1c8fd 100644 --- a/src/System.Management.Automation/engine/CommandDiscovery.cs +++ b/src/System.Management.Automation/engine/CommandDiscovery.cs @@ -138,13 +138,6 @@ namespace System.Management.Automation Context = context; discoveryTracer.ShowHeaders = false; - - // Cache the ScriptInfo for the scripts defined in the RunspaceConfiguration - - _cachedScriptInfo = - new Dictionary(StringComparer.OrdinalIgnoreCase); - - LoadScriptInfo(); } private void AddCmdletToCache(CmdletConfigurationEntry entry) @@ -274,28 +267,6 @@ namespace System.Management.Automation } } - private void LoadScriptInfo() - { - if (Context.RunspaceConfiguration != null) - { - foreach (ScriptConfigurationEntry entry in Context.RunspaceConfiguration.Scripts) - { - try - { - _cachedScriptInfo.Add(entry.Name, new ScriptInfo(entry.Name, ScriptBlock.Create(Context, entry.Definition), Context)); - } - catch (ArgumentException) - { - PSNotSupportedException notSupported = - PSTraceSource.NewNotSupportedException( - DiscoveryExceptions.DuplicateScriptName, - entry.Name); - - throw notSupported; - } - } - } - } #endregion ctor #region internal methods @@ -358,56 +329,6 @@ namespace System.Management.Automation return processor; } - //Minishell ExternalScriptInfo scriptInfo - - internal CommandProcessorBase CreateScriptProcessorForMiniShell(ExternalScriptInfo scriptInfo, bool useLocalScope, SessionStateInternal sessionState) - { - VerifyScriptRequirements(scriptInfo, Context); - - if (String.IsNullOrEmpty(scriptInfo.RequiresApplicationID)) - { - if (scriptInfo.RequiresPSSnapIns != null && scriptInfo.RequiresPSSnapIns.Any()) - { - Collection requiresMissingPSSnapIns = GetPSSnapinNames(scriptInfo.RequiresPSSnapIns); - - ScriptRequiresException scriptRequiresException = - new ScriptRequiresException( - scriptInfo.Name, - requiresMissingPSSnapIns, - "ScriptRequiresMissingPSSnapIns", - true); - throw scriptRequiresException; - } - - return CreateCommandProcessorForScript(scriptInfo, Context, useLocalScope, sessionState); - } - else - { - if (String.Equals( - Context.ShellID, - scriptInfo.RequiresApplicationID, - StringComparison.OrdinalIgnoreCase)) - { - return CreateCommandProcessorForScript(scriptInfo, Context, useLocalScope, sessionState); - } - else - { - // Throw a runtime exception - - string shellPath = GetShellPathFromRegistry(scriptInfo.RequiresApplicationID); - - ScriptRequiresException sre = - new ScriptRequiresException( - scriptInfo.Name, - scriptInfo.RequiresApplicationID, - shellPath, - "ScriptRequiresUnmatchedShellId"); - - throw sre; - } - } - } - internal static void VerifyRequiredModules(ExternalScriptInfo scriptInfo, ExecutionContext context) { // Check Required Modules @@ -492,33 +413,12 @@ namespace System.Management.Automation private static void VerifyRequiredSnapins(IEnumerable requiresPSSnapIns, ExecutionContext context, out Collection requiresMissingPSSnapIns) { requiresMissingPSSnapIns = null; - bool isHostedWithInitialSessionState = false; - RunspaceConfigForSingleShell rs = null; - if (context.InitialSessionState != null) - { - isHostedWithInitialSessionState = true; - } - else if (context.RunspaceConfiguration != null) - { - rs = context.RunspaceConfiguration as RunspaceConfigForSingleShell; - Dbg.Assert(rs != null, "RunspaceConfiguration should not be null"); - } - else - { - Dbg.Assert(false, "PowerShell should be hosted with either InitialSessionState or RunspaceConfiguration"); - } + Dbg.Assert(context.InitialSessionState != null, "PowerShell should be hosted with InitialSessionState"); foreach (var requiresPSSnapIn in requiresPSSnapIns) { IEnumerable loadedPSSnapIns = null; - if (isHostedWithInitialSessionState) - { - loadedPSSnapIns = context.InitialSessionState.GetPSSnapIn(requiresPSSnapIn.Name); - } - else - { - loadedPSSnapIns = rs.ConsoleInfo.GetPSSnapIn(requiresPSSnapIn.Name, false); - } + loadedPSSnapIns = context.InitialSessionState.GetPSSnapIn(requiresPSSnapIn.Name); if (loadedPSSnapIns == null || loadedPSSnapIns.Count() == 0) { if (requiresMissingPSSnapIns == null) @@ -758,16 +658,7 @@ namespace System.Management.Automation scriptInfo.SignatureChecked = true; try { - if (!Context.IsSingleShell) - { - // in minishell mode - processor = CreateScriptProcessorForMiniShell(scriptInfo, useLocalScope ?? true, sessionState); - } - else - { - // single shell mode - processor = CreateScriptProcessorForSingleShell(scriptInfo, Context, useLocalScope ?? true, sessionState); - } + processor = CreateScriptProcessorForSingleShell(scriptInfo, Context, useLocalScope ?? true, sessionState); } catch (ScriptRequiresSyntaxException reqSyntaxException) { @@ -1724,51 +1615,6 @@ namespace System.Management.Automation } } // GetCmdletInfo - private bool _cmdletCacheInitialized = false; - - /// - /// Called by the RunspaceConfiguration when a PSSnapIn gets added to the - /// console to update the list of available cmdlets. - /// - /// - internal void UpdateCmdletCache() - { - if (!_cmdletCacheInitialized) - { - foreach (CmdletConfigurationEntry entry in Context.RunspaceConfiguration.Cmdlets) - { - AddCmdletToCache(entry); - } - - _cmdletCacheInitialized = true; - - return; - } - - foreach (CmdletConfigurationEntry entry in Context.RunspaceConfiguration.Cmdlets.UpdateList) - { - if (entry == null) - { - continue; - } - - switch (entry.Action) - { - case UpdateAction.Add: - AddCmdletToCache(entry); - break; - - case UpdateAction.Remove: - RemoveCmdletFromCache(entry); - break; - - default: - break; - } - } - } // UpdateCmdletCache - - /// /// Removes a cmdlet from the cmdlet cache. /// @@ -1814,46 +1660,6 @@ namespace System.Management.Automation return removalIndex; } - - /// - /// Gets the cached ScriptInfo for a command using the script name. - /// - /// - /// - /// The name of the script. - /// - /// - /// - /// A reference to the ScriptInfo for the command if its in the cache, - /// or null otherwise. - /// - /// - internal ScriptInfo GetScriptInfo(string name) - { - Dbg.Assert( - !String.IsNullOrEmpty(name), - "The caller should verify the name"); - - ScriptInfo result; - _cachedScriptInfo.TryGetValue(name, out result); - return result; - } // GetScriptInfo - - /// - /// Gets the script cache - /// - /// - internal Dictionary ScriptCache - { - get { return _cachedScriptInfo; } - } - - /// - /// The cache for the ScriptInfo. - /// - /// - private Dictionary _cachedScriptInfo; - internal ExecutionContext Context { get; } /// diff --git a/src/System.Management.Automation/engine/CommandSearcher.cs b/src/System.Management.Automation/engine/CommandSearcher.cs index 65ac77eb57..05fcb27255 100644 --- a/src/System.Management.Automation/engine/CommandSearcher.cs +++ b/src/System.Management.Automation/engine/CommandSearcher.cs @@ -132,18 +132,6 @@ namespace System.Management.Automation return true; } - // Advance the state - _currentState = SearchState.SearchingBuiltinScripts; - } - - if (_currentState == SearchState.SearchingBuiltinScripts) - { - _currentMatch = SearchForBuiltinScripts(); - if (_currentMatch != null) - { - return true; - } - // Advance the state _currentState = SearchState.StartSearchingForExternalCommands; } @@ -301,18 +289,6 @@ namespace System.Management.Automation return currentMatch; } - private CommandInfo SearchForBuiltinScripts() - { - CommandInfo currentMatch = null; - - if ((_commandTypes & CommandTypes.Script) != 0) - { - currentMatch = GetNextBuiltinScript(); - } - - return currentMatch; - } - private CommandInfo ProcessBuiltinScriptState() { CommandInfo currentMatch = null; @@ -1043,7 +1019,7 @@ namespace System.Management.Automation if (!_matchingCmdlet.MoveNext()) { // Advance the state - _currentState = SearchState.SearchingBuiltinScripts; + _currentState = SearchState.StartSearchingForExternalCommands; _matchingCmdlet = null; } @@ -1068,86 +1044,6 @@ namespace System.Management.Automation return result; } - /// - /// Gets the next builtin script from the collection of matching scripts. - /// If the collection doesn't exist yet it is created and the - /// enumerator is moved to the first item in the collection. - /// - /// - /// - /// A ScriptInfo for the next matching script or null if there are - /// no more matches. - /// - /// - private ScriptInfo GetNextBuiltinScript() - { - ScriptInfo result = null; - - if ((_commandResolutionOptions & SearchResolutionOptions.CommandNameIsPattern) != 0) - { - if (_matchingScript == null) - { - // Generate the enumerator of matching script names - - Collection matchingScripts = - new Collection(); - - WildcardPattern scriptMatcher = - WildcardPattern.Get( - _commandName, - WildcardOptions.IgnoreCase); - - WildcardPattern scriptExtensionMatcher = - WildcardPattern.Get( - _commandName + StringLiterals.PowerShellScriptFileExtension, - WildcardOptions.IgnoreCase); - - // Get the script cache enumerator. This acquires the cache lock, - // so be sure to dispose. - - foreach (string scriptName in _context.CommandDiscovery.ScriptCache.Keys) - { - if (scriptMatcher.IsMatch(scriptName) || - scriptExtensionMatcher.IsMatch(scriptName)) - { - matchingScripts.Add(scriptName); - } - } - _matchingScript = matchingScripts.GetEnumerator(); - } - - if (!_matchingScript.MoveNext()) - { - // Advance the state - _currentState = SearchState.StartSearchingForExternalCommands; - - _matchingScript = null; - } - else - { - result = _context.CommandDiscovery.GetScriptInfo(_matchingScript.Current); - } - } - else - { - // Advance the state - _currentState = SearchState.StartSearchingForExternalCommands; - - result = _context.CommandDiscovery.GetScriptInfo(_commandName) ?? - _context.CommandDiscovery.GetScriptInfo(_commandName + StringLiterals.PowerShellScriptFileExtension); - } - - if (result != null) - { - CommandDiscovery.discoveryTracer.WriteLine( - "Script found: {0}", - result.Name); - } - return result; - } - private IEnumerator _matchingScript; - - private string DoPowerShellRelativePathLookup() { string result = null; @@ -1632,7 +1528,6 @@ namespace System.Management.Automation _currentState = SearchState.SearchingAliases; _matchingAlias = null; _matchingCmdlet = null; - _matchingScript = null; } // Reset internal CommandOrigin CommandOrigin @@ -1676,9 +1571,6 @@ namespace System.Management.Automation // the searcher has finished function resolution and is now searching for cmdlets SearchingCmdlets, - // the search has finished cmdlet resolution and is now searching for builtin scripts - SearchingBuiltinScripts, - // the search has finished builtin script resolution and is now searching for external commands StartSearchingForExternalCommands, diff --git a/src/System.Management.Automation/engine/ExecutionContext.cs b/src/System.Management.Automation/engine/ExecutionContext.cs index 72ea7fcb6d..f39fdbb7ee 100644 --- a/src/System.Management.Automation/engine/ExecutionContext.cs +++ b/src/System.Management.Automation/engine/ExecutionContext.cs @@ -169,26 +169,8 @@ namespace System.Management.Automation /// internal AutomationEngine Engine { get; private set; } - /// - /// Get the RunspaceConfiguration instance - /// - internal RunspaceConfiguration RunspaceConfiguration { get; } - internal InitialSessionState InitialSessionState { get; } - /// - /// True if the RunspaceConfiguration/InitialSessionState is for a single shell or false otherwise. - /// - /// - internal bool IsSingleShell - { - get - { - RunspaceConfigForSingleShell runSpace = RunspaceConfiguration as RunspaceConfigForSingleShell; - return runSpace != null || InitialSessionState != null; - } - } - /// /// Added for Win8: 336382 /// Contains the name of the previous module that was processed. This @@ -259,14 +241,7 @@ namespace System.Management.Automation { if (_providerNames == null) { - if (IsSingleShell) - { - _providerNames = new SingleShellProviderNames(); - } - else - { - _providerNames = new CustomShellProviderNames(); - } + _providerNames = new SingleShellProviderNames(); } return _providerNames; } @@ -293,11 +268,6 @@ namespace System.Management.Automation { _shellId = AuthorizationManager.ShellId; } - else if (RunspaceConfiguration != null && !String.IsNullOrEmpty(RunspaceConfiguration.ShellId)) - { - // Otherwise fall back to the runspace shell id if it's there... - _shellId = RunspaceConfiguration.ShellId; - } else { // Finally fall back to the default shell id... @@ -1090,13 +1060,8 @@ namespace System.Management.Automation internal void RunspaceClosingNotification() { - if (this.RunspaceConfiguration != null) - { - this.RunspaceConfiguration.Unbind(this); - } - EngineSessionState.RunspaceClosingNotification(); - + if (_debugger != null) { _debugger.Dispose(); @@ -1114,10 +1079,7 @@ namespace System.Management.Automation } /// - /// Gets the type table instance for this engine. This is somewhat - /// complicated by the need to have a single type table in RunspaceConfig - /// shared across all bound runspaces, as well as individual tables for - /// instances created from InitialSessionState. + /// Gets the type table instance for this engine. /// internal TypeTable TypeTable { @@ -1125,22 +1087,15 @@ namespace System.Management.Automation { if (_typeTable == null) { - // Always use the type table from the RunspaceConfig if there is one, otherwise create a default one - _typeTable = (this.RunspaceConfiguration != null && RunspaceConfiguration.TypeTable != null) - ? RunspaceConfiguration.TypeTable - : new TypeTable(); + _typeTable = new TypeTable(); _typeTableWeakReference = new WeakReference(_typeTable); } return _typeTable; } - // This needs to exist so that RunspaceConfiguration can - // push it's shared type table into ExecutionContext set { - if (this.RunspaceConfiguration != null) - throw new NotImplementedException("set_TypeTable()"); _typeTable = value; - _typeTableWeakReference = value != null ? new WeakReference(value) : null; + _typeTableWeakReference = (value != null) ? new WeakReference(value) : null; } } @@ -1163,21 +1118,12 @@ namespace System.Management.Automation private WeakReference _typeTableWeakReference; /// - /// Gets the format info database for this engine. This is significantly - /// complicated by the need to have a single type table in RunspaceConfig - /// shared across all bound runspaces, as well as individual tables for - /// instances created from InitialSessionState. + /// Gets the format info database for this engine. /// internal TypeInfoDataBaseManager FormatDBManager { get { - // Use the format DB from the RunspaceConfig if there is one. - if (this.RunspaceConfiguration != null && RunspaceConfiguration.FormatDBManager != null) - { - return RunspaceConfiguration.FormatDBManager; - } - if (_formatDBManager == null) { // If no Formatter database has been created, then @@ -1193,13 +1139,9 @@ namespace System.Management.Automation } return _formatDBManager; } - - // This needs to exist so that RunspaceConfiguration can - // push it's shared format database table into ExecutionContext + set { - if (this.RunspaceConfiguration != null) - throw new NotImplementedException("set_FormatDBManager()"); _formatDBManager = value; } } @@ -1218,69 +1160,6 @@ namespace System.Management.Automation } internal PSTransactionManager transactionManager; - - private bool _assemblyCacheInitialized = false; - - /// - /// This function is called by RunspaceConfiguration.Assemblies.Update call back. - /// It's not used when constructing a runspace from an InitialSessionState object. - /// - internal void UpdateAssemblyCache() - { - string errors = ""; - - if (this.RunspaceConfiguration != null) - { - if (!_assemblyCacheInitialized) - { - foreach (AssemblyConfigurationEntry entry in this.RunspaceConfiguration.Assemblies) - { - Exception error = null; - AddAssembly(entry.Name, entry.FileName, out error); - - if (error != null) - { - errors += "\n" + error.Message; - } - } - - _assemblyCacheInitialized = true; - } - else - { - foreach (AssemblyConfigurationEntry entry in this.RunspaceConfiguration.Assemblies.UpdateList) - { - switch (entry.Action) - { - case UpdateAction.Add: - Exception error = null; - AddAssembly(entry.Name, entry.FileName, out error); - - if (error != null) - { - errors += "\n" + error.Message; - } - - break; - - case UpdateAction.Remove: - RemoveAssembly(entry.Name); - break; - - default: - break; - } - } - } - - if (!String.IsNullOrEmpty(errors)) - { - string message = StringUtil.Format(MiniShellErrors.UpdateAssemblyErrors, errors); - throw new RuntimeException(message); - } - } - } - internal Assembly AddAssembly(string name, string filename, out Exception error) { Assembly loadedAssembly = LoadAssembly(name, filename, out error); @@ -1537,27 +1416,6 @@ namespace System.Management.Automation return result; } - /// - /// Constructs an Execution context object for Automation Engine - /// - /// - /// - /// Engine that hosts this execution context - /// - /// - /// Interface that should be used for interaction with host - /// - /// - /// RunspaceConfiguration information - /// - internal ExecutionContext(AutomationEngine engine, PSHost hostInterface, RunspaceConfiguration runspaceConfiguration) - { - RunspaceConfiguration = runspaceConfiguration; - AuthorizationManager = runspaceConfiguration.AuthorizationManager; - - InitializeCommon(engine, hostInterface); - } - /// /// Constructs an Execution context object for Automation Engine /// diff --git a/src/System.Management.Automation/engine/InitialSessionState.cs b/src/System.Management.Automation/engine/InitialSessionState.cs index e20aa7a30c..1576ce2b74 100644 --- a/src/System.Management.Automation/engine/InitialSessionState.cs +++ b/src/System.Management.Automation/engine/InitialSessionState.cs @@ -1600,6 +1600,7 @@ namespace System.Management.Automation.Runspaces /// /// Creates the default PowerShell one with default cmdlets, provider etc. /// The default cmdlets, provider, etc are loaded via Modules + /// For loading Microsoft.PowerShell.Core module only. /// /// public static InitialSessionState CreateDefault2() @@ -3704,7 +3705,6 @@ namespace System.Management.Automation.Runspaces return coreSnapin; } - // WARNING: THIS CODE IS COMPLETELY DUPLICATED IN RunspaceConfigForSingleShell internal PSSnapInInfo ImportPSSnapIn(PSSnapInInfo psSnapInInfo, out PSSnapInException warning) { // See if the snapin is already loaded. If has been then there will be an entry in the @@ -4766,10 +4766,6 @@ end "Clear-Host", "", ScopedItemOptions.AllScope), //#if !CORECLR is used to disable aliases for cmdlets which are not available on OneCore #if !CORECLR - new SessionStateAliasEntry("asnp", - "Add-PSSnapIn", "", ScopedItemOptions.ReadOnly | ScopedItemOptions.AllScope), - new SessionStateAliasEntry("gsnp", - "Get-PSSnapIn", "", ScopedItemOptions.ReadOnly | ScopedItemOptions.AllScope), new SessionStateAliasEntry("gwmi", "Get-WmiObject", "", ScopedItemOptions.ReadOnly | ScopedItemOptions.AllScope), new SessionStateAliasEntry("iwmi", @@ -5048,22 +5044,6 @@ if($paths) { return result; } - - internal void SaveAsConsoleFile(string path) - { - if (null == path) - { - throw PSTraceSource.NewArgumentNullException("path"); - } - - if (!path.EndsWith(StringLiterals.PowerShellConsoleFileExtension, StringComparison.OrdinalIgnoreCase)) - { - throw PSTraceSource.NewArgumentException("path", ConsoleInfoErrorStrings.BadConsoleExtension); - } - - //ConsoleFileElement will write to file - PSConsoleFileElement.WriteToFile(path, PSVersionInfo.PSVersion, this.ImportedSnapins.Values); - } } /// @@ -5083,7 +5063,6 @@ if($paths) { try { - // WARNING: DUPLICATE CODE see RunspaceConfigForSingleShell assembly = Assembly.Load(new AssemblyName(psSnapInInfo.AssemblyName)); } catch (BadImageFormatException e) diff --git a/src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs b/src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs index 1269b141a3..896ef6bda0 100644 --- a/src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs +++ b/src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs @@ -2150,8 +2150,6 @@ namespace Microsoft.PowerShell.Commands // Indicates the ISS.Bind() should be called... bool doBind = false; - // Indicates that RunspaceConfig.Update() should be called. - bool doUpdate = false; // Set up to load any required assemblies that have been specified... List tmpAssemblyList; @@ -2233,36 +2231,28 @@ namespace Microsoft.PowerShell.Commands string loadMessage = StringUtil.Format(Modules.LoadingFile, "TypesToProcess", fileName); WriteVerbose(loadMessage); - if (Context.RunspaceConfiguration != null) + bool isAlreadyLoaded = false; + string resolvedFileName = ResolveRootedFilePath(fileName, Context) ?? fileName; + foreach (var entry in Context.InitialSessionState.Types) { - this.Context.RunspaceConfiguration.Types.Append(new TypeConfigurationEntry(fileName)); - doUpdate = true; + if (entry.FileName == null) + { + continue; + } + + string resolvedEntryFileName = ResolveRootedFilePath(entry.FileName, Context) ?? + entry.FileName; + if (resolvedEntryFileName.Equals(resolvedFileName, StringComparison.OrdinalIgnoreCase)) + { + isAlreadyLoaded = true; + break; + } } - else + + if (!isAlreadyLoaded) { - bool isAlreadyLoaded = false; - string resolvedFileName = ResolveRootedFilePath(fileName, Context) ?? fileName; - foreach (var entry in Context.InitialSessionState.Types) - { - if (entry.FileName == null) - { - continue; - } - - string resolvedEntryFileName = ResolveRootedFilePath(entry.FileName, Context) ?? - entry.FileName; - if (resolvedEntryFileName.Equals(resolvedFileName, StringComparison.OrdinalIgnoreCase)) - { - isAlreadyLoaded = true; - break; - } - } - - if (!isAlreadyLoaded) - { - iss.Types.Add(new SessionStateTypeEntry(fileName)); - doBind = true; - } + iss.Types.Add(new SessionStateTypeEntry(fileName)); + doBind = true; } } } @@ -2286,32 +2276,24 @@ namespace Microsoft.PowerShell.Commands string loadMessage = StringUtil.Format(Modules.LoadingFile, "FormatsToProcess", fileName); WriteVerbose(loadMessage); - if (Context.RunspaceConfiguration != null) + bool isAlreadyLoaded = false; + foreach (var entry in Context.InitialSessionState.Formats) { - this.Context.RunspaceConfiguration.Formats.Append(new FormatConfigurationEntry(fileName)); - doUpdate = true; + if (entry.FileName == null) + { + continue; + } + if (entry.FileName.Equals(fileName, StringComparison.OrdinalIgnoreCase)) + { + isAlreadyLoaded = true; + break; + } } - else - { - bool isAlreadyLoaded = false; - foreach (var entry in Context.InitialSessionState.Formats) - { - if (entry.FileName == null) - { - continue; - } - if (entry.FileName.Equals(fileName, StringComparison.OrdinalIgnoreCase)) - { - isAlreadyLoaded = true; - break; - } - } - if (!isAlreadyLoaded) - { - iss.Formats.Add(new SessionStateFormatEntry(fileName)); - doBind = true; - } + if (!isAlreadyLoaded) + { + iss.Formats.Add(new SessionStateFormatEntry(fileName)); + doBind = true; } } } @@ -2422,30 +2404,6 @@ namespace Microsoft.PowerShell.Commands } } } - - // Do the runspaceconfig binding... - if (doUpdate) - { - try - { - this.Context.CurrentRunspace.RunspaceConfiguration.Types.Update(true); - this.Context.CurrentRunspace.RunspaceConfiguration.Formats.Update(true); - } - catch (Exception e) - { - this.RemoveTypesAndFormatting(exportedFormatFiles, exportedTypeFiles); - ErrorRecord er = new ErrorRecord(e, "FormatXmlUpdateException", ErrorCategory.InvalidOperation, - null); - if (bailOnFirstError) - { - this.ThrowTerminatingError(er); - } - else if (writingErrors) - { - this.WriteError(er); - } - } - } } // Create a PSModuleInfo for psd1 file or take one from ModuleToProcess manifest field @@ -4039,7 +3997,7 @@ namespace Microsoft.PowerShell.Commands } } - // If the RequiredModule is one of the Engine modules, then they could have been loaded as snapins (using RunspaceConfiguration or InitialSessionState.CreateDefault()) + // If the RequiredModule is one of the Engine modules, then they could have been loaded as snapins (using InitialSessionState.CreateDefault()) if (result == null && InitialSessionState.IsEngineModule(requiredModule.Name)) { result = ModuleCmdletBase.GetEngineSnapIn(context, requiredModule.Name); @@ -5082,68 +5040,18 @@ namespace Microsoft.PowerShell.Commands { try { - if (this.Context.InitialSessionState != null) + if (((formatFilesToRemove != null) && formatFilesToRemove.Count > 0) || + ((typeFilesToRemove != null) && typeFilesToRemove.Count > 0)) { - if (((formatFilesToRemove != null) && formatFilesToRemove.Count > 0) || - ((typeFilesToRemove != null) && typeFilesToRemove.Count > 0)) + bool oldRefreshTypeFormatSetting = this.Context.InitialSessionState.RefreshTypeAndFormatSetting; + try { - bool oldRefreshTypeFormatSetting = this.Context.InitialSessionState.RefreshTypeAndFormatSetting; - try - { - this.Context.InitialSessionState.RefreshTypeAndFormatSetting = true; - InitialSessionState.RemoveTypesAndFormats(this.Context, formatFilesToRemove, typeFilesToRemove); - } - finally - { - this.Context.InitialSessionState.RefreshTypeAndFormatSetting = oldRefreshTypeFormatSetting; - } + this.Context.InitialSessionState.RefreshTypeAndFormatSetting = true; + InitialSessionState.RemoveTypesAndFormats(this.Context, formatFilesToRemove, typeFilesToRemove); } - } - else // RunspaceConfiguration - { - if (formatFilesToRemove != null && formatFilesToRemove.Count > 0) + finally { - HashSet formatRemoveSet = new HashSet(formatFilesToRemove, StringComparer.OrdinalIgnoreCase); - List formatIndicesToRemove = new List(); - - for (int index = 0; index < this.Context.RunspaceConfiguration.Formats.Count; index++) - { - string fileName = Context.RunspaceConfiguration.Formats[index].FileName; - if (fileName != null && formatRemoveSet.Contains(fileName)) - { - formatIndicesToRemove.Add(index); - } - } - - for (int i = formatIndicesToRemove.Count - 1; i >= 0; i--) - { - Context.RunspaceConfiguration.Formats.RemoveItem(formatIndicesToRemove[i]); - } - - this.Context.RunspaceConfiguration.Formats.Update(); - } - - // Remove the imported types.ps1xml files - if (typeFilesToRemove != null && typeFilesToRemove.Count > 0) - { - HashSet typeRemoveSet = new HashSet(typeFilesToRemove, StringComparer.OrdinalIgnoreCase); - List typeIndicesToRemove = new List(); - - for (int index = 0; index < this.Context.RunspaceConfiguration.Types.Count; index++) - { - string fileName = Context.RunspaceConfiguration.Types[index].FileName; - if (fileName != null && typeRemoveSet.Contains(fileName)) - { - typeIndicesToRemove.Add(index); - } - } - - for (int i = typeIndicesToRemove.Count - 1; i >= 0; i--) - { - Context.RunspaceConfiguration.Types.RemoveItem(typeIndicesToRemove[i]); - } - - this.Context.RunspaceConfiguration.Types.Update(); + this.Context.InitialSessionState.RefreshTypeAndFormatSetting = oldRefreshTypeFormatSetting; } } } diff --git a/src/System.Management.Automation/engine/ProviderNames.cs b/src/System.Management.Automation/engine/ProviderNames.cs index c78aaf5648..56bf1d65fd 100644 --- a/src/System.Management.Automation/engine/ProviderNames.cs +++ b/src/System.Management.Automation/engine/ProviderNames.cs @@ -49,89 +49,6 @@ namespace System.Management.Automation internal abstract string Registry { get; } } - /// - /// The provider names for custom shells - /// - internal class CustomShellProviderNames : ProviderNames - { - /// - /// Gets the name of the EnvironmentProvider - /// - internal override string Environment - { - get - { - return "Environment"; - } - } - - /// - /// Gets the name of the Certificate - /// - internal override string Certificate - { - get - { - return "Certificate"; - } - } - - /// - /// Gets the name of the VariableProvider - /// - internal override string Variable - { - get - { - return "Variable"; - } - } - - /// - /// Gets the name of the AliasProvider - /// - internal override string Alias - { - get - { - return "Alias"; - } - } - - /// - /// Gets the name of the FunctionProvider - /// - internal override string Function - { - get - { - return "Function"; - } - } - - /// - /// Gets the name of the FileSystemProvider - /// - internal override string FileSystem - { - get - { - return "FileSystem"; - } - } - - /// - /// Gets the name of the RegistryProvider - /// - internal override string Registry - { - get - { - return "Registry"; - } - } - } - /// /// The provider names for the single shell /// diff --git a/src/System.Management.Automation/minishell/api/RunspaceConfigurationEntry.cs b/src/System.Management.Automation/engine/RunspaceConfigurationEntry.cs similarity index 95% rename from src/System.Management.Automation/minishell/api/RunspaceConfigurationEntry.cs rename to src/System.Management.Automation/engine/RunspaceConfigurationEntry.cs index b0e8a80d70..97932f2465 100644 --- a/src/System.Management.Automation/minishell/api/RunspaceConfigurationEntry.cs +++ b/src/System.Management.Automation/engine/RunspaceConfigurationEntry.cs @@ -7,6 +7,47 @@ Copyright (c) Microsoft Corporation. All rights reserved. namespace System.Management.Automation.Runspaces { + /// + /// Enum for describing different kind information that can be configured in runspace configuration. + /// + internal enum RunspaceConfigurationCategory + { + /// + /// Cmdlets + /// + Cmdlets, + + /// + /// Providers + /// + Providers, + + /// + /// Assemblies + /// + Assemblies, + + /// + /// Scripts + /// + Scripts, + + /// + /// Initialization scripts + /// + InitializationScripts, + + /// + /// Types + /// + Types, + + /// + /// Formats + /// + Formats, + } + /// /// Define class for runspace configuration entry. /// diff --git a/src/System.Management.Automation/engine/SessionState.cs b/src/System.Management.Automation/engine/SessionState.cs index 9070f0e207..4745143511 100644 --- a/src/System.Management.Automation/engine/SessionState.cs +++ b/src/System.Management.Automation/engine/SessionState.cs @@ -379,28 +379,6 @@ namespace System.Management.Automation RunspaceInit.PSHOMEDescription); this.GlobalScope.SetVariable(v.Name, v, false, true, this, CommandOrigin.Internal, fastPath: true); - - // $Console - set the console file for this shell, if there is one, "" otherwise... - SetConsoleVariable(); - } - - /// - /// Set the $Console variable in this session state instance... - /// - internal void SetConsoleVariable() - { - // $Console - set the console file for this shell, if there is one, "" otherwise... - string consoleFileName = string.Empty; - RunspaceConfigForSingleShell rcss = ExecutionContext.RunspaceConfiguration as RunspaceConfigForSingleShell; - if (rcss != null && rcss.ConsoleInfo != null && !string.IsNullOrEmpty(rcss.ConsoleInfo.Filename)) - { - consoleFileName = rcss.ConsoleInfo.Filename; - } - PSVariable v = new PSVariable(SpecialVariables.ConsoleFileName, - consoleFileName, - ScopedItemOptions.ReadOnly | ScopedItemOptions.AllScope, - RunspaceInit.ConsoleDescription); - this.GlobalScope.SetVariable(v.Name, v, false, true, this, CommandOrigin.Internal, fastPath: true); } /// @@ -500,13 +478,7 @@ namespace System.Management.Automation CmdletProviderContext context = new CmdletProviderContext(this.ExecutionContext); - Collection keys = new Collection(); - foreach (string key in Providers.Keys) - { - keys.Add(key); - } - - foreach (string providerName in keys) + foreach (string providerName in Providers.Keys) { // All errors are ignored. diff --git a/src/System.Management.Automation/engine/SessionStateProviderAPIs.cs b/src/System.Management.Automation/engine/SessionStateProviderAPIs.cs index 311be3a90b..54721d4877 100644 --- a/src/System.Management.Automation/engine/SessionStateProviderAPIs.cs +++ b/src/System.Management.Automation/engine/SessionStateProviderAPIs.cs @@ -52,48 +52,6 @@ namespace System.Management.Automation } private Dictionary _providersCurrentWorkingDrive = new Dictionary(); - private bool _providersInitialized = false; - - /// - /// Gets called by the RunspaceConfiguration when a PSSnapin gets added or removed. - /// - /// - internal void UpdateProviders() - { - // This should only be called from Update() on a runspace configuration q.e.d. runspace configuration - // should never be null when this gets called... - if (this.ExecutionContext.RunspaceConfiguration == null) - throw PSTraceSource.NewInvalidOperationException(); - - if (this == ExecutionContext.TopLevelSessionState && !_providersInitialized) - { - foreach (ProviderConfigurationEntry providerConfig in this.ExecutionContext.RunspaceConfiguration.Providers) - { - AddProvider(providerConfig); - } - - _providersInitialized = true; - return; - } - - foreach (ProviderConfigurationEntry providerConfig in this.ExecutionContext.RunspaceConfiguration.Providers.UpdateList) - { - switch (providerConfig.Action) - { - case UpdateAction.Add: - AddProvider(providerConfig); - break; - - case UpdateAction.Remove: - RemoveProvider(providerConfig); - break; - - default: - break; - } - } - } - /// /// Entrypoint used by to add a provider to the current session state /// based on a SessionStateProviderEntry. @@ -109,20 +67,6 @@ namespace System.Management.Automation ); } - /// - /// Internal method used by RunspaceConfig for updating providers. - /// - /// - private ProviderInfo AddProvider(ProviderConfigurationEntry providerConfig) - { - return AddProvider(providerConfig.ImplementingType, - providerConfig.Name, - providerConfig.HelpFileName, - providerConfig.PSSnapIn, - null - ); - } - private ProviderInfo AddProvider(Type implementingType, string name, string helpFileName, PSSnapInInfo psSnapIn, PSModuleInfo module) { ProviderInfo provider = null; @@ -1028,7 +972,7 @@ namespace System.Management.Automation } } - if (ExecutionContext.IsSingleShell && !String.IsNullOrEmpty(providerName.PSSnapInName)) + if (!String.IsNullOrEmpty(providerName.PSSnapInName)) { // Be sure the PSSnapin/Module name matches @@ -1234,8 +1178,6 @@ namespace System.Management.Automation } } // InitializeProvider - - /// /// Creates and adds a provider to the provider container /// @@ -1512,51 +1454,6 @@ namespace System.Management.Automation #region Remove Provider - private void RemoveProvider(ProviderConfigurationEntry entry) - { - try - { - CmdletProviderContext context = new CmdletProviderContext(this.ExecutionContext); - - string providerName = GetProviderName(entry); - RemoveProvider(providerName, true, context); - - context.ThrowFirstErrorOrDoNothing(); - } - catch (LoopFlowException) - { - throw; - } - catch (PipelineStoppedException) - { - throw; - } - catch (ActionPreferenceStopException) - { - throw; - } - catch (Exception e) // Catch-all OK, 3rd party callout - { - // NTRAID#Windows OS Bugs-1009281-2004/02/11-JeffJon - this.ExecutionContext.ReportEngineStartupError(e); - } - } - - private string GetProviderName(ProviderConfigurationEntry entry) - { - string name = entry.Name; - if (entry.PSSnapIn != null) - { - name = - string.Format( - System.Globalization.CultureInfo.InvariantCulture, - "{0}\\{1}", - entry.PSSnapIn.Name, - entry.Name); - } - return name; - } - /// /// Removes the provider of the given name. /// diff --git a/src/System.Management.Automation/engine/SpecialVariables.cs b/src/System.Management.Automation/engine/SpecialVariables.cs index 90b9585976..5c837976f7 100644 --- a/src/System.Management.Automation/engine/SpecialVariables.cs +++ b/src/System.Management.Automation/engine/SpecialVariables.cs @@ -217,7 +217,6 @@ namespace System.Management.Automation #region AllScope variables created in every session - internal const string ConsoleFileName = "ConsoleFileName"; internal const string ExecutionContext = "ExecutionContext"; internal const string Home = "HOME"; internal const string Host = "Host"; @@ -232,7 +231,6 @@ namespace System.Management.Automation internal static List AllScopeSessionVariables = new List { - ConsoleFileName, ExecutionContext, Home, Host, @@ -297,7 +295,6 @@ namespace System.Management.Automation // also exists. internal static readonly string[] AllScopeVariables = { SpecialVariables.Question, - SpecialVariables.ConsoleFileName, SpecialVariables.ExecutionContext, SpecialVariables.False, SpecialVariables.Home, diff --git a/src/System.Management.Automation/engine/TypeTable.cs b/src/System.Management.Automation/engine/TypeTable.cs index 35ef970d80..1b2fba40da 100644 --- a/src/System.Management.Automation/engine/TypeTable.cs +++ b/src/System.Management.Automation/engine/TypeTable.cs @@ -4218,7 +4218,6 @@ namespace System.Management.Automation.Runspaces Update(errors, typeData, false); StandardMembersUpdated(); - // Throw exception if there are any errors FormatAndTypeDataHelper.ThrowExceptionOnError("ErrorsUpdatingTypes", errors, RunspaceConfigurationCategory.Types); } @@ -4245,7 +4244,6 @@ namespace System.Management.Automation.Runspaces Update(errors, typeData, true); StandardMembersUpdated(); - // Throw exception if there are any errors FormatAndTypeDataHelper.ThrowExceptionOnError("ErrorsUpdatingTypes", errors, RunspaceConfigurationCategory.Types); } diff --git a/src/System.Management.Automation/engine/hostifaces/Connection.cs b/src/System.Management.Automation/engine/hostifaces/Connection.cs index 40973e81f6..8828d3d547 100644 --- a/src/System.Management.Automation/engine/hostifaces/Connection.cs +++ b/src/System.Management.Automation/engine/hostifaces/Connection.cs @@ -16,7 +16,7 @@ namespace System.Management.Automation.Runspaces #region Exceptions /// - /// Exception thrown when state of the runspace is different from + /// Exception thrown when state of the runspace is different from /// expected state of runspace. /// [Serializable] @@ -34,10 +34,10 @@ namespace System.Management.Automation.Runspaces } /// - /// Initializes a new instance of InvalidRunspaceStateException with a specified error message. + /// Initializes a new instance of InvalidRunspaceStateException with a specified error message. /// /// - /// The message that describes the error. + /// The message that describes the error. /// public InvalidRunspaceStateException(string message) : base(message) @@ -45,12 +45,12 @@ namespace System.Management.Automation.Runspaces } /// - /// Initializes a new instance of the InvalidRunspaceStateException class - /// with a specified error message and a reference to the inner exception - /// that is the cause of this exception. + /// Initializes a new instance of the InvalidRunspaceStateException class + /// with a specified error message and a reference to the inner exception + /// that is the cause of this exception. /// /// - /// The message that describes the error. + /// The message that describes the error. /// /// /// The exception that is the cause of the current exception. @@ -61,7 +61,7 @@ namespace System.Management.Automation.Runspaces } /// - /// Initializes a new instance of the InvalidRunspaceStateException + /// Initializes a new instance of the InvalidRunspaceStateException /// with a specified error message and current and expected state. /// /// The message that describes the error. @@ -90,11 +90,11 @@ namespace System.Management.Automation.Runspaces /// class with serialized data. /// /// - /// The that holds the serialized object + /// The that holds the serialized object /// data about the exception being thrown. /// /// - /// The that contains contextual information + /// The that contains contextual information /// about the source or destination. /// protected InvalidRunspaceStateException(SerializationInfo info, StreamingContext context) @@ -214,13 +214,13 @@ namespace System.Management.Automation.Runspaces UseNewThread = 1, /// - /// Creates a new thread for the first invocation and then re-uses + /// Creates a new thread for the first invocation and then re-uses /// that thread in subsequent invocations. /// ReuseThread = 2, /// - /// Doesn't create a new thread; the execution occurs on the + /// Doesn't create a new thread; the execution occurs on the /// thread that calls Invoke. /// /// @@ -250,7 +250,7 @@ namespace System.Management.Automation.Runspaces /// Constructor for state changes with an optional error /// /// The state of runspace. - /// A non-null exception if the state change was + /// A non-null exception if the state change was /// caused by an error, otherwise; null. /// internal RunspaceStateInfo(RunspaceState state, Exception reason) @@ -263,7 +263,7 @@ namespace System.Management.Automation.Runspaces /// /// Copy constructor to support cloning. /// - /// The source + /// The source /// RunspaceStateInfo /// internal RunspaceStateInfo(RunspaceStateInfo runspaceStateInfo) @@ -284,8 +284,8 @@ namespace System.Management.Automation.Runspaces /// The reason for the state change, if caused by an error. /// /// - /// The value of this property is non-null if the state - /// changed due to an error. Otherwise, the value of this + /// The value of this property is non-null if the state + /// changed due to an error. Otherwise, the value of this /// property is null. /// public Exception Reason { get; } @@ -326,7 +326,7 @@ namespace System.Management.Automation.Runspaces /// /// Constructs RunspaceStateEventArgs using RunspaceStateInfo /// - /// The information about + /// The information about /// current state of the runspace. /// runspaceStateInfo is null /// @@ -347,8 +347,8 @@ namespace System.Management.Automation.Runspaces /// Information about state of the runspace /// /// - /// This value indicates the state of the runspace after the - /// change. + /// This value indicates the state of the runspace after the + /// change. /// public RunspaceStateInfo RunspaceStateInfo { get; } @@ -647,19 +647,6 @@ namespace System.Management.Automation.Runspaces protected set; } - /// - /// RunspaceConfiguration information for this runspace. - /// -#if CORECLR - internal -#else - public -#endif - abstract RunspaceConfiguration RunspaceConfiguration - { - get; - } - /// /// InitialSessionState information for this runspace. /// @@ -669,7 +656,7 @@ namespace System.Management.Automation.Runspaces } /// - /// Get unique id for this instance of runspace. It is primarily used + /// Get unique id for this instance of runspace. It is primarily used /// for logging purposes /// public Guid InstanceId { get; @@ -941,7 +928,7 @@ namespace System.Management.Automation.Runspaces ((pipelineState == PipelineState.Stopped) && (this.RunspaceStateInfo.State == RunspaceState.Opened))) && (remoteCommand != null) && (cmdInstanceId != null) && (remoteCommand.CommandId == cmdInstanceId)) { - // Completed, Failed, and Stopped with Runspace.Opened states are command finish states and we know + // Completed, Failed, and Stopped with Runspace.Opened states are command finish states and we know // that the command is finished on the server. // Setting ConnectCommands to null indicates that the runspace is free to run other // commands. @@ -1255,12 +1242,12 @@ namespace System.Management.Automation.Runspaces /// Disconnects the runspace synchronously. /// /// - /// Disconnects the remote runspace and any running command from the server + /// Disconnects the remote runspace and any running command from the server /// machine. Any data generated by the running command on the server is /// cached on the server machine. This runspace object goes to the disconnected - /// state. This object can be reconnected to the server by calling the + /// state. This object can be reconnected to the server by calling the /// Connect() method. - /// If the remote runspace on the server remains disconnected for the IdleTimeout + /// If the remote runspace on the server remains disconnected for the IdleTimeout /// value (as defined in the WSManConnectionInfo object) then it is closed and /// torn down on the server. /// @@ -1273,12 +1260,12 @@ namespace System.Management.Automation.Runspaces /// Disconnects the runspace asynchronously. /// /// - /// Disconnects the remote runspace and any running command from the server + /// Disconnects the remote runspace and any running command from the server /// machine. Any data generated by the running command on the server is /// cached on the server machine. This runspace object goes to the disconnected - /// state. This object can be reconnected to the server by calling the + /// state. This object can be reconnected to the server by calling the /// Connect() method. - /// If the remote runspace on the server remains disconnected for the IdleTimeout + /// If the remote runspace on the server remains disconnected for the IdleTimeout /// value (as defined in the WSManConnectionInfo object) then it is closed and /// torn down on the server. /// @@ -1316,8 +1303,8 @@ namespace System.Management.Automation.Runspaces public abstract void ConnectAsync(); /// - /// Creates a PipeLine object in the disconnected state for the currently disconnected - /// remote running command associated with this runspace. + /// Creates a PipeLine object in the disconnected state for the currently disconnected + /// remote running command associated with this runspace. /// /// Pipeline object in disconnected state. public abstract Pipeline CreateDisconnectedPipeline(); @@ -1370,7 +1357,7 @@ namespace System.Management.Automation.Runspaces /// Close the runspace Asynchronously. /// /// - /// Attempts to execute pipelines after a call to + /// Attempts to execute pipelines after a call to /// close will fail. /// /// @@ -1410,7 +1397,7 @@ namespace System.Management.Automation.Runspaces public abstract Pipeline CreatePipeline(string command, bool addToHistory); /// - /// Creates a nested pipeline. + /// Creates a nested pipeline. /// /// /// Nested pipelines are needed for nested prompt scenario. Nested @@ -1420,7 +1407,7 @@ namespace System.Management.Automation.Runspaces public abstract Pipeline CreateNestedPipeline(); /// - /// Creates a nested pipeline. + /// Creates a nested pipeline. /// /// A valid command string /// if true command is added to history @@ -1438,14 +1425,14 @@ namespace System.Management.Automation.Runspaces internal abstract Pipeline GetCurrentlyRunningPipeline(); /// - /// Private data to be used by applications built on top of PowerShell. - /// + /// Private data to be used by applications built on top of PowerShell. + /// /// Local runspace is created with application private data set to an empty . - /// - /// Remote runspace gets its application private data from the server (set when creating a remote runspace pool) - /// Calling this method on a remote runspace will block until the data is received from the server. + /// + /// Remote runspace gets its application private data from the server (set when creating a remote runspace pool) + /// Calling this method on a remote runspace will block until the data is received from the server. /// The server will send application private data before reaching state. - /// + /// /// Runspaces that are part of a inherit application private data from the pool. /// public abstract PSPrimitiveDictionary GetApplicationPrivateData(); @@ -1639,7 +1626,7 @@ namespace System.Management.Automation.Runspaces } /// - /// This class provides subset of functionality provided by + /// This class provides subset of functionality provided by /// session state. /// public class SessionStateProxy @@ -1658,23 +1645,23 @@ namespace System.Management.Automation.Runspaces /// /// Set a variable in session state. /// - /// + /// /// /// The name of the item to set. /// - /// + /// /// /// The new value of the item being set. /// - /// + /// /// /// name is null /// - /// + /// /// /// Runspace is not open. /// - /// + /// /// /// Another SessionStateProxy call or another pipeline is in progress. /// @@ -1688,25 +1675,25 @@ namespace System.Management.Automation.Runspaces } /// - /// Get a variable out of session state. + /// Get a variable out of session state. /// - /// + /// /// /// name of variable to look up /// - /// + /// /// /// The value of the specified variable. /// - /// + /// /// /// name is null /// - /// + /// /// /// Runspace is not open. /// - /// + /// /// /// Another SessionStateProxy call or another pipeline is in progress. /// @@ -1727,11 +1714,11 @@ namespace System.Management.Automation.Runspaces /// /// Get the list of applications out of session state. /// - /// + /// /// /// Runspace is not open. /// - /// + /// /// /// Another SessionStateProxy call or another pipeline is in progress. /// @@ -1746,11 +1733,11 @@ namespace System.Management.Automation.Runspaces /// /// Get the list of scripts out of session state. /// - /// + /// /// /// Runspace is not open. /// - /// + /// /// /// Another SessionStateProxy call or another pipeline is in progress. /// @@ -1765,11 +1752,11 @@ namespace System.Management.Automation.Runspaces /// /// Get the APIs to access drives out of session state /// - /// + /// /// /// Runspace is not open. /// - /// + /// /// /// Another SessionStateProxy call or another pipeline is in progress. /// @@ -1781,11 +1768,11 @@ namespace System.Management.Automation.Runspaces /// /// Get/Set the language mode out of session state. /// - /// + /// /// /// Runspace is not open. /// - /// + /// /// /// Another SessionStateProxy call or another pipeline is in progress. /// @@ -1798,11 +1785,11 @@ namespace System.Management.Automation.Runspaces /// /// Get the module info out of session state. /// - /// + /// /// /// Runspace is not open. /// - /// + /// /// /// Another SessionStateProxy call or another pipeline is in progress. /// @@ -1815,11 +1802,11 @@ namespace System.Management.Automation.Runspaces /// /// Get the APIs to access paths and locations out of session state. /// - /// + /// /// /// Runspace is not open. /// - /// + /// /// /// Another SessionStateProxy call or another pipeline is in progress. /// @@ -1831,11 +1818,11 @@ namespace System.Management.Automation.Runspaces /// /// Get the APIs to access a provider out of session state. /// - /// + /// /// /// Runspace is not open. /// - /// + /// /// /// Another SessionStateProxy call or another pipeline is in progress. /// @@ -1847,11 +1834,11 @@ namespace System.Management.Automation.Runspaces /// /// Get the APIs to access variables out of session state. /// - /// + /// /// /// Runspace is not open. /// - /// + /// /// /// Another SessionStateProxy call or another pipeline is in progress. /// @@ -1863,14 +1850,14 @@ namespace System.Management.Automation.Runspaces /// /// Get the APIs to build script blocks and execute script out of session state. /// - /// + /// /// /// Runspace is not open. /// - /// + /// /// /// Another SessionStateProxy call or another pipeline is in progress. - /// + /// public virtual CommandInvocationIntrinsics InvokeCommand { get { return _runspace.InvokeCommand; } @@ -1879,11 +1866,11 @@ namespace System.Management.Automation.Runspaces /// /// Gets the instance of the provider interface APIs out of session state. /// - /// + /// /// /// Runspace is not open. /// - /// + /// /// /// Another SessionStateProxy call or another pipeline is in progress. /// diff --git a/src/System.Management.Automation/engine/hostifaces/ConnectionBase.cs b/src/System.Management.Automation/engine/hostifaces/ConnectionBase.cs index e32dfc6a28..152075ff89 100644 --- a/src/System.Management.Automation/engine/hostifaces/ConnectionBase.cs +++ b/src/System.Management.Automation/engine/hostifaces/ConnectionBase.cs @@ -37,22 +37,14 @@ namespace System.Management.Automation.Runspaces /// /// host is null. /// - /// - /// configuration information for this minshell. - /// - protected RunspaceBase(PSHost host, RunspaceConfiguration runspaceConfiguration) + protected RunspaceBase(PSHost host) { if (host == null) { throw PSTraceSource.NewArgumentNullException("host"); } - if (runspaceConfiguration == null) - { - throw PSTraceSource.NewArgumentNullException("runspaceConfiguration"); - } - + InitialSessionState = InitialSessionState.CreateDefault(); Host = host; - RunspaceConfiguration = runspaceConfiguration; } /// @@ -143,17 +135,7 @@ namespace System.Management.Automation.Runspaces protected PSHost Host { get; } /// - /// runspaceConfiguration information for this runspace - /// -#if CORECLR - internal -#else - public -#endif - override RunspaceConfiguration RunspaceConfiguration { get; } - - /// - /// runspaceConfiguration information for this runspace + /// InitialSessionState information for this runspace /// public override InitialSessionState InitialSessionState { get; } diff --git a/src/System.Management.Automation/engine/hostifaces/ConnectionFactory.cs b/src/System.Management.Automation/engine/hostifaces/ConnectionFactory.cs index 5a17b72da5..5a1630cec6 100644 --- a/src/System.Management.Automation/engine/hostifaces/ConnectionFactory.cs +++ b/src/System.Management.Automation/engine/hostifaces/ConnectionFactory.cs @@ -34,8 +34,6 @@ namespace System.Management.Automation.Runspaces /// /// Creates a runspace using host of type . - /// This runspace is created using the information - /// from EntryAssembly. /// /// /// A runspace object. @@ -67,69 +65,9 @@ namespace System.Management.Automation.Runspaces throw PSTraceSource.NewArgumentNullException("host"); } - return CreateRunspace(host, RunspaceConfiguration.Create()); + return new LocalRunspace(host, InitialSessionState.CreateDefault()); } - /// - /// Creates a runspace using - /// - /// - /// RunspaceConfiguration information for the runspace. - /// - /// - /// A runspace object - /// - /// - /// Thrown when runspaceConfiguration is null - /// - internal static Runspace CreateRunspace(RunspaceConfiguration runspaceConfiguration) - { - if (runspaceConfiguration == null) - { - throw PSTraceSource.NewArgumentNullException("runspaceConfiguration"); - } - - PSHost host = new DefaultHost(CultureInfo.CurrentCulture, CultureInfo.CurrentUICulture); - - return CreateRunspace(host, runspaceConfiguration); - } - - /// - /// Creates a runspace using specified PSHost and RunspaceConfiguration - /// - /// - /// Host implementation for runspace. - /// - /// - /// RunspaceConfiguration information for the runspace. - /// - /// - /// A runspace object - /// - /// - /// Thrown when host is null - /// - /// - /// Thrown when runspaceConfiguration is null - /// - internal static Runspace CreateRunspace(PSHost host, RunspaceConfiguration runspaceConfiguration) - { - if (host == null) - { - throw PSTraceSource.NewArgumentNullException("host"); - } - - if (runspaceConfiguration == null) - { - throw PSTraceSource.NewArgumentNullException("runspaceConfiguration"); - } - - return new LocalRunspace(host, runspaceConfiguration); - } - - //========================================================= - // Create runspaces with InitialSessionState instead of RunspaceConfig objects... - /// /// Creates a runspace using /// @@ -228,22 +166,15 @@ namespace System.Management.Automation.Runspaces #region RunspacePool Factory /// - /// Creates a RunspacePool using default RunspaceConfiguration - /// with MaxRunspaces 1 and MinRunspaces 1. + /// Creates a RunspacePool with MaxRunspaces 1 and MinRunspaces 1. /// public static RunspacePool CreateRunspacePool() { - return CreateRunspacePool(1, 1, - RunspaceConfiguration.Create(), - new DefaultHost - ( - CultureInfo.CurrentCulture, - CultureInfo.CurrentUICulture - )); + return CreateRunspacePool(1, 1); } /// - /// Creates a RunspacePool using default RunspaceConfiguration. + /// Creates a RunspacePool /// /// limits the number of Runspaces that can exist in this /// pool. The minimum pool size is set to . @@ -263,7 +194,6 @@ namespace System.Management.Automation.Runspaces public static RunspacePool CreateRunspacePool(int minRunspaces, int maxRunspaces) { return CreateRunspacePool(minRunspaces, maxRunspaces, - RunspaceConfiguration.Create(), new DefaultHost ( CultureInfo.CurrentCulture, @@ -281,7 +211,7 @@ namespace System.Management.Automation.Runspaces /// Runspace in the pool. /// /// - /// RunspaceConfiguration is null. + /// InitialSessionState is null. /// [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Runspace")] public static RunspacePool CreateRunspacePool(InitialSessionState initialSessionState) @@ -318,44 +248,7 @@ namespace System.Management.Automation.Runspaces [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Runspaces")] public static RunspacePool CreateRunspacePool(int minRunspaces, int maxRunspaces, PSHost host) { - return CreateRunspacePool(minRunspaces, maxRunspaces, RunspaceConfiguration.Create(), host); - } - - /// - /// Creates a RunspacePool using the supplied , - /// and - /// - /// - /// The minimum number of Runspaces that can exist in this pool. - /// Should be greater than or equal to 1. - /// - /// - /// The maximum number of Runspaces that can exist in this pool. - /// Should be greater than or equal to 1. - /// - /// - /// RunspaceConfiguration to use when creating a new Runspace in the - /// pool. - /// - /// - /// RunspaceConfiguration is null. - /// - /// - /// The explicit PSHost implementation. - /// - /// - /// is null. - /// is null. - /// - /// - /// Maximum runspaces is less than 1. - /// Minimum runspaces is less than 1. - /// - private static RunspacePool CreateRunspacePool(int minRunspaces, int maxRunspaces, - RunspaceConfiguration runspaceConfiguration, PSHost host) - { - return new RunspacePool(minRunspaces, - maxRunspaces, runspaceConfiguration, host); + return new RunspacePool(minRunspaces, maxRunspaces, host); } /// @@ -375,13 +268,13 @@ namespace System.Management.Automation.Runspaces /// pool. /// /// - /// RunspaceConfiguration is null. + /// InitialSessionState is null. /// /// /// The explicit PSHost implementation. /// /// - /// is null. + /// is null. /// is null. /// /// diff --git a/src/System.Management.Automation/engine/hostifaces/LocalConnection.cs b/src/System.Management.Automation/engine/hostifaces/LocalConnection.cs index 550d55b5e5..d58876ac92 100644 --- a/src/System.Management.Automation/engine/hostifaces/LocalConnection.cs +++ b/src/System.Management.Automation/engine/hostifaces/LocalConnection.cs @@ -31,22 +31,6 @@ namespace System.Management.Automation.Runspaces { #region constructors - /// - /// Construct an instance of an Runspace using a custom implementation - /// of PSHost. - /// - /// - /// The explicit PSHost implementation - /// - /// - /// configuration information for this minshell. - /// - [SuppressMessage("Microsoft.Maintainability", "CA1505:AvoidUnmaintainableCode")] - internal LocalRunspace(PSHost host, RunspaceConfiguration runspaceConfig) - : base(host, runspaceConfig) - { - } - /// /// Construct an instance of an Runspace using a custom implementation /// of PSHost. @@ -641,6 +625,7 @@ namespace System.Management.Automation.Runspaces /// private void DoOpenHelper() { + Dbg.Assert(InitialSessionState != null, "InitialSessionState should not be null"); // NTRAID#Windows Out Of Band Releases-915851-2005/09/13 if (_disposed) { @@ -653,17 +638,10 @@ namespace System.Management.Automation.Runspaces { _transcriptionData = new TranscriptionData(); - if (InitialSessionState != null) - { - // All ISS-based configuration of the engine itself is done by AutomationEngine, - // which calls InitialSessionState.Bind(). Anything that doesn't - // require an active and open runspace should be done in ISS.Bind() - _engine = new AutomationEngine(Host, null, InitialSessionState); - } - else - { - _engine = new AutomationEngine(Host, RunspaceConfiguration, null); - } + // All ISS-based configuration of the engine itself is done by AutomationEngine, + // which calls InitialSessionState.Bind(). Anything that doesn't + // require an active and open runspace should be done in ISS.Bind() + _engine = new AutomationEngine(Host, InitialSessionState); _engine.Context.CurrentRunspace = this; //Log engine for start of engine life @@ -773,14 +751,7 @@ namespace System.Management.Automation.Runspaces logContext.HostVersion = Host.Version.ToString(); logContext.RunspaceId = InstanceId.ToString(); logContext.Severity = severity.ToString(); - if (this.RunspaceConfiguration == null) - { - logContext.ShellId = Utils.DefaultPowerShellShellID; - } - else - { - logContext.ShellId = this.RunspaceConfiguration.ShellId; - } + logContext.ShellId = Utils.DefaultPowerShellShellID; MshLog.LogEngineHealthEvent( logContext, id, @@ -867,7 +838,7 @@ namespace System.Management.Automation.Runspaces } if ((hostRunspace == null) || (this == hostRunspace)) - { + { // We should close transcripting only if we are closing the last opened runspace. foreach (Runspace runspace in RunspaceList) { @@ -883,7 +854,6 @@ namespace System.Management.Automation.Runspaces { host.StopAllTranscribing(); } - AmsiUtils.Uninitialize(); } } @@ -929,7 +899,7 @@ namespace System.Management.Automation.Runspaces //Log engine lifecycle event. MshLog.LogEngineLifecycleEvent(_engine.Context, EngineState.Stopped); - + //All pipelines have been canceled. Close the runspace. _engine = null; _commandFactory = null; diff --git a/src/System.Management.Automation/engine/hostifaces/RunspaceInit.cs b/src/System.Management.Automation/engine/hostifaces/RunspaceInit.cs index 46cb04c4e4..fab0055732 100644 --- a/src/System.Management.Automation/engine/hostifaces/RunspaceInit.cs +++ b/src/System.Management.Automation/engine/hostifaces/RunspaceInit.cs @@ -31,24 +31,6 @@ namespace System.Management.Automation.Runspaces // Add the variables that must always be there... ss.InitializeFixedVariables(); - - // If this is being built from a runspace configuration, then - // add all of the default entries. When initializing from an InitialSessionState - // object, it will contain the defaults if so desired. - if (this.RunspaceConfiguration != null) - { - bool addSetStrictMode = true; - foreach (RunspaceConfigurationEntry entry in this.RunspaceConfiguration.Cmdlets) - { - if (entry.Name.Equals("Set-StrictMode", StringComparison.OrdinalIgnoreCase)) - { - addSetStrictMode = false; - break; - } - } - // Add all of the built-in variable, function and alias definitions... - ss.AddBuiltInEntries(addSetStrictMode); - } } } } diff --git a/src/System.Management.Automation/engine/hostifaces/RunspaceInvoke.cs b/src/System.Management.Automation/engine/hostifaces/RunspaceInvoke.cs index abbc841ef3..e364b62514 100644 --- a/src/System.Management.Automation/engine/hostifaces/RunspaceInvoke.cs +++ b/src/System.Management.Automation/engine/hostifaces/RunspaceInvoke.cs @@ -27,64 +27,7 @@ namespace System.Management.Automation /// public RunspaceInvoke() { - RunspaceConfiguration rc = RunspaceConfiguration.Create(); - _runspace = RunspaceFactory.CreateRunspace(rc); - _runspace.Open(); - if (Runspace.DefaultRunspace == null) - { - Runspace.DefaultRunspace = _runspace; - } - } - - /// - /// Creates a RunspaceInvoke for invoking commands. Underlying Runspace is created using - /// specified RunspaceConfiguration - /// - /// RunspaceConfiguration used for creating the runspace - /// - /// - /// Thrown when runspaceConfiguration is null - /// - public RunspaceInvoke(RunspaceConfiguration runspaceConfiguration) - { - if (runspaceConfiguration == null) - { - throw PSTraceSource.NewArgumentNullException("runspaceConfiguration"); - } - _runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration); - _runspace.Open(); - if (Runspace.DefaultRunspace == null) - { - Runspace.DefaultRunspace = _runspace; - } - } - - /// - /// Creates a RunspaceInvoke for invoking commands. Underlying Runspace is created using the - /// specified console file. - /// - /// Console file used for creating the underlying - /// runspace. - /// - /// Thrown when consoleFilePath is null - /// - /// - /// Thrown when errors occurs in loading one or more PSSnapins. - /// - public RunspaceInvoke(string consoleFilePath) - { - if (consoleFilePath == null) - { - throw PSTraceSource.NewArgumentNullException("consoleFilePath"); - } - - PSConsoleLoadException warnings; - RunspaceConfiguration rc = RunspaceConfiguration.Create(consoleFilePath, out warnings); - if (warnings != null) - { - throw warnings; - } - _runspace = RunspaceFactory.CreateRunspace(rc); + _runspace = RunspaceFactory.CreateRunspace(); _runspace.Open(); if (Runspace.DefaultRunspace == null) { diff --git a/src/System.Management.Automation/engine/hostifaces/RunspacePool.cs b/src/System.Management.Automation/engine/hostifaces/RunspacePool.cs index 78dc8d777d..7d938b5172 100644 --- a/src/System.Management.Automation/engine/hostifaces/RunspacePool.cs +++ b/src/System.Management.Automation/engine/hostifaces/RunspacePool.cs @@ -528,29 +528,23 @@ namespace System.Management.Automation.Runspaces /// The maximum number of Runspaces that can exist in this pool. /// Should be greater than or equal to 1. /// - /// - /// RunspaceConfiguration to use when creating a new Runspace. - /// /// /// The explicit PSHost implementation. /// /// - /// RunspaceConfiguration is null. /// Host is null. /// /// /// Maximum runspaces is less than 1. /// Minimum runspaces is less than 1. /// - internal RunspacePool(int minRunspaces, int maxRunspaces, - RunspaceConfiguration runspaceConfiguration, PSHost host) + internal RunspacePool(int minRunspaces, int maxRunspaces, PSHost host) { // Currently we support only Local Runspace Pool.. // this needs to be changed once remote runspace pool // is implemented - _internalPool = new RunspacePoolInternal(minRunspaces, - maxRunspaces, runspaceConfiguration, host); + _internalPool = new RunspacePoolInternal(minRunspaces, maxRunspaces, host); } /// diff --git a/src/System.Management.Automation/engine/hostifaces/RunspacePoolInternal.cs b/src/System.Management.Automation/engine/hostifaces/RunspacePoolInternal.cs index bd43333cde..9aa7442b27 100644 --- a/src/System.Management.Automation/engine/hostifaces/RunspacePoolInternal.cs +++ b/src/System.Management.Automation/engine/hostifaces/RunspacePoolInternal.cs @@ -31,7 +31,6 @@ namespace System.Management.Automation.Runspaces.Internal // pool services on this queue. protected Queue ultimateRequestQueue; protected RunspacePoolStateInfo stateInfo; - protected RunspaceConfiguration rsConfig; protected InitialSessionState _initialSessionState; protected PSHost host; protected Guid instanceId; @@ -52,9 +51,6 @@ namespace System.Management.Automation.Runspaces.Internal /// supplied , /// and /// - /// - /// RunspaceConfiguration to use when creating a new Runspace. - /// /// /// The maximum number of Runspaces that can exist in this pool. /// Should be greater than or equal to 1. @@ -76,25 +72,19 @@ namespace System.Management.Automation.Runspaces.Internal /// public RunspacePoolInternal(int minRunspaces, int maxRunspaces, - RunspaceConfiguration runspaceConfiguration, PSHost host) : this(minRunspaces, maxRunspaces) { - if (runspaceConfiguration == null) - { - throw PSTraceSource.NewArgumentNullException("runspaceConfiguration"); - } - if (host == null) { throw PSTraceSource.NewArgumentNullException("host"); } - rsConfig = runspaceConfiguration; this.host = host; pool = new Stack(); runspaceRequestQueue = new Queue(); ultimateRequestQueue = new Queue(); + _initialSessionState = InitialSessionState.CreateDefault(); } /// @@ -262,18 +252,6 @@ namespace System.Management.Automation.Runspaces.Internal private PSPrimitiveDictionary _applicationPrivateData; - /// - /// Gets the RunspaceConfiguration object that this pool uses - /// to create the runspaces. - /// - public RunspaceConfiguration RunspaceConfiguration - { - get - { - return rsConfig; - } - } - /// /// Gets the InitialSessionState object that this pool uses /// to create the runspaces. @@ -1272,21 +1250,10 @@ namespace System.Management.Automation.Runspaces.Internal /// protected Runspace CreateRunspace() { + Dbg.Assert(null != _initialSessionState, "_initialSessionState should not be null"); // TODO: exceptions thrown here need to be documented // runspace.Open() did not document all the exceptions. - Runspace result = null; - - // if host is null we are already throwing an - // exception, hence a check is not required at this - // point - if (rsConfig != null) - { - result = RunspaceFactory.CreateRunspace(host, rsConfig); - } - else - { - result = RunspaceFactory.CreateRunspaceFromSessionStateNoClone(host, _initialSessionState); - } + Runspace result = RunspaceFactory.CreateRunspaceFromSessionStateNoClone(host, _initialSessionState); result.ThreadOptions = this.ThreadOptions == PSThreadOptions.Default ? PSThreadOptions.ReuseThread : this.ThreadOptions; #if !CORECLR // No ApartmentState In CoreCLR diff --git a/src/System.Management.Automation/engine/remoting/client/remoterunspace.cs b/src/System.Management.Automation/engine/remoting/client/remoterunspace.cs index 6499ec7cad..96c1f0eb0d 100644 --- a/src/System.Management.Automation/engine/remoting/client/remoterunspace.cs +++ b/src/System.Management.Automation/engine/remoting/client/remoterunspace.cs @@ -217,22 +217,6 @@ namespace System.Management.Automation #region Properties - /// - /// runspaceConfiguration information for this runspace - /// - internal override RunspaceConfiguration RunspaceConfiguration - { - get - { -#pragma warning disable 56503 - - throw PSTraceSource.NewNotImplementedException(); - -#pragma warning restore 56503 - } - } - - /// /// initialsessionstate information for this runspace /// diff --git a/src/System.Management.Automation/help/HelpProvider.cs b/src/System.Management.Automation/help/HelpProvider.cs index 3460834874..d9a40346dc 100644 --- a/src/System.Management.Automation/help/HelpProvider.cs +++ b/src/System.Management.Automation/help/HelpProvider.cs @@ -240,17 +240,6 @@ namespace System.Management.Automation return returnValue; } - /// - /// Helper function which checks whether PSSnapIns are supported in current invocation. - /// - /// true if supported,false otherwise. - internal bool AreSnapInsSupported() - { - RunspaceConfigForSingleShell runspace = _helpSystem.ExecutionContext.RunspaceConfiguration as RunspaceConfigForSingleShell; - - return (null == runspace ? false : true); - } - /// /// Gets the search paths. If the current shell is single-shell based, then the returned /// search path contains all the directories of currently active PSSnapIns diff --git a/src/System.Management.Automation/help/HelpProviderWithFullCache.cs b/src/System.Management.Automation/help/HelpProviderWithFullCache.cs index b6aabb34ea..ee6a0f4971 100644 --- a/src/System.Management.Automation/help/HelpProviderWithFullCache.cs +++ b/src/System.Management.Automation/help/HelpProviderWithFullCache.cs @@ -34,10 +34,7 @@ namespace System.Management.Automation /// The HelpInfo found. Null if nothing is found internal sealed override IEnumerable ExactMatchHelp(HelpRequest helpRequest) { - // If the current invocation is a singleshell based - // then we have to constantly update the cache as - // snapins might get added / removed. - if (!this.CacheFullyLoaded || AreSnapInsSupported()) + if (!this.CacheFullyLoaded) { LoadCache(); } @@ -69,10 +66,7 @@ namespace System.Management.Automation /// a collection of help info objects internal sealed override IEnumerable SearchHelp(HelpRequest helpRequest, bool searchOnlyContent) { - // If the current invocation is a singleshell based - // then we have to constantly update the cache as - // snapins might get added / removed. - if (!this.CacheFullyLoaded || AreSnapInsSupported()) + if (!this.CacheFullyLoaded) { LoadCache(); } diff --git a/src/System.Management.Automation/help/HelpSystem.cs b/src/System.Management.Automation/help/HelpSystem.cs index 66813e93e9..13aaf69551 100644 --- a/src/System.Management.Automation/help/HelpSystem.cs +++ b/src/System.Management.Automation/help/HelpSystem.cs @@ -254,24 +254,6 @@ namespace System.Management.Automation } _searchPaths = new Collection(); - RunspaceConfigForSingleShell runspace = this.ExecutionContext.RunspaceConfiguration as RunspaceConfigForSingleShell; - - if (null != runspace) - { - // SingleShell case. Check active snapins... - MshConsoleInfo currentConsole = runspace.ConsoleInfo; - - if ((null == currentConsole) || (null == currentConsole.ExternalPSSnapIns)) - { - return _searchPaths; - } - - foreach (PSSnapInInfo snapin in currentConsole.ExternalPSSnapIns) - { - _searchPaths.Add(snapin.ApplicationBase); - } - } - // add loaded modules paths to the search path if (null != ExecutionContext.Modules) { diff --git a/src/System.Management.Automation/help/MUIFileSearcher.cs b/src/System.Management.Automation/help/MUIFileSearcher.cs index bac0b7b30a..798ca01b44 100644 --- a/src/System.Management.Automation/help/MUIFileSearcher.cs +++ b/src/System.Management.Automation/help/MUIFileSearcher.cs @@ -138,7 +138,7 @@ namespace System.Management.Automation { string fileName = Path.GetFileName(filePath); if (wildcardPattern.IsMatch(fileName)) - { + { result.Add(filePath); } } @@ -335,8 +335,7 @@ namespace System.Management.Automation /// Get the file in different search paths corresponding to current culture. /// /// The file name to search is the filename part of path parameter. (Normally path - /// parameter should contain only the filename part. But it is possible for - /// RunspaceConfiguration to directly specify a hard coded path for help file there). + /// parameter should contain only the filename part). /// /// /// This is the path to the file. If it has a path, we need to search under that path first diff --git a/src/System.Management.Automation/minishell/api/RunspaceConfiguration.cs b/src/System.Management.Automation/minishell/api/RunspaceConfiguration.cs deleted file mode 100644 index 2bfa8ad6fc..0000000000 --- a/src/System.Management.Automation/minishell/api/RunspaceConfiguration.cs +++ /dev/null @@ -1,791 +0,0 @@ -/********************************************************************++ -Copyright (c) Microsoft Corporation. All rights reserved. ---********************************************************************/ - -#pragma warning disable 1634, 1691 - -using System.Collections.Generic; -using System.Collections.ObjectModel; -using System.Reflection; -using System.Security; -using System.Management.Automation.Host; -using Microsoft.PowerShell.Commands.Internal.Format; - -namespace System.Management.Automation.Runspaces -{ - /// - /// Defines configuration information for runspace. - /// - /// - /// - /// - /// Developers may want to derive from this class when writing their own - /// host to host monad engine. This will allow host to control the behaviour - /// of monad engine through customized runspace configuration. - /// - /// - /// - /// - internal abstract class RunspaceConfiguration - { - #region RunspaceConfiguration Factory - - /// - /// Create an instance of RunspaceConfiguration type implemented from an assembly. - /// - /// Assembly name from which to find runspace configuration implementation - /// Runspace configuration instance created - /// - /// Exception thrown when is null or empty. - /// - /// - public static RunspaceConfiguration Create(string assemblyName) - { - if (String.IsNullOrEmpty(assemblyName)) - { - throw PSTraceSource.NewArgumentNullException("assemblyName"); - } - - // If this assembly can't be loaded, Assembly.Load will throw an exception. - // Just let the exception bubble up. - - Assembly assembly = null; - foreach (Assembly asm in ClrFacade.GetAssemblies()) - { - if (string.Equals(asm.GetName().Name, assemblyName, StringComparison.OrdinalIgnoreCase)) - { - assembly = asm; - break; - } - } - - if (assembly == null) - { - assembly = Assembly.Load(new AssemblyName(assemblyName)); - } - - return Create(assembly); - } - - /// - /// Create one single shell runspace configuration object from console file - /// - /// - /// - /// - public static RunspaceConfiguration Create(string consoleFilePath, out PSConsoleLoadException warnings) - { - return RunspaceConfigForSingleShell.Create(consoleFilePath, out warnings); - } - - /// - /// Create one single shell runspace configuration object for a msh version. - /// - /// - public static RunspaceConfiguration Create() - { - return RunspaceConfigForSingleShell.CreateDefaultConfiguration(); - } - -#if V2 - /// - /// This is a RunspaceConfiguration factory function which will create an instance - /// based on a type specified in a particular assembly. - /// - /// This function will likely being exposed as a public interface in v2. - /// - /// - /// - /// - private static RunspaceConfiguration Create(string assemblyName, string runspaceConfigTypeName) - { - // If this assembly can't be loaded, Assembly.Load will throw an exception. - // Just let the exception bubble up. - Assembly assembly = Assembly.Load(assemblyName); - - try - { - Type runspaceConfigType = assembly.GetType(runspaceConfigTypeName, true); - return Create(runspaceConfigType); - } - catch (SecurityException) - { - throw new RunspaceConfigurationTypeException(assembly.FullName, runspaceConfigTypeName); - } - } -#endif - - /// - /// Create an RunspaceConfiguration-derived instance based an RunspaceConfiguration-derived type - /// in specified assembly. The type name is defined by assembly attribute: - /// RunspaceConfigurationTypeAttribute. - /// - /// - /// Runspace configuration instance created - /// - /// Exception thrown when is null or empty. - /// - private static RunspaceConfiguration Create(Assembly assembly) - { - if (assembly == null) - throw PSTraceSource.NewArgumentNullException("assembly"); - - object[] attributes = assembly.GetCustomAttributes(typeof(RunspaceConfigurationTypeAttribute), false); - - if (attributes == null || attributes.Length == 0) - { - throw new RunspaceConfigurationAttributeException("RunspaceConfigurationAttributeNotExist", assembly.FullName); - } - - if (attributes.Length > 1) - { - throw new RunspaceConfigurationAttributeException("RunspaceConfigurationAttributeDuplicate", assembly.FullName); - } - - RunspaceConfigurationTypeAttribute runspaceConfigTypeAttribute = (RunspaceConfigurationTypeAttribute)attributes[0]; - - try - { - Type runspaceConfigType = assembly.GetType(runspaceConfigTypeAttribute.RunspaceConfigurationType, true, false); - return Create(runspaceConfigType); - } - catch (SecurityException) - { - throw new RunspaceConfigurationTypeException(assembly.FullName, runspaceConfigTypeAttribute.RunspaceConfigurationType); - } - } - - /// - /// Create RunspaceConfiguration-derived instance based on a derived type. - /// - /// A type that is derived from RunspaceConfiguration - /// - private static RunspaceConfiguration Create(Type runspaceConfigType) - { - MethodInfo method = - runspaceConfigType.GetMethod("Create", BindingFlags.Public | BindingFlags.Static); - - if (method == null) - return null; - - return (RunspaceConfiguration)method.Invoke(null, null); - } - - #endregion RunspaceConfiguration Factory - - #region Shell Id - - /// - /// Gets the shell id for current runspace configuration. - /// - public abstract string ShellId - { - get; - } - - #endregion - - #region PSSnapin Api's - - /// - /// Add a PSSnapin to runspace configuration. - /// - /// - /// This member provides logic for adding PSSnapin. - /// - /// RunspaceConfiguration derived class should not override this member. - /// - /// - /// name of the PSSnapin - /// warning message - /// - public PSSnapInInfo AddPSSnapIn(string name, out PSSnapInException warning) - { - return DoAddPSSnapIn(name, out warning); - } - - internal virtual PSSnapInInfo DoAddPSSnapIn(string name, out PSSnapInException warning) - { - throw PSTraceSource.NewNotSupportedException(); - } - - /// - /// Remove a PSSnapin from runspace configuration. - /// - /// - /// This member provides logic for removing PSSnapin. - /// - /// RunspaceConfiguration derived class should not override this member. - /// - /// name of the PSSnapin - /// warning message - /// - public PSSnapInInfo RemovePSSnapIn(string name, out PSSnapInException warning) - { - return DoRemovePSSnapIn(name, out warning); - } - - internal virtual PSSnapInInfo DoRemovePSSnapIn(string name, out PSSnapInException warning) - { - throw PSTraceSource.NewNotSupportedException(); - } - - #endregion - - #region Cmdlet, Provider, Relationship - - private RunspaceConfigurationEntryCollection _cmdlets; - - /// - /// Gets the cmdlets defined in runspace configuration. - /// - /// - /// RunspaceConfiguration derived class can override this member to provide its own cmdlet list. - /// - public virtual RunspaceConfigurationEntryCollection Cmdlets - { - get { - return _cmdlets ?? (_cmdlets = new RunspaceConfigurationEntryCollection()); - } - } - - private RunspaceConfigurationEntryCollection _providers; - - /// - /// Gets the providers defined in runspace configuration. - /// - /// - /// RunspaceConfiguration derived class can override this member to provide its own provider list. - /// - public virtual RunspaceConfigurationEntryCollection Providers - { - get { - return _providers ?? - (_providers = new RunspaceConfigurationEntryCollection()); - } - } - - /* - /// - /// Gets the relationships defined in runspace configuration. - /// - public virtual Collection RelationshipData - { - get; - } - */ - - #endregion - - #region Types and Formats - - /// - /// types.ps1xml information - /// - /// - internal TypeTable TypeTable - { - get { return _typeTable ?? (_typeTable = new TypeTable()); } - } - private TypeTable _typeTable; - - private RunspaceConfigurationEntryCollection _types; - - /// - /// Gets the type data files defined in runspace configuration. - /// - /// - /// RunspaceConfiguration derived class can override this member to provide its own types to be - /// loaded at engine start up. - /// - public virtual RunspaceConfigurationEntryCollection Types - { - get { return _types ?? (_types = new RunspaceConfigurationEntryCollection()); } - } - - private RunspaceConfigurationEntryCollection _formats; - - /// - /// Gets the format data files defined in runspace configuration. - /// - /// - /// RunspaceConfiguration derived class can override this member to provide its own formats to be - /// loaded at engine start up. - /// - public virtual RunspaceConfigurationEntryCollection Formats - { - get { - return _formats ?? (_formats = new RunspaceConfigurationEntryCollection()); - } - } - - internal TypeInfoDataBaseManager FormatDBManager { get; } = new TypeInfoDataBaseManager(); - - #endregion - - #region Script Data - - private RunspaceConfigurationEntryCollection _scripts; - - /// - /// Gets the scripts defined in runspace configuration. - /// - /// - /// RunspaceConfiguration derived class can override this member to provide its own scripts - /// to be include in monad engine. - /// - public virtual RunspaceConfigurationEntryCollection Scripts - { - get { - return _scripts ?? (_scripts = new RunspaceConfigurationEntryCollection()); - } - } - - #endregion - - #region Profile configuration - - private RunspaceConfigurationEntryCollection _initializationScripts; - - /// - /// Gets the initialization scripts defined in runspace configuration. - /// - /// - /// RunspaceConfiguration derived class can override this member to provide its own initialization scripts - /// to be run at the start up of monad engine. - /// - public virtual RunspaceConfigurationEntryCollection InitializationScripts - { - get { - return _initializationScripts ?? - (_initializationScripts = new RunspaceConfigurationEntryCollection()); - } - } - - #endregion - - #region Assemblies - - private RunspaceConfigurationEntryCollection _assemblies; - - /// - /// Gets the assemblies defined in runspace configuration. - /// - /// - /// RunspaceConfiguration derived class can override this member to provide its own assemblies - /// to be loaded at the start up of monad engine. - /// - public virtual RunspaceConfigurationEntryCollection Assemblies - { - get { - return _assemblies ?? - (_assemblies = new RunspaceConfigurationEntryCollection()); - } - } - - #endregion - - #region Security Manager - - private AuthorizationManager _authorizationManager = null; - - /// - /// Gets the authorization manager to be used for runspace. - /// - /// - /// RunspaceConfiguration derived class can override this member to provide its own authorization - /// manager for monad engine. - /// - public virtual AuthorizationManager AuthorizationManager - { - get { - return _authorizationManager ?? - (_authorizationManager = new Microsoft.PowerShell.PSAuthorizationManager(this.ShellId)); - } - } - - #endregion - - #region Configuration Update - - private PSHost _host = null; - internal void Bind(ExecutionContext executionContext) - { - _host = executionContext.EngineHostInterface; - - Initialize(executionContext); - - this.Assemblies.OnUpdate += executionContext.UpdateAssemblyCache; - - s_runspaceInitTracer.WriteLine("initializing assembly list"); - try - { - this.Assemblies.Update(true); - } - catch (RuntimeException e) - { - s_runspaceInitTracer.WriteLine("assembly list initialization failed"); - MshLog.LogEngineHealthEvent( - executionContext, - MshLog.EVENT_ID_CONFIGURATION_FAILURE, - e, - Severity.Error); - executionContext.ReportEngineStartupError(e.Message); - - throw; - } - - if (executionContext.CommandDiscovery != null) - { - this.Cmdlets.OnUpdate += executionContext.CommandDiscovery.UpdateCmdletCache; - - // Force an update here so that cmdlet cache is updated in engine. - s_runspaceInitTracer.WriteLine("initializing cmdlet list"); - try - { - this.Cmdlets.Update(true); - } - catch (PSNotSupportedException e) - { - s_runspaceInitTracer.WriteLine("cmdlet list initialization failed"); - MshLog.LogEngineHealthEvent( - executionContext, - MshLog.EVENT_ID_CONFIGURATION_FAILURE, - e, - Severity.Error); - executionContext.ReportEngineStartupError(e.Message); - throw; - } - } - - if (executionContext.EngineSessionState != null) - { - this.Providers.OnUpdate += executionContext.EngineSessionState.UpdateProviders; - - // Force an update here so that provider cache is updated in engine. - s_runspaceInitTracer.WriteLine("initializing provider list"); - try - { - this.Providers.Update(true); - } - catch (PSNotSupportedException e) - { - s_runspaceInitTracer.WriteLine("provider list initialization failed"); - MshLog.LogEngineHealthEvent( - executionContext, - MshLog.EVENT_ID_CONFIGURATION_FAILURE, - e, - Severity.Error); - executionContext.ReportEngineStartupError(e.Message); - throw; - } - } - } - - internal void Unbind(ExecutionContext executionContext) - { - if (executionContext == null) - return; - - if (executionContext.CommandDiscovery != null) - { - this.Cmdlets.OnUpdate -= executionContext.CommandDiscovery.UpdateCmdletCache; - } - - if (executionContext.EngineSessionState != null) - { - this.Providers.OnUpdate -= executionContext.EngineSessionState.UpdateProviders; - } - - this.Assemblies.OnUpdate -= executionContext.UpdateAssemblyCache; - } - - private bool _initialized = false; - private Object _syncObject = new Object(); - internal void Initialize(ExecutionContext executionContext) - { -#pragma warning disable 56517 - - lock (_syncObject) - { - if (!_initialized) - { - _initialized = true; - - this.Types.OnUpdate += this.UpdateTypes; - this.Formats.OnUpdate += this.UpdateFormats; - - s_runspaceInitTracer.WriteLine("initializing types information"); - try - { - this.UpdateTypes(); - } - catch (RuntimeException e) - { - s_runspaceInitTracer.WriteLine("type information initialization failed"); - MshLog.LogEngineHealthEvent( - executionContext, - MshLog.EVENT_ID_CONFIGURATION_FAILURE, - e, - Severity.Warning); - executionContext.ReportEngineStartupError(e.Message); - } - - s_runspaceInitTracer.WriteLine("initializing format information"); - try - { - this.UpdateFormats(true); - } - catch (RuntimeException e) - { - s_runspaceInitTracer.WriteLine("format information initialization failed"); - MshLog.LogEngineHealthEvent( - executionContext, - MshLog.EVENT_ID_CONFIGURATION_FAILURE, - e, - Severity.Warning); - executionContext.ReportEngineStartupError(e.Message); - } - } - } - -#pragma warning restore 56517 - } - - internal void UpdateTypes() - { - Collection independentErrors = new Collection(); - Collection entryIndicesToRemove = new Collection(); - Collection PSSnapinFiles = FormatAndTypeDataHelper.GetFormatAndTypesErrors( - this, - _host, - this.Types, - independentErrors, - entryIndicesToRemove); - - if (entryIndicesToRemove.Count > 0) - { - RemoveNeedlessEntries(RunspaceConfigurationCategory.Types, entryIndicesToRemove); - } - - this.TypeTable.Update(PSSnapinFiles, _authorizationManager, _host); - FormatAndTypeDataHelper.ThrowExceptionOnError( - "ErrorsUpdatingTypes", - independentErrors, - PSSnapinFiles, - RunspaceConfigurationCategory.Types); - } - - internal void UpdateFormats() - { - UpdateFormats(false); - } - - private void UpdateFormats(bool preValidated) - { - Collection independentErrors = new Collection(); - Collection entryIndicesToRemove = new Collection(); - Collection PSSnapinFiles = FormatAndTypeDataHelper.GetFormatAndTypesErrors( - this, - _host, - this.Formats, - independentErrors, - entryIndicesToRemove); - - if (entryIndicesToRemove.Count > 0) - { - RemoveNeedlessEntries(RunspaceConfigurationCategory.Formats, entryIndicesToRemove); - } - - this.FormatDBManager.UpdateDataBase(PSSnapinFiles, this.AuthorizationManager, _host, preValidated); - FormatAndTypeDataHelper.ThrowExceptionOnError( - "ErrorsUpdatingFormats", - independentErrors, - PSSnapinFiles, - RunspaceConfigurationCategory.Formats); - } - - /// - /// Remove duplicate entries and entries with files that cannot be loaded from the Types and Formats - /// - /// - /// - private void RemoveNeedlessEntries(RunspaceConfigurationCategory category, IList entryIndicesToRemove) - { - for (int i = entryIndicesToRemove.Count - 1; i >= 0; i--) - { - if (category == RunspaceConfigurationCategory.Types) - { - this.Types.RemoveItem(entryIndicesToRemove[i]); - } - else if (category == RunspaceConfigurationCategory.Formats) - { - this.Formats.RemoveItem(entryIndicesToRemove[i]); - } - } - } - - #endregion - - [TraceSource("RunspaceInit", "Initialization code for Runspace")] - private static PSTraceSource s_runspaceInitTracer = PSTraceSource.GetTracer("RunspaceInit", "Initialization code for Runspace", false); - } - - /// - /// Enum for describing different kind information that can be configured in runspace configuration. - /// - /// - /// - internal enum RunspaceConfigurationCategory - { - /// - /// Cmdlets - /// - Cmdlets, - - /// - /// Providers - /// - Providers, - - /// - /// Assemblies - /// - Assemblies, - - /// - /// Scripts - /// - Scripts, - - /// - /// Initialization scripts - /// - InitializationScripts, - - /// - /// Types - /// - Types, - - /// - /// Formats - /// - Formats, - } - -#if V2 - - /// - /// Defines enum for runspace constraints - /// - [Flags] - public enum RunspaceConstraints - { - /// - /// No constraint. - /// - None, - - /// - /// Allow console applications. - /// - AllowConsoleApplication, - - /// - /// Allow user interaction. - /// - AllowUserInteraction, - - /// - /// For GUI cases. - /// - AllowGUIDesktop - } - -#endif -} diff --git a/src/System.Management.Automation/minishell/api/RunspaceConfigurationAttributeException.cs b/src/System.Management.Automation/minishell/api/RunspaceConfigurationAttributeException.cs deleted file mode 100644 index 01231c44cf..0000000000 --- a/src/System.Management.Automation/minishell/api/RunspaceConfigurationAttributeException.cs +++ /dev/null @@ -1,194 +0,0 @@ -/********************************************************************++ -Copyright (c) Microsoft Corporation. All rights reserved. ---********************************************************************/ - -using System.Runtime.Serialization; -using System.Reflection; -using System.Security.Permissions; - -namespace System.Management.Automation.Runspaces -{ - /// - /// Defines exception thrown when runspace configuration attribute is not defined correctly. - /// - /// - [Serializable] - public class RunspaceConfigurationAttributeException : SystemException, IContainsErrorRecord - { - /// - /// Initiate an instance of RunspaceConfigurationAttributeException. - /// - /// Error detail for the exception - /// Assembly on which runspace configuration attribute is defined or should be defined. - internal RunspaceConfigurationAttributeException(string error, string assemblyName) : base() - { - _error = error; - _assemblyName = assemblyName; - CreateErrorRecord(); - } - - /// - /// Initiate an instance of RunspaceConfigurationAttributeException. - /// - public RunspaceConfigurationAttributeException() : base() - { - } - - /// - /// Initiate an instance of RunspaceConfigurationAttributeException. - /// - /// Error message - public RunspaceConfigurationAttributeException(string message) - : base(message) - { - } - - /// - /// Initiate an instance of RunspaceConfigurationAttributeException. - /// - /// Error message - /// Inner exception - public RunspaceConfigurationAttributeException(string message, Exception innerException) - : base(message, innerException) - { - } - - /// - /// Initiate an instance of RunspaceConfigurationAttributeException. - /// - /// Error detail - /// Assembly on which runspace configuration attribute is defined or should be defined. - /// The inner exception of this exception - internal RunspaceConfigurationAttributeException(string error, string assemblyName, Exception innerException) : base(innerException.Message, innerException) - { - _error = error; - _assemblyName = assemblyName; - CreateErrorRecord(); - } - - /// - /// Create the internal error record based on helpTopic. - /// The ErrorRecord created will be stored in the _errorRecord member. - /// - private void CreateErrorRecord() - { - // if _error is empty, this exception is created using default - // constructor. Don't create the error record since there is - // no useful information anyway. - if (!String.IsNullOrEmpty(_error) && !String.IsNullOrEmpty(_assemblyName)) - { - _errorRecord = new ErrorRecord(new ParentContainsErrorRecordException(this), _error, ErrorCategory.ResourceUnavailable, null); - _errorRecord.ErrorDetails = new ErrorDetails(typeof(RunspaceConfigurationAttributeException).GetTypeInfo().Assembly, "MiniShellErrors", _error, _assemblyName); - } - } - - private ErrorRecord _errorRecord; - - /// - /// Gets error record embedded in this exception. - /// - /// - public ErrorRecord ErrorRecord - { - get - { - return _errorRecord; - } - } - - private string _error = ""; - - /// - /// Get localized error message. - /// - /// error - public string Error - { - get - { - return _error; - } - } - - private string _assemblyName = ""; - - /// - /// Gets assembly name on which runspace configuration attribute is defined or should be defined. - /// - /// error - public string AssemblyName - { - get - { - return _assemblyName; - } - } - - /// - /// Gets message for this exception. - /// - public override string Message - { - get - { - if (_errorRecord != null) - { - return _errorRecord.ToString(); - } - - return base.Message; - } - } - - #region Serialization - - /// - /// Initiate a RunspaceConfigurationAttributeException instance. - /// - /// Serialization information - /// Streaming context - protected RunspaceConfigurationAttributeException(SerializationInfo info, - StreamingContext context) - : base(info, context) - { - _error = info.GetString("Error"); - _assemblyName = info.GetString("AssemblyName"); - - CreateErrorRecord(); - } - - /// - /// Get object data from serialization information. - /// - /// Serialization information - /// Streaming context - [SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)] - public override void GetObjectData(SerializationInfo info, StreamingContext context) - { - if (info == null) - { - throw PSTraceSource.NewArgumentNullException("info"); - } - - base.GetObjectData(info, context); - - info.AddValue("Error", _error); - info.AddValue("AssemblyName", _assemblyName); - } - - #endregion Serialization - } -} - diff --git a/src/System.Management.Automation/minishell/api/RunspaceConfigurationEntryCollection.cs b/src/System.Management.Automation/minishell/api/RunspaceConfigurationEntryCollection.cs deleted file mode 100644 index ca02629b7c..0000000000 --- a/src/System.Management.Automation/minishell/api/RunspaceConfigurationEntryCollection.cs +++ /dev/null @@ -1,434 +0,0 @@ -/********************************************************************++ -Copyright (c) Microsoft Corporation. All rights reserved. ---********************************************************************/ - -#pragma warning disable 1634, 1691 -#pragma warning disable 56517 - -using System.Collections; -using System.Collections.Generic; -using System.Collections.ObjectModel; -using Dbg = System.Management.Automation.Diagnostics; - -namespace System.Management.Automation.Runspaces -{ - internal delegate void RunspaceConfigurationEntryUpdateEventHandler(); - - /// - /// Define class for runspace configuration entry collection. - /// - /// -#if CORECLR - internal -#else - public -#endif - sealed class RunspaceConfigurationEntryCollection : IEnumerable where T : RunspaceConfigurationEntry - { - /// - /// Constructor - /// - public RunspaceConfigurationEntryCollection() - { - } - - /// - /// Constructor - /// - /// - public RunspaceConfigurationEntryCollection(IEnumerable items) - { - if (items == null) - { - throw PSTraceSource.NewArgumentNullException("item"); - } - AddBuiltInItem(items); - } - - private Collection _data = new Collection(); - private int _builtInEnd = 0; // this is the index of first after item. - - private Collection _updateList = new Collection(); - internal Collection UpdateList - { - get - { - return _updateList; - } - } - - /// - /// Get items at a index position. - /// - /// - /// - public T this[int index] - { - get - { - lock (_syncObject) - { - return _data[index]; - } - } - } - - /// - /// Get number of items in the collection. - /// - public int Count - { - get - { - lock (_syncObject) - { - return _data.Count; - } - } - } - - /// - /// Reset items in the collection - /// - public void Reset() - { - lock (_syncObject) - { - for (int i = _data.Count - 1; i >= 0; i--) - { - if (!_data[i].BuiltIn) - { - RecordRemove(_data[i]); - - _data.RemoveAt(i); - } - } - - _builtInEnd = _data.Count; - } - } - - /// - /// Remove one item from the collection - /// - /// - /// when is out of range. - public void RemoveItem(int index) - { - lock (_syncObject) - { - if (index < 0 || index >= _data.Count) - { - throw PSTraceSource.NewArgumentOutOfRangeException("index", index); - } - - RecordRemove(_data[index]); - - _data.RemoveAt(index); - - if (index < _builtInEnd) - _builtInEnd--; - - return; - } - } - - /// - /// Remove multiple items in the collection. - /// - /// - /// - /// when is out of range. - public void RemoveItem(int index, int count) - { - lock (_syncObject) - { - if (index < 0 || index + count > _data.Count) - { - throw PSTraceSource.NewArgumentOutOfRangeException("index", index); - } - - for (int i = index + count - 1; i >= index; i--) - { - RecordRemove(_data[i]); - - _data.RemoveAt(i); - } - - int _numBeforeBuiltInEnd = Math.Min(count, _builtInEnd - index); - - if (_numBeforeBuiltInEnd > 0) - _builtInEnd -= _numBeforeBuiltInEnd; - - return; - } - } - - /// - /// Remove one item from the collection - /// - /// - internal void Remove(T item) - { - lock (_syncObject) - { - int index = _data.IndexOf(item); - - if (index < 0 || index >= _data.Count) - { - Dbg.Assert(false, "Index from which to remove the item is out of range."); - throw PSTraceSource.NewArgumentOutOfRangeException("index", index); - } - - RecordRemove(item); - _data.Remove(item); - - if (index < _builtInEnd) - _builtInEnd--; - - return; - } - } - - /// - /// Prepend an item to the collection. - /// - /// - public void Prepend(T item) - { - lock (_syncObject) - { - RecordAdd(item); - - item._builtIn = false; - _data.Insert(0, item); - _builtInEnd++; - } - } - - /// - /// Prepend items into the collection - /// - /// - public void Prepend(IEnumerable items) - { - if (items == null) - { - throw new ArgumentNullException("items"); - } - - lock (_syncObject) - { - int i = 0; - foreach (T t in items) - { - RecordAdd(t); - - t._builtIn = false; - _data.Insert(i++, t); - _builtInEnd++; - } - } - } - - /// - /// Append one item to the collection - /// - /// - public void Append(T item) - { - lock (_syncObject) - { - RecordAdd(item); - - item._builtIn = false; - _data.Add(item); - } - } - - /// - /// Append items to the collection. - /// - /// - public void Append(IEnumerable items) - { - if (items == null) - { - throw new ArgumentNullException("items"); - } - - lock (_syncObject) - { - foreach (T t in items) - { - RecordAdd(t); - - t._builtIn = false; - _data.Add(t); - } - } - } - - internal void AddBuiltInItem(T item) - { - lock (_syncObject) - { - item._builtIn = true; - - RecordAdd(item); - - _data.Insert(_builtInEnd, item); - _builtInEnd++; - } - } - - internal void AddBuiltInItem(IEnumerable items) - { - lock (_syncObject) - { - foreach (T t in items) - { - t._builtIn = true; - - RecordAdd(t); - - _data.Insert(_builtInEnd, t); - _builtInEnd++; - } - } - } - - internal void RemovePSSnapIn(string PSSnapinName) - { - lock (_syncObject) - { - for (int i = _data.Count - 1; i >= 0; i--) - { - if (_data[i].PSSnapIn != null) - { - if (_data[i].PSSnapIn.Name.Equals(PSSnapinName, StringComparison.Ordinal)) - { - RecordRemove(_data[i]); - - _data.RemoveAt(i); - - if (i < _builtInEnd) - _builtInEnd--; - } - } - } - } - } - - /// - /// Get enumerator for this collection. - /// - /// - /// - IEnumerator System.Collections.IEnumerable.GetEnumerator() - { - return _data.GetEnumerator(); - } - - /// - /// Get enumerator for this collection. - /// - /// - /// - IEnumerator System.Collections.Generic.IEnumerable.GetEnumerator() - { - return _data.GetEnumerator(); - } - - /// - /// Update others about the collection change. - /// - public void Update() - { - Update(false); - } - - internal void Update(bool force) - { - lock (_syncObject) - { - if (OnUpdate != null && (force || _updateList.Count > 0)) - { - OnUpdate(); - - // Here we need to clear the Action for each item - // since _updateList is sharing data with _data. - foreach (T t in _updateList) - { - t._action = UpdateAction.None; - } - - _updateList.Clear(); - } - } - } - - private void RecordRemove(T t) - { - // if this item was added recently, simply remove the add action. - if (t.Action == UpdateAction.Add) - { - t._action = UpdateAction.None; - _updateList.Remove(t); - } - else - { - t._action = UpdateAction.Remove; - _updateList.Add(t); - } - } - - private void RecordAdd(T t) - { - // if this item was removed recently, simply remove the add action. - if (t.Action == UpdateAction.Remove) - { - t._action = UpdateAction.None; - _updateList.Remove(t); - } - else - { - t._action = UpdateAction.Add; - _updateList.Add(t); - } - } - - //object to use for locking - private object _syncObject = new object(); - - /// - /// OnUpdate handler should lock the object itself. - /// - internal event RunspaceConfigurationEntryUpdateEventHandler OnUpdate; - } -} - -#pragma warning restore 56517 diff --git a/src/System.Management.Automation/minishell/api/RunspaceConfigurationHelper.cs b/src/System.Management.Automation/minishell/api/RunspaceConfigurationHelper.cs deleted file mode 100644 index 472c7705ea..0000000000 --- a/src/System.Management.Automation/minishell/api/RunspaceConfigurationHelper.cs +++ /dev/null @@ -1,29 +0,0 @@ -/********************************************************************++ -Copyright (c) Microsoft Corporation. All rights reserved. ---********************************************************************/ - -namespace System.Management.Automation.Runspaces -{ - /// - /// RunspaceConfigurationHelper define some constants to be used by - /// both Minishell api and makekit. - /// - /// Be very careful when trying to change values for strings below since - /// it will be used by makekit process also. - /// - /// This file will be built with both monad engine and makekit. - /// - internal static class RunspaceConfigurationHelper - { - internal const string IntrinsicTypeResourceName = "intrinsicTypes"; - internal const string BuiltInTypeResourceName = "builtInTypes"; - internal const string IntrinsicFormatResourceName = "intrinsicFormats"; - internal const string BuiltInFormatResourceName = "builtInFormats"; - internal const string ScriptResourceName = "script"; - internal const string ProfileResourceName = "initialization"; - internal const string HelpResourceName = "help"; - internal const string ShellHelpResourceKey = "ShellHelp"; - internal const string ShellBannerResourceKey = "ShellBanner"; - internal const string ResourceListKey = "__resourceList__"; - } -} diff --git a/src/System.Management.Automation/minishell/api/RunspaceConfigurationTypeAttribute.cs b/src/System.Management.Automation/minishell/api/RunspaceConfigurationTypeAttribute.cs deleted file mode 100644 index d090ffe509..0000000000 --- a/src/System.Management.Automation/minishell/api/RunspaceConfigurationTypeAttribute.cs +++ /dev/null @@ -1,31 +0,0 @@ -/********************************************************************++ -Copyright (c) Microsoft Corporation. All rights reserved. ---********************************************************************/ - -namespace System.Management.Automation.Runspaces -{ - /// - /// Define the class for runspace configuration type attribute. - /// - /// - [AttributeUsage(AttributeTargets.Assembly)] - internal sealed class RunspaceConfigurationTypeAttribute : Attribute - { - /// - /// Initiate an instance of RunspaceConfigurationTypeAttribute. - /// - /// Runspace configuration type - public RunspaceConfigurationTypeAttribute(string runspaceConfigurationType) - { - RunspaceConfigurationType = runspaceConfigurationType; - } - - /// - /// Get runspace configuration type - /// - public string RunspaceConfigurationType { get; } - } -} diff --git a/src/System.Management.Automation/minishell/api/RunspaceConfigurationTypeException.cs b/src/System.Management.Automation/minishell/api/RunspaceConfigurationTypeException.cs deleted file mode 100644 index 34315b8983..0000000000 --- a/src/System.Management.Automation/minishell/api/RunspaceConfigurationTypeException.cs +++ /dev/null @@ -1,181 +0,0 @@ -/********************************************************************++ -Copyright (c) Microsoft Corporation. All rights reserved. ---********************************************************************/ - -using System.Runtime.Serialization; -using System.Reflection; -using System.Security.Permissions; - -namespace System.Management.Automation.Runspaces -{ - /// - /// Define class for runspace configuration type. - /// - /// - [Serializable] - internal class RunspaceConfigurationTypeException : SystemException, IContainsErrorRecord - { - /// - /// Initiate an instance for RunspaceConfigurationTypeException - /// - /// Name of the assembly where is defined. - /// Runspace configuration type - internal RunspaceConfigurationTypeException(string assemblyName, string typeName) : base() - { - _assemblyName = assemblyName; - _typeName = typeName; - CreateErrorRecord(); - } - - /// - /// Initiate an instance for RunspaceConfigurationTypeException - /// - public RunspaceConfigurationTypeException() : base() - { - } - - /// - /// Initiate an instance for RunspaceConfigurationTypeException - /// - /// Name of the assembly where is defined. - /// Runspace configuration type defined in - /// Inner exception of this exception - internal RunspaceConfigurationTypeException(string assemblyName, string typeName, Exception innerException) : base(innerException.Message, innerException) - { - _assemblyName = assemblyName; - _typeName = typeName; - CreateErrorRecord(); - } - - /// - /// Initiate an instance for RunspaceConfigurationTypeException - /// - /// Error message - public RunspaceConfigurationTypeException(string message) - : base(message) - { - } - - /// - /// Initiate an instance for RunspaceConfigurationTypeException - /// - /// Error message - /// Inner exception of this exception - public RunspaceConfigurationTypeException(string message, Exception innerException) - : base(message, innerException) - { - } - - /// - /// Create the internal error record based on assembly name and type. - /// The ErrorRecord created will be stored in the _errorRecord member. - /// - private void CreateErrorRecord() - { - if (!String.IsNullOrEmpty(_assemblyName) && !String.IsNullOrEmpty(_typeName)) - { - _errorRecord = new ErrorRecord(new ParentContainsErrorRecordException(this), "UndefinedRunspaceConfigurationType", ErrorCategory.ResourceUnavailable, null); - _errorRecord.ErrorDetails = new ErrorDetails(typeof(RunspaceConfigurationTypeException).GetTypeInfo().Assembly, "MiniShellErrors", "UndefinedRunspaceConfigurationType", _assemblyName, _typeName); - } - } - - private ErrorRecord _errorRecord; - - /// - /// Get the error record embedded in this exception. - /// - public ErrorRecord ErrorRecord - { - get - { - return _errorRecord; - } - } - - private string _assemblyName = ""; - - /// - /// Get name of the assembly where runspace configuration type is defined. - /// - public string AssemblyName - { - get - { - return _assemblyName; - } - } - - private string _typeName = ""; - - /// - /// Get the runspace configuration type. - /// - public string TypeName - { - get - { - return _typeName; - } - } - - /// - /// Get message for this exception. - /// - public override string Message - { - get - { - if (_errorRecord != null) - { - return _errorRecord.ToString(); - } - - return base.Message; - } - } - - #region Serialization - - /// - /// Initiate a RunspaceConfigurationAttributeException instance. - /// - /// Serialization information - /// Streaming context - protected RunspaceConfigurationTypeException(SerializationInfo info, - StreamingContext context) - : base(info, context) - { - _typeName = info.GetString("TypeName"); - _assemblyName = info.GetString("AssemblyName"); - } - - /// - /// Get object data from serialization information. - /// - /// Serialization information - /// Streaming context - [SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)] - public override void GetObjectData(SerializationInfo info, StreamingContext context) - { - if (info == null) - { - throw PSTraceSource.NewArgumentNullException("info"); - } - - base.GetObjectData(info, context); - - info.AddValue("TypeName", _typeName); - info.AddValue("AssemblyName", _assemblyName); - } - - #endregion Serialization - } -} - diff --git a/src/System.Management.Automation/resources/MiniShellErrors.resx b/src/System.Management.Automation/resources/MiniShellErrors.resx index 6df5009313..152a8f7ad6 100644 --- a/src/System.Management.Automation/resources/MiniShellErrors.resx +++ b/src/System.Management.Automation/resources/MiniShellErrors.resx @@ -117,18 +117,6 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - The RunspaceConfiguration-derived type {1} is not defined in assembly {0}. - - - Cannot create the RunspaceConfiguration object because an assembly attribute of type RunspaceConfigurationType is not defined in assembly {0}. - - - Cannot create the RunspaceConfiguration object because there is more than one assembly attribute of the type RunspaceConfigurationType defined in the assembly {0}. - - - Cannot reflect on the entry assembly and determine the value of the RunspaceConfigurationType attribute. - The update is not supported for the runspace configuration category {0}. diff --git a/src/System.Management.Automation/singleshell/Commands/ConsoleCommands.cs b/src/System.Management.Automation/singleshell/Commands/ConsoleCommands.cs deleted file mode 100644 index 8f4303d726..0000000000 --- a/src/System.Management.Automation/singleshell/Commands/ConsoleCommands.cs +++ /dev/null @@ -1,602 +0,0 @@ -/********************************************************************++ -Copyright (c) Microsoft Corporation. All rights reserved. ---********************************************************************/ - -using System; -using System.IO; -using System.Collections.ObjectModel; -using System.Management.Automation; -using System.Management.Automation.Runspaces; -using System.Management.Automation.Internal; -using Dbg = System.Management.Automation.Diagnostics; - -#pragma warning disable 1634, 1691 // Stops compiler from warning about unknown warnings - -namespace Microsoft.PowerShell.Commands -{ - #region ConsoleCmdletsBase - /// - /// Base class for all the Console related cmdlets. - /// - public abstract class ConsoleCmdletsBase : PSCmdlet - { - /// - /// Runspace configuration for the current engine - /// - /// - /// Console cmdlets need object to work with. - /// - internal RunspaceConfigForSingleShell Runspace - { - get - { - RunspaceConfigForSingleShell runSpace = Context.RunspaceConfiguration as RunspaceConfigForSingleShell; - return runSpace; - } - } - - /// - /// InitialSessionState for the current engine - /// - internal InitialSessionState InitialSessionState - { - get - { - return Context.InitialSessionState; - } - } - - /// - /// Throws a terminating error. - /// - /// Object which caused this exception. - /// ErrorId for this error. - /// Complete exception object. - /// ErrorCategory for this exception. - internal void ThrowError( - Object targetObject, - string errorId, - Exception innerException, - ErrorCategory category) - { - ThrowTerminatingError(new ErrorRecord(innerException, errorId, category, targetObject)); - } - } - #endregion - - #region export-console - - /// - /// Class that implements export-console cmdlet. - /// - [Cmdlet(VerbsData.Export, "Console", SupportsShouldProcess = true, HelpUri = "https://go.microsoft.com/fwlink/?LinkID=113298")] - public sealed class ExportConsoleCommand : ConsoleCmdletsBase - { - #region Parameters - - /// - /// Property that gets/sets console file name. - /// - /// - /// If a parameter is not supplied then the file represented by $console - /// will be used for saving. - /// - [Parameter(Position = 0, Mandatory = false, ValueFromPipeline = true, - ValueFromPipelineByPropertyName = true)] - [Alias("PSPath")] - public string Path - { - get - { - return _fileName; - } - - set - { - _fileName = value; - } - } - private string _fileName; - - /// - /// Property that sets force parameter. This will reset the read-only - /// attribute on an existing file before deleting it. - /// - [Parameter()] - public SwitchParameter Force - { - get - { - return _force; - } - set - { - _force = value; - } - } - private bool _force; - - /// - /// Property that prevents file overwrite. - /// - [Parameter()] - [Alias("NoOverwrite")] - public SwitchParameter NoClobber - { - get - { - return _noclobber; - } - set - { - _noclobber = value; - } - } - private bool _noclobber; - - #endregion - - #region Overrides - - /// - /// Saves the current console info into a file. - /// - protected override void ProcessRecord() - { - // Get filename.. - string file = GetFileName(); - - // if file is null or empty..prompt the user for filename - if (string.IsNullOrEmpty(file)) - { - file = PromptUserForFile(); - } - - // if file is still empty..write error and back out.. - if (string.IsNullOrEmpty(file)) - { - PSArgumentException ae = PSTraceSource.NewArgumentException("file", ConsoleInfoErrorStrings.FileNameNotResolved); - ThrowError(file, "FileNameNotResolved", ae, ErrorCategory.InvalidArgument); - } - - if (WildcardPattern.ContainsWildcardCharacters(file)) - { - ThrowError(file, "WildCardNotSupported", - PSTraceSource.NewInvalidOperationException(ConsoleInfoErrorStrings.ConsoleFileWildCardsNotSupported, - file), ErrorCategory.InvalidOperation); - } - - // Ofcourse, you cant write to a file from HKLM: etc.. - string resolvedPath = ResolveProviderAndPath(file); - - // If resolvedPath is empty just return.. - if (string.IsNullOrEmpty(resolvedPath)) - { - return; - } - - // Check whether the file ends with valid extension - if (!resolvedPath.EndsWith(StringLiterals.PowerShellConsoleFileExtension, - StringComparison.OrdinalIgnoreCase)) - { - // file does not end with proper extension..create one.. - resolvedPath = resolvedPath + StringLiterals.PowerShellConsoleFileExtension; - } - - if (!ShouldProcess(this.Path)) // should this be resolvedPath? - return; - - //check if destination file exists. - if (File.Exists(resolvedPath)) - { - if (NoClobber) - { - string message = StringUtil.Format( - ConsoleInfoErrorStrings.FileExistsNoClobber, - resolvedPath, - "NoClobber"); // prevents localization - Exception uae = new UnauthorizedAccessException(message); - ErrorRecord errorRecord = new ErrorRecord( - uae, "NoClobber", ErrorCategory.ResourceExists, resolvedPath); - // NOTE: this call will throw - ThrowTerminatingError(errorRecord); - } - // Check if the file is read-only - System.IO.FileAttributes attrib = System.IO.File.GetAttributes(resolvedPath); - if ((attrib & System.IO.FileAttributes.ReadOnly) == System.IO.FileAttributes.ReadOnly) - { - if (Force) - { - RemoveFileThrowIfError(resolvedPath); - // Note, we do not attempt to set read-only on the new file - } - else - { - ThrowError(file, "ConsoleFileReadOnly", - PSTraceSource.NewArgumentException(file, ConsoleInfoErrorStrings.ConsoleFileReadOnly, resolvedPath), - ErrorCategory.InvalidArgument); - } - } - } - - try - { - if (this.Runspace != null) - { - this.Runspace.SaveAsConsoleFile(resolvedPath); - } - else if (InitialSessionState != null) - { - this.InitialSessionState.SaveAsConsoleFile(resolvedPath); - } - else - { - Dbg.Assert(false, "Both RunspaceConfiguration and InitialSessionState should not be null"); - throw PSTraceSource.NewInvalidOperationException(ConsoleInfoErrorStrings.CmdletNotAvailable); - } - } - catch (PSArgumentException mae) - { - ThrowError(resolvedPath, - "PathNotAbsolute", mae, ErrorCategory.InvalidArgument); - } - catch (PSArgumentNullException mane) - { - ThrowError(resolvedPath, - "PathNull", mane, ErrorCategory.InvalidArgument); - } - catch (ArgumentException ae) - { - ThrowError(resolvedPath, - "InvalidCharactersInPath", ae, ErrorCategory.InvalidArgument); - } - - // looks like saving succeeded. - // Now try changing $console - Exception e = null; - try - { - //Update $Console variable - Context.EngineSessionState.SetConsoleVariable(); - } - catch (ArgumentNullException ane) - { - e = ane; - } - catch (ArgumentOutOfRangeException aor) - { - e = aor; - } - catch (ArgumentException ae) - { - e = ae; - } - catch (SessionStateUnauthorizedAccessException sue) - { - e = sue; - } - catch (ProviderNotFoundException pnf) - { - e = pnf; - } - catch (System.Management.Automation.DriveNotFoundException dnfe) - { - e = dnfe; - } - catch (NotSupportedException ne) - { - e = ne; - } - catch (ProviderInvocationException pin) - { - e = pin; - } - - if (e != null) - { - throw PSTraceSource.NewInvalidOperationException(e, - ConsoleInfoErrorStrings.ConsoleVariableCannotBeSet, resolvedPath); - } - } - - #endregion - - #region Private Methods - - /// - /// Removes file specified by destination - /// - /// Absolute path of the file to be removed. - private void RemoveFileThrowIfError(string destination) - { - Diagnostics.Assert(System.IO.Path.IsPathRooted(destination), - "RemoveFile expects an absolute path"); - - System.IO.FileInfo destfile = new System.IO.FileInfo(destination); - if (destfile != null) - { - Exception e = null; - try - { - //Make sure the file is not read only - destfile.Attributes = destfile.Attributes & ~(FileAttributes.ReadOnly | FileAttributes.Hidden); - destfile.Delete(); - } - catch (FileNotFoundException fnf) - { - e = fnf; - } - catch (DirectoryNotFoundException dnf) - { - e = dnf; - } - catch (UnauthorizedAccessException uac) - { - e = uac; - } - catch (System.Security.SecurityException se) - { - e = se; - } - catch (ArgumentNullException ane) - { - e = ane; - } - catch (ArgumentException ae) - { - e = ae; - } - catch (PathTooLongException pe) - { - e = pe; - } - catch (NotSupportedException ns) - { - e = ns; - } - catch (IOException ioe) - { - e = ioe; - } - - if (e != null) - { - throw PSTraceSource.NewInvalidOperationException(e, - ConsoleInfoErrorStrings.ExportConsoleCannotDeleteFile, - destfile); - } - } - } - - /// - /// Resolves the specified path and verifies the path belongs to - /// FileSystemProvider. - /// - /// Path to resolve - /// A fully qualified string representing filename. - private string ResolveProviderAndPath(string path) - { - // Construct cmdletprovidercontext - CmdletProviderContext cmdContext = new CmdletProviderContext(this); - // First resolve path - PathInfo resolvedPath = ResolvePath(path, true, cmdContext); - - // Check whether this is FileSystemProvider.. - if (resolvedPath != null) - { - if (resolvedPath.Provider.ImplementingType == typeof(FileSystemProvider)) - { - return resolvedPath.Path; - } - - throw PSTraceSource.NewInvalidOperationException(ConsoleInfoErrorStrings.ProviderNotSupported, resolvedPath.Provider.Name); - } - - return null; - } - - /// - /// Resolves the specified path to PathInfo objects - /// - /// - /// - /// The path to be resolved. Each path may contain glob characters. - /// - /// - /// - /// If true, resolves the path even if it doesn't exist. - /// - /// - /// - /// The context under which the command is running. - /// - /// - /// - /// A string representing the resolved path. - /// - /// - private PathInfo ResolvePath( - string pathToResolve, - bool allowNonexistingPaths, - CmdletProviderContext currentCommandContext) - { - Collection results = new Collection(); - - try - { - // First resolve path - Collection pathInfos = - SessionState.Path.GetResolvedPSPathFromPSPath( - pathToResolve, - currentCommandContext); - - foreach (PathInfo pathInfo in pathInfos) - { - results.Add(pathInfo); - } - } - catch (PSNotSupportedException notSupported) - { - WriteError( - new ErrorRecord( - notSupported.ErrorRecord, - notSupported)); - } - catch (System.Management.Automation.DriveNotFoundException driveNotFound) - { - WriteError( - new ErrorRecord( - driveNotFound.ErrorRecord, - driveNotFound)); - } - catch (ProviderNotFoundException providerNotFound) - { - WriteError( - new ErrorRecord( - providerNotFound.ErrorRecord, - providerNotFound)); - } - catch (ItemNotFoundException pathNotFound) - { - if (allowNonexistingPaths) - { - ProviderInfo provider = null; - System.Management.Automation.PSDriveInfo drive = null; - string unresolvedPath = - SessionState.Path.GetUnresolvedProviderPathFromPSPath( - pathToResolve, - currentCommandContext, - out provider, - out drive); - - PathInfo pathInfo = - new PathInfo( - drive, - provider, - unresolvedPath, - SessionState); - results.Add(pathInfo); - } - else - { - WriteError( - new ErrorRecord( - pathNotFound.ErrorRecord, - pathNotFound)); - } - } - - if (results.Count == 1) - { - return results[0]; - } - else if (results.Count > 1) - { - Exception e = PSTraceSource.NewNotSupportedException(); - WriteError( - new ErrorRecord(e, - "NotSupported", - ErrorCategory.NotImplemented, - results)); - return null; - } - else - { - return null; - } - } // ResolvePath - - /// - /// Gets the filename for the current operation. If Name parameter is empty - /// checks $console. If $console is not present, prompts user? - /// - /// - /// A string representing filename. - /// If filename cannot be deduced returns null. - /// - /// - /// 1. $console points to an PSObject that cannot be converted to string. - /// - private string GetFileName() - { - if (!string.IsNullOrEmpty(_fileName)) - { - // if user specified input..just return - return _fileName; - } - // no input is specified.. - // check whether $console is set - PSVariable consoleVariable = Context.SessionState.PSVariable.Get(SpecialVariables.ConsoleFileName); - if (consoleVariable == null) - { - return string.Empty; - } - - string consoleValue = consoleVariable.Value as string; - - if (consoleValue == null) - { - // $console is not in string format - // Check whether it is in PSObject format - PSObject consolePSObject = consoleVariable.Value as PSObject; - - if ((consolePSObject != null) && ((consolePSObject.BaseObject as string) != null)) - { - consoleValue = consolePSObject.BaseObject as string; - } - } - - if (consoleValue != null) - { - // ConsoleFileName variable is found.. - return consoleValue; - } - - throw PSTraceSource.NewArgumentException("fileName", ConsoleInfoErrorStrings.ConsoleCannotBeConvertedToString); - } - - /// - /// Prompt user for filename. - /// - /// - /// User input in string format. - /// If user chooses not to export, an empty string is returned. - /// - /// No exception is thrown - private string PromptUserForFile() - { - // ask user what to do.. - if (ShouldContinue( - ConsoleInfoErrorStrings.PromptForExportConsole, - null)) - { - string caption = StringUtil.Format(ConsoleInfoErrorStrings.FileNameCaptionForExportConsole, "export-console"); - string message = ConsoleInfoErrorStrings.FileNamePromptMessage; - - // Construct a field description object of required parameters - Collection desc = new Collection(); - desc.Add(new System.Management.Automation.Host.FieldDescription("Name")); - - // get user input from the host - System.Collections.Generic.Dictionary returnValue = - this.PSHostInternal.UI.Prompt(caption, message, desc); - - if ((returnValue != null) && (returnValue["Name"] != null)) - { - return (returnValue["Name"].BaseObject as string); - } - - // We dont have any input.. - return string.Empty; - } - - // If user chooses not to export, return empty string. - return string.Empty; - } - - #endregion - } - - #endregion -} - diff --git a/src/System.Management.Automation/singleshell/Commands/MshSnapinCommands.cs b/src/System.Management.Automation/singleshell/Commands/MshSnapinCommands.cs deleted file mode 100644 index 5e8890a31c..0000000000 --- a/src/System.Management.Automation/singleshell/Commands/MshSnapinCommands.cs +++ /dev/null @@ -1,850 +0,0 @@ -/********************************************************************++ -Copyright (c) Microsoft Corporation. All rights reserved. ---********************************************************************/ - -using System; -using System.Collections.Generic; -using System.Collections.ObjectModel; -using System.Management.Automation; -using System.Management.Automation.Runspaces; - -#pragma warning disable 1634, 1691 // Stops compiler from warning about unknown warnings - -namespace Microsoft.PowerShell.Commands -{ - #region PSSnapInCommandBase - /// - /// Base class for all the pssnapin related cmdlets. - /// - public abstract class PSSnapInCommandBase : PSCmdlet, IDisposable - { - #region IDisposable Members - - /// - /// Set to true when object is disposed - /// - /// - private bool _disposed; - - /// - /// Dispose method unloads the app domain and the - /// resource reader if it was created. - /// - /// - public void Dispose() - { - if (_disposed == false) - { - if (_resourceReader != null) - { - _resourceReader.Dispose(); - _resourceReader = null; - } - GC.SuppressFinalize(this); - } - _disposed = true; - } - - #endregion IDisposable Members - - #region Cmdlet Methods - - /// - /// Disposes the resource reader. - /// - /// - protected override void EndProcessing() - { - if (_resourceReader != null) - { - _resourceReader.Dispose(); - _resourceReader = null; - } - } - - #endregion CmdletMethods - - #region Internal Methods - - /// - /// Runspace configuration for the current engine - /// - /// - /// PSSnapIn cmdlets need object to work with. - /// - internal RunspaceConfigForSingleShell Runspace - { - get - { - RunspaceConfigForSingleShell runSpace = Context.RunspaceConfiguration as RunspaceConfigForSingleShell; - - if (runSpace == null) - { - return null; - } - - return runSpace; - } - } - - /// - /// Writes a non-terminating error onto the pipeline. - /// - /// Object which caused this exception. - /// ErrorId for this error. - /// Complete exception object. - /// ErrorCategory for this exception. - internal void WriteNonTerminatingError( - Object targetObject, - string errorId, - Exception innerException, - ErrorCategory category) - { - WriteError(new ErrorRecord(innerException, errorId, category, targetObject)); - } - - - /// - /// Searches the input list for the pattern supplied. - /// - /// Input list - /// pattern with wildcards - /// - /// A collection of string objects (representing PSSnapIn name) - /// that match the pattern. - /// - /// - /// Please note that this method will use WildcardPattern class. - /// So it wont support all the 'regex' patterns - /// - internal Collection - SearchListForPattern(Collection searchList, - string pattern) - { - Collection listToReturn = new Collection(); - - if (null == searchList) - { - // return an empty list - return listToReturn; - } - - WildcardPattern matcher = WildcardPattern.Get(pattern, WildcardOptions.IgnoreCase); - // We are doing WildCard search - foreach (PSSnapInInfo psSnapIn in searchList) - { - if (matcher.IsMatch(psSnapIn.Name)) - { - listToReturn.Add(psSnapIn.Name); - } - } - - // the returned list might contain 0 objects - return listToReturn; - } - - /// - /// See if the snapin is already loaded..returns load snapin info if true, null otherwise. - /// - /// - internal static PSSnapInInfo IsSnapInLoaded(Collection loadedSnapins, PSSnapInInfo psSnapInInfo) - { - if (null == loadedSnapins) - { - return null; - } - - foreach (PSSnapInInfo loadedPSSnapInInfo in loadedSnapins) - { - // See if the assembly-qualified names match and return the existing PSSnapInInfo - // if they do. - string loadedSnapInAssemblyName = loadedPSSnapInInfo.AssemblyName; - if (string.Equals(loadedPSSnapInInfo.Name, psSnapInInfo.Name, StringComparison.OrdinalIgnoreCase) && - !string.IsNullOrEmpty(loadedSnapInAssemblyName) - && string.Equals(loadedSnapInAssemblyName, psSnapInInfo.AssemblyName, System.StringComparison.OrdinalIgnoreCase)) - { - return loadedPSSnapInInfo; - } - } - - return null; - } - - /// - /// Routine to get the list of loaded snapins... - /// - /// - protected internal Collection GetSnapIns(string pattern) - { - // If RunspaceConfiguration is not null, then return the list that it has - if (Runspace != null) - { - if (pattern != null) - return Runspace.ConsoleInfo.GetPSSnapIn(pattern, _shouldGetAll); - else - return Runspace.ConsoleInfo.PSSnapIns; - } - - WildcardPattern matcher = null; - if (!String.IsNullOrEmpty(pattern)) - { - bool doWildCardSearch = WildcardPattern.ContainsWildcardCharacters(pattern); - - if (!doWildCardSearch) - { - // Verify PSSnapInID.. - // This will throw if it not a valid name - PSSnapInInfo.VerifyPSSnapInFormatThrowIfError(pattern); - } - matcher = WildcardPattern.Get(pattern, WildcardOptions.IgnoreCase); - } - - Collection snapins = new Collection(); - if (_shouldGetAll) - { - foreach (PSSnapInInfo snapinKey in PSSnapInReader.ReadAll()) - { - if (matcher == null || matcher.IsMatch(snapinKey.Name)) - snapins.Add(snapinKey); - } - } - else - { - // Otherwise, just scan through the list of cmdlets and rebuild the table. - List cmdlets = InvokeCommand.GetCmdlets(); - Dictionary snapinTable = new Dictionary(); - foreach (CmdletInfo cmdlet in cmdlets) - { - PSSnapInInfo snapin = cmdlet.PSSnapIn; - if (snapin != null && !snapinTable.ContainsKey(snapin)) - snapinTable.Add(snapin, true); - } - - foreach (PSSnapInInfo snapinKey in snapinTable.Keys) - { - if (matcher == null || matcher.IsMatch(snapinKey.Name)) - snapins.Add(snapinKey); - } - } - return snapins; - } - - /// - /// Use to indicate if all registered snapins should be listed by GetSnapins... - /// - protected internal bool ShouldGetAll - { - get { return _shouldGetAll; } - set { _shouldGetAll = value; } - } - private bool _shouldGetAll; - - /// - /// A single instance of the resource indirect reader. This is used to load the - /// managed resource assemblies in a different app-domain so that they can be unloaded. - /// For perf reasons we only want to create one instance for the duration of the command - /// and be sure it gets disposed when the command completes. - /// - /// - internal RegistryStringResourceIndirect ResourceReader - { - get { - return _resourceReader ?? (_resourceReader = RegistryStringResourceIndirect.GetResourceIndirectReader()); - } - } - private RegistryStringResourceIndirect _resourceReader; - #endregion - } - - #endregion - - #region Add-PSSnapIn - - /// - /// Class that implements add-pssnapin cmdlet. - /// - [Cmdlet(VerbsCommon.Add, "PSSnapin", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=113281")] - [OutputType(typeof(PSSnapInInfo))] - public sealed class AddPSSnapinCommand : PSSnapInCommandBase - { - #region Parameters - - /// - /// Property that gets/sets PSSnapIn Ids for the cmdlet. - /// - /// An array of strings representing PSSnapIn ids. - [Parameter( - Position = 0, - Mandatory = true, - ValueFromPipelineByPropertyName = true)] - public string[] Name - { - get - { - return _pssnapins; - } - - set - { - _pssnapins = value; - } - } - private string[] _pssnapins; - - /// - /// Gets or sets the Passthru flag for the operation. - /// If true, the PSSnapInInfo object is passed down the - /// output pipeline. - /// - [Parameter()] - public SwitchParameter PassThru - { - get - { - return _passThru; - } - set - { - _passThru = value; - } - } - private bool _passThru; - - #endregion - - #region Overrides - - /// - /// Adds pssnapins to console file and loads the pssnapin dlls into - /// the current monad runtime. - /// - /// - /// The new pssnapin information is not stored in the console file until - /// the file is saved. - /// - protected override void ProcessRecord() - { - // Cache for the information stored in the registry - // update the cache the first time a wildcard is found.. - Collection listToSearch = null; - - foreach (string pattern in _pssnapins) - { - Exception exception = null; - Collection listToAdd = new Collection(); - - try - { - // check whether there are any wildcard characters - bool doWildCardSearch = WildcardPattern.ContainsWildcardCharacters(pattern); - if (doWildCardSearch) - { - // wildcard found in the pattern - // Get all the possible candidates for current monad version - if (listToSearch == null) - { - // cache snapin registry information... - - // For 3.0 PowerShell, we still use "1" as the registry version key for - // Snapin and Custom shell lookup/discovery. - // For 3.0 PowerShell, we use "3" as the registry version key only for Engine - // related data like ApplicationBase etc. - listToSearch = PSSnapInReader.ReadAll(PSVersionInfo.RegistryVersion1Key); - } - - listToAdd = SearchListForPattern(listToSearch, pattern); - - // listToAdd wont be null.. - Diagnostics.Assert(listToAdd != null, "Pattern matching returned null"); - if (listToAdd.Count == 0) - { - if (_passThru) - { - // passThru is specified and we have nothing to add... - WriteNonTerminatingError(pattern, "NoPSSnapInsFound", - PSTraceSource.NewArgumentException(pattern, - MshSnapInCmdletResources.NoPSSnapInsFound, pattern), - ErrorCategory.InvalidArgument); - } - - continue; - } - } - else - { - listToAdd.Add(pattern); - } - - // now add all the snapins for this pattern... - AddPSSnapIns(listToAdd); - } - catch (PSArgumentException ae) - { - exception = ae; - } - catch (System.Security.SecurityException se) - { - exception = se; - } - - if (exception != null) - { - WriteNonTerminatingError(pattern, - "AddPSSnapInRead", - exception, - ErrorCategory.InvalidArgument); - } - } - } - - #endregion - - #region Private Methods - - /// - /// Adds one or more snapins - /// - /// List of snapin IDs - /// - /// This is a helper method and should not throw any - /// exceptions. All exceptions are caught and displayed - /// to the user using write* methods - /// - private void AddPSSnapIns(Collection snapInList) - { - if (snapInList == null) - { - // nothing to add - return; - } - - //BUGBUG TODO - brucepay - this is a workaround for not being able to dynamically update - // the set of cmdlets in a runspace if there is no RunspaceConfiguration object. - // This is a temporary fix to unblock remoting tests and need to be corrected/completed - // before we can ship... - - // If there is no RunspaceConfig object, then - // use an InitialSessionState object to gather and - // bind the cmdlets from the snapins... - if (Context.RunspaceConfiguration == null) - { - Collection loadedSnapins = base.GetSnapIns(null); - InitialSessionState iss = InitialSessionState.Create(); - bool isAtleastOneSnapinLoaded = false; - foreach (string snapIn in snapInList) - { - if (InitialSessionState.IsEngineModule(snapIn)) - { - WriteNonTerminatingError(snapIn, "LoadSystemSnapinAsModule", - PSTraceSource.NewArgumentException(snapIn, - MshSnapInCmdletResources.LoadSystemSnapinAsModule, snapIn), - ErrorCategory.InvalidArgument); - } - else - { - PSSnapInException warning; - try - { - // Read snapin data - PSSnapInInfo newPSSnapIn = PSSnapInReader.Read(Utils.GetCurrentMajorVersion(), snapIn); - PSSnapInInfo psSnapInInfo = IsSnapInLoaded(loadedSnapins, newPSSnapIn); - - // that means snapin is not already loaded ..so load the snapin - // now. - if (null == psSnapInInfo) - { - psSnapInInfo = iss.ImportPSSnapIn(snapIn, out warning); - isAtleastOneSnapinLoaded = true; - Context.InitialSessionState.ImportedSnapins.Add(psSnapInInfo.Name, psSnapInInfo); - } - // Write psSnapInInfo object only if passthru is specified. - if (_passThru) - { - // Load the pssnapin info properties that are localizable and redirected in the registry - psSnapInInfo.LoadIndirectResources(ResourceReader); - WriteObject(psSnapInInfo); - } - } - - catch (PSSnapInException pse) - { - WriteNonTerminatingError(snapIn, "AddPSSnapInRead", pse, ErrorCategory.InvalidData); - } - } - } - - if (isAtleastOneSnapinLoaded) - { - // Now update the session state with the new stuff... - iss.Bind(Context, /*updateOnly*/ true); - } - - return; - } - - foreach (string psSnapIn in snapInList) - { - Exception exception = null; - - try - { - PSSnapInException warning = null; - - PSSnapInInfo psSnapInInfo = this.Runspace.AddPSSnapIn(psSnapIn, out warning); - - if (warning != null) - { - WriteNonTerminatingError(psSnapIn, "AddPSSnapInRead", warning, ErrorCategory.InvalidData); - } - - // Write psSnapInInfo object only if passthru is specified. - if (_passThru) - { - // Load the pssnapin info properties that are localizable and redirected in the registry - psSnapInInfo.LoadIndirectResources(ResourceReader); - WriteObject(psSnapInInfo); - } - } - catch (PSArgumentException ae) - { - exception = ae; - } - catch (PSSnapInException sle) - { - exception = sle; - } - catch (System.Security.SecurityException se) - { - exception = se; - } - - if (exception != null) - { - WriteNonTerminatingError(psSnapIn, - "AddPSSnapInRead", - exception, - ErrorCategory.InvalidArgument); - } - } - } - - #endregion - } - - #endregion - - #region Remove-PSSnapIn - - /// - /// Class that implements remove-pssnapin cmdlet. - /// - [Cmdlet(VerbsCommon.Remove, "PSSnapin", SupportsShouldProcess = true, HelpUri = "https://go.microsoft.com/fwlink/?LinkID=113378")] - [OutputType(typeof(PSSnapInInfo))] - public sealed class RemovePSSnapinCommand : PSSnapInCommandBase - { - #region Parameters - - /// - /// Property that gets/sets PSSnapIn Ids for the cmdlet. - /// - /// An array of strings representing PSSnapIn ids. - [Parameter( - Position = 0, - Mandatory = true, - ValueFromPipelineByPropertyName = true)] - public string[] Name - { - get - { - return _pssnapins; - } - - set - { - _pssnapins = value; - } - } - private string[] _pssnapins; - - - /// - /// Gets or sets the Passthru flag for the operation. - /// If true, the PSSnapInInfo object is also passed - /// down the output pipeline. - /// - [Parameter()] - public SwitchParameter PassThru - { - get - { - return _passThru; - } - set - { - _passThru = value; - } - } - private bool _passThru; - - #endregion - - #region Overrides - - /// - /// Removes pssnapins from the current console file. - /// - /// - /// The pssnapin is not unloaded from the current engine. So all the cmdlets that are - /// represented by this pssnapin will continue to work. - /// - protected override void ProcessRecord() - { - foreach (string psSnapIn in _pssnapins) - { - Collection snapIns = GetSnapIns(psSnapIn); - - // snapIns won't be null.. - Diagnostics.Assert(snapIns != null, "GetSnapIns() returned null"); - if (snapIns.Count == 0) - { - WriteNonTerminatingError(psSnapIn, "NoPSSnapInsFound", - PSTraceSource.NewArgumentException(psSnapIn, - MshSnapInCmdletResources.NoPSSnapInsFound, psSnapIn), - ErrorCategory.InvalidArgument); - - continue; - } - - foreach (PSSnapInInfo snapIn in snapIns) - { - // confirm the operation first - // this is always false if WhatIf is set - if (ShouldProcess(snapIn.Name)) - { - Exception exception = null; - - if (this.Runspace == null && this.Context.InitialSessionState != null) - { - try - { - // Check if this snapin can be removed - - // Monad has specific restrictions on the mshsnapinid like - // mshsnapinid should be A-Za-z0-9.-_ etc. - PSSnapInInfo.VerifyPSSnapInFormatThrowIfError(snapIn.Name); - - if (MshConsoleInfo.IsDefaultPSSnapIn(snapIn.Name, this.Context.InitialSessionState.defaultSnapins)) - { - throw PSTraceSource.NewArgumentException(snapIn.Name, ConsoleInfoErrorStrings.CannotRemoveDefault, snapIn.Name); - } - - // Handle the initial session state case... - InitialSessionState iss = InitialSessionState.Create(); - PSSnapInException warning; - - // Get the snapin information... - iss.ImportPSSnapIn(snapIn, out warning); - iss.Unbind(Context); - Context.InitialSessionState.ImportedSnapins.Remove(snapIn.Name); - } - catch (PSArgumentException ae) - { - exception = ae; - } - - if (exception != null) - { - WriteNonTerminatingError(psSnapIn, "RemovePSSnapIn", exception, ErrorCategory.InvalidArgument); - } - } - else - { - try - { - PSSnapInException warning = null; - - PSSnapInInfo psSnapInInfo = this.Runspace.RemovePSSnapIn(snapIn.Name, out warning); - - if (warning != null) - { - WriteNonTerminatingError(snapIn.Name, "RemovePSSnapInRead", warning, ErrorCategory.InvalidData); - } - - if (_passThru) - { - // Load the pssnapin info properties that are localizable and redirected in the registry - psSnapInInfo.LoadIndirectResources(ResourceReader); - WriteObject(psSnapInInfo); - } - } - catch (PSArgumentException ae) - { - exception = ae; - } - - if (exception != null) - { - WriteNonTerminatingError(psSnapIn, "RemovePSSnapIn", exception, ErrorCategory.InvalidArgument); - } - } - } // ShouldContinue - } - } - } - - #endregion - } - - #endregion - - #region Get-PSSnapIn - - /// - /// Class that implements get-pssnapin cmdlet. - /// - [Cmdlet(VerbsCommon.Get, "PSSnapin", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=113330")] - [OutputType(typeof(PSSnapInInfo))] - public sealed class GetPSSnapinCommand : PSSnapInCommandBase - { - #region Parameters - - /// - /// Name(s) of PSSnapIn(s). - /// - [Parameter(Position = 0, Mandatory = false)] - [ValidateNotNullOrEmpty()] - public string[] Name - { - get - { - return _pssnapins; - } - set - { - _pssnapins = value; - } - } - private string[] _pssnapins; - - /// - /// Property that determines whether to get all pssnapins that are currently - /// registered ( in registry ). - /// - /// - /// A boolean that determines whether to get all pssnapins that are currently - /// registered ( in registry ). - /// - [Parameter(Mandatory = false)] - public SwitchParameter Registered - { - get - { - return ShouldGetAll; - } - set - { - ShouldGetAll = value; - } - } - - - #endregion - - #region Overrides - - /// - /// Constructs PSSnapInfo objects as requested by the user and writes them to the - /// output buffer. - /// - protected override void BeginProcessing() - { - if (_pssnapins != null) - { - foreach (string psSnapIn in _pssnapins) - { - Exception exception = null; - - try - { - Collection psSnapInInfoList = GetSnapIns(psSnapIn); - - // psSnapInInfoList wont be null.. - Diagnostics.Assert(psSnapInInfoList != null, "ConsoleInfo.GetPSSnapIn returned null"); - if (psSnapInInfoList.Count == 0) - { - WriteNonTerminatingError(psSnapIn, "NoPSSnapInsFound", - PSTraceSource.NewArgumentException(psSnapIn, - MshSnapInCmdletResources.NoPSSnapInsFound, psSnapIn), - ErrorCategory.InvalidArgument); - - continue; - } - - foreach (PSSnapInInfo pssnapinInfo in psSnapInInfoList) - { - // Load the pssnapin info properties that are localizable and redirected in the registry - pssnapinInfo.LoadIndirectResources(ResourceReader); - WriteObject(pssnapinInfo); - } - } - catch (System.Security.SecurityException se) - { - exception = se; - } - catch (PSArgumentException ae) - { - exception = ae; - } - - if (exception != null) - { - WriteNonTerminatingError(psSnapIn, "GetPSSnapInRead", exception, ErrorCategory.InvalidArgument); - } - } - } - else if (ShouldGetAll) - { - Exception exception = null; - - try - { - Collection psSnapInInfoList = PSSnapInReader.ReadAll(); - foreach (PSSnapInInfo pssnapinInfo in psSnapInInfoList) - { - // Load the pssnapin info properties that are localizable and redirected in the registry - pssnapinInfo.LoadIndirectResources(ResourceReader); - WriteObject(pssnapinInfo); - } - } - catch (System.Security.SecurityException se) - { - exception = se; - } - catch (PSArgumentException ae) - { - exception = ae; - } - - if (exception != null) - { - WriteNonTerminatingError(this, "GetPSSnapInRead", exception, ErrorCategory.InvalidArgument); - } - } - else - { - // this should never throw.. - Collection psSnapInInfoList = GetSnapIns(null); - foreach (PSSnapInInfo pssnapinInfo in psSnapInInfoList) - { - // Load the pssnapin info properties that are localizable and redirected in the registry - pssnapinInfo.LoadIndirectResources(ResourceReader); - WriteObject(pssnapinInfo); - } - } - } - - #endregion - } - - #endregion -} diff --git a/src/System.Management.Automation/singleshell/config/MshConsoleInfo.cs b/src/System.Management.Automation/singleshell/config/MshConsoleInfo.cs deleted file mode 100644 index 064e071f90..0000000000 --- a/src/System.Management.Automation/singleshell/config/MshConsoleInfo.cs +++ /dev/null @@ -1,959 +0,0 @@ -/********************************************************************++ -Copyright (c) Microsoft Corporation. All rights reserved. ---********************************************************************/ - -using System.Globalization; -using System.IO; -using System.Xml; -using System.Text; -using System.Collections.ObjectModel; -using System.Management.Automation.Internal; -using System.Collections.Generic; - -namespace System.Management.Automation.Runspaces -{ - /// - /// Class that understands Monad Console File Format. The format for the console - /// file is applied/read by this class only. - /// - /// Functionality: - /// 1. Schema version verification check. - /// 2. Data values for the content represented by Console file. Later this data - /// is used by other components ( MshConsoleInfo ) to construct Monad Types ( like - /// PSSnapInInfo ). - /// 3. Owns responsibility to read/write Files. - /// - /// Risk: - /// File Acces related security issues. - /// - /// Requires: - /// Might require Permissions to read/write into files. - /// - /// - internal class PSConsoleFileElement - { - #region Console format (xml) tags - // Create constants for each of the xml tags - private const string MSHCONSOLEFILE = "PSConsoleFile"; - private const string CSCHEMAVERSION = "ConsoleSchemaVersion"; - private const string CSCHEMAVERSIONNUMBER = "1.0"; - private const string PSVERSION = "PSVersion"; - private const string SNAPINS = "PSSnapIns"; - private const string SNAPIN = "PSSnapIn"; - private const string SNAPINNAME = "Name"; - - #endregion - - #region Data - - /// - /// MonadVersion from the console file - /// - internal string MonadVersion { get; } - - /// - /// List of MshSnapin IDs from the console file - /// - internal Collection PSSnapIns { get; } - - #endregion - - #region Constructor - - private PSConsoleFileElement(string version) - { - MonadVersion = version; - // Dont make collections null... - // making them null, wont be good for foreach statements - PSSnapIns = new Collection(); - } - - #endregion - - #region tracer - - private static readonly PSTraceSource s_mshsnapinTracer = PSTraceSource.GetTracer("MshSnapinLoadUnload", "Loading and unloading mshsnapins", false); - #endregion - - #region Static Methods - - /// - /// Writes MshConsoleInfo object in Monad Console format into the file specified by the . - /// - /// The absolute path of the file into which the content is saved. - /// The version of PowerShell. - /// The external snapins that are loaded in the console - /// - /// The path value is null. - /// - /// - internal static void WriteToFile(string path, Version version, IEnumerable snapins) - { - Diagnostics.Assert(path != null, "Filename should not be null"); - - s_mshsnapinTracer.WriteLine("Saving console info to file {0}.", path); - - XmlWriterSettings settings = new XmlWriterSettings(); - settings.Indent = true; - settings.Encoding = Encoding.UTF8; - - using (Stream stream = new FileStream(path, FileMode.OpenOrCreate)) - { - // Generate xml for the consoleinfo object. - using (XmlWriter writer = XmlWriter.Create(stream, settings)) - { - writer.WriteStartDocument(); - writer.WriteStartElement(MSHCONSOLEFILE); - writer.WriteAttributeString(CSCHEMAVERSION, CSCHEMAVERSIONNUMBER); - - writer.WriteStartElement(PSVERSION); - writer.WriteString(version.ToString()); - writer.WriteEndElement(); // MonadVersion - - writer.WriteStartElement(SNAPINS); - foreach (PSSnapInInfo mshSnapIn in snapins) - { - writer.WriteStartElement(SNAPIN); - writer.WriteAttributeString(SNAPINNAME, mshSnapIn.Name); - writer.WriteEndElement(); - } - writer.WriteEndElement(); // MshSnapins - - writer.WriteEndElement(); // Monad_ConsoleFile - writer.WriteEndDocument(); - } - } - - s_mshsnapinTracer.WriteLine("Saving console info succeeded."); - } - - /// - /// Reads a Monad Console file specified by and constructs - /// a PSConsoleFileElement. - /// - /// The absolute path of the file to read from. - /// A MShConsoleFileElement object that represents content of the console file. - /// The return object wont be null. - /// - /// There is a load or parser error in the XML. - /// - /// - internal static PSConsoleFileElement CreateFromFile(string path) - { - Diagnostics.Assert(path != null, "Filename should not be null"); - - s_mshsnapinTracer.WriteLine("Loading console info from file {0}.", path); - - XmlDocument doc = InternalDeserializer.LoadUnsafeXmlDocument( - new FileInfo(path), - false, /* ignore whitespace, comments, etc. */ - null); /* default maxCharactersInDocument */ - - // Validate content - if (doc[MSHCONSOLEFILE] == null) - { - s_mshsnapinTracer.TraceError("Console file {0} doesn't contain tag {1}.", path, MSHCONSOLEFILE); - - throw new XmlException( - StringUtil.Format(ConsoleInfoErrorStrings.MonadConsoleNotFound, path)); - } - - if ((doc[MSHCONSOLEFILE][PSVERSION] == null) || - (string.IsNullOrEmpty(doc[MSHCONSOLEFILE][PSVERSION].InnerText))) - { - s_mshsnapinTracer.TraceError("Console file {0} doesn't contain tag {1}.", path, PSVERSION); - - throw new XmlException( - StringUtil.Format(ConsoleInfoErrorStrings.MonadVersionNotFound, path)); - } - - // This will never be null.. - XmlElement xmlElement = (XmlElement)doc[MSHCONSOLEFILE]; - - if (xmlElement.HasAttribute(CSCHEMAVERSION)) - { - if (!xmlElement.GetAttribute(CSCHEMAVERSION).Equals(CSCHEMAVERSIONNUMBER, StringComparison.OrdinalIgnoreCase)) - { - string resourceTemplate = - StringUtil.Format(ConsoleInfoErrorStrings.BadConsoleVersion, path); - string message = string.Format(CultureInfo.CurrentCulture, - resourceTemplate, CSCHEMAVERSIONNUMBER); - - s_mshsnapinTracer.TraceError(message); - - throw new XmlException(message); - } - } - else - { - s_mshsnapinTracer.TraceError("Console file {0} doesn't contain tag schema version.", path); - - throw new XmlException( - StringUtil.Format(ConsoleInfoErrorStrings.BadConsoleVersion, path)); - } - - //process MonadVersion - //This will never be null.. - xmlElement = (XmlElement)doc[MSHCONSOLEFILE][PSVERSION]; - - // Construct PSConsoleFileElement as we seem to have valid data - PSConsoleFileElement consoleFileElement = new PSConsoleFileElement(xmlElement.InnerText.Trim()); - - bool isPSSnapInsProcessed = false; - bool isPSVersionProcessed = false; - - for (XmlNode mshSnapInsNode = doc["PSConsoleFile"].FirstChild; mshSnapInsNode != null; mshSnapInsNode = mshSnapInsNode.NextSibling) - { - if (mshSnapInsNode.NodeType == XmlNodeType.Comment) - { - // support comments inside a PSConsoleFile Element - continue; - } - - //populate mshsnapin information - xmlElement = mshSnapInsNode as XmlElement; - - if (null == xmlElement) - { - throw new XmlException(ConsoleInfoErrorStrings.BadXMLFormat); - } - - if (xmlElement.Name == PSVERSION) - { - if (isPSVersionProcessed) - { - s_mshsnapinTracer.TraceError("Console file {0} contains more than one msh versions", path); - - throw new XmlException(StringUtil.Format(ConsoleInfoErrorStrings.MultipleMshSnapinsElementNotSupported, PSVERSION)); - } - - isPSVersionProcessed = true; - continue; - } - - if (xmlElement.Name != SNAPINS) - { - s_mshsnapinTracer.TraceError("Tag {0} is not supported in console file", xmlElement.Name); - - throw new XmlException(StringUtil.Format(ConsoleInfoErrorStrings.BadXMLElementFound, xmlElement.Name, MSHCONSOLEFILE, PSVERSION, SNAPINS)); - } - - // PSSnapIns element is already processed. We dont support multiple - // PSSnapIns elements - if (isPSSnapInsProcessed) - { - s_mshsnapinTracer.TraceError("Console file {0} contains more than one mshsnapin lists", path); - - throw new XmlException(StringUtil.Format(ConsoleInfoErrorStrings.MultipleMshSnapinsElementNotSupported, SNAPINS)); - } - - // We are about to process mshsnapins element..so we should not - // process some more mshsnapins elements..this boolean keeps track - // of this. - isPSSnapInsProcessed = true; - - // decode all the child nodes of node... - for (XmlNode mshSnapInNode = xmlElement.FirstChild; mshSnapInNode != null; mshSnapInNode = mshSnapInNode.NextSibling) - { - XmlElement mshSnapInElement = mshSnapInNode as XmlElement; - - if ((null == mshSnapInElement) || (mshSnapInElement.Name != SNAPIN)) - { - throw new XmlException( - StringUtil.Format(ConsoleInfoErrorStrings.PSSnapInNotFound, mshSnapInNode.Name)); - } - - string id = mshSnapInElement.GetAttribute(SNAPINNAME); - - if (string.IsNullOrEmpty(id)) - { - throw new XmlException(ConsoleInfoErrorStrings.IDNotFound); - } - - consoleFileElement.PSSnapIns.Add(id); - - s_mshsnapinTracer.WriteLine("Found in mshsnapin {0} in console file {1}", id, path); - } - } - - return consoleFileElement; - } - - #endregion - } - - /// - /// Class that manages(reads/writes) Monad Console files and constructs objects - /// that represent data in the console files. - /// - /// Functionality: - /// 1. Access point to the console files for Runspace and cmdlets - /// 2. Depends on PSConsoleFileElement for reading/writing files - /// - /// - internal class MshConsoleInfo - { - #region Private Data - - // Monad Version that this console file depends on. - // MshSnapins that are not shipped with monad. - private readonly Collection _externalPSSnapIns; - // Monad specific mshsnapins - private Collection _defaultPSSnapIns; - // An internal representation that tells whether a console file is modified. - // A string that stores fileName of the current consoleinfo object - - #endregion - - #region Class specific data - - private static readonly PSTraceSource s_mshsnapinTracer = PSTraceSource.GetTracer("MshSnapinLoadUnload", "Loading and unloading mshsnapins", false); - - #endregion - - #region Properties - - /// - /// Monad Version that the console file depends on. - /// - internal Version PSVersion { get; } - - /// - /// Returns the major version of current console. - /// - internal string MajorVersion - { - get - { - Diagnostics.Assert(PSVersion != null, - "PSVersion is null"); - - return PSVersion.Major.ToString(System.Globalization.CultureInfo.InvariantCulture); - } - } - - /// - /// List of mshsnapins that are available. This includes both the monad - /// default mshsnapins as well as external mshsnapins as represented by the - /// console file. - /// - /// - /// The list returned is an ordered-list with default mshsnapins at the - /// start followed by external mshsnapins in the order represented by the - /// console file. - /// - internal Collection PSSnapIns - { - get - { - return MergeDefaultExternalMshSnapins(); - } - } - - /// - /// List of external mshsnapins, as represented by the console file and cmdlets - /// add-pssnapin,remove-pssnapin, that are available. - /// - internal Collection ExternalPSSnapIns - { - get - { - // externalPSSnapIns is never null - Diagnostics.Assert(_externalPSSnapIns != null, "externalPSSnapIns is null"); - - return _externalPSSnapIns; - } - } - - /// - /// A boolean which tells whether the console file is modified after it is read - /// or created. - /// - /// - /// Modification refers to addition/deletion operations of the external mshsnapins. - /// - internal bool IsDirty { get; private set; } - - /// - /// A string representing the console file name of the current MshConsoleInfo object. - /// If the filename is relative path, an absolute path will be constructed using - /// Path.GetFullPath() - /// - /// - /// Once a MshConsoleInfo object is constructed, a user may update the object by - /// adding,removing mshsnapins. These operations directly effect the state of the - /// MshConsoleInfo object but not update the console file. - /// - internal string Filename { get; private set; } - - #endregion - - #region Constructors - - /// - /// Construct a MshConsoleInfo object for the Monad version specified. - /// - /// Monad Version. - private MshConsoleInfo(Version version) - { - PSVersion = version; - IsDirty = false; - Filename = null; - - // Initialize list of mshsnapins.. - _defaultPSSnapIns = new Collection(); - _externalPSSnapIns = new Collection(); - } - - #endregion - - #region Staic Methods - - /// - /// Constructs a object for the - /// current Monad version which is already started. - /// - /// - /// One or more default mshsnapins cannot be loaded because the - /// registry is not populated correctly. - /// - internal static MshConsoleInfo CreateDefaultConfiguration() - { - // Steps: - // 1. Get the current Monad Version - // 2. Create MshConsoleInfo object. - // 3. Read default mshsnapins. - - MshConsoleInfo consoleInfo = new MshConsoleInfo(PSVersionInfo.PSVersion); - try - { - consoleInfo._defaultPSSnapIns = PSSnapInReader.ReadEnginePSSnapIns(); - } - catch (PSArgumentException ae) - { - string message = ConsoleInfoErrorStrings.CannotLoadDefaults; - // If we were unable to load default mshsnapins throw PSSnapInException - - s_mshsnapinTracer.TraceError(message); - - throw new PSSnapInException(message, ae); - } - catch (System.Security.SecurityException se) - { - string message = ConsoleInfoErrorStrings.CannotLoadDefaults; - // If we were unable to load default mshsnapins throw PSSnapInException - - s_mshsnapinTracer.TraceError(message); - - throw new PSSnapInException(message, se); - } - - return consoleInfo; - } - - /// - /// Constructs a object from a - /// Monad console file. - /// - /// - /// Monad console file name. If the filename is not absolute path. Then absolute path is - /// constructed by using Path.GetFullPath() API. - /// - /// - /// PSConsoleLoadException occurred while loading this console file. This object - /// also contains specific PSSnapInExceptions that occurred while loading. - /// - /// - /// One or more default mshsnapins cannot be loaded because the - /// registry is not populated correctly. - /// - /// - /// fileName is null. - /// - /// - /// 1. fileName does not specify proper file extension. - /// - /// - /// fileName contains one or more of the invalid characters defined in System.IO.Path.InvalidPathChars. - /// - /// - /// Unable to load/parse the file specified by fileName. - /// - internal static MshConsoleInfo CreateFromConsoleFile(string fileName, out PSConsoleLoadException cle) - { - s_mshsnapinTracer.WriteLine("Creating console info from file {0}", fileName); - - // Construct default mshsnapins - MshConsoleInfo consoleInfo = CreateDefaultConfiguration(); - - // Check whether the filename specified is an absolute path. - string absolutePath = Path.GetFullPath(fileName); - consoleInfo.Filename = absolutePath; - - // Construct externalPSSnapIns by loading file. - consoleInfo.Load(absolutePath, out cle); - - s_mshsnapinTracer.WriteLine("Console info created successfully"); - - return consoleInfo; - } - - #endregion - - #region Internal Instance Methods - - /// - /// Saves the current object to a file specified - /// by . IsDirty is set to false once file is saved. - /// - /// - /// If path is not an absolute path, then an absolute path is constructed by - /// using Path.GetFullPath() API. - /// - /// - /// 1.Path does not specify proper file extension. - /// - /// - /// 1. Path is null. - /// - /// - /// path contains one or more of the invalid characters defined in System.IO.Path.InvalidPathChars. - /// - internal void SaveAsConsoleFile(string path) - { - if (null == path) - { - throw PSTraceSource.NewArgumentNullException("path"); - } - - // Check whether the filename specified is an absolute path. - string absolutePath = path; - - if (!Path.IsPathRooted(absolutePath)) - { - absolutePath = Path.GetFullPath(Filename); - } - - // Ignore case when looking for file extension. - if (!absolutePath.EndsWith(StringLiterals.PowerShellConsoleFileExtension, StringComparison.OrdinalIgnoreCase)) - { - s_mshsnapinTracer.TraceError("Console file {0} doesn't have the right extension {1}.", path, StringLiterals.PowerShellConsoleFileExtension); - throw PSTraceSource.NewArgumentException("absolutePath", ConsoleInfoErrorStrings.BadConsoleExtension); - } - - //ConsoleFileElement will write to file - PSConsoleFileElement.WriteToFile(absolutePath, this.PSVersion, this.ExternalPSSnapIns); - //update the console file variable - Filename = absolutePath; - IsDirty = false; - } - - /// - /// Saves the current object to its console file. - /// IsDirty is set to false once file is saved. - /// - /// - /// Msh is loaded with default mshsnapins. $console is currently empty. - /// - internal void Save() - { - if (null == Filename) - { - throw PSTraceSource.NewInvalidOperationException(ConsoleInfoErrorStrings.SaveDefaultError); - } - - PSConsoleFileElement.WriteToFile(Filename, this.PSVersion, this.ExternalPSSnapIns); - IsDirty = false; - } - - /// - /// Adds a mshsnapin specified by to the current list of - /// mshsnapins. If the mshsnapin is successfully added, IsDirty property is set to true. - /// - /// ID of the mshsnapin which needs to be added. - /// A object corresponding to mshSnapInID. - /// PSSnapIn information must be present in the registry for this call to succeed. - /// - /// mshSnapInID is empty or null. - /// - /// - /// PSSnapIn is already loaded. - /// No PSSnapIn with given id found. - /// PSSnapIn cannot be loaded. - /// - /// - /// Caller doesn't have permission to read keys. - /// - internal PSSnapInInfo AddPSSnapIn(string mshSnapInID) - { - if (string.IsNullOrEmpty(mshSnapInID)) - { - PSTraceSource.NewArgumentNullException("mshSnapInID"); - } - - // Check whether the mshsnapin is already present in defaultmshsnapins/externalMshSnapins - if (IsDefaultPSSnapIn(mshSnapInID, _defaultPSSnapIns)) - { - s_mshsnapinTracer.TraceError("MshSnapin {0} can't be added since it is a default mshsnapin", mshSnapInID); - - throw PSTraceSource.NewArgumentException("mshSnapInID", ConsoleInfoErrorStrings.CannotLoadDefault); - } - - if (IsActiveExternalPSSnapIn(mshSnapInID)) - { - s_mshsnapinTracer.TraceError("MshSnapin {0} is already loaded.", mshSnapInID); - - throw PSTraceSource.NewArgumentException("mshSnapInID", ConsoleInfoErrorStrings.PSSnapInAlreadyExists, mshSnapInID); - } - - // Check whether the mshsnapin is present in the registry. - PSSnapInInfo newPSSnapIn = PSSnapInReader.Read(this.MajorVersion, mshSnapInID); - - if (!Utils.IsPSVersionSupported(newPSSnapIn.PSVersion.ToString())) - { - s_mshsnapinTracer.TraceError("MshSnapin {0} and current monad engine's versions don't match.", mshSnapInID); - - throw PSTraceSource.NewArgumentException("mshSnapInID", - ConsoleInfoErrorStrings.AddPSSnapInBadMonadVersion, - newPSSnapIn.PSVersion.ToString(), - PSVersion.ToString()); - } - - // new mshsnapin will never be null - //if this is a valid new mshsnapin,add this to external mshsnapins - _externalPSSnapIns.Add(newPSSnapIn); - s_mshsnapinTracer.WriteLine("MshSnapin {0} successfully added to consoleinfo list.", mshSnapInID); - //Set IsDirty to true - IsDirty = true; - - return newPSSnapIn; - } - - /// - /// Removes a mshsnapin specified by from the current - /// list. - /// - /// ID of the mshsnapin which needs to be removed - /// PSSnapInInfo object for the mshsnapin that is removed. - /// MshSnapin is removed only from the console file. Registry entry - /// is not touched. - /// - /// mshSnapInID is null. - /// - /// - /// 1. mshSnapInID is either a default mshsnapin or not loaded. - /// 2. mshSnapInId is not valid. - /// - internal PSSnapInInfo RemovePSSnapIn(string mshSnapInID) - { - if (string.IsNullOrEmpty(mshSnapInID)) - { - PSTraceSource.NewArgumentNullException("mshSnapInID"); - } - - // Monad has specific restrictions on the mshsnapinid like - // mshsnapinid should be A-Za-z0-9.-_ etc. - PSSnapInInfo.VerifyPSSnapInFormatThrowIfError(mshSnapInID); - - PSSnapInInfo removedPSSnapIn = null; - // Check external mshsnapins - foreach (PSSnapInInfo mshSnapIn in _externalPSSnapIns) - { - if (string.Equals(mshSnapInID, mshSnapIn.Name, System.StringComparison.OrdinalIgnoreCase)) - { - // We found the mshsnapin..remove from the list and break - removedPSSnapIn = mshSnapIn; - _externalPSSnapIns.Remove(mshSnapIn); - - // The state of console file is changing..so set - // dirty flag. - IsDirty = true; - break; - } - } - - if (removedPSSnapIn == null) - { - if (IsDefaultPSSnapIn(mshSnapInID, _defaultPSSnapIns)) - { - s_mshsnapinTracer.WriteLine("MshSnapin {0} can't be removed since it is a default mshsnapin.", mshSnapInID); - - throw PSTraceSource.NewArgumentException("mshSnapInID", ConsoleInfoErrorStrings.CannotRemoveDefault, mshSnapInID); - } - - throw PSTraceSource.NewArgumentException("mshSnapInID", ConsoleInfoErrorStrings.CannotRemovePSSnapIn, mshSnapInID); - } - - return removedPSSnapIn; - } - - /// - /// Searches for mshsnapin in either current console or registry as determined - /// by . - /// - /// - /// Id/WildcardPattern of the mshsnapin to search for. This can contain wildcard characters as - /// represented by WildCardPattern. - /// - /// - /// A boolean which determines whether to search in the current console or registry. - /// - /// A collection of mshsnapininfo objects. - /// - /// 1.Unable to read registry entries for mshsnapins. - /// 2.Pattern specified is not valid. If pattern doesnt contain - /// wildcard characters, this function checks for the validity - /// of the mshsnapin name. - /// - /// - /// Caller doesn't have permission to read keys. - /// - internal Collection GetPSSnapIn(string pattern, bool searchRegistry) - { - // We want to improve the search speed by noting whether we want - // to perform wildcard search. - bool doWildCardSearch = WildcardPattern.ContainsWildcardCharacters(pattern); - - if (!doWildCardSearch) - { - // Verify PSSnapInID.. - // This will throw if it not a valid name - PSSnapInInfo.VerifyPSSnapInFormatThrowIfError(pattern); - } - - // Build the list to search..If searchRegistry is true get all mshsnapins available - // from the registry, otherwise get mshsnapins from the current console. - Collection listToSearch = searchRegistry ? - PSSnapInReader.ReadAll() : PSSnapIns; - - // Create a list to return.. - Collection listToReturn = new Collection(); - - // If there is nothing to search.. - if (listToSearch == null) - return listToReturn; - - if (!doWildCardSearch) - { - // We are not doing wildcard search.. - foreach (PSSnapInInfo mshSnapIn in listToSearch) - { - if (string.Equals(mshSnapIn.Name, pattern, StringComparison.OrdinalIgnoreCase)) - { - listToReturn.Add(mshSnapIn); - } - } - } - else - { - WildcardPattern matcher = WildcardPattern.Get(pattern, WildcardOptions.IgnoreCase); - // We are doing WildCard search - foreach (PSSnapInInfo mshSnapIn in listToSearch) - { - if (matcher.IsMatch(mshSnapIn.Name)) - { - listToReturn.Add(mshSnapIn); - } - } - } - - // return whatever we found..may be 0.. - return listToReturn; - } - - #endregion - - #region Private Methods - - /// - /// Loads a Monad Console file specified by - /// - /// - /// The absolute path from which the content is loaded. - /// - /// - /// PSConsoleLoadException occurred while loading this console file. This object - /// also contains specific PSSnapInExceptions that occurred while loading. - /// - /// - /// A list of objects specified in the console file. - /// - /// - /// Path is null. - /// - /// - /// 1. Path does not specify proper file extension. - /// 2. PSSnapInId doesnt contain valid characters. - /// 3. Path is not an Absolute Path. - /// Example of valid paths:"\\MyDir\\MyFile.txt" and "C:\\MyDir". - /// - /// - /// path contains one or more of the invalid characters defined in System.IO.Path.InvalidPathChars. - /// - /// - /// Unable to load/parse the file specified by path. - /// - private Collection Load(string path, out PSConsoleLoadException cle) - { - // Initialize the out parameter.. - cle = null; - - s_mshsnapinTracer.WriteLine("Load mshsnapins from console file {0}", path); - - if (string.IsNullOrEmpty(path)) - { - throw PSTraceSource.NewArgumentNullException("path"); - } - - // Check whether the path is an absolute path - if (!Path.IsPathRooted(path)) - { - s_mshsnapinTracer.TraceError("Console file {0} needs to be a absolute path.", path); - - throw PSTraceSource.NewArgumentException("path", ConsoleInfoErrorStrings.PathNotAbsolute, path); - } - - if (!path.EndsWith(StringLiterals.PowerShellConsoleFileExtension, StringComparison.OrdinalIgnoreCase)) - { - s_mshsnapinTracer.TraceError("Console file {0} needs to have {1} extension.", path, StringLiterals.PowerShellConsoleFileExtension); - - throw PSTraceSource.NewArgumentException("path", ConsoleInfoErrorStrings.BadConsoleExtension); - } - - PSConsoleFileElement consoleFileElement; - - // exceptions are thrown to the caller - consoleFileElement = PSConsoleFileElement.CreateFromFile(path); - - // consoleFileElement will never be null.. - if (!Utils.IsPSVersionSupported(consoleFileElement.MonadVersion)) - { - s_mshsnapinTracer.TraceError("Console version {0} is not supported in current monad session.", consoleFileElement.MonadVersion); - - throw PSTraceSource.NewArgumentException("PSVersion", ConsoleInfoErrorStrings.BadMonadVersion, consoleFileElement.MonadVersion, - PSVersion.ToString()); - } - - // Create a store for exceptions - Collection exceptions = new Collection(); - - foreach (string mshsnapin in consoleFileElement.PSSnapIns) - { - try - { - this.AddPSSnapIn(mshsnapin); - } - catch (PSArgumentException ae) - { - PSSnapInException sle = new PSSnapInException(mshsnapin, ae.Message, ae); - - // Eat ArgumentException and continue.. - exceptions.Add(sle); - } - catch (System.Security.SecurityException se) - { - string message = ConsoleInfoErrorStrings.PSSnapInReadError; - PSSnapInException sle = new PSSnapInException(mshsnapin, message, se); - // Eat SecurityException and continue.. - - exceptions.Add(sle); - } - } - - // Before returning check whether there are any exceptions - if (exceptions.Count > 0) - { - cle = new PSConsoleLoadException(this, exceptions); - } - - // We are able to load console file and currently monad engine - // can service this. So mark the isdirty flag. - IsDirty = false; - - return _externalPSSnapIns; - } - - /// - /// Checks whether the mshsnapin is a default mshsnapin - /// - /// Id of the mshsnapin - /// List of default mshsnapins - /// True if PSSnapIn is default.False otherwise. - internal static bool IsDefaultPSSnapIn(string mshSnapInID, IEnumerable defaultSnapins) - { - // Check whether the mshsnapin is present in defaultmshsnapins. - foreach (PSSnapInInfo mshSnapIn in defaultSnapins) - { - if (string.Equals(mshSnapInID, mshSnapIn.Name, System.StringComparison.OrdinalIgnoreCase)) - { - return true; - } - } - - return false; - } - - /// - /// Checks whether the mshsnapin is already loaded - /// - /// Id of the mshsnapin - /// True if PSSnapIn is loaded.False otherwise. - private bool IsActiveExternalPSSnapIn(string mshSnapInID) - { - // Check whether the mshsnapin is present in externalmshsnapins. - foreach (PSSnapInInfo mshSnapIn in _externalPSSnapIns) - { - if (string.Equals(mshSnapInID, mshSnapIn.Name, System.StringComparison.OrdinalIgnoreCase)) - { - return true; - } - } - - return false; - } - - - /// - /// Constructs a new list of mshsnapins from default mshsnapins and external mshsnapins. - /// - /// A list of mshsnapins represented by the current console file - private Collection MergeDefaultExternalMshSnapins() - { - //Default mshsnapins should never be null - Diagnostics.Assert(_defaultPSSnapIns != null, "Default MshSnapins for the current console is empty"); - Collection mshSnapIns = new Collection(); - - foreach (PSSnapInInfo mshSnapIn in _defaultPSSnapIns) - { - mshSnapIns.Add(mshSnapIn); - } - - foreach (PSSnapInInfo mshSnapIn in _externalPSSnapIns) - { - mshSnapIns.Add(mshSnapIn); - } - - return mshSnapIns; - } - #endregion - } -} diff --git a/src/System.Management.Automation/singleshell/config/MshConsoleLoadException.cs b/src/System.Management.Automation/singleshell/config/MshConsoleLoadException.cs index 7eeb95dd96..ee252cd112 100644 --- a/src/System.Management.Automation/singleshell/config/MshConsoleLoadException.cs +++ b/src/System.Management.Automation/singleshell/config/MshConsoleLoadException.cs @@ -24,25 +24,6 @@ namespace System.Management.Automation.Runspaces [Serializable] public class PSConsoleLoadException : SystemException, IContainsErrorRecord { - /// - /// Initiate an instance of PSConsoleLoadException. - /// - /// Console info object for the exception - /// A collection of PSSnapInExceptions. - internal PSConsoleLoadException(MshConsoleInfo consoleInfo, Collection exceptions) - : base() - { - if (!String.IsNullOrEmpty(consoleInfo.Filename)) - _consoleFileName = consoleInfo.Filename; - - if (exceptions != null) - { - _PSSnapInExceptions = exceptions; - } - - CreateErrorRecord(); - } - /// /// Initiate an instance of PSConsoleLoadException. /// @@ -69,6 +50,23 @@ namespace System.Management.Automation.Runspaces { } + private ErrorRecord _errorRecord; + + /// + /// Gets error record embedded in this exception. + /// + /// + public ErrorRecord ErrorRecord + { + get + { + return _errorRecord; + } + } + /// /// Create the internal error record. /// The ErrorRecord created will be stored in the _errorRecord member. @@ -87,28 +85,8 @@ namespace System.Management.Automation.Runspaces } _errorRecord = new ErrorRecord(new ParentContainsErrorRecordException(this), "ConsoleLoadFailure", ErrorCategory.ResourceUnavailable, null); - _errorRecord.ErrorDetails = new ErrorDetails(String.Format(ConsoleInfoErrorStrings.ConsoleLoadFailure, _consoleFileName, sb.ToString())); } - private ErrorRecord _errorRecord; - - /// - /// Gets error record embedded in this exception. - /// - /// - public ErrorRecord ErrorRecord - { - get - { - return _errorRecord; - } - } - - private string _consoleFileName = ""; - private Collection _PSSnapInExceptions = new Collection(); internal Collection PSSnapInExceptions { @@ -135,42 +113,6 @@ namespace System.Management.Automation.Runspaces } } } - - #region Serialization - - /// - /// Initiate a PSConsoleLoadException instance. - /// - /// Serialization information - /// Streaming context - protected PSConsoleLoadException(SerializationInfo info, - StreamingContext context) - : base(info, context) - { - _consoleFileName = info.GetString("ConsoleFileName"); - - CreateErrorRecord(); - } - - /// - /// Get object data from serialization information. - /// - /// Serialization information - /// Streaming context - [SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)] - public override void GetObjectData(SerializationInfo info, StreamingContext context) - { - if (info == null) - { - throw PSTraceSource.NewArgumentNullException("info"); - } - - base.GetObjectData(info, context); - - info.AddValue("ConsoleFileName", _consoleFileName); - } - - #endregion Serialization } } diff --git a/src/System.Management.Automation/singleshell/config/RegistryStringResourceIndirect.cs b/src/System.Management.Automation/singleshell/config/RegistryStringResourceIndirect.cs deleted file mode 100644 index ef3ab91732..0000000000 --- a/src/System.Management.Automation/singleshell/config/RegistryStringResourceIndirect.cs +++ /dev/null @@ -1,611 +0,0 @@ -/********************************************************************++ -Copyright (c) Microsoft Corporation. All rights reserved. ---********************************************************************/ - -using System.Collections; -using System.Globalization; -using System.IO; -using System.Reflection; -using System.Resources; -using System.Security; -using Microsoft.Win32; -using Dbg = System.Management.Automation.Diagnostics; - -namespace System.Management.Automation -{ - /// - /// This class is responsible for loading resources using the PSSnapin dll and - /// associated registry entries. - /// - /// - /// - /// The class creates an app-domain to load the resource assemblies in. Upon dispose - /// the class unloads the app-domain to ensure the assemblies get unloaded. It uses - /// ReflectionOnlyLoad and ReflectionOnlyLoadFrom to ensure that no code can execute - /// and that dependencies are not loaded. This allows us to load assemblies that were - /// built with different version of the CLR. - /// - /// - internal sealed class RegistryStringResourceIndirect : IDisposable - { - /// - /// Creates an instance of the RegistryStringResourceIndirect class. - /// - /// - /// - /// A new instance of the RegistryStringResourceIndirect class. - /// - /// - internal static RegistryStringResourceIndirect GetResourceIndirectReader() - { - return new RegistryStringResourceIndirect(); - } - - #region IDisposable Members - - /// - /// Set to true when object is disposed - /// - /// - private bool _disposed; - - /// - /// Dispose method unloads the app domain that was - /// created in the constructor. - /// - /// - public void Dispose() - { - if (_disposed == false) - { - if (_domain != null) - { - AppDomain.Unload(_domain); - _domain = null; - _resourceRetriever = null; - } - } - _disposed = true; - } - - #endregion IDisposable Members - - /// - /// The app-domain in which the resources will be loaded. - /// - /// - private AppDomain _domain; - - /// - /// The class that is created in the app-domain which does the resource loading. - /// - /// - private ResourceRetriever _resourceRetriever; - - /// - /// Creates the app-domain and the instance of the ResourceRetriever and - /// sets the private fields with the references. - /// - /// - private void CreateAppDomain() - { - if (_domain == null) - { - // Create an app-domain to load the resource assemblies in so that they can be - // unloaded. - - _domain = AppDomain.CreateDomain("ResourceIndirectDomain"); - _resourceRetriever = - (ResourceRetriever)_domain.CreateInstanceAndUnwrap( - Assembly.GetExecutingAssembly().FullName, - "System.Management.Automation.ResourceRetriever"); - } - } - - /// - /// Retrieves a resource string based on a resource reference stored in the specified - /// registry key. - /// - /// - /// - /// The key in which there is a value that contains the reference to the resource - /// to retrieve. - /// - /// - /// - /// The name of the value in the registry key that contains the reference to the resource. - /// - /// - /// - /// The full name of the assembly from which to load the resource. - /// - /// - /// - /// The full path of the assembly from which to load the resource. - /// - /// - /// - /// The resource string that was loaded or null if it could not be found. - /// - /// - /// - /// This method ensures that an appropriate registry entry exists and that it contains - /// a properly formatted resource reference ("BaseName,ResourceID"). It then creates an - /// app-domain (or uses and existing one if it already exists on the instance of the class) - /// and an instance of the ResourceRetriever in that app-domain. It then calls the ResourceRetriever - /// to load the specified assembly and retrieve the resource. The assembly is loaded using ReflectionOnlyLoad - /// or ReflectionOnlyLoadFrom using the assemblyName or moduleName (respectively) so that - /// no code can be executed. - /// - /// The app-domain is unloaded when this class instance is disposed. - /// - /// - internal string GetResourceStringIndirect( - RegistryKey key, - string valueName, - string assemblyName, - string modulePath) - { - if (_disposed) - { - throw PSTraceSource.NewInvalidOperationException(MshSnapinInfo.ResourceReaderDisposed); - } - - if (key == null) - { - throw PSTraceSource.NewArgumentNullException("key"); - } - - if (String.IsNullOrEmpty(valueName)) - { - throw PSTraceSource.NewArgumentException("valueName"); - } - - if (String.IsNullOrEmpty(assemblyName)) - { - throw PSTraceSource.NewArgumentException("assemblyName"); - } - - if (String.IsNullOrEmpty(modulePath)) - { - throw PSTraceSource.NewArgumentException("modulePath"); - } - - string result = null; - - do // false loop - { - // Read the resource reference from the registry - string regValue = GetRegKeyValueAsString(key, valueName); - - if (regValue == null) - { - break; - } - - result = GetResourceStringIndirect(assemblyName, modulePath, regValue); - } while (false); - - return result; - } - - /// - /// Retrieves a resource string based on a resource reference supplied in . - /// - /// - /// - /// The full name of the assembly from which to load the resource. - /// - /// - /// - /// The full path of the assembly from which to load the resource. - /// - /// - /// - /// A comma separated basename and resource id pair. - /// - /// - /// - /// The resource string that was loaded or null if it could not be found. - /// - /// - /// - /// This method ensures that is a properly formatted - /// resource reference ("BaseName,ResourceID"). It then creates an app-domain (or uses - /// an existing one if it already exists on the instance of the class) and an instance - /// of the ResourceRetriever in that app-domain. It then calls the ResourceRetriever - /// to load the specified assembly and retrieve the resource. The assembly is loaded using ReflectionOnlyLoad - /// or ReflectionOnlyLoadFrom using the assemblyName or moduleName (respectively) so that - /// no code can be executed. - /// - /// The app-domain is unloaded when this class instance is disposed. - /// - /// - internal string GetResourceStringIndirect( - string assemblyName, - string modulePath, - string baseNameRIDPair) - { - if (_disposed) - { - throw PSTraceSource.NewInvalidOperationException(MshSnapinInfo.ResourceReaderDisposed); - } - - if (String.IsNullOrEmpty(assemblyName)) - { - throw PSTraceSource.NewArgumentException("assemblyName"); - } - - if (String.IsNullOrEmpty(modulePath)) - { - throw PSTraceSource.NewArgumentException("modulePath"); - } - - if (String.IsNullOrEmpty(baseNameRIDPair)) - { - throw PSTraceSource.NewArgumentException("baseNameRIDPair"); - } - - string result = null; - - do // false loop - { - // Initialize the app-domain and resource reader if not already initialized - if (_resourceRetriever == null) - { - CreateAppDomain(); - } - - // If the app-domain failed to load or the ResourceRetriever instance wasn't - // created, then return null. - - if (_resourceRetriever == null) - { - break; - } - - // Parse the resource reference - string[] resourceSplit = baseNameRIDPair.Split(Utils.Separators.Comma); - if (resourceSplit.Length != 2) - { - break; - } - - string baseName = resourceSplit[0]; - string resourceID = resourceSplit[1]; - - // Get the resource in the app-domain - result = _resourceRetriever.GetStringResource(assemblyName, modulePath, baseName, resourceID); - } while (false); - - return result; - } - - /// - /// Retrieves a string value from the registry - /// - /// - /// - /// The key to retrieve the value from. - /// - /// - /// - /// The name of the value to retrieve. - /// - /// - /// - /// The string value of the registry key value. - /// - /// - private static string GetRegKeyValueAsString(RegistryKey key, string valueName) - { - string result = null; - try - { - // Check the type of the value - RegistryValueKind kind = key.GetValueKind(valueName); - if (kind == RegistryValueKind.String) - { - // Get the value since it is a string - result = key.GetValue(valueName) as string; - } - } - catch (ArgumentException) - { - } - catch (IOException) - { - } - catch (SecurityException) - { - } - - return result; - } - } - - /// - /// This class is the worker class used by RegistryStringResourceIndirect to load the resource - /// assemblies and retrieve the resources inside the alternate app-domain. - /// - /// - internal class ResourceRetriever : MarshalByRefObject - { - /// - /// Loads the specified assembly in the app-domain and retrieves the specified resource string. - /// - /// - /// - /// Full name of the assembly to retrieve the resource from. - /// - /// - /// - /// Full path of the assembly to retrieve the resource from. - /// - /// - /// - /// The resource base name to retrieve. - /// - /// - /// - /// The resource ID of the resource to retrieve. - /// - /// - /// - /// The value of the specified string resource or null if the resource could not be found or loaded. - /// - /// - internal string GetStringResource(string assemblyName, string modulePath, string baseName, string resourceID) - { - string result = null; - - do // false loop - { - // Load the resource assembly - Assembly assembly = LoadAssembly(assemblyName, modulePath); - - if (assembly == null) - { - break; - } - - CultureInfo currentCulture = System.Globalization.CultureInfo.CurrentUICulture; - Stream stream = null; - - // Get the resource stream from the manifest - // Loop until we have reached the default culture (identified by an empty Name string in the - // CultureInfo). - do - { - string resourceStream = baseName; - if (!String.IsNullOrEmpty(currentCulture.Name)) - resourceStream += "." + currentCulture.Name; - resourceStream += ".resources"; - - stream = assembly.GetManifestResourceStream(resourceStream); - - if (stream != null) - { - break; - } - - if (String.IsNullOrEmpty(currentCulture.Name)) - { - break; - } - - currentCulture = currentCulture.Parent; - } while (true); - - if (stream == null) - { - break; - } - - // Retrieve the string resource from the stream. - result = GetString(stream, resourceID); - } while (false); - - return result; - } - - /// - /// Loads the specified assembly using ReflectionOnlyLoad or ReflectionOnlyLoadFrom - /// - /// - /// - /// The FullName of the assembly to load. This takes precedence over the modulePath and - /// will be passed to as a parameter to the ReflectionOnlyLoad. - /// - /// - /// - /// The full path of the assembly to load. This is used if the ReflectionOnlyLoad using the - /// assemblyName doesn't load the assembly. It is passed as a parameter to the ReflectionOnlyLoadFrom API. - /// - /// - /// - /// An loaded instance of the specified resource assembly or null if the assembly couldn't be loaded. - /// - /// - /// - /// Since the intent of this method is to load resource assemblies, the standard culture fallback rules - /// apply. If the assembly couldn't be loaded for the current culture we fallback to the parent culture - /// until the neutral culture is reached or an assembly is loaded. - /// - /// - private static Assembly LoadAssembly(string assemblyName, string modulePath) - { - Assembly assembly = null; - AssemblyName assemblyNameObj = new AssemblyName(assemblyName); - - // Split the path up so we can add the culture directory. - string moduleBase = Path.GetDirectoryName(modulePath); - string moduleFile = Path.GetFileName(modulePath); - - CultureInfo currentCulture = System.Globalization.CultureInfo.CurrentUICulture; - - // Loop until we have reached the default culture (identified by an empty Name string in the - // CultureInfo). - do - { - assembly = LoadAssemblyForCulture(currentCulture, assemblyNameObj, moduleBase, moduleFile); - - if (assembly != null) - { - break; - } - - if (String.IsNullOrEmpty(currentCulture.Name)) - { - break; - } - currentCulture = currentCulture.Parent; - } while (true); - - - return assembly; - } - - /// - /// Attempts to load the assembly for the specified culture - /// - /// - /// - /// The culture for which the assembly should be loaded. - /// - /// - /// - /// The name of the assembly without culture information (or at least undefined culture information). - /// - /// - /// - /// The directory containing the neutral culture assembly. - /// - /// - /// - /// The name of the assembly file. - /// - /// - /// - /// An instance of the loaded resource assembly or null if the assembly could not be loaded. - /// - /// - private static Assembly LoadAssemblyForCulture( - CultureInfo culture, - AssemblyName assemblyName, - string moduleBase, - string moduleFile) - { - Assembly assembly = null; - - // Set the assembly FullName to contain the culture we are trying to load. - assemblyName.CultureInfo = culture; - - try - { - assembly = Assembly.ReflectionOnlyLoad(assemblyName.FullName); - } - catch (FileLoadException) - { - } - catch (BadImageFormatException) - { - } - catch (FileNotFoundException) - { - } - - if (assembly != null) - return assembly; - - // Try the resources DLL - string oldAssemblyName = assemblyName.Name; - try - { - assemblyName.Name = oldAssemblyName + ".resources"; - assembly = Assembly.ReflectionOnlyLoad(assemblyName.FullName); - } - catch (FileLoadException) - { - } - catch (BadImageFormatException) - { - } - catch (FileNotFoundException) - { - } - - if (assembly != null) - return assembly; - - assemblyName.Name = oldAssemblyName; - - // Add the culture directory into the file path - string modulePath = Path.Combine(moduleBase, culture.Name); - modulePath = Path.Combine(modulePath, moduleFile); - - if (File.Exists(modulePath)) - { - try - { - assembly = Assembly.ReflectionOnlyLoadFrom(modulePath); - } - catch (FileLoadException) - { - } - catch (BadImageFormatException) - { - } - catch (FileNotFoundException) - { - } - } - return assembly; - } - - /// - /// Retrieves the specified resource string from the resource stream. - /// - /// - /// - /// The resource stream containing the desired resource. - /// - /// - /// - /// The identifier of the string resource to retrieve from the stream. - /// - /// - /// - /// The resource string or null if the resourceID could not be found. - /// - /// - private static string GetString(Stream stream, string resourceID) - { - string result = null; - - ResourceReader rr = new ResourceReader(stream); - - foreach (DictionaryEntry e in rr) - { - if (String.Equals(resourceID, (string)e.Key, StringComparison.OrdinalIgnoreCase)) - { - result = (string)e.Value; - break; - } - } - /* NTRAID#Windows Out Of Band Releases-920971-2005/09/30-JeffJon - * Whidbey v2.0.50727 has a bug where GetResourceData throws an NullReferenceException if - * the assembly used to get the ResourceReader was loaded with ReflectionOnlyLoad. This code - * would be more efficient than the iteration in the foreach loop above and should be enabled - * when we move to the RTM version of Whidbey. - * - string resourceType = null; - byte[] resourceData = null; - rr.GetResourceData(resourceID, out resourceType, out resourceData); - */ - return result; - } - } -} - - diff --git a/src/System.Management.Automation/singleshell/config/RunspaceConfigForSingleShell.cs b/src/System.Management.Automation/singleshell/config/RunspaceConfigForSingleShell.cs deleted file mode 100644 index 55895079c4..0000000000 --- a/src/System.Management.Automation/singleshell/config/RunspaceConfigForSingleShell.cs +++ /dev/null @@ -1,754 +0,0 @@ -/********************************************************************++ -Copyright (c) Microsoft Corporation. All rights reserved. ---********************************************************************/ - -using System.IO; -using System.Collections.Generic; -using System.Collections.ObjectModel; -using System.Xml; -using System.Reflection; -using System.Management.Automation.Internal; - -namespace System.Management.Automation.Runspaces -{ - /// - /// Runspace config for single shell are a special kind of runspace - /// configuration that is generated for single shells. - /// - /// This class needs to handle the standard management of - /// - /// 1. consoleInfo: each instance of this class will have - /// a consoleInfo object. - /// 2. interface to consoleInfo. This includes "open", - /// "save", and "change" of consoleInfo and console files. - /// 3. interface to mshsnapin's. This includes add, remove - /// and list mshsnapins in console file. - /// - /// This class derives from RunspaceConfiguration and supports - /// basic information for cmdlets, providers, types, formats, - /// etc. - /// - /// Eventually when minishell model goes away, RunspaceConfiguration - /// and RunspaceConfigForSingleShell may merge into one class. - /// - /// - internal class RunspaceConfigForSingleShell : RunspaceConfiguration - { - #region RunspaceConfigForSingleShell Factory - - /// - /// One or more default mshsnapins cannot be loaded because the - /// registry is not populated correctly. - /// - /// - /// fileName is null. - /// - /// - /// fileName does not specify proper file extension. - /// - /// - /// Unable to load/parse the file specified by fileName. - /// - internal new static RunspaceConfigForSingleShell Create(string consoleFile, out PSConsoleLoadException warning) - { - PSConsoleLoadException warning1 = null; - - s_mshsnapinTracer.WriteLine("Creating MshConsoleInfo. consoleFile={0}", consoleFile); - - MshConsoleInfo consoleInfo = MshConsoleInfo.CreateFromConsoleFile(consoleFile, out warning1); - - if (warning1 != null) - { - s_mshsnapinTracer.TraceWarning("There was a warning while creating MshConsoleInfo: {0}", warning1.Message); - } - - // At this time, consoleInfo should not be null. - // Otherwise, an exception should have been thrown up. - if (consoleInfo != null) - { - RunspaceConfigForSingleShell rspcfg = new RunspaceConfigForSingleShell(consoleInfo); - PSConsoleLoadException warning2 = null; - - rspcfg.LoadConsole(out warning2); - - if (warning2 != null) - { - s_mshsnapinTracer.TraceWarning("There was a warning while loading console: {0}", warning2.Message); - } - - warning = CombinePSConsoleLoadException(warning1, warning2); - - return rspcfg; - } - - warning = null; - return null; - } - - private static PSConsoleLoadException CombinePSConsoleLoadException(PSConsoleLoadException e1, PSConsoleLoadException e2) - { - if ((e1 == null || e1.PSSnapInExceptions.Count == 0) && (e2 == null || e2.PSSnapInExceptions.Count == 0)) - return null; - - if (e1 == null || e1.PSSnapInExceptions.Count == 0) - return e2; - - if (e2 == null || e2.PSSnapInExceptions.Count == 0) - return e1; - - foreach (PSSnapInException sile in e2.PSSnapInExceptions) - { - e1.PSSnapInExceptions.Add(sile); - } - - return e1; - } - - - /// - /// One or more default mshsnapins cannot be loaded because the - /// registry is not populated correctly. - /// - internal static RunspaceConfigForSingleShell CreateDefaultConfiguration() - { - s_mshsnapinTracer.WriteLine("Creating default runspace configuration."); - - MshConsoleInfo consoleInfo = MshConsoleInfo.CreateDefaultConfiguration(); - - // This should not happen. If there is a failure in creating consoleInfo, - // an exception should have been thrown up. - if (consoleInfo != null) - { - RunspaceConfigForSingleShell rspcfg = new RunspaceConfigForSingleShell(consoleInfo); - PSConsoleLoadException warning = null; - - rspcfg.LoadConsole(out warning); - - if (warning != null) - { - s_mshsnapinTracer.TraceWarning("There was a warning while loading console: {0}", warning.Message); - } - - return rspcfg; - } - - s_mshsnapinTracer.WriteLine("Default runspace configuration created."); - - return null; - } - - private RunspaceConfigForSingleShell(MshConsoleInfo consoleInfo) - { - _consoleInfo = consoleInfo; - } - - #endregion - - #region Console/MshSnapin Manipulation - - private MshConsoleInfo _consoleInfo = null; - internal MshConsoleInfo ConsoleInfo - { - get - { - return _consoleInfo; - } - } - - internal void SaveConsoleFile() - { - if (_consoleInfo == null) - return; - - _consoleInfo.Save(); - } - - internal void SaveAsConsoleFile(string filename) - { - if (_consoleInfo == null) - return; - - _consoleInfo.SaveAsConsoleFile(filename); - } - - /// - /// mshSnapInID is empty or null. - /// - /// - /// PSSnapIn is already loaded. - /// No PSSnapIn with given id found. - /// PSSnapIn cannot be loaded. - /// - /// - /// Caller doesn't have permission to read keys. - /// - internal override PSSnapInInfo DoAddPSSnapIn(string name, out PSSnapInException warning) - { - warning = null; - - s_mshsnapinTracer.WriteLine("Adding mshsnapin {0}", name); - - if (_consoleInfo == null) - return null; - - PSSnapInInfo mshsnapinInfo = null; - - try - { - mshsnapinInfo = _consoleInfo.AddPSSnapIn(name); - } - catch (PSArgumentException mae) - { - s_mshsnapinTracer.TraceError(mae.Message); - s_mshsnapinTracer.WriteLine("Adding mshsnapin {0} failed.", name); - throw; - } - catch (PSArgumentNullException mane) - { - s_mshsnapinTracer.TraceError(mane.Message); - s_mshsnapinTracer.WriteLine("Adding mshsnapin {0} failed.", name); - throw; - } - - if (mshsnapinInfo == null) - return null; - - LoadPSSnapIn(mshsnapinInfo, out warning); - - if (warning != null) - { - s_mshsnapinTracer.TraceWarning("There was a warning when loading mshsnapin {0}: {1}", name, warning.Message); - } - - s_mshsnapinTracer.WriteLine("MshSnapin {0} added", name); - - return mshsnapinInfo; - } - - /// - /// mshSnapInID is null. - /// - /// - /// mshSnapInID is either a default mshsnapin or not loaded. - /// - internal override PSSnapInInfo DoRemovePSSnapIn(string name, out PSSnapInException warning) - { - warning = null; - - if (_consoleInfo == null) - return null; - - s_mshsnapinTracer.WriteLine("Removing mshsnapin {0}", name); - - PSSnapInInfo mshsnapinInfo = _consoleInfo.RemovePSSnapIn(name); - - UnloadPSSnapIn(mshsnapinInfo, out warning); - - s_mshsnapinTracer.WriteLine("MshSnapin {0} removed", name); - - return mshsnapinInfo; - } - - internal void UpdateAll() - { - string errors = ""; - - UpdateAll(out errors); - } - - - internal void UpdateAll(out string errors) - { - errors = ""; - - this.Cmdlets.Update(); - this.Providers.Update(); - - s_mshsnapinTracer.WriteLine("Updating types and formats"); - - try - { - this.Types.Update(); - } - catch (RuntimeException e) - { - s_mshsnapinTracer.TraceWarning("There was a warning updating types: {0}", e.Message); - - errors += e.Message + "\n"; - } - - try - { - this.Formats.Update(); - } - catch (RuntimeException e) - { - s_mshsnapinTracer.TraceWarning("There was a warning updating formats: {0}", e.Message); - - errors += e.Message + "\n"; - } - - try - { - this.Assemblies.Update(); - } - catch (RuntimeException e) - { - s_mshsnapinTracer.TraceWarning("There was a warning updating assemblies: {0}", e.Message); - - errors += e.Message + "\n"; - } - - s_mshsnapinTracer.WriteLine("Types and formats updated successfully"); - } - - #endregion - - #region Load Console/MshSnapin's - - private void LoadConsole(out PSConsoleLoadException warning) - { - if (_consoleInfo == null) - { - warning = null; - return; - } - - LoadPSSnapIns(_consoleInfo.PSSnapIns, out warning); - } - - private void LoadPSSnapIn(PSSnapInInfo mshsnapinInfo, out PSSnapInException warning) - { - warning = null; - - try - { - LoadPSSnapIn(mshsnapinInfo); - } - catch (PSSnapInException) - { - if (!mshsnapinInfo.IsDefault) - _consoleInfo.RemovePSSnapIn(mshsnapinInfo.Name); - - // exception during load mshsnapin are fatal. - throw; - } - - string errors; - - UpdateAll(out errors); - - if (!String.IsNullOrEmpty(errors)) - { - s_mshsnapinTracer.TraceWarning("There was a warning while loading mshsnapin {0}:{1}", mshsnapinInfo.Name, errors); - - warning = new PSSnapInException(mshsnapinInfo.Name, errors, true); - } - } - - private void LoadPSSnapIns(Collection mshsnapinInfos, out PSConsoleLoadException warning) - { - warning = null; - - Collection mshsnapinExceptions = new Collection(); - - bool partialSuccess = false; - - foreach (PSSnapInInfo mshsnapinInfo in mshsnapinInfos) - { - try - { - LoadPSSnapIn(mshsnapinInfo); - partialSuccess = true; - } - catch (PSSnapInException e) - { - if (!mshsnapinInfo.IsDefault) - { - _consoleInfo.RemovePSSnapIn(mshsnapinInfo.Name); - } - else - { - throw; - } - - mshsnapinExceptions.Add(e); - } - } - - if (partialSuccess) - { - string errors; - UpdateAll(out errors); - - if (!String.IsNullOrEmpty(errors)) - { - s_mshsnapinTracer.TraceWarning(errors); - mshsnapinExceptions.Add(new PSSnapInException(null, errors, true)); - } - } - - if (mshsnapinExceptions.Count > 0) - { - warning = new PSConsoleLoadException(_consoleInfo, mshsnapinExceptions); - s_mshsnapinTracer.TraceWarning(warning.Message); - } - } - - private void LoadPSSnapIn(PSSnapInInfo mshsnapinInfo) - { - if (mshsnapinInfo == null) - return; - -#if !CORECLR // CustomPSSnapIn Not Supported On CSS. - if (!String.IsNullOrEmpty(mshsnapinInfo.CustomPSSnapInType)) - { - LoadCustomPSSnapIn(mshsnapinInfo); - return; - } -#endif - Assembly assembly = null; - - s_mshsnapinTracer.WriteLine("Loading assembly for mshsnapin {0}", mshsnapinInfo.Name); - - assembly = LoadMshSnapinAssembly(mshsnapinInfo); - - if (assembly == null) - { - s_mshsnapinTracer.TraceError("Loading assembly for mshsnapin {0} failed", mshsnapinInfo.Name); - return; - } - - s_mshsnapinTracer.WriteLine("Loading assembly for mshsnapin {0} succeeded", mshsnapinInfo.Name); - - Dictionary cmdlets = null; - Dictionary> aliases = null; - Dictionary providers = null; - string throwAwayHelpFile = null; - PSSnapInHelpers.AnalyzePSSnapInAssembly(assembly, assembly.Location, mshsnapinInfo, null, false, out cmdlets, out aliases, out providers, out throwAwayHelpFile); - if (cmdlets != null) - { - foreach (var c in cmdlets) - { - CmdletConfigurationEntry cmdlet = new CmdletConfigurationEntry(c.Key, c.Value.ImplementingType, - c.Value.HelpFileName, mshsnapinInfo); - _cmdlets.AddBuiltInItem(cmdlet); - } - } - if (providers != null) - { - foreach (var p in providers) - { - ProviderConfigurationEntry provider = new ProviderConfigurationEntry(p.Key, p.Value.ImplementingType, - p.Value.HelpFileName, - mshsnapinInfo); - _providers.AddBuiltInItem(provider); - } - } - - foreach (string file in mshsnapinInfo.Types) - { - string path = Path.Combine(mshsnapinInfo.ApplicationBase, file); - - TypeConfigurationEntry typeEntry = new TypeConfigurationEntry(path, path, mshsnapinInfo); - this.Types.AddBuiltInItem(typeEntry); - } - - foreach (string file in mshsnapinInfo.Formats) - { - string path = Path.Combine(mshsnapinInfo.ApplicationBase, file); - - FormatConfigurationEntry formatEntry = new FormatConfigurationEntry(path, path, mshsnapinInfo); - this.Formats.AddBuiltInItem(formatEntry); - } - - AssemblyConfigurationEntry assemblyEntry = new AssemblyConfigurationEntry(mshsnapinInfo.AssemblyName, mshsnapinInfo.AbsoluteModulePath, mshsnapinInfo); - this.Assemblies.AddBuiltInItem(assemblyEntry); - - return; - } - -#if !CORECLR // CustomPSSnapIn Not Supported On CSS. - - private void LoadCustomPSSnapIn(PSSnapInInfo mshsnapinInfo) - { - if (mshsnapinInfo == null) - return; - - if (String.IsNullOrEmpty(mshsnapinInfo.CustomPSSnapInType)) - { - return; - } - - Assembly assembly = null; - - s_mshsnapinTracer.WriteLine("Loading assembly for mshsnapin {0}", mshsnapinInfo.Name); - - assembly = LoadMshSnapinAssembly(mshsnapinInfo); - - if (assembly == null) - { - s_mshsnapinTracer.TraceError("Loading assembly for mshsnapin {0} failed", mshsnapinInfo.Name); - return; - } - - CustomPSSnapIn customPSSnapIn = null; - try - { - Type type = assembly.GetType(mshsnapinInfo.CustomPSSnapInType, true, false); - - if (type != null) - { - customPSSnapIn = (CustomPSSnapIn)Activator.CreateInstance(type); - } - - s_mshsnapinTracer.WriteLine("Loading assembly for mshsnapin {0} succeeded", mshsnapinInfo.Name); - } - catch (TypeLoadException tle) - { - throw new PSSnapInException(mshsnapinInfo.Name, tle.Message); - } - catch (ArgumentException ae) - { - throw new PSSnapInException(mshsnapinInfo.Name, ae.Message); - } - catch (MissingMethodException mme) - { - throw new PSSnapInException(mshsnapinInfo.Name, mme.Message); - } - catch (InvalidCastException ice) - { - throw new PSSnapInException(mshsnapinInfo.Name, ice.Message); - } - catch (TargetInvocationException tie) - { - if (tie.InnerException != null) - { - throw new PSSnapInException(mshsnapinInfo.Name, tie.InnerException.Message); - } - - throw new PSSnapInException(mshsnapinInfo.Name, tie.Message); - } - - MergeCustomPSSnapIn(mshsnapinInfo, customPSSnapIn); - return; - } - - private void MergeCustomPSSnapIn(PSSnapInInfo mshsnapinInfo, CustomPSSnapIn customPSSnapIn) - { - if (mshsnapinInfo == null || customPSSnapIn == null) - return; - - s_mshsnapinTracer.WriteLine("Merging configuration from custom mshsnapin {0}", mshsnapinInfo.Name); - - if (customPSSnapIn.Cmdlets != null) - { - foreach (CmdletConfigurationEntry entry in customPSSnapIn.Cmdlets) - { - CmdletConfigurationEntry cmdlet = new CmdletConfigurationEntry(entry.Name, entry.ImplementingType, entry.HelpFileName, mshsnapinInfo); - _cmdlets.AddBuiltInItem(cmdlet); - } - } - - if (customPSSnapIn.Providers != null) - { - foreach (ProviderConfigurationEntry entry in customPSSnapIn.Providers) - { - ProviderConfigurationEntry provider = new ProviderConfigurationEntry(entry.Name, entry.ImplementingType, entry.HelpFileName, mshsnapinInfo); - _providers.AddBuiltInItem(provider); - } - } - - if (customPSSnapIn.Types != null) - { - foreach (TypeConfigurationEntry entry in customPSSnapIn.Types) - { - string path = Path.Combine(mshsnapinInfo.ApplicationBase, entry.FileName); - TypeConfigurationEntry type = new TypeConfigurationEntry(entry.Name, path, mshsnapinInfo); - _types.AddBuiltInItem(type); - } - } - - if (customPSSnapIn.Formats != null) - { - foreach (FormatConfigurationEntry entry in customPSSnapIn.Formats) - { - string path = Path.Combine(mshsnapinInfo.ApplicationBase, entry.FileName); - FormatConfigurationEntry format = new FormatConfigurationEntry(entry.Name, path, mshsnapinInfo); - _formats.AddBuiltInItem(format); - } - } - - AssemblyConfigurationEntry assemblyEntry = new AssemblyConfigurationEntry(mshsnapinInfo.AssemblyName, mshsnapinInfo.AbsoluteModulePath, mshsnapinInfo); - this.Assemblies.AddBuiltInItem(assemblyEntry); - - s_mshsnapinTracer.WriteLine("Configuration from custom mshsnapin {0} merged", mshsnapinInfo.Name); - } -#endif - - private void UnloadPSSnapIn(PSSnapInInfo mshsnapinInfo, out PSSnapInException warning) - { - warning = null; - - if (mshsnapinInfo != null) - { - this.Cmdlets.RemovePSSnapIn(mshsnapinInfo.Name); - this.Providers.RemovePSSnapIn(mshsnapinInfo.Name); - this.Assemblies.RemovePSSnapIn(mshsnapinInfo.Name); - this.Types.RemovePSSnapIn(mshsnapinInfo.Name); - this.Formats.RemovePSSnapIn(mshsnapinInfo.Name); - - string errors; - UpdateAll(out errors); - - if (!String.IsNullOrEmpty(errors)) - { - s_mshsnapinTracer.TraceWarning(errors); - warning = new PSSnapInException(mshsnapinInfo.Name, errors, true); - } - } - } - - private Assembly LoadMshSnapinAssembly(PSSnapInInfo mshsnapinInfo) - { - Assembly assembly = null; - - s_mshsnapinTracer.WriteLine("Loading assembly from GAC. Assembly Name: {0}", mshsnapinInfo.AssemblyName); - - try - { - // WARNING: DUPLICATE CODE see InitialSessionState - assembly = Assembly.Load(new AssemblyName(mshsnapinInfo.AssemblyName)); - } - catch (FileLoadException e) - { - s_mshsnapinTracer.TraceWarning("Not able to load assembly {0}: {1}", mshsnapinInfo.AssemblyName, e.Message); - } - catch (BadImageFormatException e) - { - s_mshsnapinTracer.TraceWarning("Not able to load assembly {0}: {1}", mshsnapinInfo.AssemblyName, e.Message); - } - catch (FileNotFoundException e) - { - s_mshsnapinTracer.TraceWarning("Not able to load assembly {0}: {1}", mshsnapinInfo.AssemblyName, e.Message); - } - - if (assembly != null) - return assembly; - - s_mshsnapinTracer.WriteLine("Loading assembly from path: {0}", mshsnapinInfo.AssemblyName); - - try - { - AssemblyName assemblyName = AssemblyName.GetAssemblyName(mshsnapinInfo.AbsoluteModulePath); - if (string.Compare(assemblyName.FullName, mshsnapinInfo.AssemblyName, StringComparison.OrdinalIgnoreCase) != 0) - { - string message = StringUtil.Format(ConsoleInfoErrorStrings.PSSnapInAssemblyNameMismatch, mshsnapinInfo.AbsoluteModulePath, mshsnapinInfo.AssemblyName); - s_mshsnapinTracer.TraceError(message); - throw new PSSnapInException(mshsnapinInfo.Name, message); - } - - assembly = Assembly.LoadFrom(mshsnapinInfo.AbsoluteModulePath); - } - catch (FileLoadException e) - { - s_mshsnapinTracer.TraceError("Not able to load assembly {0}: {1}", mshsnapinInfo.AssemblyName, e.Message); - throw new PSSnapInException(mshsnapinInfo.Name, e.Message); - } - catch (BadImageFormatException e) - { - s_mshsnapinTracer.TraceError("Not able to load assembly {0}: {1}", mshsnapinInfo.AssemblyName, e.Message); - throw new PSSnapInException(mshsnapinInfo.Name, e.Message); - } - catch (FileNotFoundException e) - { - s_mshsnapinTracer.TraceError("Not able to load assembly {0}: {1}", mshsnapinInfo.AssemblyName, e.Message); - throw new PSSnapInException(mshsnapinInfo.Name, e.Message); - } - - return assembly; - } - - #endregion - - #region RunspaceConfiguration Api implementation - - /// - /// Gets the shell id for current runspace configuration. - /// - /// - public override string ShellId - { - get - { - return Utils.DefaultPowerShellShellID; - } - } - - private RunspaceConfigurationEntryCollection _cmdlets = - new RunspaceConfigurationEntryCollection(); - - /// - /// Gets the cmdlets defined in runspace configuration. - /// - public override RunspaceConfigurationEntryCollection Cmdlets - { - get - { - return _cmdlets; - } - } - - private RunspaceConfigurationEntryCollection _providers = new RunspaceConfigurationEntryCollection(); - - /// - /// Gets the providers defined in runspace configuration. - /// - public override RunspaceConfigurationEntryCollection Providers - { - get - { - return _providers; - } - } - - private RunspaceConfigurationEntryCollection _types; - - /// - /// Gets the type data files defined in runspace configuration. - /// - public override RunspaceConfigurationEntryCollection Types - { - get { return _types ?? (_types = new RunspaceConfigurationEntryCollection()); } - } - - private RunspaceConfigurationEntryCollection _formats; - - /// - /// Gets the format data files defined in runspace configuration. - /// - public override RunspaceConfigurationEntryCollection Formats - { - get { - return _formats ?? (_formats = new RunspaceConfigurationEntryCollection()); - } - } - - private RunspaceConfigurationEntryCollection _initializationScripts; - - /// - /// Gets the initialization scripts defined in runspace configuration. - /// - public override RunspaceConfigurationEntryCollection InitializationScripts - { - get { - return _initializationScripts ?? - (_initializationScripts = new RunspaceConfigurationEntryCollection()); - } - } - - #endregion - - private static PSTraceSource s_mshsnapinTracer = PSTraceSource.GetTracer("MshSnapinLoadUnload", "Loading and unloading mshsnapins", false); - } -} diff --git a/src/System.Management.Automation/singleshell/installer/CustomMshSnapin.cs b/src/System.Management.Automation/singleshell/installer/CustomMshSnapin.cs deleted file mode 100644 index 57e1fd05e8..0000000000 --- a/src/System.Management.Automation/singleshell/installer/CustomMshSnapin.cs +++ /dev/null @@ -1,122 +0,0 @@ -/********************************************************************++ -Copyright (c) Microsoft Corporation. All rights reserved. ---********************************************************************/ - -using System; -using System.Collections; -using System.Collections.Generic; -using System.Collections.ObjectModel; -using System.ComponentModel; -using System.Reflection; -using Microsoft.Win32; -using System.IO; -using System.Management.Automation.Runspaces; - -namespace System.Management.Automation -{ - /// - /// - /// Raw mshsnapin is a class for allowing mshsnapin developers to directly - /// specify the set of cmdlets, providers, types, formats, assemblies - /// available in the mshsnapin. - /// - /// To use this class, mshsnapin developers will drive from it and fill - /// in details about cmdlet, provider, type, format, assemblies. - /// - /// This class will also facilitate the registration of the mshsnapin - /// through installutil.exe. - /// - /// This class will be built with monad core engine dll. - /// - /// - /// - /// Developers should derive from this class to implement their own - /// custom mshsnapins. - /// - /// Derived mshsnapins should be denoted with [RunInstaller] attribute - /// so that installutil.exe can directly install the mshsnapin into registry. - /// - public abstract class CustomPSSnapIn : PSSnapInInstaller - { - internal string CustomPSSnapInType - { - get - { - return this.GetType().FullName; - } - } - - private Collection _cmdlets; - - /// - /// Gets the cmdlets defined in custom mshsnapin. - /// - /// - /// This member can be derived to provide the list of cmdlets to be included for this mshsnapin. - /// - public virtual Collection Cmdlets - { - get { return _cmdlets ?? (_cmdlets = new Collection()); } - } - - private Collection _providers; - - /// - /// Gets the providers defined in custom mshsnapin. - /// - /// - /// This member can be derived to provide the list of providers to be included for this mshsnapin. - /// - public virtual Collection Providers - { - get { return _providers ?? (_providers = new Collection()); } - } - - private Collection _types; - - /// - /// Gets the types defined in custom mshsnapin. - /// - /// - /// This member can be derived to provide the list of types to be included for this mshsnapin. - /// - public virtual Collection Types - { - get { return _types ?? (_types = new Collection()); } - } - - private Collection _formats; - - /// - /// Gets the formatsdefined in raw mshsnapin. - /// - /// - /// This member can be derived to provide the list of formats to be included for this mshsnapin. - /// - public virtual Collection Formats - { - get { return _formats ?? (_formats = new Collection()); } - } - - private Dictionary _regValues = null; - - /// - /// - /// - internal override Dictionary RegValues - { - get - { - if (_regValues == null) - { - _regValues = base.RegValues; - - if (!String.IsNullOrEmpty(this.CustomPSSnapInType)) - _regValues[RegistryStrings.MshSnapin_CustomPSSnapInType] = this.CustomPSSnapInType; - } - - return _regValues; - } - } - } -} diff --git a/src/System.Management.Automation/singleshell/installer/MshCoreMshSnapin.cs b/src/System.Management.Automation/singleshell/installer/MshCoreMshSnapin.cs deleted file mode 100644 index 04c7d88e5f..0000000000 --- a/src/System.Management.Automation/singleshell/installer/MshCoreMshSnapin.cs +++ /dev/null @@ -1,113 +0,0 @@ -/********************************************************************++ -Copyright (c) Microsoft Corporation. All rights reserved. ---********************************************************************/ - -using System; -using System.Collections; -using System.Collections.Generic; -using System.Collections.ObjectModel; -using System.ComponentModel; -using System.Configuration.Install; -using System.Reflection; -using Microsoft.Win32; -using System.IO; -using System.Management.Automation; - -namespace Microsoft.PowerShell -{ - /// - /// - /// MshCoreMshSnapin (or MshCoreMshSnapinInstaller) is a class for facilitating registry - /// of necessary information for monad core mshsnapin. - /// - /// This class will be built with monad core engine dll - /// (System.Management.Automation.dll). - /// - /// - /// - [RunInstaller(true)] - public sealed class PSCorePSSnapIn : PSSnapIn - { - /// - /// Create an instance of this class. - /// - public PSCorePSSnapIn() - : base() - { - } - - /// - /// Get name of this mshsnapin. - /// - public override string Name - { - get - { - return RegistryStrings.CoreMshSnapinName; - } - } - - /// - /// Get the default vendor string for this mshsnapin. - /// - public override string Vendor - { - get - { - return "Microsoft"; - } - } - - /// - /// Get resource information for vendor. This is a string of format: resourceBaseName,resourceName. - /// - public override string VendorResource - { - get - { - return "CoreMshSnapInResources,Vendor"; - } - } - - /// - /// Get the default description string for this mshsnapin. - /// - public override string Description - { - get - { - return "This PSSnapIn contains MSH management cmdlets used to manage components affecting the MSH engine."; - } - } - - /// - /// Get resource information for description. This is a string of format: resourceBaseName,resourceName. - /// - public override string DescriptionResource - { - get - { - return "CoreMshSnapInResources,Description"; - } - } - - /// - /// Get type files to be used for this mshsnapin. - /// - public override string[] Types { get; } = new string[] { "types.ps1xml" }; - - /// - /// Get format files to be used for this mshsnapin. - /// - public override string[] Formats { get; } = new string[] { - "Certificate.format.ps1xml", - "DotNetTypes.format.ps1xml", - "FileSystem.format.ps1xml", - "Help.format.ps1xml", - "HelpV3.format.ps1xml", - "PowerShellCore.format.ps1xml", - "PowerShellTrace.format.ps1xml", - "Registry.format.ps1xml" - }; - } -} diff --git a/src/System.Management.Automation/singleshell/installer/MshInstaller.cs b/src/System.Management.Automation/singleshell/installer/MshInstaller.cs deleted file mode 100644 index de0a3e9d7d..0000000000 --- a/src/System.Management.Automation/singleshell/installer/MshInstaller.cs +++ /dev/null @@ -1,220 +0,0 @@ -/********************************************************************++ -Copyright (c) Microsoft Corporation. All rights reserved. ---********************************************************************/ - -using System; -using System.Collections; -using System.Collections.Generic; -using System.Collections.ObjectModel; -using System.ComponentModel; -using System.Configuration.Install; -using System.Reflection; -using Microsoft.Win32; -using System.IO; -using System.Text; -using System.Threading; -using System.Globalization; -using Dbg = System.Management.Automation.Diagnostics; - -namespace System.Management.Automation -{ - /// - /// - /// PSInstaller is a class for facilitating installation - /// of monad engine and monad PSSnapin's. - /// - /// This class implements installer api from CLR. At install - /// time, installation utilities (like InstallUtil.exe) will - /// call api implementation functions in this class automatically. - /// This includes functions like Install, Uninstall, Rollback - /// and Commit. - /// - /// This class is an abstract class for handling installation needs - /// that are common for all monad components, which include, - /// - /// 1. accessing system registry - /// 2. support of additional command line parameters. - /// 3. writing registry files - /// 4. automatically extract information like vender, version, etc. - /// - /// Different monad component will derive from this class. Two common - /// components that need install include, - /// - /// 1. PSSnapin. Installation of PSSnapin will require information - /// about PSSnapin assembly, version, vendor, etc to be - /// written to registry. - /// - /// 2. Engine. Installation of monad engine will require information - /// about engine assembly, version, CLR information to be - /// written to registry. - /// - /// - /// - /// - /// This is an abstract class to be derived by monad engine and PSSnapin installers - /// only. Developer should not directly derive from this class. - /// - public abstract class PSInstaller : Installer - { - private static string[] MshRegistryRoots - { - get - { - // For 3.0 PowerShell, we use "3" as the registry version key only for Engine - // related data like ApplicationBase. - // For 3.0 PowerShell, we still use "1" as the registry version key for - // Snapin and Custom shell lookup/discovery. - return new string[] { - "HKEY_LOCAL_MACHINE\\Software\\Microsoft\\PowerShell\\" + PSVersionInfo.RegistryVersion1Key + "\\" - }; - } - } - - /// - /// - /// - internal abstract string RegKey - { - get; - } - - /// - /// - /// - internal abstract Dictionary RegValues - { - get; - } - - /// - /// - /// - /// - public sealed override void Install(IDictionary stateSaver) - { - base.Install(stateSaver); - - WriteRegistry(); - - return; - } - - private void WriteRegistry() - { - foreach (string root in MshRegistryRoots) - { - RegistryKey key = GetRegistryKey(root + RegKey); - - foreach (var pair in RegValues) - { - key.SetValue(pair.Key, pair.Value); - } - } - } - - private RegistryKey GetRegistryKey(string keyPath) - { - RegistryKey root = GetRootHive(keyPath); - - if (root == null) - return null; - - return root.CreateSubKey(GetSubkeyPath(keyPath)); - } - - private static string GetSubkeyPath(string keyPath) - { - int index = keyPath.IndexOf('\\'); - if (index > 0) - { - return keyPath.Substring(index + 1); - } - - return null; - } - - private static RegistryKey GetRootHive(string keyPath) - { - string root; - - int index = keyPath.IndexOf('\\'); - if (index > 0) - { - root = keyPath.Substring(0, index); - } - else - { - root = keyPath; - } - - switch (root.ToUpperInvariant()) - { - case "HKEY_CURRENT_USER": - return Registry.CurrentUser; - case "HKEY_LOCAL_MACHINE": - return Registry.LocalMachine; - case "HKEY_CLASSES_ROOT": - return Registry.ClassesRoot; - case "HKEY_CURRENT_CONFIG": - return Registry.CurrentConfig; - case "HKEY_PERFORMANCE_DATA": - return Registry.PerformanceData; - case "HKEY_USERS": - return Registry.Users; - } - - return null; - } - - /// - /// Uninstall this msh component - /// - /// - public sealed override void Uninstall(IDictionary savedState) - { - base.Uninstall(savedState); - - if (this.Context != null && this.Context.Parameters != null && this.Context.Parameters.ContainsKey("RegFile")) - { - string regFile = this.Context.Parameters["RegFile"]; - - // If regfile is specified. Don't uninstall. - if (!String.IsNullOrEmpty(regFile)) - return; - } - - string keyName; - string parentKey; - - int index = RegKey.LastIndexOf('\\'); - if (index >= 0) - { - parentKey = RegKey.Substring(0, index); - keyName = RegKey.Substring(index + 1); - } - else - { - parentKey = ""; - keyName = RegKey; - } - - foreach (string root in MshRegistryRoots) - { - RegistryKey key = GetRegistryKey(root + parentKey); - - key.DeleteSubKey(keyName); - } - - return; - } - - /// - /// Rollback this msh component - /// - /// - public sealed override void Rollback(IDictionary savedState) - { - Uninstall(savedState); - } - } -} diff --git a/src/System.Management.Automation/singleshell/installer/MshSnapin.cs b/src/System.Management.Automation/singleshell/installer/MshSnapin.cs deleted file mode 100644 index 72d178740a..0000000000 --- a/src/System.Management.Automation/singleshell/installer/MshSnapin.cs +++ /dev/null @@ -1,88 +0,0 @@ -/********************************************************************++ -Copyright (c) Microsoft Corporation. All rights reserved. ---********************************************************************/ - -using System; -using System.Collections; -using System.Collections.Generic; -using System.Collections.ObjectModel; -using System.ComponentModel; -using System.Reflection; -using Microsoft.Win32; -using System.IO; -using System.Management.Automation.Runspaces; - -namespace System.Management.Automation -{ - /// - /// - /// MshSnapin is a class for regular mshsnapin's which is constructed - /// based on mshsnapin assembly. - /// - /// This class derives from PSSnapInInstaller and will be used as the base - /// for all regular mshsnapins. - /// - /// - /// - /// - /// Developers should derive from this class when implementing their own - /// mshsnapins. - /// - /// Derived mshsnapins should be denoted with [RunInstaller] attribute - /// so that installutil.exe can directly install the mshsnapin into registry. - /// - public abstract class PSSnapIn : PSSnapInInstaller - { - /// - /// Gets list of format files to be loaded for this mshsnapin. - /// - /// - /// This member can be derived to provide the list of formats to be loaded for this mshsnapin. - /// - public virtual string[] Formats - { - get - { - return null; - } - } - - /// - /// Gets list of type files to be loaded for this mshsnapin. - /// - /// - /// This member can be derived to provide the list of types to be loaded for this mshsnapin. - /// - public virtual string[] Types - { - get - { - return null; - } - } - - private Dictionary _regValues = null; - - /// - /// - /// - internal override Dictionary RegValues - { - get - { - if (_regValues == null) - { - _regValues = base.RegValues; - - if (this.Types != null && this.Types.Length > 0) - _regValues[RegistryStrings.MshSnapin_BuiltInTypes] = this.Types; - - if (this.Formats != null && this.Formats.Length > 0) - _regValues[RegistryStrings.MshSnapin_BuiltInFormats] = this.Formats; - } - - return _regValues; - } - } - } -} diff --git a/src/System.Management.Automation/singleshell/installer/MshSnapinInstaller.cs b/src/System.Management.Automation/singleshell/installer/MshSnapinInstaller.cs deleted file mode 100644 index f56cae6e93..0000000000 --- a/src/System.Management.Automation/singleshell/installer/MshSnapinInstaller.cs +++ /dev/null @@ -1,159 +0,0 @@ -/********************************************************************++ -Copyright (c) Microsoft Corporation. All rights reserved. ---********************************************************************/ - -using System.Collections.Generic; -using System.Configuration.Install; -using System.IO; - -namespace System.Management.Automation -{ - /// - /// - /// MshSnapinBase (or MshSnapinInstaller) is a class for facilitating registry of necessary - /// information for monad mshsnapin's. - /// - /// This class will be built with monad core engine dll - /// (System.Management.Automation.dll). - /// - /// This is the base class for two kinds of mshsnapins: MshSnapin and CustomMshSnapin. - /// - /// Each mshsnapin assembly should derive from this class (indirectly) and fill - /// in information about mshsnapin name, vendor, and version. - /// - /// At install time, installation utilities (like InstallUtil.exe) will - /// call install this engine assembly based on the implementation in - /// this class. - /// - /// This class derives from base class PSInstaller. PSInstaller will - /// handle the details about how information got written into registry. - /// Here, the information about registry content is provided. - /// - /// The reason of not calling this class MshSnapinInstaller is to "hide" the details - /// that MshSnapin class is actually doing installation. It is also more intuitive - /// since people deriving from this class will think there are really - /// implementing a class for mshsnapin. - /// - /// - /// - /// - /// This is an abstract class to be derived by monad mshsnapin and custom mshsnapin. - /// MshSnapin developer should not directly derive from this class. - /// - public abstract class PSSnapInInstaller : PSInstaller - { - /// - /// - /// - public abstract string Name - { - get; - } - - /// - /// - /// - public abstract string Vendor - { - get; - } - - /// - /// - /// - public virtual string VendorResource - { - get - { - return null; - } - } - - /// - /// - /// - public abstract string Description - { - get; - } - - /// - /// - /// - public virtual string DescriptionResource - { - get - { - return null; - } - } - - /// - /// - /// - private string MshSnapinVersion - { - get - { - return this.GetType().Assembly.GetName().Version.ToString(); - } - } - - private string _psVersion = null; - /// - /// - /// - private string PSVersion - { - get { return _psVersion ?? (_psVersion = PSVersionInfo.FeatureVersionString); } - } - - /// - /// - /// - internal sealed override string RegKey - { - get - { - PSSnapInInfo.VerifyPSSnapInFormatThrowIfError(this.Name); - - return RegistryStrings.MshSnapinKey + "\\" + Name; - } - } - - private Dictionary _regValues = null; - /// - /// - /// - internal override Dictionary RegValues - { - get - { - if (_regValues == null) - { - _regValues = new Dictionary(); - _regValues[RegistryStrings.MshSnapin_MonadVersion] = this.PSVersion; - - if (!String.IsNullOrEmpty(this.Vendor)) - _regValues[RegistryStrings.MshSnapin_Vendor] = this.Vendor; - - if (!String.IsNullOrEmpty(this.Description)) - _regValues[RegistryStrings.MshSnapin_Description] = this.Description; - - if (!String.IsNullOrEmpty(this.VendorResource)) - _regValues[RegistryStrings.MshSnapin_VendorResource] = this.VendorResource; - - if (!String.IsNullOrEmpty(this.DescriptionResource)) - _regValues[RegistryStrings.MshSnapin_DescriptionResource] = this.DescriptionResource; - - _regValues[RegistryStrings.MshSnapin_Version] = this.MshSnapinVersion; - _regValues[RegistryStrings.MshSnapin_ApplicationBase] = Path.GetDirectoryName(this.GetType().Assembly.Location); - _regValues[RegistryStrings.MshSnapin_AssemblyName] = this.GetType().Assembly.FullName; - _regValues[RegistryStrings.MshSnapin_ModuleName] = this.GetType().Assembly.Location; - } - - return _regValues; - } - } - } -} diff --git a/src/System.Management.Automation/minishell/api/FormatAndTypeDataHelper.cs b/src/System.Management.Automation/utils/FormatAndTypeDataHelper.cs similarity index 63% rename from src/System.Management.Automation/minishell/api/FormatAndTypeDataHelper.cs rename to src/System.Management.Automation/utils/FormatAndTypeDataHelper.cs index 4af4b553ea..2817677295 100644 --- a/src/System.Management.Automation/minishell/api/FormatAndTypeDataHelper.cs +++ b/src/System.Management.Automation/utils/FormatAndTypeDataHelper.cs @@ -69,89 +69,9 @@ namespace System.Management.Automation.Runspaces private const string DuplicateFile = "DuplicateFile"; internal const string ValidationException = "ValidationException"; - private static string GetBaseFolder( - RunspaceConfiguration runspaceConfiguration, - Collection independentErrors) + private static string GetBaseFolder(Collection independentErrors) { - string returnValue = CommandDiscovery.GetShellPathFromRegistry(runspaceConfiguration.ShellId); - if (returnValue == null) - { - returnValue = Path.GetDirectoryName(PsUtils.GetMainModule(System.Diagnostics.Process.GetCurrentProcess()).FileName); - } - else - { - returnValue = Path.GetDirectoryName(returnValue); - if (!Directory.Exists(returnValue)) - { - string newReturnValue = Path.GetDirectoryName(typeof(FormatAndTypeDataHelper).GetTypeInfo().Assembly.Location); - string error = StringUtil.Format(TypesXmlStrings.CannotFindRegistryKeyPath, returnValue, - Utils.GetRegistryConfigurationPath(runspaceConfiguration.ShellId), "\\Path", newReturnValue); - independentErrors.Add(error); - returnValue = newReturnValue; - } - } - return returnValue; - } - - internal static Collection GetFormatAndTypesErrors( - RunspaceConfiguration runspaceConfiguration, - PSHost host, - IEnumerable configurationEntryCollection, - Collection independentErrors, - Collection entryIndicesToRemove) - { - Collection returnValue = new Collection(); - - string baseFolder = GetBaseFolder(runspaceConfiguration, independentErrors); - var psHome = Utils.DefaultPowerShellAppBase; - - // this hashtable will be used to check whether this is duplicated file for types or formats. - HashSet fullFileNameSet = new HashSet(StringComparer.OrdinalIgnoreCase); - int index = -1; - - foreach (var configurationEntry in configurationEntryCollection) - { - string fileName; - string psSnapinName = configurationEntry.PSSnapIn == null ? runspaceConfiguration.ShellId : configurationEntry.PSSnapIn.Name; - index++; - var typeEntry = configurationEntry as TypeConfigurationEntry; - if (typeEntry != null) - { - fileName = typeEntry.FileName; - - if (fileName == null) - { - returnValue.Add(new PSSnapInTypeAndFormatErrors(psSnapinName, typeEntry.TypeData, typeEntry.IsRemove)); - continue; - } - } - else - { - FormatConfigurationEntry formatEntry = (FormatConfigurationEntry)configurationEntry; - fileName = formatEntry.FileName; - - if (fileName == null) - { - returnValue.Add(new PSSnapInTypeAndFormatErrors(psSnapinName, formatEntry.FormatData)); - continue; - } - } - - bool checkFileExists = configurationEntry.PSSnapIn == null || string.Equals(psHome, configurationEntry.PSSnapIn.AbsoluteModulePath, StringComparison.OrdinalIgnoreCase); - bool needToRemoveEntry = false; - string fullFileName = GetAndCheckFullFileName(psSnapinName, fullFileNameSet, baseFolder, fileName, independentErrors, ref needToRemoveEntry, checkFileExists); - if (fullFileName == null) - { - if (needToRemoveEntry) - { - entryIndicesToRemove.Add(index); - } - continue; - } - - returnValue.Add(new PSSnapInTypeAndFormatErrors(psSnapinName, fullFileName)); - } - return returnValue; + return Path.GetDirectoryName(PsUtils.GetMainModule(System.Diagnostics.Process.GetCurrentProcess()).FileName); } private static string GetAndCheckFullFileName( diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Update-FormatData.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Update-FormatData.Tests.ps1 index 24e07d43e7..28b726f7b6 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Update-FormatData.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Update-FormatData.Tests.ps1 @@ -56,6 +56,20 @@ Describe "Update-FormatData basic functionality" -Tags "CI" { { Update-FormatData -Append $testfile -WhatIf } | Should Not Throw { Update-FormatData -Prepend $testfile -WhatIf } | Should Not Throw } + + It "Update with invalid format xml should fail" -Pending { + $xmlContent = @" + + + + T2 + + + +"@ + $xmlContent | Out-File -FilePath "$testdrive\test.format.ps1xml" -Encoding ascii + { Update-FormatData -Path "$testdrive\test.format.ps1xml" -ErrorAction Stop } | ShouldBeErrorId "FormatXmlUpdateException,Microsoft.PowerShell.Commands.UpdateFormatDataCommand" + } } diff --git a/test/powershell/engine/Api/BasicEngine.Tests.ps1 b/test/powershell/engine/Api/BasicEngine.Tests.ps1 index 05ce39c8b5..16e5d7dd88 100644 --- a/test/powershell/engine/Api/BasicEngine.Tests.ps1 +++ b/test/powershell/engine/Api/BasicEngine.Tests.ps1 @@ -10,7 +10,7 @@ Describe 'Basic engine APIs' -Tags "CI" { $result = $ps.Invoke() $result.Count | Should Be 1 - $result[0].PSSnapIn.Name | Should Be "Microsoft.WSMan.Management" + $result[0].Source | Should Be "Microsoft.WSMan.Management" } }