Skip to content

Commit 6aa3188

Browse files
daxian-dbwiSazonov
authored andcommitted
Remove the unused setting key ConsolePrompting and avoid unnecessary string creation when querying ExecutionPolicy setting (PowerShell#10985)
1 parent 7ddfb82 commit 6aa3188

File tree

1 file changed

+15
-35
lines changed

1 file changed

+15
-35
lines changed

src/System.Management.Automation/engine/PSConfiguration.cs

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ public enum ConfigScope
4747
/// </remarks>
4848
internal sealed class PowerShellConfig
4949
{
50-
private const string configFileName = "powershell.config.json";
50+
private const string ConfigFileName = "powershell.config.json";
51+
private const string ExecutionPolicyDefaultShellKey = "Microsoft.PowerShell:ExecutionPolicy";
5152

5253
// Provide a singleton
5354
internal static readonly PowerShellConfig Instance = new PowerShellConfig();
@@ -79,13 +80,13 @@ private PowerShellConfig()
7980
{
8081
// Sets the system-wide configuration file.
8182
systemWideConfigDirectory = Utils.DefaultPowerShellAppBase;
82-
systemWideConfigFile = Path.Combine(systemWideConfigDirectory, configFileName);
83+
systemWideConfigFile = Path.Combine(systemWideConfigDirectory, ConfigFileName);
8384

8485
// Sets the per-user configuration directory
8586
// Note: This directory may or may not exist depending upon the execution scenario.
8687
// Writes will attempt to create the directory if it does not already exist.
8788
perUserConfigDirectory = Platform.ConfigDirectory;
88-
perUserConfigFile = Path.Combine(perUserConfigDirectory, configFileName);
89+
perUserConfigFile = Path.Combine(perUserConfigDirectory, ConfigFileName);
8990

9091
emptyConfig = new JObject();
9192
configRoots = new JObject[2];
@@ -153,49 +154,28 @@ internal string GetModulePath(ConfigScope scope)
153154
/// <returns>The execution policy if found. Null otherwise.</returns>
154155
internal string GetExecutionPolicy(ConfigScope scope, string shellId)
155156
{
156-
string execPolicy = null;
157-
158-
string valueName = string.Concat(shellId, ":", "ExecutionPolicy");
159-
string rawExecPolicy = ReadValueFromFile<string>(scope, valueName);
160-
161-
if (!string.IsNullOrEmpty(rawExecPolicy))
162-
{
163-
execPolicy = rawExecPolicy;
164-
}
165-
166-
return execPolicy;
157+
string key = GetExecutionPolicySettingKey(shellId);
158+
string execPolicy = ReadValueFromFile<string>(scope, key);
159+
return string.IsNullOrEmpty(execPolicy) ? null : execPolicy;
167160
}
168161

169162
internal void RemoveExecutionPolicy(ConfigScope scope, string shellId)
170163
{
171-
string valueName = string.Concat(shellId, ":", "ExecutionPolicy");
172-
RemoveValueFromFile<string>(scope, valueName);
164+
string key = GetExecutionPolicySettingKey(shellId);
165+
RemoveValueFromFile<string>(scope, key);
173166
}
174167

175168
internal void SetExecutionPolicy(ConfigScope scope, string shellId, string executionPolicy)
176169
{
177-
string valueName = string.Concat(shellId, ":", "ExecutionPolicy");
178-
WriteValueToFile<string>(scope, valueName, executionPolicy);
179-
}
180-
181-
/// <summary>
182-
/// Existing Key = HKLM\SOFTWARE\Microsoft\PowerShell\1\ShellIds
183-
/// Proposed value = existing default. Probably "1"
184-
///
185-
/// Schema:
186-
/// {
187-
/// "ConsolePrompting" : bool
188-
/// }
189-
/// </summary>
190-
/// <returns>Whether console prompting should happen. If the value cannot be read it defaults to false.</returns>
191-
internal bool GetConsolePrompting()
192-
{
193-
return ReadValueFromFile<bool>(ConfigScope.AllUsers, "ConsolePrompting");
170+
string key = GetExecutionPolicySettingKey(shellId);
171+
WriteValueToFile<string>(scope, key, executionPolicy);
194172
}
195173

196-
internal void SetConsolePrompting(bool shouldPrompt)
174+
private string GetExecutionPolicySettingKey(string shellId)
197175
{
198-
WriteValueToFile<bool>(ConfigScope.AllUsers, "ConsolePrompting", shouldPrompt);
176+
return string.Equals(shellId, Utils.DefaultPowerShellShellID, StringComparison.Ordinal)
177+
? ExecutionPolicyDefaultShellKey
178+
: string.Concat(shellId, ":", "ExecutionPolicy");
199179
}
200180

201181
/// <summary>

0 commit comments

Comments
 (0)