From 3e0cb9439ee833c03c6ec35fd74187cd72732b2e Mon Sep 17 00:00:00 2001 From: Tyler Leonhardt Date: Sat, 23 Jun 2018 13:29:47 -0700 Subject: [PATCH 1/4] remove all references to the Pipeline class --- PowerShellEditorServices.Common.props | 1 - .../Session/PowerShell3Operations.cs | 15 +++---- .../Session/PowerShellContext.cs | 45 +++++-------------- 3 files changed, 16 insertions(+), 45 deletions(-) diff --git a/PowerShellEditorServices.Common.props b/PowerShellEditorServices.Common.props index 2f07fcc7f..d9679732e 100644 --- a/PowerShellEditorServices.Common.props +++ b/PowerShellEditorServices.Common.props @@ -9,6 +9,5 @@ git https://github.com/PowerShell/PowerShellEditorServices portable - 1.0.3 diff --git a/src/PowerShellEditorServices/Session/PowerShell3Operations.cs b/src/PowerShellEditorServices/Session/PowerShell3Operations.cs index 2199e1839..3d9cb8943 100644 --- a/src/PowerShellEditorServices/Session/PowerShell3Operations.cs +++ b/src/PowerShellEditorServices/Session/PowerShell3Operations.cs @@ -6,11 +6,12 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Management.Automation; using System.Management.Automation.Runspaces; namespace Microsoft.PowerShell.EditorServices.Session { + using System.Management.Automation; + internal class PowerShell3Operations : IVersionSpecificOperations { public void ConfigureDebugger(Runspace runspace) @@ -32,15 +33,11 @@ public IEnumerable ExecuteCommandInDebugger( out DebuggerResumeAction? debuggerResumeAction) { IEnumerable executionResult = null; - - using (var nestedPipeline = currentRunspace.CreateNestedPipeline()) + using (var pwsh = PowerShell.Create()) { - foreach (var command in psCommand.Commands) - { - nestedPipeline.Commands.Add(command); - } - - var results = nestedPipeline.Invoke(); + pwsh.Runspace = currentRunspace; + pwsh.Commands = psCommand; + var results = pwsh.Invoke(); if (typeof(TResult) != typeof(PSObject)) { diff --git a/src/PowerShellEditorServices/Session/PowerShellContext.cs b/src/PowerShellEditorServices/Session/PowerShellContext.cs index f8d6e69a3..b129d79a9 100644 --- a/src/PowerShellEditorServices/Session/PowerShellContext.cs +++ b/src/PowerShellEditorServices/Session/PowerShellContext.cs @@ -823,20 +823,11 @@ await this.ExecuteCommand( internal static TResult ExecuteScriptAndGetItem(string scriptToExecute, Runspace runspace, TResult defaultValue = default(TResult)) { - Pipeline pipeline = null; - - try + using (var pwsh = PowerShell.Create()) { - if (runspace.RunspaceAvailability == RunspaceAvailability.AvailableForNestedCommand) - { - pipeline = runspace.CreateNestedPipeline(scriptToExecute, false); - } - else - { - pipeline = runspace.CreatePipeline(scriptToExecute, false); - } - - Collection results = pipeline.Invoke(); + pwsh.Runspace = runspace; + Collection results = pwsh.AddScript(scriptToExecute) + .Invoke(null, new PSInvocationSettings() { AddToHistory = false }); if (results.Count == 0 || results.FirstOrDefault() == null) { @@ -858,10 +849,6 @@ await this.ExecuteCommand( .FirstOrDefault(); } } - finally - { - pipeline.Dispose(); - } } /// @@ -1526,6 +1513,12 @@ private async Task GetSessionDetailsInRunspace() } } + private SessionDetails GetSessionDetailsInNestedPipeline() + { + + return GetSessionDetailsInRunspace(this.CurrentRunspace.Runspace); + } + private SessionDetails GetSessionDetailsInRunspace(Runspace runspace) { SessionDetails sessionDetails = @@ -1561,24 +1554,6 @@ private SessionDetails GetSessionDetailsInDebugger() }); } - private SessionDetails GetSessionDetailsInNestedPipeline() - { - using (var pipeline = this.CurrentRunspace.Runspace.CreateNestedPipeline()) - { - return this.GetSessionDetails( - command => - { - pipeline.Commands.Clear(); - pipeline.Commands.Add(command.Commands[0]); - - return - pipeline - .Invoke() - .FirstOrDefault(); - }); - } - } - private void SetProfileVariableInCurrentRunspace(ProfilePaths profilePaths) { // Create the $profile variable From b20021f93101b6ab79fa9da25163d229c90989cd Mon Sep 17 00:00:00 2001 From: Tyler Leonhardt Date: Sat, 23 Jun 2018 19:21:55 -0700 Subject: [PATCH 2/4] patrick's feedback --- .../Session/PowerShellContext.cs | 19 ++----------------- 1 file changed, 2 insertions(+), 17 deletions(-) diff --git a/src/PowerShellEditorServices/Session/PowerShellContext.cs b/src/PowerShellEditorServices/Session/PowerShellContext.cs index b129d79a9..1c2065cd2 100644 --- a/src/PowerShellEditorServices/Session/PowerShellContext.cs +++ b/src/PowerShellEditorServices/Session/PowerShellContext.cs @@ -826,28 +826,13 @@ await this.ExecuteCommand( using (var pwsh = PowerShell.Create()) { pwsh.Runspace = runspace; - Collection results = pwsh.AddScript(scriptToExecute) - .Invoke(null, new PSInvocationSettings() { AddToHistory = false }); + Collection results = pwsh.AddScript(scriptToExecute).Invoke(); if (results.Count == 0 || results.FirstOrDefault() == null) { return defaultValue; } - - if (typeof(TResult) != typeof(PSObject)) - { - return results - .Select(pso => pso.BaseObject) - .OfType() - .FirstOrDefault(); - } - else - { - return - results - .OfType() - .FirstOrDefault(); - } + return results.FirstOrDefault(); } } From bfe3d43f410f3098b0100eaadff6c0544b1774da Mon Sep 17 00:00:00 2001 From: Tyler Leonhardt Date: Sun, 24 Jun 2018 00:43:28 -0700 Subject: [PATCH 3/4] debugging --- .../Session/PowerShell3Operations.cs | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/src/PowerShellEditorServices/Session/PowerShell3Operations.cs b/src/PowerShellEditorServices/Session/PowerShell3Operations.cs index 3d9cb8943..047944359 100644 --- a/src/PowerShellEditorServices/Session/PowerShell3Operations.cs +++ b/src/PowerShellEditorServices/Session/PowerShell3Operations.cs @@ -37,19 +37,7 @@ public IEnumerable ExecuteCommandInDebugger( { pwsh.Runspace = currentRunspace; pwsh.Commands = psCommand; - var results = pwsh.Invoke(); - - if (typeof(TResult) != typeof(PSObject)) - { - executionResult = - results - .Select(pso => pso.BaseObject) - .Cast(); - } - else - { - executionResult = results.Cast(); - } + executionResult = pwsh.Invoke(); } // Write the output to the host if necessary From d8d1e51d74fff732ec6737a1b037ff679099ef0d Mon Sep 17 00:00:00 2001 From: Tyler Leonhardt Date: Mon, 2 Jul 2018 07:38:06 -0700 Subject: [PATCH 4/4] address patrick's feedback --- src/PowerShellEditorServices/Session/PowerShellContext.cs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/PowerShellEditorServices/Session/PowerShellContext.cs b/src/PowerShellEditorServices/Session/PowerShellContext.cs index 1c2065cd2..5ae22c03e 100644 --- a/src/PowerShellEditorServices/Session/PowerShellContext.cs +++ b/src/PowerShellEditorServices/Session/PowerShellContext.cs @@ -827,12 +827,7 @@ await this.ExecuteCommand( { pwsh.Runspace = runspace; Collection results = pwsh.AddScript(scriptToExecute).Invoke(); - - if (results.Count == 0 || results.FirstOrDefault() == null) - { - return defaultValue; - } - return results.FirstOrDefault(); + return results.DefaultIfEmpty(defaultValue).First(); } }