Skip to content

Commit e7198c6

Browse files
authored
Merge pull request #742 from spedrickson/JsonRPCShellRun
add initial ShellRun support for JsonRPC
2 parents 2ebdead + a7ef589 commit e7198c6

File tree

6 files changed

+48
-8
lines changed

6 files changed

+48
-8
lines changed

Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ public interface IPublicAPI
2929
/// </summary>
3030
void RestartApp();
3131

32+
/// <summary>
33+
/// Run a shell command
34+
/// </summary>
35+
/// <param name="cmd">The command or program to run</param>
36+
/// <param name="filename">the shell type to run, e.g. powershell.exe</param>
37+
/// <exception cref="FileNotFoundException">Thrown when unable to find the file specified in the command </exception>
38+
/// <exception cref="Win32Exception">Thrown when error occurs during the execution of the command </exception>
39+
void ShellRun(string cmd, string filename = "cmd.exe");
40+
3241
/// <summary>
3342
/// Save everything, all of Flow Launcher and plugins' data and settings
3443
/// </summary>

Flow.Launcher.Plugin/SharedCommands/ShellCommand.cs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics;
44
using System.Linq;
@@ -60,17 +60,39 @@ private static string GetWindowTitle(IntPtr hwnd)
6060
return sb.ToString();
6161
}
6262

63-
public static ProcessStartInfo SetProcessStartInfo(this string fileName, string workingDirectory = "", string arguments = "", string verb = "")
63+
public static ProcessStartInfo SetProcessStartInfo(this string fileName, string workingDirectory = "", string arguments = "", string verb = "", bool createNoWindow = false)
6464
{
6565
var info = new ProcessStartInfo
6666
{
6767
FileName = fileName,
6868
WorkingDirectory = workingDirectory,
6969
Arguments = arguments,
70-
Verb = verb
70+
Verb = verb,
71+
CreateNoWindow = createNoWindow
7172
};
7273

7374
return info;
7475
}
76+
77+
/// <summary>
78+
/// Runs a windows command using the provided ProcessStartInfo
79+
/// </summary>
80+
/// <exception cref="FileNotFoundException">Thrown when unable to find the file specified in the command </exception>
81+
/// <exception cref="Win32Exception">Thrown when error occurs during the execution of the command </exception>
82+
public static void Execute(ProcessStartInfo info)
83+
{
84+
Execute(Process.Start, info);
85+
}
86+
87+
/// <summary>
88+
/// Runs a windows command using the provided ProcessStartInfo using a custom execute command function
89+
/// </summary>
90+
/// <param name="Func startProcess">allows you to pass in a custom command execution function</param>
91+
/// <exception cref="FileNotFoundException">Thrown when unable to find the file specified in the command </exception>
92+
/// <exception cref="Win32Exception">Thrown when error occurs during the execution of the command </exception>
93+
public static void Execute(Func<ProcessStartInfo, Process> startProcess, ProcessStartInfo info)
94+
{
95+
startProcess(info);
96+
}
7597
}
7698
}

Flow.Launcher/PublicAPIInstance.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Net;
@@ -14,6 +14,7 @@
1414
using Flow.Launcher.Plugin;
1515
using Flow.Launcher.ViewModel;
1616
using Flow.Launcher.Plugin.SharedModels;
17+
using Flow.Launcher.Plugin.SharedCommands;
1718
using System.Threading;
1819
using System.IO;
1920
using Flow.Launcher.Infrastructure.Http;
@@ -106,6 +107,14 @@ public void OpenSettingDialog()
106107
});
107108
}
108109

110+
public void ShellRun(string cmd, string filename = "cmd.exe")
111+
{
112+
var args = filename == "cmd.exe" ? $"/C {cmd}" : $"{cmd}";
113+
114+
var startInfo = ShellCommand.SetProcessStartInfo(filename, arguments: args, createNoWindow: true);
115+
ShellCommand.Execute(startInfo);
116+
}
117+
109118
public void StartLoadingBar() => _mainVM.ProgressBarVisibility = Visibility.Visible;
110119

111120
public void StopLoadingBar() => _mainVM.ProgressBarVisibility = Visibility.Collapsed;

Plugins/Flow.Launcher.Plugin.Shell/Main.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ private void Execute(Func<ProcessStartInfo, Process> startProcess, ProcessStartI
263263
{
264264
try
265265
{
266-
startProcess(info);
266+
ShellCommand.Execute(startProcess, info);
267267
}
268268
catch (FileNotFoundException e)
269269
{

Plugins/Flow.Launcher.Plugin.Sys/Main.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,8 @@ private List<Result> Commands()
190190
var info = ShellCommand.SetProcessStartInfo("shutdown", arguments:"/h");
191191
info.WindowStyle = ProcessWindowStyle.Hidden;
192192
info.UseShellExecute = true;
193-
194-
Process.Start(info);
193+
194+
ShellCommand.Execute(info);
195195

196196
return true;
197197
}

Plugins/Flow.Launcher.Plugin.Sys/plugin.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"Name": "System Commands",
55
"Description": "Provide System related commands. e.g. shutdown,lock, setting etc.",
66
"Author": "qianlifeng",
7-
"Version": "1.5.0",
7+
"Version": "1.5.1",
88
"Language": "csharp",
99
"Website": "https://github.com/Flow-Launcher/Flow.Launcher",
1010
"ExecuteFileName": "Flow.Launcher.Plugin.Sys.dll",

0 commit comments

Comments
 (0)