Flow.Launcher/Flow.Launcher.Core/Plugin/PythonPluginV2.cs

104 lines
3.3 KiB
C#
Raw Normal View History

2022-09-01 02:34:47 +00:00
using System;
2023-03-26 19:04:06 +00:00
using System.Collections.Generic;
2022-09-01 02:34:47 +00:00
using System.Diagnostics;
using System.IO;
2023-06-02 15:05:09 +00:00
using System.Text;
2022-09-01 02:34:47 +00:00
using System.Threading;
using System.Threading.Tasks;
2023-06-02 15:05:09 +00:00
using System.Windows.Input;
2023-06-24 16:43:21 +00:00
using Flow.Launcher.Core.Plugin.JsonRPCV2Models;
2022-09-01 02:34:47 +00:00
using Flow.Launcher.Infrastructure;
using Flow.Launcher.Plugin;
2023-06-02 15:05:09 +00:00
using Microsoft.VisualStudio.Threading;
using StreamJsonRpc;
2022-09-01 02:34:47 +00:00
namespace Flow.Launcher.Core.Plugin
{
2023-06-24 16:43:21 +00:00
internal class PythonPluginV2 : JsonRPCPluginV2, IReloadable, IDisposable
2022-09-01 02:34:47 +00:00
{
private readonly ProcessStartInfo _startInfo;
private Process _process;
2023-06-02 15:05:09 +00:00
2022-09-01 02:34:47 +00:00
public override string SupportedLanguage { get; set; } = AllowedLanguage.Python;
2023-06-24 16:43:21 +00:00
protected override JsonRpc RPC { get; set; }
2023-06-02 15:05:09 +00:00
2022-09-01 02:34:47 +00:00
public PythonPluginV2(string filename)
{
_startInfo = new ProcessStartInfo
{
FileName = filename,
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = true
};
// temp fix for issue #667
var path = Path.Combine(Constant.ProgramDirectory, JsonRpc);
_startInfo.EnvironmentVariables["PYTHONPATH"] = path;
_startInfo.EnvironmentVariables["FLOW_VERSION"] = Constant.Version;
_startInfo.EnvironmentVariables["FLOW_PROGRAM_DIRECTORY"] = Constant.ProgramDirectory;
_startInfo.EnvironmentVariables["FLOW_APPLICATION_DIRECTORY"] = Constant.ApplicationDirectory;
//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");
}
2023-03-26 19:04:06 +00:00
public override List<Result> LoadContextMenus(Result selectedResult)
{
throw new NotImplementedException();
}
2023-06-24 16:43:21 +00:00
2022-09-01 02:34:47 +00:00
public override async Task InitAsync(PluginInitContext context)
{
_startInfo.ArgumentList.Add(context.CurrentPluginMetadata.ExecuteFilePath);
_startInfo.WorkingDirectory = context.CurrentPluginMetadata.PluginDirectory;
_process = Process.Start(_startInfo);
2023-06-02 15:05:09 +00:00
2022-09-01 02:34:47 +00:00
ArgumentNullException.ThrowIfNull(_process);
2023-06-02 15:05:09 +00:00
2023-06-24 16:43:21 +00:00
SetupJsonRPC(_process, context.API);
2023-06-02 15:05:09 +00:00
2023-06-24 16:43:21 +00:00
await base.InitAsync(context);
}
2023-06-02 15:05:09 +00:00
2023-06-24 16:43:21 +00:00
public void Dispose()
{
_process.Kill(true);
_process.Dispose();
base.Dispose();
}
2023-06-02 15:05:09 +00:00
2023-06-24 16:43:21 +00:00
public void ReloadData()
{
var oldProcess = _process;
_process = Process.Start(_startInfo);
ArgumentNullException.ThrowIfNull(_process);
SetupJsonRPC(_process, Context.API);
oldProcess.Kill(true);
oldProcess.Dispose();
}
private void SetupJsonRPC(Process process, IPublicAPI api)
{
var formatter = new JsonMessageFormatter();
var handler = new NewLineDelimitedMessageHandler(process.StandardInput.BaseStream,
process.StandardOutput.BaseStream,
formatter);
2023-07-03 04:23:30 +00:00
ErrorStream = _process.StandardError;
2023-06-24 16:43:21 +00:00
RPC = new JsonRpc(handler, new JsonRPCPublicAPI(api));
RPC.SynchronizationContext = null;
RPC.StartListening();
2022-09-01 02:34:47 +00:00
}
}
}