Keep the Sync version for ContextMenu and Callback

This commit is contained in:
张弘韬 2021-02-08 14:43:17 +08:00
parent 0da21b1ed7
commit b15319ea4d
3 changed files with 57 additions and 15 deletions

View file

@ -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);
}
}
}

View file

@ -131,26 +131,20 @@ namespace Flow.Launcher.Core.Plugin
return await ExecuteAsync(start, token);
}
protected async Task<string> 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<string> 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<List<Result>> QueryAsync(Query query, CancellationToken token)
{
string output = await ExecuteQueryAsync(query, token);

View file

@ -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);
}
}
}