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

80 lines
2.7 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;
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-03-26 19:04:06 +00:00
internal class PythonPluginV2 : JsonRpcPluginV2
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-02 15:05:09 +00:00
protected override JsonRpc Rpc { get; set; }
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-02 15:05:09 +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
var formatter = new JsonMessageFormatter();
var handler = new NewLineDelimitedMessageHandler(_process.StandardInput.BaseStream,
_process.StandardOutput.BaseStream,
formatter);
Rpc = new JsonRpc(handler, context.API);
Rpc.StartListening();
_ = _process.StandardError.ReadToEndAsync().ContinueWith(e => throw new Exception(e.Result));
2022-09-01 02:34:47 +00:00
await base.InitAsync(context);
}
}
}