Skip to content
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

User/ryanbod/shell plugin fxcop #6043

28 changes: 17 additions & 11 deletions src/modules/launcher/Plugins/Microsoft.Plugin.Shell/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ namespace Microsoft.Plugin.Shell
{
public class Main : IPlugin, ISettingProvider, IPluginI18n, IContextMenu, ISavable
{
private readonly Settings _settings;
private readonly PluginJsonStorage<Settings> _storage;
private readonly ShellPluginSettings _settings;
private readonly PluginJsonStorage<ShellPluginSettings> _storage;

private string IconPath { get; set; }

private PluginInitContext _context;

public Main()
{
_storage = new PluginJsonStorage<Settings>();
_storage = new PluginJsonStorage<ShellPluginSettings>();
_settings = _storage.Load();
}

Expand All @@ -40,8 +40,14 @@ public void Save()
_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<Result> Query(Query query)
{
if (query == null)
{
throw new ArgumentNullException(nameof(query));
}

List<Result> results = new List<Result>();
string cmd = query.Search;
if (string.IsNullOrEmpty(cmd))
Expand Down Expand Up @@ -71,20 +77,20 @@ public List<Result> Query(Query query)

private List<Result> GetHistoryCmds(string cmd, Result result)
{
IEnumerable<Result> history = _settings.Count.Where(o => o.Key.Contains(cmd))
IEnumerable<Result> 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 =>
{
Expand Down Expand Up @@ -121,7 +127,7 @@ private List<Result> ResultsFromlHistory()
.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 =>
{
Expand All @@ -140,13 +146,13 @@ private ProcessStartInfo PrepareProcessStartInfo(string command, bool runAsAdmin
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)
Expand All @@ -160,7 +166,7 @@ private ProcessStartInfo PrepareProcessStartInfo(string command, bool runAsAdmin

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))
Expand Down Expand Up @@ -221,7 +227,7 @@ private void Execute(Func<ProcessStartInfo, Process> startProcess, ProcessStartI
}
}

private bool ExistInPath(string filename)
private static bool ExistInPath(string filename)
{
if (File.Exists(filename))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@

<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2020.1.0" />
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="3.3.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="System.Runtime" Version="4.3.1" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@

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; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was the false initialisation removed because we are planning to override Win+R?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alekhyareddy28 . Nope this was to fix CA1805. It is redundant to initialize a field to its default value.

Rule description
The .NET runtime initializes all fields of reference types to their default values before running the constructor. In most cases, explicitly initializing a field to its default value in a constructor is redundant, adding maintenance costs and potentially degrading performance (such as with increased assembly size), and the explicit initialization can be removed.


public bool LeaveShellOpen { get; set; }

public bool RunAsAdministrator { get; set; } = false;
public bool RunAsAdministrator { get; set; }

public Dictionary<string, int> Count { get; set; } = new Dictionary<string, int>();
public Dictionary<string, int> Count { get; } = new Dictionary<string, int>();

public void AddCmdHistory(string cmdName)
{
Expand All @@ -33,7 +33,7 @@ public void AddCmdHistory(string cmdName)
}
}

public enum Shell
public enum ExecutionShell
{
Cmd = 0,
Powershell = 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,7 +22,7 @@ private void CMDSetting_OnLoaded(object sender, RoutedEventArgs re)
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) =>
{
Expand Down Expand Up @@ -56,8 +56,8 @@ private void CMDSetting_OnLoaded(object sender, RoutedEventArgs re)
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;
};
}
}
Expand Down