Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 12 additions & 23 deletions Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
using Flow.Launcher.Core.Resource;
using System;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Flow.Launcher.Infrastructure.Logger;
using Flow.Launcher.Core.Resource;
using Flow.Launcher.Plugin;
using Microsoft.IO;
using System.Windows;

namespace Flow.Launcher.Core.Plugin
{
Expand All @@ -20,7 +18,9 @@ namespace Flow.Launcher.Core.Plugin
/// </summary>
internal abstract class JsonRPCPlugin : JsonRPCPluginBase
{
public const string JsonRPC = "JsonRPC";
public new const string JsonRPC = "JsonRPC";

private static readonly string ClassName = nameof(JsonRPCPlugin);

protected abstract Task<Stream> RequestAsync(JsonRPCRequestModel rpcRequest, CancellationToken token = default);
protected abstract string Request(JsonRPCRequestModel rpcRequest, CancellationToken token = default);
Expand All @@ -29,9 +29,6 @@ internal abstract class JsonRPCPlugin : JsonRPCPluginBase

private int RequestId { get; set; }

private string SettingConfigurationPath => Path.Combine(Context.CurrentPluginMetadata.PluginDirectory, "SettingsTemplate.yaml");
private string SettingPath => Path.Combine(Context.CurrentPluginMetadata.PluginSettingsDirectoryPath, "Settings.json");

public override List<Result> LoadContextMenus(Result selectedResult)
{
var request = new JsonRPCRequestModel(RequestId++,
Expand All @@ -57,13 +54,6 @@ public override List<Result> LoadContextMenus(Result selectedResult)
}
};

private static readonly JsonSerializerOptions settingSerializeOption = new()
{
WriteIndented = true
};

private readonly Dictionary<string, FrameworkElement> _settingControls = new();

private async Task<List<Result>> DeserializedResultAsync(Stream output)
{
await using (output)
Expand Down Expand Up @@ -122,7 +112,6 @@ protected override async Task<bool> ExecuteResultAsync(JsonRPCResult result)
return !result.JsonRPCAction.DontHideAfterAction;
}


/// <summary>
/// Execute external program and return the output
/// </summary>
Expand Down Expand Up @@ -160,20 +149,20 @@ protected string Execute(ProcessStartInfo startInfo)
var error = standardError.ReadToEnd();
if (!string.IsNullOrEmpty(error))
{
Log.Error($"|JsonRPCPlugin.Execute|{error}");
Context.API.LogError(ClassName, error);
return string.Empty;
}

Log.Error("|JsonRPCPlugin.Execute|Empty standard output and standard error.");
Context.API.LogError(ClassName, "Empty standard output and standard error.");
return string.Empty;
}

return result;
}
catch (Exception e)
{
Log.Exception(
$"|JsonRPCPlugin.Execute|Exception for filename <{startInfo.FileName}> with argument <{startInfo.Arguments}>",
Context.API.LogException(ClassName,
$"Exception for filename <{startInfo.FileName}> with argument <{startInfo.Arguments}>",
e);
return string.Empty;
}
Expand All @@ -184,7 +173,7 @@ protected async Task<Stream> ExecuteAsync(ProcessStartInfo startInfo, Cancellati
using var process = Process.Start(startInfo);
if (process == null)
{
Log.Error("|JsonRPCPlugin.ExecuteAsync|Can't start new process");
Context.API.LogError(ClassName, "Can't start new process");
return Stream.Null;
}

Expand All @@ -204,7 +193,7 @@ protected async Task<Stream> ExecuteAsync(ProcessStartInfo startInfo, Cancellati
}
catch (Exception e)
{
Log.Exception("|JsonRPCPlugin.ExecuteAsync|Exception when kill process", e);
Context.API.LogException(ClassName, "Exception when kill process", e);
}
});

