Flow.Launcher/Flow.Launcher.Core/Plugin/PluginsLoader.cs

211 lines
9.2 KiB
C#
Raw Permalink Normal View History

2020-05-20 21:48:52 +00:00
using System;
2014-12-26 11:36:43 +00:00
using System.Collections.Generic;
using System.IO;
2014-12-26 11:36:43 +00:00
using System.Linq;
using System.Reflection;
2020-05-20 23:02:51 +00:00
using System.Threading.Tasks;
using System.Windows.Forms;
2021-01-31 06:49:08 +00:00
using Droplex;
2020-04-21 09:12:17 +00:00
using Flow.Launcher.Infrastructure;
using Flow.Launcher.Infrastructure.Logger;
using Flow.Launcher.Infrastructure.UserSettings;
using Flow.Launcher.Plugin;
2021-01-31 06:49:08 +00:00
using Flow.Launcher.Plugin.SharedCommands;
using System.Diagnostics;
using Stopwatch = Flow.Launcher.Infrastructure.Stopwatch;
2014-12-26 11:36:43 +00:00
2020-04-21 09:12:17 +00:00
namespace Flow.Launcher.Core.Plugin
2014-12-26 11:36:43 +00:00
{
public static class PluginsLoader
2014-12-26 11:36:43 +00:00
{
public const string PythonExecutable = "pythonw.exe";
2016-05-12 01:45:35 +00:00
public static List<PluginPair> Plugins(List<PluginMetadata> metadatas, PluginsSettings settings)
{
var dotnetPlugins = DotNetPlugins(metadatas);
2021-01-31 06:49:08 +00:00
var pythonPlugins = PythonPlugins(metadatas, settings);
var executablePlugins = ExecutablePlugins(metadatas);
var plugins = dotnetPlugins.Concat(pythonPlugins).Concat(executablePlugins).ToList();
return plugins;
}
public static IEnumerable<PluginPair> DotNetPlugins(List<PluginMetadata> source)
2014-12-26 11:36:43 +00:00
{
var erroredPlugins = new List<string>();
2014-12-26 11:36:43 +00:00
var plugins = new List<PluginPair>();
var metadatas = source.Where(o => AllowedLanguage.IsDotNet(o.Language));
2014-12-26 11:36:43 +00:00
foreach (var metadata in metadatas)
2014-12-26 11:36:43 +00:00
{
var milliseconds = Stopwatch.Debug(
$"|PluginsLoader.DotNetPlugins|Constructor init cost for {metadata.Name}", () =>
2016-11-30 01:08:29 +00:00
{
#if DEBUG
var assemblyLoader = new PluginAssemblyLoader(metadata.ExecuteFilePath);
var assembly = assemblyLoader.LoadAssemblyAndDependencies();
2021-03-23 09:25:46 +00:00
var type = assemblyLoader.FromAssemblyGetTypeOfInterface(assembly,
typeof(IAsyncPlugin));
2021-03-23 09:25:46 +00:00
var plugin = Activator.CreateInstance(type) as IAsyncPlugin;
#else
Assembly assembly = null;
2021-03-23 09:25:46 +00:00
IAsyncPlugin plugin = null;
try
{
var assemblyLoader = new PluginAssemblyLoader(metadata.ExecuteFilePath);
assembly = assemblyLoader.LoadAssemblyAndDependencies();
2021-03-23 09:25:46 +00:00
var type = assemblyLoader.FromAssemblyGetTypeOfInterface(assembly,
typeof(IAsyncPlugin));
2021-03-23 09:25:46 +00:00
plugin = Activator.CreateInstance(type) as IAsyncPlugin;
}
catch (Exception e) when (assembly == null)
{
Log.Exception($"|PluginsLoader.DotNetPlugins|Couldn't load assembly for the plugin: {metadata.Name}", e);
}
catch (InvalidOperationException e)
{
Log.Exception($"|PluginsLoader.DotNetPlugins|Can't find the required IPlugin interface for the plugin: <{metadata.Name}>", e);
}
catch (ReflectionTypeLoadException e)
{
Log.Exception($"|PluginsLoader.DotNetPlugins|The GetTypes method was unable to load assembly types for the plugin: <{metadata.Name}>", e);
}
catch (Exception e)
{
Log.Exception($"|PluginsLoader.DotNetPlugins|The following plugin has errored and can not be loaded: <{metadata.Name}>", e);
}
2021-03-23 09:25:46 +00:00
#endif
if (plugin == null)
{
erroredPlugins.Add(metadata.Name);
return;
}
2021-03-23 09:25:46 +00:00
plugins.Add(new PluginPair {Plugin = plugin, Metadata = metadata});
});
2016-11-30 01:08:29 +00:00
metadata.InitTime += milliseconds;
}
if (erroredPlugins.Count > 0)
{
var errorPluginString = String.Join(Environment.NewLine, erroredPlugins);
2020-05-21 20:48:03 +00:00
var errorMessage = "The following "
+ (erroredPlugins.Count > 1 ? "plugins have " : "plugin has ")
+ "errored and cannot be loaded:";
2020-05-21 20:48:03 +00:00
Task.Run(() =>
{
MessageBox.Show($"{errorMessage}{Environment.NewLine}{Environment.NewLine}" +
$"{errorPluginString}{Environment.NewLine}{Environment.NewLine}" +
$"Please refer to the logs for more information", "",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
});
}
return plugins;
}
2021-01-31 06:49:08 +00:00
public static IEnumerable<PluginPair> PythonPlugins(List<PluginMetadata> source, PluginsSettings settings)
{
2021-01-31 06:49:08 +00:00
if (!source.Any(o => o.Language.ToUpper() == AllowedLanguage.Python))
return new List<PluginPair>();
if (!string.IsNullOrEmpty(settings.PythonDirectory) && FilesFolders.LocationExists(settings.PythonDirectory))
2021-06-18 14:54:04 +00:00
return SetPythonPathForPluginPairs(source, Path.Combine(settings.PythonDirectory, PythonExecutable));
var pythonPath = string.Empty;
if (MessageBox.Show("Flow detected you have installed Python plugins, which " +
"will need Python to run. Would you like to download Python? " +
2021-06-18 14:54:04 +00:00
Environment.NewLine + Environment.NewLine +
"Click no if it's already installed, " +
"and you will be prompted to select the folder that contains the Python executable",
string.Empty, MessageBoxButtons.YesNo) == DialogResult.No
&& string.IsNullOrEmpty(settings.PythonDirectory))
{
2021-06-18 14:54:04 +00:00
var dlg = new FolderBrowserDialog
2021-01-31 06:49:08 +00:00
{
2021-06-18 14:54:04 +00:00
SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)
};
2021-01-31 06:49:08 +00:00
2021-06-18 14:54:04 +00:00
var result = dlg.ShowDialog();
if (result == DialogResult.OK)
{
string pythonDirectory = dlg.SelectedPath;
if (!string.IsNullOrEmpty(pythonDirectory))
2021-01-31 06:49:08 +00:00
{
2021-06-18 14:54:04 +00:00
pythonPath = Path.Combine(pythonDirectory, PythonExecutable);
if (File.Exists(pythonPath))
2021-01-31 06:49:08 +00:00
{
2021-06-18 14:54:04 +00:00
settings.PythonDirectory = pythonDirectory;
Constant.PythonPath = pythonPath;
}
else
{
MessageBox.Show("Can't find python in given directory");
2021-01-31 06:49:08 +00:00
}
}
}
2021-06-18 14:54:04 +00:00
}
else
{
var installedPythonDirectory = Path.Combine(DataLocation.DataDirectory(), "PythonEmbeddable");
2021-06-17 22:27:23 +00:00
2021-06-18 14:54:04 +00:00
// Python 3.8.9 is used for Windows 7 compatibility
DroplexPackage.Drop(App.python_3_8_9_embeddable, installedPythonDirectory).Wait();
2021-01-31 06:49:08 +00:00
2021-06-18 14:54:04 +00:00
pythonPath = Path.Combine(installedPythonDirectory, PythonExecutable);
if (FilesFolders.FileExists(pythonPath))
{
settings.PythonDirectory = installedPythonDirectory;
Constant.PythonPath = pythonPath;
}
else
{
Log.Error("PluginsLoader",
$"Failed to set Python path after Droplex install, {pythonPath} does not exist",
"PythonPlugins");
2021-01-31 06:49:08 +00:00
}
2020-06-24 22:55:20 +00:00
}
2021-01-31 06:49:08 +00:00
2021-06-18 14:54:04 +00:00
if (string.IsNullOrEmpty(settings.PythonDirectory) || string.IsNullOrEmpty(pythonPath))
2020-06-24 22:55:20 +00:00
{
MessageBox.Show(
"Unable to set Python executable path, please try from Flow's settings (scroll down to the bottom).");
2021-03-23 09:25:46 +00:00
Log.Error("PluginsLoader",
$"Not able to successfully set Python path, the PythonDirectory variable is still an empty string.",
2021-01-31 06:49:08 +00:00
"PythonPlugins");
return new List<PluginPair>();
2020-06-24 22:55:20 +00:00
}
2021-01-31 06:49:08 +00:00
2021-06-18 14:54:04 +00:00
return SetPythonPathForPluginPairs(source, pythonPath);
}
private static IEnumerable<PluginPair> SetPythonPathForPluginPairs(List<PluginMetadata> source, string pythonPath)
=> source
2021-01-31 06:49:08 +00:00
.Where(o => o.Language.ToUpper() == AllowedLanguage.Python)
.Select(metadata => new PluginPair
{
2021-06-18 14:54:04 +00:00
Plugin = new PythonPlugin(pythonPath),
Metadata = metadata
2021-01-31 06:49:08 +00:00
})
.ToList();
2016-05-07 15:56:29 +00:00
2021-06-18 14:54:04 +00:00
public static IEnumerable<PluginPair> ExecutablePlugins(IEnumerable<PluginMetadata> source)
2016-05-07 15:56:29 +00:00
{
2020-06-24 22:55:20 +00:00
return source
.Where(o => o.Language.ToUpper() == AllowedLanguage.Executable)
.Select(metadata => new PluginPair
{
Plugin = new ExecutablePlugin(metadata.ExecuteFilePath), Metadata = metadata
2020-06-24 22:55:20 +00:00
});
2016-05-07 15:56:29 +00:00
}
2014-12-26 11:36:43 +00:00
}
}