From caea82207f928a0a769c9625b9341e8553ac73b6 Mon Sep 17 00:00:00 2001 From: Robert Holt Date: Thu, 28 Oct 2021 14:53:36 -0700 Subject: [PATCH 1/4] Use AddCommand to run profiles --- .../Utility/PowerShellExtensions.cs | 28 +++++-------------- .../Utility/PSCommandExtensions.cs | 15 ++++++++++ 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/src/PowerShellEditorServices/Services/PowerShell/Utility/PowerShellExtensions.cs b/src/PowerShellEditorServices/Services/PowerShell/Utility/PowerShellExtensions.cs index 5724776d5..c3e571315 100644 --- a/src/PowerShellEditorServices/Services/PowerShell/Utility/PowerShellExtensions.cs +++ b/src/PowerShellEditorServices/Services/PowerShell/Utility/PowerShellExtensions.cs @@ -9,7 +9,6 @@ using Microsoft.PowerShell.EditorServices.Hosting; using Microsoft.PowerShell.EditorServices.Utility; using System.Collections.Generic; -using System.IO; namespace Microsoft.PowerShell.EditorServices.Services.PowerShell.Utility { @@ -166,10 +165,13 @@ public static void LoadProfiles(this PowerShell pwsh, ProfilePathInfo profilePat { var profileVariable = new PSObject(); - pwsh.AddProfileMemberAndLoadIfExists(profileVariable, nameof(profilePaths.AllUsersAllHosts), profilePaths.AllUsersAllHosts) - .AddProfileMemberAndLoadIfExists(profileVariable, nameof(profilePaths.AllUsersCurrentHost), profilePaths.AllUsersCurrentHost) - .AddProfileMemberAndLoadIfExists(profileVariable, nameof(profilePaths.CurrentUserAllHosts), profilePaths.CurrentUserAllHosts) - .AddProfileMemberAndLoadIfExists(profileVariable, nameof(profilePaths.CurrentUserCurrentHost), profilePaths.CurrentUserCurrentHost); + var psCommand = new PSCommand() + .AddProfileLoadIfExists(profileVariable, nameof(profilePaths.AllUsersAllHosts), profilePaths.AllUsersAllHosts) + .AddProfileLoadIfExists(profileVariable, nameof(profilePaths.AllUsersCurrentHost), profilePaths.AllUsersCurrentHost) + .AddProfileLoadIfExists(profileVariable, nameof(profilePaths.CurrentUserAllHosts), profilePaths.CurrentUserAllHosts) + .AddProfileLoadIfExists(profileVariable, nameof(profilePaths.CurrentUserCurrentHost), profilePaths.CurrentUserCurrentHost); + + pwsh.InvokeCommand(psCommand); pwsh.Runspace.SessionStateProxy.SetVariable("PROFILE", profileVariable); } @@ -200,22 +202,6 @@ public static string GetErrorString(this PowerShell pwsh) return sb.ToString(); } - private static PowerShell AddProfileMemberAndLoadIfExists(this PowerShell pwsh, PSObject profileVariable, string profileName, string profilePath) - { - profileVariable.Members.Add(new PSNoteProperty(profileName, profilePath)); - - if (File.Exists(profilePath)) - { - var psCommand = new PSCommand() - .AddScript(profilePath, useLocalScope: false) - .AddOutputCommand(); - - pwsh.InvokeCommand(psCommand); - } - - return pwsh; - } - private static StringBuilder AddErrorString(this StringBuilder sb, ErrorRecord error, int errorIndex) { sb.Append("Error #").Append(errorIndex).Append(':').AppendLine() diff --git a/src/PowerShellEditorServices/Utility/PSCommandExtensions.cs b/src/PowerShellEditorServices/Utility/PSCommandExtensions.cs index 55c8eed20..df03bb334 100644 --- a/src/PowerShellEditorServices/Utility/PSCommandExtensions.cs +++ b/src/PowerShellEditorServices/Utility/PSCommandExtensions.cs @@ -2,6 +2,7 @@ // Licensed under the MIT License. using System; +using System.IO; using System.Linq.Expressions; using System.Management.Automation; using System.Management.Automation.Runspaces; @@ -61,6 +62,20 @@ public static PSCommand MergePipelineResults(this PSCommand psCommand) return psCommand; } + public static PSCommand AddProfileLoadIfExists(this PSCommand psCommand, PSObject profileVariable, string profileName, string profilePath) + { + profileVariable.Members.Add(new PSNoteProperty(profileName, profilePath)); + + if (File.Exists(profilePath)) + { + psCommand + .AddCommand(profilePath, useLocalScope: false) + .AddStatement(); + } + + return psCommand; + } + /// /// Get a representation of the PSCommand, for logging purposes. /// From 6a384589f09f4e7a7de5f1da877a347ac8236489 Mon Sep 17 00:00:00 2001 From: Robert Holt Date: Thu, 28 Oct 2021 15:10:06 -0700 Subject: [PATCH 2/4] Fix syntax error in host start options for debugger --- .../Services/PowerShell/Utility/PowerShellExtensions.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/PowerShellEditorServices/Services/PowerShell/Utility/PowerShellExtensions.cs b/src/PowerShellEditorServices/Services/PowerShell/Utility/PowerShellExtensions.cs index c3e571315..122b73b4e 100644 --- a/src/PowerShellEditorServices/Services/PowerShell/Utility/PowerShellExtensions.cs +++ b/src/PowerShellEditorServices/Services/PowerShell/Utility/PowerShellExtensions.cs @@ -169,7 +169,8 @@ public static void LoadProfiles(this PowerShell pwsh, ProfilePathInfo profilePat .AddProfileLoadIfExists(profileVariable, nameof(profilePaths.AllUsersAllHosts), profilePaths.AllUsersAllHosts) .AddProfileLoadIfExists(profileVariable, nameof(profilePaths.AllUsersCurrentHost), profilePaths.AllUsersCurrentHost) .AddProfileLoadIfExists(profileVariable, nameof(profilePaths.CurrentUserAllHosts), profilePaths.CurrentUserAllHosts) - .AddProfileLoadIfExists(profileVariable, nameof(profilePaths.CurrentUserCurrentHost), profilePaths.CurrentUserCurrentHost); + .AddProfileLoadIfExists(profileVariable, nameof(profilePaths.CurrentUserCurrentHost), profilePaths.CurrentUserCurrentHost) + .AddOutputCommand(); pwsh.InvokeCommand(psCommand); From 007ab6dca853704d4e16d94a64d8bcea7ea9e861 Mon Sep 17 00:00:00 2001 From: Robert Holt Date: Thu, 28 Oct 2021 15:38:02 -0700 Subject: [PATCH 3/4] Fix profile output --- .../Services/PowerShell/Utility/PowerShellExtensions.cs | 1 - src/PowerShellEditorServices/Utility/PSCommandExtensions.cs | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PowerShellEditorServices/Services/PowerShell/Utility/PowerShellExtensions.cs b/src/PowerShellEditorServices/Services/PowerShell/Utility/PowerShellExtensions.cs index 122b73b4e..b07098425 100644 --- a/src/PowerShellEditorServices/Services/PowerShell/Utility/PowerShellExtensions.cs +++ b/src/PowerShellEditorServices/Services/PowerShell/Utility/PowerShellExtensions.cs @@ -170,7 +170,6 @@ public static void LoadProfiles(this PowerShell pwsh, ProfilePathInfo profilePat .AddProfileLoadIfExists(profileVariable, nameof(profilePaths.AllUsersCurrentHost), profilePaths.AllUsersCurrentHost) .AddProfileLoadIfExists(profileVariable, nameof(profilePaths.CurrentUserAllHosts), profilePaths.CurrentUserAllHosts) .AddProfileLoadIfExists(profileVariable, nameof(profilePaths.CurrentUserCurrentHost), profilePaths.CurrentUserCurrentHost) - .AddOutputCommand(); pwsh.InvokeCommand(psCommand); diff --git a/src/PowerShellEditorServices/Utility/PSCommandExtensions.cs b/src/PowerShellEditorServices/Utility/PSCommandExtensions.cs index df03bb334..634ec2674 100644 --- a/src/PowerShellEditorServices/Utility/PSCommandExtensions.cs +++ b/src/PowerShellEditorServices/Utility/PSCommandExtensions.cs @@ -70,6 +70,7 @@ public static PSCommand AddProfileLoadIfExists(this PSCommand psCommand, PSObjec { psCommand .AddCommand(profilePath, useLocalScope: false) + .AddOutputCommand() .AddStatement(); } From e7bcfe1bb4973dc46e8eb5cf4453d249010c0909 Mon Sep 17 00:00:00 2001 From: Robert Holt Date: Thu, 28 Oct 2021 15:43:44 -0700 Subject: [PATCH 4/4] Add semicolon... --- .../Services/PowerShell/Utility/PowerShellExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PowerShellEditorServices/Services/PowerShell/Utility/PowerShellExtensions.cs b/src/PowerShellEditorServices/Services/PowerShell/Utility/PowerShellExtensions.cs index b07098425..c3e571315 100644 --- a/src/PowerShellEditorServices/Services/PowerShell/Utility/PowerShellExtensions.cs +++ b/src/PowerShellEditorServices/Services/PowerShell/Utility/PowerShellExtensions.cs @@ -169,7 +169,7 @@ public static void LoadProfiles(this PowerShell pwsh, ProfilePathInfo profilePat .AddProfileLoadIfExists(profileVariable, nameof(profilePaths.AllUsersAllHosts), profilePaths.AllUsersAllHosts) .AddProfileLoadIfExists(profileVariable, nameof(profilePaths.AllUsersCurrentHost), profilePaths.AllUsersCurrentHost) .AddProfileLoadIfExists(profileVariable, nameof(profilePaths.CurrentUserAllHosts), profilePaths.CurrentUserAllHosts) - .AddProfileLoadIfExists(profileVariable, nameof(profilePaths.CurrentUserCurrentHost), profilePaths.CurrentUserCurrentHost) + .AddProfileLoadIfExists(profileVariable, nameof(profilePaths.CurrentUserCurrentHost), profilePaths.CurrentUserCurrentHost); pwsh.InvokeCommand(psCommand);