Expand All @@ -225,7 +214,7 @@ protected async Task<Stream> ExecuteAsync(ProcessStartInfo startInfo, Cancellati
{
case (0, 0):
const string errorMessage = "Empty JSON-RPC Response.";
Log.Warn($"|{nameof(JsonRPCPlugin)}.{nameof(ExecuteAsync)}|{errorMessage}");
Context.API.LogWarn(ClassName, errorMessage);
break;
case (_, not 0):
throw new InvalidDataException(Encoding.UTF8.GetString(errorBuffer.ToArray())); // The process has exited with an error message
Expand Down
11 changes: 4 additions & 7 deletions Flow.Launcher.Core/Plugin/JsonRPCPluginBase.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using Flow.Launcher.Core.Resource;
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Flow.Launcher.Core.Resource;
using Flow.Launcher.Plugin;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
Expand All @@ -19,10 +19,9 @@ namespace Flow.Launcher.Core.Plugin
/// </summary>
public abstract class JsonRPCPluginBase : IAsyncPlugin, IContextMenu, ISettingProvider, ISavable
{
protected PluginInitContext Context;
public const string JsonRPC = "JsonRPC";

private int RequestId { get; set; }
protected PluginInitContext Context;

private string SettingConfigurationPath =>
Path.Combine(Context.CurrentPluginMetadata.PluginDirectory, "SettingsTemplate.yaml");
Expand Down Expand Up @@ -107,7 +106,6 @@ protected void ExecuteFlowLauncherAPI(string method, object[] parameters)

public abstract Task<List<Result>> QueryAsync(Query query, CancellationToken token);


private async Task InitSettingAsync()
{
JsonRpcConfigurationModel configuration = null;
Expand All @@ -119,7 +117,6 @@ private async Task InitSettingAsync()
await File.ReadAllTextAsync(SettingConfigurationPath));
}


Settings ??= new JsonRPCPluginSettings
{
Configuration = configuration, SettingPath = SettingPath, API = Context.API
Expand All @@ -130,7 +127,7 @@ private async Task InitSettingAsync()

public virtual async Task InitAsync(PluginInitContext context)
{
this.Context = context;
Context = context;
await InitSettingAsync();
}

Expand Down
28 changes: 22 additions & 6 deletions Flow.Launcher.Core/Plugin/JsonRPCPluginV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@
using StreamJsonRpc;
using IAsyncDisposable = System.IAsyncDisposable;


namespace Flow.Launcher.Core.Plugin
{
internal abstract class JsonRPCPluginV2 : JsonRPCPluginBase, IAsyncDisposable, IAsyncReloadable, IResultUpdated
{
public const string JsonRpc = "JsonRPC";

private static readonly string ClassName = nameof(JsonRPCPluginV2);

protected abstract IDuplexPipe ClientPipe { get; set; }

protected StreamReader ErrorStream { get; set; }

private JsonRpc RPC { get; set; }


protected override async Task<bool> ExecuteResultAsync(JsonRPCResult result)
{
var res = await RPC.InvokeAsync<JsonRPCExecuteResponse>(result.JsonRPCAction.Method,
Expand Down Expand Up @@ -55,7 +55,6 @@ public override async Task<List<Result>> QueryAsync(Query query, CancellationTok
return results;
}


public override async Task InitAsync(PluginInitContext context)
{
await base.InitAsync(context);
Expand Down Expand Up @@ -88,7 +87,6 @@ protected enum MessageHandlerType

protected abstract MessageHandlerType MessageHandler { get; }


private void SetupJsonRPC()
{
var formatter = new SystemTextJsonFormatter { JsonSerializerOptions = RequestSerializeOption };
Expand Down Expand Up @@ -118,8 +116,17 @@ public virtual async Task ReloadDataAsync()
{
await RPC.InvokeAsync("reload_data", Context);
}
catch (RemoteMethodNotFoundException e)
catch (RemoteMethodNotFoundException)
{
// Ignored
}
catch (ConnectionLostException)
{
// Ignored
}
catch (Exception e)
{
Context.API.LogException(ClassName, $"Failed to call reload_data for plugin {Context.CurrentPluginMetadata.Name}", e);
}
}

Expand All @@ -129,8 +136,17 @@ public virtual async ValueTask DisposeAsync()
{
await RPC.InvokeAsync("close");
}
catch (RemoteMethodNotFoundException e)
catch (RemoteMethodNotFoundException)
{
// Ignored
}
catch (ConnectionLostException)
{
// Ignored
}
catch (Exception e)
{
Context.API.LogException(ClassName, $"Failed to call close for plugin {Context.CurrentPluginMetadata.Name}", e);
}
finally
{
Expand Down
Loading