From 79bb68d7847954d42c9737eaa1511f02ce4c7514 Mon Sep 17 00:00:00 2001 From: ryanbodrug-microsoft <56318517+ryanbodrug-microsoft@users.noreply.github.com> Date: Fri, 21 Aug 2020 12:10:41 -0700 Subject: [PATCH] User/ryanbod/shell plugin fxcop (#6043) * Enabling FxCop static analysis. * Fixes for CA2227 Change 'Count' to be read-only by removing the property setter. https://docs.microsoft.com/en-us/visualstudio/code-quality/ca2227?view=vs-2019 * Fix for: CA1822: Mark members as static https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1822?view=vs-2019 * Fix for CA1805: Do not initialize unnecessarily. https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1805?view=vs-2019 * Fix for: Error CA1724 The type name Settings conflicts in whole or in part with the namespace name 'Microsoft.PowerToys.Settings'. Change either name to eliminate the conflict. Microsoft.Plugin.Shell C:\repos\powertoys\src\modules\launcher\Plugins\Microsoft.Plugin.Shell\Settings.cs 9 Active * Severity Code Description Project File Line Suppression State Error CA1724 The type name Settings conflicts in whole or in part with the namespace name 'Microsoft.PowerToys.Settings'. Change either name to eliminate the conflict. Microsoft.Plugin.Shell C:\repos\powertoys\src\modules\launcher\Plugins\Microsoft.Plugin.Shell\Settings.cs 9 Active * Fix for CA1307: Specify StringComparison & CA1305: Specify IFormatProvider https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1307?view=vs-2019 https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1305?view=vs-2019 * Fix for CA1062: Validate arguments of public methods https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1062?view=vs-2019 * Suppressing CA1031 Modify 'Query' to catch a more specific allowed exception type, or rethrow the exception' --- .../Plugins/Microsoft.Plugin.Shell/Main.cs | 28 +++++++++++-------- .../Microsoft.Plugin.Shell.csproj | 4 +++ .../{Settings.cs => ShellPluginSettings.cs} | 12 ++++---- .../ShellSetting.xaml.cs | 10 +++---- 4 files changed, 32 insertions(+), 22 deletions(-) rename src/modules/launcher/Plugins/Microsoft.Plugin.Shell/{Settings.cs => ShellPluginSettings.cs} (69%) diff --git a/src/modules/launcher/Plugins/Microsoft.Plugin.Shell/Main.cs b/src/modules/launcher/Plugins/Microsoft.Plugin.Shell/Main.cs index 5adaa2a90b..a93664e80b 100644 --- a/src/modules/launcher/Plugins/Microsoft.Plugin.Shell/Main.cs +++ b/src/modules/launcher/Plugins/Microsoft.Plugin.Shell/Main.cs @@ -22,8 +22,8 @@ namespace Microsoft.Plugin.Shell { public class Main : IPlugin, ISettingProvider, IPluginI18n, IContextMenu, ISavable { - private readonly Settings _settings; - private readonly PluginJsonStorage _storage; + private readonly ShellPluginSettings _settings; + private readonly PluginJsonStorage _storage; private string IconPath { get; set; } @@ -31,7 +31,7 @@ namespace Microsoft.Plugin.Shell public Main() { - _storage = new PluginJsonStorage(); + _storage = new PluginJsonStorage(); _settings = _storage.Load(); } @@ -40,8 +40,14 @@ namespace Microsoft.Plugin.Shell _storage.Save(); } + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "Keeping the process alive, but logging the exception")] public List Query(Query query) { + if (query == null) + { + throw new ArgumentNullException(nameof(query)); + } + List results = new List(); string cmd = query.Search; if (string.IsNullOrEmpty(cmd)) @@ -71,20 +77,20 @@ namespace Microsoft.Plugin.Shell private List GetHistoryCmds(string cmd, Result result) { - IEnumerable history = _settings.Count.Where(o => o.Key.Contains(cmd)) + IEnumerable history = _settings.Count.Where(o => o.Key.Contains(cmd, StringComparison.CurrentCultureIgnoreCase)) .OrderByDescending(o => o.Value) .Select(m => { if (m.Key == cmd) { - result.SubTitle = "Shell: " + string.Format(_context.API.GetTranslation("wox_plugin_cmd_cmd_has_been_executed_times"), m.Value); + result.SubTitle = "Shell: " + string.Format(CultureInfo.CurrentCulture, _context.API.GetTranslation("wox_plugin_cmd_cmd_has_been_executed_times"), m.Value); return null; } var ret = new Result { Title = m.Key, - SubTitle = "Shell: " + string.Format(_context.API.GetTranslation("wox_plugin_cmd_cmd_has_been_executed_times"), m.Value), + SubTitle = "Shell: " + string.Format(CultureInfo.CurrentCulture, _context.API.GetTranslation("wox_plugin_cmd_cmd_has_been_executed_times"), m.Value), IcoPath = IconPath, Action = c => { @@ -121,7 +127,7 @@ namespace Microsoft.Plugin.Shell .Select(m => new Result { Title = m.Key, - SubTitle = "Shell: " + string.Format(_context.API.GetTranslation("wox_plugin_cmd_cmd_has_been_executed_times"), m.Value), + SubTitle = "Shell: " + string.Format(CultureInfo.CurrentCulture, _context.API.GetTranslation("wox_plugin_cmd_cmd_has_been_executed_times"), m.Value), IcoPath = IconPath, Action = c => { @@ -140,13 +146,13 @@ namespace Microsoft.Plugin.Shell var runAsAdministratorArg = !runAsAdministrator && !_settings.RunAsAdministrator ? string.Empty : "runas"; ProcessStartInfo info; - if (_settings.Shell == Shell.Cmd) + if (_settings.Shell == ExecutionShell.Cmd) { var arguments = _settings.LeaveShellOpen ? $"/k \"{command}\"" : $"/c \"{command}\" & pause"; info = ShellCommand.SetProcessStartInfo("cmd.exe", workingDirectory, arguments, runAsAdministratorArg); } - else if (_settings.Shell == Shell.Powershell) + else if (_settings.Shell == ExecutionShell.Powershell) { string arguments; if (_settings.LeaveShellOpen) @@ -160,7 +166,7 @@ namespace Microsoft.Plugin.Shell info = ShellCommand.SetProcessStartInfo("powershell.exe", workingDirectory, arguments, runAsAdministratorArg); } - else if (_settings.Shell == Shell.RunCommand) + else if (_settings.Shell == ExecutionShell.RunCommand) { // Open explorer if the path is a file or directory if (Directory.Exists(command) || File.Exists(command)) @@ -221,7 +227,7 @@ namespace Microsoft.Plugin.Shell } } - private bool ExistInPath(string filename) + private static bool ExistInPath(string filename) { if (File.Exists(filename)) { diff --git a/src/modules/launcher/Plugins/Microsoft.Plugin.Shell/Microsoft.Plugin.Shell.csproj b/src/modules/launcher/Plugins/Microsoft.Plugin.Shell/Microsoft.Plugin.Shell.csproj index 0a0b286e36..33515fce70 100644 --- a/src/modules/launcher/Plugins/Microsoft.Plugin.Shell/Microsoft.Plugin.Shell.csproj +++ b/src/modules/launcher/Plugins/Microsoft.Plugin.Shell/Microsoft.Plugin.Shell.csproj @@ -101,6 +101,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/src/modules/launcher/Plugins/Microsoft.Plugin.Shell/Settings.cs b/src/modules/launcher/Plugins/Microsoft.Plugin.Shell/ShellPluginSettings.cs similarity index 69% rename from src/modules/launcher/Plugins/Microsoft.Plugin.Shell/Settings.cs rename to src/modules/launcher/Plugins/Microsoft.Plugin.Shell/ShellPluginSettings.cs index d1d9f73799..b7b5bc7636 100644 --- a/src/modules/launcher/Plugins/Microsoft.Plugin.Shell/Settings.cs +++ b/src/modules/launcher/Plugins/Microsoft.Plugin.Shell/ShellPluginSettings.cs @@ -6,19 +6,19 @@ using System.Collections.Generic; namespace Microsoft.Plugin.Shell { - public class Settings + public class ShellPluginSettings { - public Shell Shell { get; set; } = Shell.RunCommand; + public ExecutionShell Shell { get; set; } = ExecutionShell.RunCommand; // not overriding Win+R // crutkas we need to earn the right for Win+R override - public bool ReplaceWinR { get; set; } = false; + public bool ReplaceWinR { get; set; } public bool LeaveShellOpen { get; set; } - public bool RunAsAdministrator { get; set; } = false; + public bool RunAsAdministrator { get; set; } - public Dictionary Count { get; set; } = new Dictionary(); + public Dictionary Count { get; } = new Dictionary(); public void AddCmdHistory(string cmdName) { @@ -33,7 +33,7 @@ namespace Microsoft.Plugin.Shell } } - public enum Shell + public enum ExecutionShell { Cmd = 0, Powershell = 1, diff --git a/src/modules/launcher/Plugins/Microsoft.Plugin.Shell/ShellSetting.xaml.cs b/src/modules/launcher/Plugins/Microsoft.Plugin.Shell/ShellSetting.xaml.cs index 8c1939b33f..fe978c5c97 100644 --- a/src/modules/launcher/Plugins/Microsoft.Plugin.Shell/ShellSetting.xaml.cs +++ b/src/modules/launcher/Plugins/Microsoft.Plugin.Shell/ShellSetting.xaml.cs @@ -9,9 +9,9 @@ namespace Microsoft.Plugin.Shell { public partial class CMDSetting : UserControl { - private readonly Settings _settings; + private readonly ShellPluginSettings _settings; - public CMDSetting(Settings settings) + public CMDSetting(ShellPluginSettings settings) { InitializeComponent(); _settings = settings; @@ -22,7 +22,7 @@ namespace Microsoft.Plugin.Shell ReplaceWinR.IsChecked = _settings.ReplaceWinR; LeaveShellOpen.IsChecked = _settings.LeaveShellOpen; AlwaysRunAsAdministrator.IsChecked = _settings.RunAsAdministrator; - LeaveShellOpen.IsEnabled = _settings.Shell != Shell.RunCommand; + LeaveShellOpen.IsEnabled = _settings.Shell != ExecutionShell.RunCommand; LeaveShellOpen.Checked += (o, e) => { @@ -56,8 +56,8 @@ namespace Microsoft.Plugin.Shell ShellComboBox.SelectedIndex = (int)_settings.Shell; ShellComboBox.SelectionChanged += (o, e) => { - _settings.Shell = (Shell)ShellComboBox.SelectedIndex; - LeaveShellOpen.IsEnabled = _settings.Shell != Shell.RunCommand; + _settings.Shell = (ExecutionShell)ShellComboBox.SelectedIndex; + LeaveShellOpen.IsEnabled = _settings.Shell != ExecutionShell.RunCommand; }; } }