From ffd6109b5886f65b8a30759c0dfa9a01d2b7ecc2 Mon Sep 17 00:00:00 2001 From: Jeremy Date: Wed, 16 Feb 2022 09:00:21 +1100 Subject: [PATCH] add IPathSelected to JsonRPC plugins --- Flow.Launcher.Core/Plugin/ExecutablePlugin.cs | 9 +++-- Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs | 35 +++++++++++++++---- Flow.Launcher.Core/Plugin/PythonPlugin.cs | 7 ++-- 3 files changed, 40 insertions(+), 11 deletions(-) diff --git a/Flow.Launcher.Core/Plugin/ExecutablePlugin.cs b/Flow.Launcher.Core/Plugin/ExecutablePlugin.cs index 049d1c583..dbf6773b7 100644 --- a/Flow.Launcher.Core/Plugin/ExecutablePlugin.cs +++ b/Flow.Launcher.Core/Plugin/ExecutablePlugin.cs @@ -21,12 +21,15 @@ namespace Flow.Launcher.Core.Plugin RedirectStandardOutput = true, RedirectStandardError = true }; - - // required initialisation for below request calls + + // required initialisation for below request calls _startInfo.ArgumentList.Add(string.Empty); } - protected override Task RequestAsync(JsonRPCRequestModel request, CancellationToken token = default) + protected override Task RequestAsync( + JsonRPCRequestModel request, + CancellationToken token = default, + bool ignoreEmptyResponse = false) { // since this is not static, request strings will build up in ArgumentList if index is not specified _startInfo.ArgumentList[0] = request.ToString(); diff --git a/Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs b/Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs index 4cfa83382..102c44e02 100644 --- a/Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs +++ b/Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs @@ -35,7 +35,7 @@ namespace Flow.Launcher.Core.Plugin /// Represent the plugin that using JsonPRC /// every JsonRPC plugin should has its own plugin instance /// - internal abstract class JsonRPCPlugin : IAsyncPlugin, IContextMenu, ISettingProvider, ISavable + internal abstract class JsonRPCPlugin : IAsyncPlugin, IContextMenu, ISettingProvider, ISavable, IPathSelected { protected PluginInitContext context; public const string JsonRPC = "JsonRPC"; @@ -44,7 +44,11 @@ namespace Flow.Launcher.Core.Plugin /// The language this JsonRPCPlugin support /// public abstract string SupportedLanguage { get; set; } - protected abstract Task RequestAsync(JsonRPCRequestModel rpcRequest, CancellationToken token = default); + protected abstract Task RequestAsync( + JsonRPCRequestModel rpcRequest, + CancellationToken token = default, + bool ignoreEmptyResponse = false); + protected abstract string Request(JsonRPCRequestModel rpcRequest, CancellationToken token = default); private static readonly RecyclableMemoryStreamManager BufferManager = new(); @@ -238,7 +242,10 @@ namespace Flow.Launcher.Core.Plugin } } - protected async Task ExecuteAsync(ProcessStartInfo startInfo, CancellationToken token = default) + protected async Task ExecuteAsync( + ProcessStartInfo startInfo, + CancellationToken token = default, + bool ignoreEmptyResponse = false) { Process process = null; bool disposed = false; @@ -265,7 +272,7 @@ namespace Flow.Launcher.Core.Plugin try { - // token expire won't instantly trigger the exception, + // token expire won't instantly trigger the exception, // manually kill process at before await source.CopyToAsync(buffer, token); } @@ -281,10 +288,13 @@ namespace Flow.Launcher.Core.Plugin if (buffer.Length == 0) { - var errorMessage = process.StandardError.EndOfStream ? + var endOfStream = process.StandardError.EndOfStream; + var errorMessage = endOfStream ? "Empty JSONRPC Response" : await process.StandardError.ReadToEndAsync(); - throw new InvalidDataException($"{context.CurrentPluginMetadata.Name}|{errorMessage}"); + + if (!endOfStream || (endOfStream && !ignoreEmptyResponse)) + throw new InvalidDataException($"{context.CurrentPluginMetadata.Name}|{errorMessage}"); } if (!process.StandardError.EndOfStream) @@ -534,5 +544,18 @@ namespace Flow.Launcher.Core.Plugin } } } + + public async Task PathSelected(string filePath, string hotkey) + { + var request = new JsonRPCRequestModel + { + Method = "PathSelected", + Parameters = new object[] + { + filePath, hotkey + } + }; + await RequestAsync(request, ignoreEmptyResponse: true); + } } } \ No newline at end of file diff --git a/Flow.Launcher.Core/Plugin/PythonPlugin.cs b/Flow.Launcher.Core/Plugin/PythonPlugin.cs index 8f7e5760a..60d34b279 100644 --- a/Flow.Launcher.Core/Plugin/PythonPlugin.cs +++ b/Flow.Launcher.Core/Plugin/PythonPlugin.cs @@ -37,11 +37,14 @@ namespace Flow.Launcher.Core.Plugin _startInfo.ArgumentList.Add("-B"); } - protected override Task RequestAsync(JsonRPCRequestModel request, CancellationToken token = default) + protected override Task RequestAsync( + JsonRPCRequestModel request, + CancellationToken token = default, + bool ignoreEmptyResponse = false) { _startInfo.ArgumentList[2] = request.ToString(); - return ExecuteAsync(_startInfo, token); + return ExecuteAsync(_startInfo, token, ignoreEmptyResponse: ignoreEmptyResponse); } protected override string Request(JsonRPCRequestModel rpcRequest, CancellationToken token = default)