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-07-03 08:19:27 +00:00
|
|
|
|
using System.IO.Pipelines;
|
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;
|
2023-07-03 08:19:27 +00:00
|
|
|
|
using Nerdbank.Streams;
|
2023-06-02 15:05:09 +00:00
|
|
|
|
using StreamJsonRpc;
|
2022-09-01 02:34:47 +00:00
|
|
|
|
|
|
|
|
|
|
namespace Flow.Launcher.Core.Plugin
|
|
|
|
|
|
{
|
2023-07-03 08:19:27 +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-07-03 08:19:27 +00:00
|
|
|
|
protected override IDuplexPipe ClientPipe { 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);
|
|
|
|
|
|
ArgumentNullException.ThrowIfNull(_process);
|
2023-07-03 08:19:27 +00:00
|
|
|
|
|
|
|
|
|
|
SetupPipe(_process);
|
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-07-03 08:19:27 +00:00
|
|
|
|
public override async ValueTask DisposeAsync()
|
2023-06-24 16:43:21 +00:00
|
|
|
|
{
|
|
|
|
|
|
_process.Kill(true);
|
2023-07-03 08:19:27 +00:00
|
|
|
|
await _process.WaitForExitAsync();
|
2023-06-24 16:43:21 +00:00
|
|
|
|
_process.Dispose();
|
2023-07-03 08:19:27 +00:00
|
|
|
|
await base.DisposeAsync();
|
2023-06-24 16:43:21 +00:00
|
|
|
|
}
|
2023-06-02 15:05:09 +00:00
|
|
|
|
|
2023-07-03 08:19:27 +00:00
|
|
|
|
private void SetupPipe(Process process)
|
|
|
|
|
|
{
|
|
|
|
|
|
var (reader, writer) = (PipeReader.Create(process.StandardOutput.BaseStream),
|
|
|
|
|
|
PipeWriter.Create(process.StandardInput.BaseStream));
|
|
|
|
|
|
ClientPipe = new DuplexPipe(reader, writer);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override async Task ReloadDataAsync()
|
2023-06-24 16:43:21 +00:00
|
|
|
|
{
|
|
|
|
|
|
var oldProcess = _process;
|
|
|
|
|
|
_process = Process.Start(_startInfo);
|
|
|
|
|
|
ArgumentNullException.ThrowIfNull(_process);
|
2023-07-03 08:19:27 +00:00
|
|
|
|
SetupPipe(_process);
|
|
|
|
|
|
await base.ReloadDataAsync();
|
2023-06-24 16:43:21 +00:00
|
|
|
|
oldProcess.Kill(true);
|
2023-07-03 08:19:27 +00:00
|
|
|
|
await oldProcess.WaitForExitAsync();
|
2023-06-24 16:43:21 +00:00
|
|
|
|
oldProcess.Dispose();
|
|
|
|
|
|
}
|
2022-09-01 02:34:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|