diff --git a/Plugins/Wox.Plugin.CMD/CMDHistory.cs b/Plugins/Wox.Plugin.CMD/CMDHistory.cs index 22e8877b59..2734097395 100644 --- a/Plugins/Wox.Plugin.CMD/CMDHistory.cs +++ b/Plugins/Wox.Plugin.CMD/CMDHistory.cs @@ -2,10 +2,11 @@ namespace Wox.Plugin.CMD { - public class CMDHistory + public class Settings { + public Shell Shell { get; set; } = Shell.CMD; public bool ReplaceWinR { get; set; } = true; - public bool LeaveCmdOpen { get; set; } + public bool LeaveShellOpen { get; set; } public Dictionary Count = new Dictionary(); public void AddCmdHistory(string cmdName) @@ -20,4 +21,12 @@ namespace Wox.Plugin.CMD } } } + + public enum Shell + { + CMD = 0, + Powershell = 1, + RunCommand = 2, + + } } diff --git a/Plugins/Wox.Plugin.CMD/CMDSetting.xaml b/Plugins/Wox.Plugin.CMD/CMDSetting.xaml index 88f6dd838c..a0d4ae7d96 100644 --- a/Plugins/Wox.Plugin.CMD/CMDSetting.xaml +++ b/Plugins/Wox.Plugin.CMD/CMDSetting.xaml @@ -9,17 +9,17 @@ - - + + + - - - - - - - - + + + + CMD + PowerShell + RunCommand + diff --git a/Plugins/Wox.Plugin.CMD/CMDSetting.xaml.cs b/Plugins/Wox.Plugin.CMD/CMDSetting.xaml.cs index 7b666cd17a..8261109a68 100644 --- a/Plugins/Wox.Plugin.CMD/CMDSetting.xaml.cs +++ b/Plugins/Wox.Plugin.CMD/CMDSetting.xaml.cs @@ -5,9 +5,9 @@ namespace Wox.Plugin.CMD { public partial class CMDSetting : UserControl { - private readonly CMDHistory _settings; + private readonly Settings _settings; - public CMDSetting(CMDHistory settings) + public CMDSetting(Settings settings) { InitializeComponent(); _settings = settings; @@ -15,27 +15,35 @@ namespace Wox.Plugin.CMD private void CMDSetting_OnLoaded(object sender, RoutedEventArgs re) { - cbReplaceWinR.IsChecked = _settings.ReplaceWinR; - cbLeaveCmdOpen.IsChecked = _settings.LeaveCmdOpen; + ReplaceWinR.IsChecked = _settings.ReplaceWinR; + LeaveShellOpen.IsChecked = _settings.LeaveShellOpen; + LeaveShellOpen.IsEnabled = _settings.Shell != Shell.RunCommand; - cbLeaveCmdOpen.Checked += (o, e) => + LeaveShellOpen.Checked += (o, e) => { - _settings.LeaveCmdOpen = true; + _settings.LeaveShellOpen = true; }; - cbLeaveCmdOpen.Unchecked += (o, e) => + LeaveShellOpen.Unchecked += (o, e) => { - _settings.LeaveCmdOpen = false; + _settings.LeaveShellOpen = false; }; - cbReplaceWinR.Checked += (o, e) => + ReplaceWinR.Checked += (o, e) => { _settings.ReplaceWinR = true; }; - cbReplaceWinR.Unchecked += (o, e) => + ReplaceWinR.Unchecked += (o, e) => { _settings.ReplaceWinR = false; }; + + ShellComboBox.SelectedIndex = (int) _settings.Shell; + ShellComboBox.SelectionChanged += (o, e) => + { + _settings.Shell = (Shell) ShellComboBox.SelectedIndex; + LeaveShellOpen.IsEnabled = _settings.Shell != Shell.RunCommand; + }; } } } diff --git a/Plugins/Wox.Plugin.CMD/Main.cs b/Plugins/Wox.Plugin.CMD/Main.cs index 1a65f21e2f..fe6dde7ac9 100644 --- a/Plugins/Wox.Plugin.CMD/Main.cs +++ b/Plugins/Wox.Plugin.CMD/Main.cs @@ -19,12 +19,12 @@ namespace Wox.Plugin.CMD private bool WinRStroked; private readonly KeyboardSimulator keyboardSimulator = new KeyboardSimulator(new InputSimulator()); - private readonly CMDHistory _settings; - private readonly PluginJsonStorage _storage; + private readonly Settings _settings; + private readonly PluginJsonStorage _storage; public CMD() { - _storage = new PluginJsonStorage(); + _storage = new PluginJsonStorage(); _settings = _storage.Load(); } @@ -79,7 +79,7 @@ namespace Wox.Plugin.CMD IcoPath = "Images/cmd.png", Action = c => { - ExecuteCMD(m); + ExecuteCommand(m); return true; } })); @@ -112,7 +112,7 @@ namespace Wox.Plugin.CMD IcoPath = "Images/cmd.png", Action = c => { - ExecuteCMD(m.Key); + ExecuteCommand(m.Key); return true; } }; @@ -131,7 +131,7 @@ namespace Wox.Plugin.CMD IcoPath = "Images/cmd.png", Action = c => { - ExecuteCMD(cmd); + ExecuteCommand(cmd); return true; } }; @@ -149,28 +149,73 @@ namespace Wox.Plugin.CMD IcoPath = "Images/cmd.png", Action = c => { - ExecuteCMD(m.Key); + ExecuteCommand(m.Key); return true; } }).Take(5); return history.ToList(); } - private void ExecuteCMD(string cmd, bool runAsAdministrator = false) + private void ExecuteCommand(string command, bool runAsAdministrator = false) { - var arguments = _settings.LeaveCmdOpen ? $"/k {cmd}" : $"/c {cmd} & pause"; - var info = new ProcessStartInfo + command = command.Trim(); + command = Environment.ExpandEnvironmentVariables(command); + + ProcessStartInfo info; + if (_settings.Shell == Shell.CMD) { - UseShellExecute = true, - FileName = "cmd.exe", - WorkingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), - Arguments = arguments, - Verb = runAsAdministrator ? "runas" : "" - }; + var arguments = _settings.LeaveShellOpen ? $"/k \"{command}\"" : $"/c \"{command}\" & pause"; + info = new ProcessStartInfo + { + FileName = "cmd.exe", + Arguments = arguments, + }; + } + else if (_settings.Shell == Shell.RunCommand) + { + var parts = command.Split(new[] { ' ' }, 2); + var filename = parts[0]; + var path = FullPath(filename); + if (string.IsNullOrEmpty(path) || parts.Length == 1) + { + info = new ProcessStartInfo(command); + } + else + { + var arguemtns = parts[1]; + info = new ProcessStartInfo + { + FileName = filename, + Arguments = arguemtns + }; + } + } + else + { + string arguments; + if (_settings.LeaveShellOpen) + { + arguments = $"-NoExit \"{command}\""; + } + else + { + arguments = $"\"{command} ; Read-Host -Prompt \\\"Press Enter to continue\\\"\""; + } + info = new ProcessStartInfo + { + FileName = "powershell.exe", + Arguments = arguments + }; + } + + info.UseShellExecute = true; + info.WorkingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); + info.Verb = runAsAdministrator ? "runas" : ""; + try { Process.Start(info); - _settings.AddCmdHistory(cmd); + _settings.AddCmdHistory(command); } catch (FileNotFoundException e) { @@ -178,6 +223,39 @@ namespace Wox.Plugin.CMD } } + private string FullPath(string filename) + { + if (File.Exists(filename)) + { + return filename; + } + else + { + var values = Environment.GetEnvironmentVariable("PATH"); + if (values != null) + { + foreach (var path in values.Split(';')) + { + var fullPath1 = Path.Combine(path, filename); + var fullPath2 = Path.Combine(path, filename + ".exe"); + if (File.Exists(fullPath1)) + { + return fullPath1; + } + else if (File.Exists(fullPath2)) + { + return fullPath2; + } + } + return string.Empty; + } + else + { + return string.Empty; + } + } + } + public void Init(PluginInitContext context) { this.context = context; @@ -237,7 +315,7 @@ namespace Wox.Plugin.CMD Action = c => { context.API.HideApp(); - ExecuteCMD(selectedResult.Title, true); + ExecuteCommand(selectedResult.Title, true); return true; }, IcoPath = "Images/cmd.png" diff --git a/Wox/Helper/SingleInstance.cs b/Wox/Helper/SingleInstance.cs index cdfb6c46fd..2ea597a9aa 100644 --- a/Wox/Helper/SingleInstance.cs +++ b/Wox/Helper/SingleInstance.cs @@ -204,8 +204,6 @@ namespace Wox.Helper where TApplication: Application , ISingleInstanceApp { - public const string Restart = "Restart"; - #region Private Fields ///