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

187 lines
7.4 KiB
C#
Raw 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;
using System.Runtime.Loader;
2020-05-20 23:02:51 +00:00
using System.Threading.Tasks;
using System.Windows.Forms;
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;
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 PATH = "PATH";
public const string Python = "python";
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);
var pythonPlugins = PythonPlugins(metadatas, settings.PythonDirectory);
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();
var type = assemblyLoader.FromAssemblyGetTypeOfInterface(assembly, typeof(IPlugin),
typeof(IAsyncPlugin));
var plugin = Activator.CreateInstance(type);
#else
Assembly assembly = null;
object plugin = null;
try
{
var assemblyLoader = new PluginAssemblyLoader(metadata.ExecuteFilePath);
assembly = assemblyLoader.LoadAssemblyAndDependencies();
var type = assemblyLoader.FromAssemblyGetTypeOfInterface(assembly, typeof(IPlugin),
typeof(IAsyncPlugin));
plugin = Activator.CreateInstance(type);
}
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);
}
if (plugin == null)
{
erroredPlugins.Add(metadata.Name);
return;
}
#endif
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;
}
2020-06-24 22:55:20 +00:00
public static IEnumerable<PluginPair> PythonPlugins(List<PluginMetadata> source, string pythonDirectory)
{
2020-06-24 22:55:20 +00:00
// try to set Constant.PythonPath, either from
// PATH or from the given pythonDirectory
if (string.IsNullOrEmpty(pythonDirectory))
{
var paths = Environment.GetEnvironmentVariable(PATH);
if (paths != null)
{
2020-06-24 22:55:20 +00:00
var pythonInPath = paths
.Split(';')
.Where(p => p.ToLower().Contains(Python))
.Any();
if (pythonInPath)
2014-12-26 11:36:43 +00:00
{
2020-06-24 22:55:20 +00:00
Constant.PythonPath = PythonExecutable;
2014-12-26 11:36:43 +00:00
}
else
2014-12-26 11:36:43 +00:00
{
2017-01-24 00:24:20 +00:00
Log.Error("|PluginsLoader.PythonPlugins|Python can't be found in PATH.");
2014-12-26 11:36:43 +00:00
}
}
else
2014-12-26 11:36:43 +00:00
{
2017-01-24 00:24:20 +00:00
Log.Error("|PluginsLoader.PythonPlugins|PATH environment variable is not set.");
2014-12-26 11:36:43 +00:00
}
}
else
{
2020-06-24 22:55:20 +00:00
var path = Path.Combine(pythonDirectory, PythonExecutable);
if (File.Exists(path))
{
2020-06-24 22:55:20 +00:00
Constant.PythonPath = path;
}
else
{
2020-06-24 22:55:20 +00:00
Log.Error($"|PluginsLoader.PythonPlugins|Can't find python executable in {path}");
}
}
2020-06-24 22:55:20 +00:00
// if we have a path to the python executable,
// load every python plugin pair.
if (String.IsNullOrEmpty(Constant.PythonPath))
{
2020-06-24 22:55:20 +00:00
return new List<PluginPair>();
}
else
{
return source
.Where(o => o.Language.ToUpper() == AllowedLanguage.Python)
.Select(metadata => new PluginPair
{
Plugin = new PythonPlugin(Constant.PythonPath),
Metadata = metadata
});
}
2014-12-26 11:36:43 +00:00
}
2016-05-07 15:56:29 +00:00
public static IEnumerable<PluginPair> ExecutablePlugins(IEnumerable<PluginMetadata> source)
{
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
});
2016-05-07 15:56:29 +00:00
}
2014-12-26 11:36:43 +00:00
}
2021-01-17 08:10:26 +00:00
}