fix some bug (v1 still broken)

This commit is contained in:
Hongtao Zhang 2023-03-26 14:04:06 -05:00
parent 683f6ebce4
commit a3367abd7a
No known key found for this signature in database
GPG key ID: 75F655B91C7AC9BB
6 changed files with 38 additions and 27 deletions

View file

@ -47,7 +47,7 @@ namespace Flow.Launcher.Core.Plugin
public abstract List<Result> LoadContextMenus(Result selectedResult);
private static readonly JsonSerializerOptions options = new()
protected static readonly JsonSerializerOptions options = new()
{
PropertyNameCaseInsensitive = true,
#pragma warning disable SYSLIB0020
@ -155,6 +155,8 @@ namespace Flow.Launcher.Core.Plugin
API = Context.API
};
await Settings.InitializeAsync();
}
public virtual async Task InitAsync(PluginInitContext context)
@ -165,7 +167,7 @@ namespace Flow.Launcher.Core.Plugin
public void Save()
{
Settings.Save();
Settings?.Save();
}
public Control CreateSettingPanel()
{

View file

@ -11,7 +11,7 @@ using Flow.Launcher.Plugin;
namespace Flow.Launcher.Core.Plugin
{
public abstract class JsonRpcPluginV2 : IAsyncPlugin, IContextMenu, ISettingProvider, ISavable
internal abstract class JsonRpcPluginV2 : JsonRPCPluginBase
{
public abstract string SupportedLanguage { get; set; }
@ -56,14 +56,10 @@ namespace Flow.Launcher.Core.Plugin
await JsonSerializer.SerializeAsync(InputStream, fullMessage, cancellationToken: token);
}
public async Task<List<Result>> QueryAsync(Query query, CancellationToken token)
protected override async Task<List<Result>> QueryRequestAsync(JsonRPCRequestModel query, CancellationToken token)
{
int currentRequestId = Interlocked.Add(ref RequestId, 1);
var message = new JsonRPCRequestModel(currentRequestId, "query", new object[]
{
query
});
await InputMessageChannel.Writer.WriteAsync(message, token);
await InputMessageChannel.Writer.WriteAsync(query, token);
await Task.Delay(50, token);
await InputStream.FlushAsync(token);
var task = new TaskCompletionSource<JsonRPCQueryResponseModel>();
@ -72,8 +68,9 @@ namespace Flow.Launcher.Core.Plugin
//TODO: Parse Result
return new List<Result>();
}
public virtual Task InitAsync(PluginInitContext context)
public override async Task InitAsync(PluginInitContext context)
{
await base.InitAsync(context);
InputMessageChannel = Channel.CreateUnbounded<JsonRPCRequestModel>();
MessageCancellationTokenSource = new CancellationTokenSource();
SendMessageAsync(context.CurrentPluginMetadata, MessageCancellationTokenSource.Token);
@ -81,20 +78,6 @@ namespace Flow.Launcher.Core.Plugin
// MessageTask =
// (SendMessageAsync(context.CurrentPluginMetadata, MessageCancellationTokenSource.Token),
// ReceiveMessageAsync(MessageCancellationTokenSource.Token));
return Task.CompletedTask;
}
public List<Result> LoadContextMenus(Result selectedResult)
{
throw new System.NotImplementedException();
}
public Control CreateSettingPanel()
{
// TODO: Implement CreateSettingPanel
return new Control();
}
public void Save()
{
// TODO: Save settings
}
}
}

View file

@ -35,6 +35,19 @@ namespace Flow.Launcher.Core.Plugin
{
_storage = new JsonStorage<Dictionary<string, object>>(SettingPath);
Settings = await _storage.LoadAsync();
foreach (var (type, attributes) in Configuration.Body)
{
if (attributes.Name == null)
{
continue;
}
if (!Settings.ContainsKey(attributes.Name))
{
Settings[attributes.Name] = attributes.DefaultValue;
}
}
}

View file

@ -1,6 +1,7 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Flow.Launcher.Infrastructure;
@ -38,7 +39,7 @@ namespace Flow.Launcher.Core.Plugin
protected override Task<Stream> RequestAsync(JsonRPCRequestModel request, CancellationToken token = default)
{
_startInfo.ArgumentList[2] = request.ToString();
_startInfo.ArgumentList[2] = JsonSerializer.Serialize(request);
return ExecuteAsync(_startInfo, token);
}

View file

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading;
@ -8,7 +9,7 @@ using Flow.Launcher.Plugin;
namespace Flow.Launcher.Core.Plugin
{
public class PythonPluginV2 : JsonRpcPluginV2
internal class PythonPluginV2 : JsonRpcPluginV2
{
private readonly ProcessStartInfo _startInfo;
private Process _process;
@ -42,8 +43,16 @@ namespace Flow.Launcher.Core.Plugin
//Add -B flag to tell python don't write .py[co] files. Because .pyc contains location infos which will prevent python portable
_startInfo.ArgumentList.Add("-B");
}
public override List<Result> LoadContextMenus(Result selectedResult)
{
throw new NotImplementedException();
}
protected override Task<bool> ExecuteResultAsync(JsonRPCResult result)
{
throw new NotImplementedException();
}
public override async Task InitAsync(PluginInitContext context)
{
_startInfo.ArgumentList.Add(context.CurrentPluginMetadata.ExecuteFilePath);

View file

@ -34,6 +34,9 @@ namespace Flow.Launcher.Infrastructure.Storage
public JsonStorage(string filePath)
{
FilePath = filePath;
DirectoryPath = Path.GetDirectoryName(filePath) ?? throw new ArgumentException("Invalid file path");
Helper.ValidateDirectory(DirectoryPath);
}
public async Task<T> LoadAsync()