From b15319ea4da793d8401a97b0c782fd3164860097 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=BC=98=E9=9F=AC?= Date: Mon, 8 Feb 2021 14:43:17 +0800 Subject: [PATCH] Keep the Sync version for ContextMenu and Callback --- Flow.Launcher.Core/Plugin/ExecutablePlugin.cs | 4 +- Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs | 64 +++++++++++++++---- Flow.Launcher.Core/Plugin/PythonPlugin.cs | 4 +- 3 files changed, 57 insertions(+), 15 deletions(-) diff --git a/Flow.Launcher.Core/Plugin/ExecutablePlugin.cs b/Flow.Launcher.Core/Plugin/ExecutablePlugin.cs index cbf048f16..d0d47aa58 100644 --- a/Flow.Launcher.Core/Plugin/ExecutablePlugin.cs +++ b/Flow.Launcher.Core/Plugin/ExecutablePlugin.cs @@ -39,7 +39,7 @@ namespace Flow.Launcher.Core.Plugin protected override string ExecuteCallback(JsonRPCRequestModel rpcRequest) { _startInfo.Arguments = $"\"{rpcRequest}\""; - return ExecuteAsync(_startInfo).GetAwaiter().GetResult(); + return Execute(_startInfo); } protected override string ExecuteContextMenu(Result selectedResult) @@ -52,7 +52,7 @@ namespace Flow.Launcher.Core.Plugin _startInfo.Arguments = $"\"{request}\""; - return ExecuteAsync(_startInfo).GetAwaiter().GetResult(); + return Execute(_startInfo); } } } \ No newline at end of file diff --git a/Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs b/Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs index 626013cf0..7d221b74d 100644 --- a/Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs +++ b/Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs @@ -131,26 +131,20 @@ namespace Flow.Launcher.Core.Plugin return await ExecuteAsync(start, token); } - protected async Task ExecuteAsync(ProcessStartInfo startInfo, CancellationToken token = default) + protected string Execute(ProcessStartInfo startInfo) { try { using var process = Process.Start(startInfo); - if (process == null) - { - Log.Error("|JsonRPCPlugin.Execute|Can't start new process"); - return string.Empty; - } - + if (process == null) return string.Empty; + using var standardOutput = process.StandardOutput; - var result = await standardOutput.ReadToEndAsync(); - if (token.IsCancellationRequested) - return string.Empty; + var result = standardOutput.ReadToEnd(); if (string.IsNullOrEmpty(result)) { using var standardError = process.StandardError; - var error = await standardError.ReadToEndAsync(); + var error = standardError.ReadToEnd(); if (!string.IsNullOrEmpty(error)) { Log.Error($"|JsonRPCPlugin.Execute|{error}"); @@ -168,6 +162,7 @@ namespace Flow.Launcher.Core.Plugin } return result; + } catch (Exception e) { @@ -178,6 +173,53 @@ namespace Flow.Launcher.Core.Plugin } } + protected async Task ExecuteAsync(ProcessStartInfo startInfo, CancellationToken token = default) + { + try + { + using var process = Process.Start(startInfo); + if (process == null) + { + Log.Error("|JsonRPCPlugin.ExecuteAsync|Can't start new process"); + return string.Empty; + } + + using var standardOutput = process.StandardOutput; + var result = await standardOutput.ReadToEndAsync(); + if (token.IsCancellationRequested) + return string.Empty; + + if (string.IsNullOrEmpty(result)) + { + using var standardError = process.StandardError; + var error = await standardError.ReadToEndAsync(); + if (!string.IsNullOrEmpty(error)) + { + Log.Error($"|JsonRPCPlugin.ExecuteAsync|{error}"); + return string.Empty; + } + + Log.Error("|JsonRPCPlugin.ExecuteAsync|Empty standard output and standard error."); + return string.Empty; + } + + if (result.StartsWith("DEBUG:")) + { + MessageBox.Show(new Form {TopMost = true}, result.Substring(6)); + return string.Empty; + } + + return result; + } + catch (Exception e) + { + Log.Exception( + $"|JsonRPCPlugin.ExecuteAsync|Exception for filename <{startInfo.FileName}> with argument <{startInfo.Arguments}>", + e); + return string.Empty; + } + } + public async Task> QueryAsync(Query query, CancellationToken token) { string output = await ExecuteQueryAsync(query, token); diff --git a/Flow.Launcher.Core/Plugin/PythonPlugin.cs b/Flow.Launcher.Core/Plugin/PythonPlugin.cs index 314726735..750684954 100644 --- a/Flow.Launcher.Core/Plugin/PythonPlugin.cs +++ b/Flow.Launcher.Core/Plugin/PythonPlugin.cs @@ -50,7 +50,7 @@ namespace Flow.Launcher.Core.Plugin _startInfo.Arguments = $"-B \"{context.CurrentPluginMetadata.ExecuteFilePath}\" \"{rpcRequest}\""; _startInfo.WorkingDirectory = context.CurrentPluginMetadata.PluginDirectory; // TODO: Async Action - return ExecuteAsync(_startInfo).GetAwaiter().GetResult(); + return Execute(_startInfo); } protected override string ExecuteContextMenu(Result selectedResult) { @@ -62,7 +62,7 @@ namespace Flow.Launcher.Core.Plugin _startInfo.WorkingDirectory = context.CurrentPluginMetadata.PluginDirectory; // TODO: Async Action - return ExecuteAsync(_startInfo).GetAwaiter().GetResult(); + return Execute(_startInfo); } } } \ No newline at end of file