Skip to content

Commit caea822

Browse files
committed
Use AddCommand to run profiles
1 parent 83244eb commit caea822

File tree

2 files changed

+22
-21
lines changed

2 files changed

+22
-21
lines changed

src/PowerShellEditorServices/Services/PowerShell/Utility/PowerShellExtensions.cs

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
using Microsoft.PowerShell.EditorServices.Hosting;
1010
using Microsoft.PowerShell.EditorServices.Utility;
1111
using System.Collections.Generic;
12-
using System.IO;
1312

1413
namespace Microsoft.PowerShell.EditorServices.Services.PowerShell.Utility
1514
{
@@ -166,10 +165,13 @@ public static void LoadProfiles(this PowerShell pwsh, ProfilePathInfo profilePat
166165
{
167166
var profileVariable = new PSObject();
168167

169-
pwsh.AddProfileMemberAndLoadIfExists(profileVariable, nameof(profilePaths.AllUsersAllHosts), profilePaths.AllUsersAllHosts)
170-
.AddProfileMemberAndLoadIfExists(profileVariable, nameof(profilePaths.AllUsersCurrentHost), profilePaths.AllUsersCurrentHost)
171-
.AddProfileMemberAndLoadIfExists(profileVariable, nameof(profilePaths.CurrentUserAllHosts), profilePaths.CurrentUserAllHosts)
172-
.AddProfileMemberAndLoadIfExists(profileVariable, nameof(profilePaths.CurrentUserCurrentHost), profilePaths.CurrentUserCurrentHost);
168+
var psCommand = new PSCommand()
169+
.AddProfileLoadIfExists(profileVariable, nameof(profilePaths.AllUsersAllHosts), profilePaths.AllUsersAllHosts)
170+
.AddProfileLoadIfExists(profileVariable, nameof(profilePaths.AllUsersCurrentHost), profilePaths.AllUsersCurrentHost)
171+
.AddProfileLoadIfExists(profileVariable, nameof(profilePaths.CurrentUserAllHosts), profilePaths.CurrentUserAllHosts)
172+
.AddProfileLoadIfExists(profileVariable, nameof(profilePaths.CurrentUserCurrentHost), profilePaths.CurrentUserCurrentHost);
173+
174+
pwsh.InvokeCommand(psCommand);
173175

174176
pwsh.Runspace.SessionStateProxy.SetVariable("PROFILE", profileVariable);
175177
}
@@ -200,22 +202,6 @@ public static string GetErrorString(this PowerShell pwsh)
200202
return sb.ToString();
201203
}
202204

203-
private static PowerShell AddProfileMemberAndLoadIfExists(this PowerShell pwsh, PSObject profileVariable, string profileName, string profilePath)
204-
{
205-
profileVariable.Members.Add(new PSNoteProperty(profileName, profilePath));
206-
207-
if (File.Exists(profilePath))
208-
{
209-
var psCommand = new PSCommand()
210-
.AddScript(profilePath, useLocalScope: false)
211-
.AddOutputCommand();
212-
213-
pwsh.InvokeCommand(psCommand);
214-
}
215-
216-
return pwsh;
217-
}
218-
219205
private static StringBuilder AddErrorString(this StringBuilder sb, ErrorRecord error, int errorIndex)
220206
{
221207
sb.Append("Error #").Append(errorIndex).Append(':').AppendLine()

src/PowerShellEditorServices/Utility/PSCommandExtensions.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT License.
33

44
using System;
5+
using System.IO;
56
using System.Linq.Expressions;
67
using System.Management.Automation;
78
using System.Management.Automation.Runspaces;
@@ -61,6 +62,20 @@ public static PSCommand MergePipelineResults(this PSCommand psCommand)
6162
return psCommand;
6263
}
6364

65+
public static PSCommand AddProfileLoadIfExists(this PSCommand psCommand, PSObject profileVariable, string profileName, string profilePath)
66+
{
67+
profileVariable.Members.Add(new PSNoteProperty(profileName, profilePath));
68+
69+
if (File.Exists(profilePath))
70+
{
71+
psCommand
72+
.AddCommand(profilePath, useLocalScope: false)
73+
.AddStatement();
74+
}
75+
76+
return psCommand;
77+
}
78+
6479
/// <summary>
6580
/// Get a representation of the PSCommand, for logging purposes.
6681
/// </summary>

0 commit comments

Comments
 (0)