From d41dd780ce631d1ef0aa61dd53a68204bb85fc72 Mon Sep 17 00:00:00 2001 From: Spencer Hedrick Date: Sat, 9 Oct 2021 02:36:38 -0700 Subject: [PATCH 1/7] add initial ShellRun support for JsonRPC --- Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs | 6 ++++++ Flow.Launcher/PublicAPIInstance.cs | 10 ++++++++++ 2 files changed, 16 insertions(+) diff --git a/Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs b/Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs index d9cdf5581d0..3cadae1761c 100644 --- a/Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs +++ b/Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs @@ -29,6 +29,12 @@ public interface IPublicAPI /// void RestartApp(); + /// + /// Run a shell command or external program + /// + /// The command or program to run + void ShellRun(string cmd); + /// /// Save everything, all of Flow Launcher and plugins' data and settings /// diff --git a/Flow.Launcher/PublicAPIInstance.cs b/Flow.Launcher/PublicAPIInstance.cs index 6f995671dd2..c528b82298c 100644 --- a/Flow.Launcher/PublicAPIInstance.cs +++ b/Flow.Launcher/PublicAPIInstance.cs @@ -103,6 +103,16 @@ public void OpenSettingDialog() }); } + public void ShellRun(string cmd) + { + System.Diagnostics.Process process = new(); + var startInfo = process.StartInfo; + startInfo.FileName = "cmd.exe"; + startInfo.Arguments = $"/C {cmd}"; + startInfo.CreateNoWindow = true; + process.Start(); + } + public void StartLoadingBar() => _mainVM.ProgressBarVisibility = Visibility.Visible; public void StopLoadingBar() => _mainVM.ProgressBarVisibility = Visibility.Collapsed; From ae7de8db0932ee71b0506cca74479dd371ccde3a Mon Sep 17 00:00:00 2001 From: Jeremy Date: Wed, 17 Nov 2021 21:00:36 +1100 Subject: [PATCH 2/7] refactor to use ShellCommand --- Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs | 2 ++ .../SharedCommands/ShellCommand.cs | 28 +++++++++++++++++-- Flow.Launcher/PublicAPIInstance.cs | 12 +++----- Plugins/Flow.Launcher.Plugin.Shell/Main.cs | 2 +- Plugins/Flow.Launcher.Plugin.Sys/Main.cs | 4 +-- 5 files changed, 34 insertions(+), 14 deletions(-) diff --git a/Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs b/Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs index 3cadae1761c..7d4741630ba 100644 --- a/Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs +++ b/Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs @@ -33,6 +33,8 @@ public interface IPublicAPI /// Run a shell command or external program /// /// The command or program to run + /// Thrown when unable to find the file specified in the command + /// Thrown when error occurs during the execution of the command void ShellRun(string cmd); /// diff --git a/Flow.Launcher.Plugin/SharedCommands/ShellCommand.cs b/Flow.Launcher.Plugin/SharedCommands/ShellCommand.cs index c5d43a3d9df..a2eea19a720 100644 --- a/Flow.Launcher.Plugin/SharedCommands/ShellCommand.cs +++ b/Flow.Launcher.Plugin/SharedCommands/ShellCommand.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; @@ -60,17 +60,39 @@ private static string GetWindowTitle(IntPtr hwnd) return sb.ToString(); } - public static ProcessStartInfo SetProcessStartInfo(this string fileName, string workingDirectory = "", string arguments = "", string verb = "") + public static ProcessStartInfo SetProcessStartInfo(this string fileName, string workingDirectory = "", string arguments = "", string verb = "", bool createNoWindow = false) { var info = new ProcessStartInfo { FileName = fileName, WorkingDirectory = workingDirectory, Arguments = arguments, - Verb = verb + Verb = verb, + CreateNoWindow = createNoWindow }; return info; } + + /// + /// Runs a windows command using the provided ProcessStartInfo + /// + /// Thrown when unable to find the file specified in the command + /// Thrown when error occurs during the execution of the command + public static void Execute(ProcessStartInfo info) + { + Execute(Process.Start, info); + } + + /// + /// Runs a windows command using the provided ProcessStartInfo using a custom execute command function + /// + /// allows you to pass in a custom command execution function + /// Thrown when unable to find the file specified in the command + /// Thrown when error occurs during the execution of the command + public static void Execute(Func startProcess, ProcessStartInfo info) + { + startProcess(info); + } } } diff --git a/Flow.Launcher/PublicAPIInstance.cs b/Flow.Launcher/PublicAPIInstance.cs index c528b82298c..6ca14c215b6 100644 --- a/Flow.Launcher/PublicAPIInstance.cs +++ b/Flow.Launcher/PublicAPIInstance.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Net; @@ -14,6 +14,7 @@ using Flow.Launcher.Plugin; using Flow.Launcher.ViewModel; using Flow.Launcher.Plugin.SharedModels; +using Flow.Launcher.Plugin.SharedCommands; using System.Threading; using System.IO; using Flow.Launcher.Infrastructure.Http; @@ -102,15 +103,10 @@ public void OpenSettingDialog() SettingWindow sw = SingletonWindowOpener.Open(this, _settingsVM); }); } - public void ShellRun(string cmd) { - System.Diagnostics.Process process = new(); - var startInfo = process.StartInfo; - startInfo.FileName = "cmd.exe"; - startInfo.Arguments = $"/C {cmd}"; - startInfo.CreateNoWindow = true; - process.Start(); + var startInfo = ShellCommand.SetProcessStartInfo("cmd.exe", arguments: $"/C {cmd}", createNoWindow: true); + ShellCommand.Execute(startInfo); } public void StartLoadingBar() => _mainVM.ProgressBarVisibility = Visibility.Visible; diff --git a/Plugins/Flow.Launcher.Plugin.Shell/Main.cs b/Plugins/Flow.Launcher.Plugin.Shell/Main.cs index 0a934ab32e0..c6b4999e1a1 100644 --- a/Plugins/Flow.Launcher.Plugin.Shell/Main.cs +++ b/Plugins/Flow.Launcher.Plugin.Shell/Main.cs @@ -251,7 +251,7 @@ private void Execute(Func startProcess, ProcessStartI { try { - startProcess(info); + ShellCommand.Execute(startProcess, info); } catch (FileNotFoundException e) { diff --git a/Plugins/Flow.Launcher.Plugin.Sys/Main.cs b/Plugins/Flow.Launcher.Plugin.Sys/Main.cs index 282c8a25dd0..fcf2df4a592 100644 --- a/Plugins/Flow.Launcher.Plugin.Sys/Main.cs +++ b/Plugins/Flow.Launcher.Plugin.Sys/Main.cs @@ -190,8 +190,8 @@ private List Commands() var info = ShellCommand.SetProcessStartInfo("shutdown", arguments:"/h"); info.WindowStyle = ProcessWindowStyle.Hidden; info.UseShellExecute = true; - - Process.Start(info); + + ShellCommand.Execute(info); return true; } From 02d101a30b520c8d2ab2b26b6b347b72e6339c87 Mon Sep 17 00:00:00 2001 From: Jeremy Date: Wed, 17 Nov 2021 21:04:00 +1100 Subject: [PATCH 3/7] fix formatting --- Flow.Launcher/PublicAPIInstance.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Flow.Launcher/PublicAPIInstance.cs b/Flow.Launcher/PublicAPIInstance.cs index 1d0136c6fda..360529ea922 100644 --- a/Flow.Launcher/PublicAPIInstance.cs +++ b/Flow.Launcher/PublicAPIInstance.cs @@ -106,6 +106,7 @@ public void OpenSettingDialog() SettingWindow sw = SingletonWindowOpener.Open(this, _settingsVM); }); } + public void ShellRun(string cmd) { var startInfo = ShellCommand.SetProcessStartInfo("cmd.exe", arguments: $"/C {cmd}", createNoWindow: true); From 3e38a9035cca56eef9e23bda088bba4b56dfd9b6 Mon Sep 17 00:00:00 2001 From: Jeremy Date: Wed, 17 Nov 2021 21:07:44 +1100 Subject: [PATCH 4/7] version bump plugins --- Plugins/Flow.Launcher.Plugin.Shell/plugin.json | 2 +- Plugins/Flow.Launcher.Plugin.Sys/plugin.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Plugins/Flow.Launcher.Plugin.Shell/plugin.json b/Plugins/Flow.Launcher.Plugin.Shell/plugin.json index 6b20eeef9ef..b71dba2da25 100644 --- a/Plugins/Flow.Launcher.Plugin.Shell/plugin.json +++ b/Plugins/Flow.Launcher.Plugin.Shell/plugin.json @@ -4,7 +4,7 @@ "Name": "Shell", "Description": "Provide executing commands from Flow Launcher", "Author": "qianlifeng", - "Version": "1.4.5", + "Version": "1.4.6", "Language": "csharp", "Website": "https://github.com/Flow-Launcher/Flow.Launcher", "ExecuteFileName": "Flow.Launcher.Plugin.Shell.dll", diff --git a/Plugins/Flow.Launcher.Plugin.Sys/plugin.json b/Plugins/Flow.Launcher.Plugin.Sys/plugin.json index 42e8058e511..1a8f008c3ed 100644 --- a/Plugins/Flow.Launcher.Plugin.Sys/plugin.json +++ b/Plugins/Flow.Launcher.Plugin.Sys/plugin.json @@ -4,7 +4,7 @@ "Name": "System Commands", "Description": "Provide System related commands. e.g. shutdown,lock, setting etc.", "Author": "qianlifeng", - "Version": "1.5.0", + "Version": "1.5.1", "Language": "csharp", "Website": "https://github.com/Flow-Launcher/Flow.Launcher", "ExecuteFileName": "Flow.Launcher.Plugin.Sys.dll", From ef56d91c2ff117fd19b0e2cc06b749d0b431632b Mon Sep 17 00:00:00 2001 From: Jeremy Date: Thu, 18 Nov 2021 05:56:51 +1100 Subject: [PATCH 5/7] add filename param to ShellRun command for running other shell types --- Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs | 2 +- Flow.Launcher/PublicAPIInstance.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs b/Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs index a5602ac13d4..0897de8a4ca 100644 --- a/Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs +++ b/Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs @@ -35,7 +35,7 @@ public interface IPublicAPI /// The command or program to run /// Thrown when unable to find the file specified in the command /// Thrown when error occurs during the execution of the command - void ShellRun(string cmd); + void ShellRun(string cmd, string filename = "cmd.exe"); /// /// Save everything, all of Flow Launcher and plugins' data and settings diff --git a/Flow.Launcher/PublicAPIInstance.cs b/Flow.Launcher/PublicAPIInstance.cs index 360529ea922..eda173ee167 100644 --- a/Flow.Launcher/PublicAPIInstance.cs +++ b/Flow.Launcher/PublicAPIInstance.cs @@ -107,9 +107,9 @@ public void OpenSettingDialog() }); } - public void ShellRun(string cmd) + public void ShellRun(string cmd, string filename = "cmd.exe") { - var startInfo = ShellCommand.SetProcessStartInfo("cmd.exe", arguments: $"/C {cmd}", createNoWindow: true); + var startInfo = ShellCommand.SetProcessStartInfo(filename, arguments: $"/C {cmd}", createNoWindow: true); ShellCommand.Execute(startInfo); } From 8cf93907e98e330d7e8e165bcc887888f2c98cb7 Mon Sep 17 00:00:00 2001 From: Jeremy Date: Thu, 18 Nov 2021 05:59:37 +1100 Subject: [PATCH 6/7] update comment --- Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs b/Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs index 0897de8a4ca..908284bb987 100644 --- a/Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs +++ b/Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs @@ -30,9 +30,10 @@ public interface IPublicAPI void RestartApp(); /// - /// Run a shell command or external program + /// Run a shell command /// /// The command or program to run + /// the shell type to run, e.g. powershell.exe /// Thrown when unable to find the file specified in the command /// Thrown when error occurs during the execution of the command void ShellRun(string cmd, string filename = "cmd.exe"); From a7ef5895b700cc4c6e60fac7780309bc12660b2b Mon Sep 17 00:00:00 2001 From: Jeremy Date: Thu, 18 Nov 2021 06:15:38 +1100 Subject: [PATCH 7/7] update StartInfo arguments if cmd shell type --- Flow.Launcher/PublicAPIInstance.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Flow.Launcher/PublicAPIInstance.cs b/Flow.Launcher/PublicAPIInstance.cs index eda173ee167..d0dc548744d 100644 --- a/Flow.Launcher/PublicAPIInstance.cs +++ b/Flow.Launcher/PublicAPIInstance.cs @@ -109,7 +109,9 @@ public void OpenSettingDialog() public void ShellRun(string cmd, string filename = "cmd.exe") { - var startInfo = ShellCommand.SetProcessStartInfo(filename, arguments: $"/C {cmd}", createNoWindow: true); + var args = filename == "cmd.exe" ? $"/C {cmd}" : $"{cmd}"; + + var startInfo = ShellCommand.SetProcessStartInfo(filename, arguments: args, createNoWindow: true); ShellCommand.Execute(startInfo); }