Skip to content

Commit 4095eb4

Browse files
committed
Improve code quality
1 parent 8730a5f commit 4095eb4

File tree

3 files changed

+15
-15
lines changed

3 files changed

+15
-15
lines changed

Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using System.Threading;
88
using System.Threading.Tasks;
99
using Flow.Launcher.Core.Resource;
10-
using Flow.Launcher.Infrastructure.Logger;
1110
using Flow.Launcher.Plugin;
1211
using Microsoft.IO;
1312

@@ -21,6 +20,8 @@ internal abstract class JsonRPCPlugin : JsonRPCPluginBase
2120
{
2221
public new const string JsonRPC = "JsonRPC";
2322

23+
private static readonly string ClassName = nameof(JsonRPCPlugin);
24+
2425
protected abstract Task<Stream> RequestAsync(JsonRPCRequestModel rpcRequest, CancellationToken token = default);
2526
protected abstract string Request(JsonRPCRequestModel rpcRequest, CancellationToken token = default);
2627

@@ -148,20 +149,20 @@ protected string Execute(ProcessStartInfo startInfo)
148149
var error = standardError.ReadToEnd();
149150
if (!string.IsNullOrEmpty(error))
150151
{
151-
Log.Error($"|JsonRPCPlugin.Execute|{error}");
152+
Context.API.LogError(ClassName, error);
152153
return string.Empty;
153154
}
154155

155-
Log.Error("|JsonRPCPlugin.Execute|Empty standard output and standard error.");
156+
Context.API.LogError(ClassName, "Empty standard output and standard error.");
156157
return string.Empty;
157158
}
158159

159160
return result;
160161
}
161162
catch (Exception e)
162163
{
163-
Log.Exception(
164-
$"|JsonRPCPlugin.Execute|Exception for filename <{startInfo.FileName}> with argument <{startInfo.Arguments}>",
164+
Context.API.LogException(ClassName,
165+
$"Exception for filename <{startInfo.FileName}> with argument <{startInfo.Arguments}>",
165166
e);
166167
return string.Empty;
167168
}
@@ -172,7 +173,7 @@ protected async Task<Stream> ExecuteAsync(ProcessStartInfo startInfo, Cancellati
172173
using var process = Process.Start(startInfo);
173174
if (process == null)
174175
{
175-
Log.Error("|JsonRPCPlugin.ExecuteAsync|Can't start new process");
176+
Context.API.LogError(ClassName, "Can't start new process");
176177
return Stream.Null;
177178
}
178179

@@ -192,7 +193,7 @@ protected async Task<Stream> ExecuteAsync(ProcessStartInfo startInfo, Cancellati
192193
}
193194
catch (Exception e)
194195
{
195-
Log.Exception("|JsonRPCPlugin.ExecuteAsync|Exception when kill process", e);
196+
Context.API.LogException(ClassName, "Exception when kill process", e);
196197
}
197198
});
198199

@@ -213,7 +214,7 @@ protected async Task<Stream> ExecuteAsync(ProcessStartInfo startInfo, Cancellati
213214
{
214215
case (0, 0):
215216
const string errorMessage = "Empty JSON-RPC Response.";
216-
Log.Warn($"|{nameof(JsonRPCPlugin)}.{nameof(ExecuteAsync)}|{errorMessage}");
217+
Context.API.LogWarn(ClassName, errorMessage);
217218
break;
218219
case (_, not 0):
219220
throw new InvalidDataException(Encoding.UTF8.GetString(errorBuffer.ToArray())); // The process has exited with an error message

Flow.Launcher.Core/Plugin/JsonRPCPluginBase.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@ namespace Flow.Launcher.Core.Plugin
1919
/// </summary>
2020
public abstract class JsonRPCPluginBase : IAsyncPlugin, IContextMenu, ISettingProvider, ISavable
2121
{
22-
protected PluginInitContext Context;
2322
public const string JsonRPC = "JsonRPC";
2423

25-
private int RequestId { get; set; }
24+
protected PluginInitContext Context;
2625

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

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

110-
111109
private async Task InitSettingAsync()
112110
{
113111
JsonRpcConfigurationModel configuration = null;
@@ -119,7 +117,6 @@ private async Task InitSettingAsync()
119117
await File.ReadAllTextAsync(SettingConfigurationPath));
120118
}
121119

122-
123120
Settings ??= new JsonRPCPluginSettings
124121
{
125122
Configuration = configuration, SettingPath = SettingPath, API = Context.API
@@ -130,7 +127,7 @@ private async Task InitSettingAsync()
130127

131128
public virtual async Task InitAsync(PluginInitContext context)
132129
{
133-
this.Context = context;
130+
Context = context;
134131
await InitSettingAsync();
135132
}
136133

Flow.Launcher.Core/Plugin/JsonRPCPluginV2.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ internal abstract class JsonRPCPluginV2 : JsonRPCPluginBase, IAsyncDisposable, I
1616
{
1717
public const string JsonRpc = "JsonRPC";
1818

19+
private static readonly string ClassName = nameof(JsonRPCPluginV2);
20+
1921
protected abstract IDuplexPipe ClientPipe { get; set; }
2022

2123
protected StreamReader ErrorStream { get; set; }
@@ -122,7 +124,7 @@ public virtual async Task ReloadDataAsync()
122124
}
123125
catch (Exception e)
124126
{
125-
Context.API.LogException(nameof(JsonRPCPluginV2), $"Failed to call reload_data for plugin {Context.CurrentPluginMetadata.Name}", e);
127+
Context.API.LogException(ClassName, $"Failed to call reload_data for plugin {Context.CurrentPluginMetadata.Name}", e);
126128
}
127129
}
128130

@@ -140,7 +142,7 @@ public virtual async ValueTask DisposeAsync()
140142
}
141143
catch (Exception e)
142144
{
143-
Context.API.LogException(nameof(JsonRPCPluginV2), $"Failed to call close for plugin {Context.CurrentPluginMetadata.Name}", e);
145+
Context.API.LogException(ClassName, $"Failed to call close for plugin {Context.CurrentPluginMetadata.Name}", e);
144146
}
145147
finally
146148
{

0 commit comments

Comments
 (0)