Skip to content

Ensure profiles are run at startup and output is shown #1602

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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()
Expand Down
16 changes: 16 additions & 0 deletions src/PowerShellEditorServices/Utility/PSCommandExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -61,6 +62,21 @@ 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)
.AddOutputCommand()
.AddStatement();
}

return psCommand;
}

/// <summary>
/// Get a representation of the PSCommand, for logging purposes.
/// </summary>
Expand Down