Clean up '#if CORE' in PropertyAccessor.cs (#4429)

* Cleanup '#if CORE' in PropertyAccessor.cs

* Remove RegistryAccessor class
This commit is contained in:
Ilya 2017-08-03 19:34:21 +04:00 committed by Mike Richmond
parent 1a003a8af3
commit 8034dd6dd1

View file

@ -11,10 +11,9 @@ using System.Threading;
using System.Management.Automation;
using Microsoft.Win32;
#if CORECLR
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
#endif
namespace System.Management.Automation
{
@ -31,13 +30,7 @@ namespace System.Management.Automation
/// </summary>
static ConfigPropertyAccessor()
{
#if CORECLR
Instance = Platform.IsInbox
? (ConfigPropertyAccessor) new RegistryAccessor()
: new JsonConfigFileAccessor();
#else
Instance = new RegistryAccessor();
#endif
Instance = new JsonConfigFileAccessor();
}
/// <summary>
/// The instance of the ConfigPropertyAccessor to use to interact with properties.
@ -111,7 +104,6 @@ namespace System.Management.Automation
#endregion // Interface Methods
}
#if CORECLR
/// <summary>
/// JSON configuration file accessor
///
@ -490,307 +482,5 @@ namespace System.Management.Automation
}
}
}
#endif // CORECLR
internal class RegistryAccessor : ConfigPropertyAccessor
{
private const string DisablePromptToUpdateHelpRegPath = "Software\\Microsoft\\PowerShell";
private const string DisablePromptToUpdateHelpRegPath32 = "Software\\Wow6432Node\\Microsoft\\PowerShell";
private const string DisablePromptToUpdateHelpRegKey = "DisablePromptToUpdateHelp";
private const string DefaultSourcePathRegPath = "Software\\Policies\\Microsoft\\Windows\\PowerShell\\UpdatableHelp";
private const string DefaultSourcePathRegKey = "DefaultSourcePath";
internal RegistryAccessor()
{
}
/// <summary>
/// Gets the specified module path from the appropriate Environment entry in the registry.
/// </summary>
/// <param name="scope"></param>
/// <returns>The specified module path. Null if not present.</returns>
internal override string GetModulePath(PropertyScope scope)
{
if (PropertyScope.CurrentUser == scope)
{
return ModuleIntrinsics.GetExpandedEnvironmentVariable(Constants.PSModulePathEnvVar, EnvironmentVariableTarget.User);
}
else
{
return ModuleIntrinsics.GetExpandedEnvironmentVariable(Constants.PSModulePathEnvVar, EnvironmentVariableTarget.Machine);
}
}
/// <summary>
///
/// </summary>
/// <param name="scope"></param>
/// <param name="shellId"></param>
/// <returns>The execution policy string if found, otherwise null.</returns>
internal override string GetExecutionPolicy(PropertyScope scope, string shellId)
{
string regKeyName = Utils.GetRegistryConfigurationPath(shellId);
RegistryKey scopedKey = Registry.LocalMachine;
// Override if set to another value;
if (PropertyScope.CurrentUser == scope)
{
scopedKey = Registry.CurrentUser;
}
return GetRegistryString(scopedKey, regKeyName, "ExecutionPolicy");
}
internal override void SetExecutionPolicy(PropertyScope scope, string shellId, string executionPolicy)
{
string regKeyName = Utils.GetRegistryConfigurationPath(shellId);
RegistryKey scopedKey = Registry.LocalMachine;
// Override if set to another value;
if (PropertyScope.CurrentUser == scope)
{
scopedKey = Registry.CurrentUser;
}
using (RegistryKey key = scopedKey.CreateSubKey(regKeyName))
{
if (null != key)
{
key.SetValue("ExecutionPolicy", executionPolicy, RegistryValueKind.String);
}
}
}
internal override void RemoveExecutionPolicy(PropertyScope scope, string shellId)
{
string regKeyName = Utils.GetRegistryConfigurationPath(shellId);
RegistryKey scopedKey = Registry.LocalMachine;
// Override if set to another value;
if (PropertyScope.CurrentUser == scope)
{
scopedKey = Registry.CurrentUser;
}
using (RegistryKey key = scopedKey.OpenSubKey(regKeyName, true))
{
if (key != null)
{
if (key.GetValue("ExecutionPolicy") != null)
key.DeleteValue("ExecutionPolicy");
}
}
}
/// <summary>
/// Existing Key = HKLM\SOFTWARE\Microsoft\PowerShell\1\ShellIds
/// Proposed value = existing default. Probably "1"
/// </summary>
/// <returns>Whether console prompting should happen.</returns>
internal override bool GetConsolePrompting()
{
string policyKeyName = Utils.GetRegistryConfigurationPrefix();
string tempPrompt = GetRegistryString(Registry.LocalMachine, policyKeyName, "ConsolePrompting");
if (null != tempPrompt)
{
return Convert.ToBoolean(tempPrompt, CultureInfo.InvariantCulture);
}
else
{
return false;
}
}
internal override void SetConsolePrompting(bool shouldPrompt)
{
string policyKeyName = Utils.GetRegistryConfigurationPrefix();
SetRegistryString(Registry.LocalMachine, policyKeyName, "ConsolePrompting", shouldPrompt.ToString());
}
/// <summary>
/// Existing Key = HKLM\SOFTWARE\Microsoft\PowerShell
/// Proposed value = Existing default. Probably "0"
/// </summary>
/// <returns>Boolean indicating whether Update-Help should prompt</returns>
internal override bool GetDisablePromptToUpdateHelp()
{
using (RegistryKey hklm = Registry.LocalMachine.OpenSubKey(DisablePromptToUpdateHelpRegPath))
{
if (hklm != null)
{
object disablePromptToUpdateHelp = hklm.GetValue(DisablePromptToUpdateHelpRegKey, null, RegistryValueOptions.None);
if (disablePromptToUpdateHelp == null)
{
return true;
}
else
{
int result;
if (LanguagePrimitives.TryConvertTo<int>(disablePromptToUpdateHelp, out result))
{
return (result != 1);
}
return true;
}
}
else
{
return true;
}
}
}
internal override void SetDisablePromptToUpdateHelp(bool prompt)
{
int valueToSet = prompt ? 1 : 0;
using (RegistryKey hklm = Registry.LocalMachine.OpenSubKey(DisablePromptToUpdateHelpRegPath, true))
{
if (hklm != null)
{
hklm.SetValue(DisablePromptToUpdateHelpRegKey, valueToSet, RegistryValueKind.DWord);
}
}
using (RegistryKey hklm = Registry.LocalMachine.OpenSubKey(DisablePromptToUpdateHelpRegPath32, true))
{
if (hklm != null)
{
hklm.SetValue(DisablePromptToUpdateHelpRegKey, valueToSet, RegistryValueKind.DWord);
}
}
}
/// <summary>
/// Existing Key = HKCU and HKLM\Software\Policies\Microsoft\Windows\PowerShell\UpdatableHelp
/// Proposed value = blank.This should be supported though
/// </summary>
/// <returns></returns>
internal override string GetDefaultSourcePath()
{
return GetRegistryString(Registry.LocalMachine, DefaultSourcePathRegPath, DefaultSourcePathRegKey);
}
internal override void SetDefaultSourcePath(string defaultPath)
{
SetRegistryString(Registry.LocalMachine, DefaultSourcePathRegPath, DefaultSourcePathRegKey, defaultPath);
}
/// <summary>
/// Reads a DWORD from the Registry. Exceptions are intentionally allowed to pass through to
/// the caller because different classes and methods within the code base handle Registry
/// exceptions differently. Some suppress exceptions and others pass them to the user.
/// </summary>
/// <param name="rootKey"></param>
/// <param name="pathToKey"></param>
/// <param name="valueName"></param>
/// <returns></returns>
private int? GetRegistryDword(RegistryKey rootKey, string pathToKey, string valueName)
{
using (RegistryKey regKey = rootKey.OpenSubKey(pathToKey))
{
if (null == regKey)
{
// Key not found
return null;
}
// verify the value kind as a string
RegistryValueKind kind = regKey.GetValueKind(valueName);
if (kind == RegistryValueKind.DWord)
{
return regKey.GetValue(valueName) as int?;
}
else
{
// The function expected a DWORD, but got another type. This is a coding error or a registry key typing error.
return null;
}
}
}
/// <summary>
/// Exceptions are intentionally allowed to pass through to
/// the caller because different classes and methods within the code base handle Registry
/// exceptions differently. Some suppress exceptions and others pass them to the user.
/// </summary>
/// <param name="rootKey"></param>
/// <param name="pathToKey"></param>
/// <param name="valueName"></param>
/// <param name="value"></param>
private void SetRegistryDword(RegistryKey rootKey, string pathToKey, string valueName, int value)
{
using (RegistryKey regKey = rootKey.OpenSubKey(pathToKey))
{
if (null != regKey)
{
regKey.SetValue(valueName, value, RegistryValueKind.DWord);
}
}
}
/// <summary>
/// Exceptions are intentionally allowed to pass through to
/// the caller because different classes and methods within the code base handle Registry
/// exceptions differently. Some suppress exceptions and others pass them to the user.
/// </summary>
/// <param name="rootKey"></param>
/// <param name="pathToKey"></param>
/// <param name="valueName"></param>
/// <returns></returns>
private string GetRegistryString(RegistryKey rootKey, string pathToKey, string valueName)
{
using (RegistryKey regKey = rootKey.OpenSubKey(pathToKey))
{
if (null == regKey)
{
// Key not found
return null;
}
object regValue = regKey.GetValue(valueName);
if (null != regValue)
{
// verify the value kind as a string
RegistryValueKind kind = regKey.GetValueKind(valueName);
if (kind == RegistryValueKind.ExpandString ||
kind == RegistryValueKind.String)
{
return regValue as string;
}
}
// The function expected a string, but got another type or the value doesn't exist.
return null;
}
}
/// <summary>
/// Exceptions are intentionally allowed to pass through to
/// the caller because different classes and methods within the code base handle Registry
/// exceptions differently. Some suppress exceptions and others pass them to the user.
/// </summary>
/// <param name="rootKey"></param>
/// <param name="pathToKey"></param>
/// <param name="valueName"></param>
/// <param name="value"></param>
/// <returns></returns>
private void SetRegistryString(RegistryKey rootKey, string pathToKey, string valueName, string value)
{
using (RegistryKey key = rootKey.CreateSubKey(pathToKey))
{
if (null != key)
{
key.SetValue(valueName, value, RegistryValueKind.String);
}
}
}
}
} // Namespace System.Management.Automation