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:44:50 +00:00
|
|
|
|
internal sealed class PythonPluginV2 : ProcessStreamPluginV2
|
2022-09-01 02:34:47 +00:00
|
|
|
|
{
|
2023-07-03 08:44:50 +00:00
|
|
|
|
protected override ProcessStartInfo StartInfo { get; set; }
|
2024-02-19 05:46:24 +00:00
|
|
|
|
|
2022-09-01 02:34:47 +00:00
|
|
|
|
public PythonPluginV2(string filename)
|
|
|
|
|
|
{
|
2024-02-19 05:46:24 +00:00
|
|
|
|
StartInfo = new ProcessStartInfo { FileName = filename, };
|
2022-09-01 02:34:47 +00:00
|
|
|
|
|
|
|
|
|
|
var path = Path.Combine(Constant.ProgramDirectory, JsonRpc);
|
2023-07-03 08:44:50 +00:00
|
|
|
|
StartInfo.EnvironmentVariables["PYTHONPATH"] = path;
|
2022-09-01 02:34:47 +00:00
|
|
|
|
|
|
|
|
|
|
//Add -B flag to tell python don't write .py[co] files. Because .pyc contains location infos which will prevent python portable
|
2023-07-03 08:44:50 +00:00
|
|
|
|
StartInfo.ArgumentList.Add("-B");
|
2023-06-24 16:43:21 +00:00
|
|
|
|
}
|
2024-02-19 05:46:24 +00:00
|
|
|
|
|
2024-02-16 04:06:19 +00:00
|
|
|
|
public override async Task InitAsync(PluginInitContext context)
|
|
|
|
|
|
{
|
2024-12-05 08:18:13 +00:00
|
|
|
|
// Run .py files via `-c <code>`
|
|
|
|
|
|
if (context.CurrentPluginMetadata.ExecuteFilePath.EndsWith(".py", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
|
{
|
|
|
|
|
|
var rootDirectory = context.CurrentPluginMetadata.PluginDirectory;
|
|
|
|
|
|
var libDirectory = Path.Combine(rootDirectory, "lib");
|
|
|
|
|
|
var pluginDirectory = Path.Combine(rootDirectory, "plugin");
|
|
|
|
|
|
var filePath = context.CurrentPluginMetadata.ExecuteFilePath;
|
|
|
|
|
|
|
|
|
|
|
|
// This makes it easier for plugin authors to import their own modules.
|
|
|
|
|
|
// They won't have to add `.`, `./lib`, or `./plugin` to their sys.path manually.
|
|
|
|
|
|
// Instead of running the .py file directly, we pass the code we want to run as a CLI argument.
|
|
|
|
|
|
// This code sets sys.path for the plugin author and then runs the .py file via runpy.
|
|
|
|
|
|
StartInfo.ArgumentList.Add("-c");
|
|
|
|
|
|
StartInfo.ArgumentList.Add(
|
|
|
|
|
|
$"""
|
|
|
|
|
|
import sys
|
|
|
|
|
|
sys.path.append(r'{rootDirectory}')
|
|
|
|
|
|
sys.path.append(r'{libDirectory}')
|
|
|
|
|
|
sys.path.append(r'{pluginDirectory}')
|
|
|
|
|
|
|
|
|
|
|
|
import runpy
|
|
|
|
|
|
runpy.run_path(r'{filePath}', None, '__main__')
|
|
|
|
|
|
"""
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
// Run .pyz files as is
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
StartInfo.ArgumentList.Add(context.CurrentPluginMetadata.ExecuteFilePath);
|
|
|
|
|
|
}
|
2024-02-16 04:06:19 +00:00
|
|
|
|
await base.InitAsync(context);
|
|
|
|
|
|
}
|
2024-02-19 05:46:24 +00:00
|
|
|
|
|
|
|
|
|
|
protected override MessageHandlerType MessageHandler { get; } = MessageHandlerType.NewLineDelimited;
|
2022-09-01 02:34:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|