2022-08-08 04:31:38 +00:00
|
|
|
|
using System;
|
2014-12-26 11:36:43 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Reflection;
|
2020-05-20 23:02:51 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2020-05-26 04:11:20 +00:00
|
|
|
|
using System.Windows.Forms;
|
2022-11-21 22:20:27 +00:00
|
|
|
|
using Flow.Launcher.Core.ExternalPlugins.Environments;
|
2020-04-21 09:12:17 +00:00
|
|
|
|
using Flow.Launcher.Infrastructure.Logger;
|
|
|
|
|
|
using Flow.Launcher.Infrastructure.UserSettings;
|
|
|
|
|
|
using Flow.Launcher.Plugin;
|
2021-06-07 11:17:46 +00:00
|
|
|
|
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
|
|
|
|
{
|
2016-05-05 00:57:03 +00:00
|
|
|
|
public static class PluginsLoader
|
2014-12-26 11:36:43 +00:00
|
|
|
|
{
|
2016-05-12 01:45:35 +00:00
|
|
|
|
public static List<PluginPair> Plugins(List<PluginMetadata> metadatas, PluginsSettings settings)
|
2016-05-10 00:08:54 +00:00
|
|
|
|
{
|
2020-06-24 23:59:06 +00:00
|
|
|
|
var dotnetPlugins = DotNetPlugins(metadatas);
|
2022-10-23 09:05:12 +00:00
|
|
|
|
|
2022-11-21 22:20:27 +00:00
|
|
|
|
var pythonEnv = new PythonEnvironment(metadatas, settings);
|
|
|
|
|
|
var tsEnv = new TypeScriptEnvironment(metadatas, settings);
|
|
|
|
|
|
var jsEnv = new JavaScriptEnvironment(metadatas, settings);
|
|
|
|
|
|
var pythonPlugins = pythonEnv.Setup();
|
|
|
|
|
|
var tsPlugins = tsEnv.Setup();
|
|
|
|
|
|
var jsPlugins = jsEnv.Setup();
|
2022-10-23 09:05:12 +00:00
|
|
|
|
|
2016-05-10 00:08:54 +00:00
|
|
|
|
var executablePlugins = ExecutablePlugins(metadatas);
|
2022-10-23 09:05:12 +00:00
|
|
|
|
|
|
|
|
|
|
var plugins = dotnetPlugins
|
|
|
|
|
|
.Concat(pythonPlugins)
|
|
|
|
|
|
.Concat(tsPlugins)
|
2022-10-23 20:54:15 +00:00
|
|
|
|
.Concat(jsPlugins)
|
2022-10-23 09:05:12 +00:00
|
|
|
|
.Concat(executablePlugins)
|
|
|
|
|
|
.ToList();
|
2016-05-10 00:08:54 +00:00
|
|
|
|
return plugins;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-04-30 07:22:28 +00:00
|
|
|
|
public static IEnumerable<PluginPair> DotNetPlugins(List<PluginMetadata> source)
|
2014-12-26 11:36:43 +00:00
|
|
|
|
{
|
2020-05-20 22:47:09 +00:00
|
|
|
|
var erroredPlugins = new List<string>();
|
|
|
|
|
|
|
2014-12-26 11:36:43 +00:00
|
|
|
|
var plugins = new List<PluginPair>();
|
2020-04-30 07:22:28 +00:00
|
|
|
|
var metadatas = source.Where(o => AllowedLanguage.IsDotNet(o.Language));
|
2014-12-26 11:36:43 +00:00
|
|
|
|
|
2016-05-05 00:57:03 +00:00
|
|
|
|
foreach (var metadata in metadatas)
|
2014-12-26 11:36:43 +00:00
|
|
|
|
{
|
2021-01-02 09:25:13 +00:00
|
|
|
|
var milliseconds = Stopwatch.Debug(
|
|
|
|
|
|
$"|PluginsLoader.DotNetPlugins|Constructor init cost for {metadata.Name}", () =>
|
2016-11-30 01:08:29 +00:00
|
|
|
|
{
|
2021-01-02 09:25:13 +00:00
|
|
|
|
Assembly assembly = null;
|
2021-03-23 09:25:46 +00:00
|
|
|
|
IAsyncPlugin plugin = null;
|
2021-01-02 09:25:13 +00:00
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var assemblyLoader = new PluginAssemblyLoader(metadata.ExecuteFilePath);
|
|
|
|
|
|
assembly = assemblyLoader.LoadAssemblyAndDependencies();
|
|
|
|
|
|
|
2021-03-23 09:25:46 +00:00
|
|
|
|
var type = assemblyLoader.FromAssemblyGetTypeOfInterface(assembly,
|
2021-01-02 09:25:13 +00:00
|
|
|
|
typeof(IAsyncPlugin));
|
|
|
|
|
|
|
2021-03-23 09:25:46 +00:00
|
|
|
|
plugin = Activator.CreateInstance(type) as IAsyncPlugin;
|
2021-01-02 09:25:13 +00:00
|
|
|
|
}
|
2022-08-09 23:31:29 +00:00
|
|
|
|
#if DEBUG
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw;
|
|
|
|
|
|
}
|
|
|
|
|
|
#else
|
2021-01-02 09:25:13 +00:00
|
|
|
|
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
|
2022-08-09 23:31:29 +00:00
|
|
|
|
|
2021-01-02 09:25:13 +00:00
|
|
|
|
if (plugin == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
erroredPlugins.Add(metadata.Name);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2021-03-23 09:25:46 +00:00
|
|
|
|
|
2021-06-07 11:17:46 +00:00
|
|
|
|
plugins.Add(new PluginPair {Plugin = plugin, Metadata = metadata});
|
2020-06-24 23:59:06 +00:00
|
|
|
|
});
|
2016-11-30 01:08:29 +00:00
|
|
|
|
metadata.InitTime += milliseconds;
|
2016-05-05 00:57:03 +00:00
|
|
|
|
}
|
2020-05-20 22:47:09 +00:00
|
|
|
|
|
|
|
|
|
|
if (erroredPlugins.Count > 0)
|
|
|
|
|
|
{
|
2020-06-24 23:59:06 +00:00
|
|
|
|
var errorPluginString = String.Join(Environment.NewLine, erroredPlugins);
|
2020-05-20 22:47:09 +00:00
|
|
|
|
|
2020-05-21 20:48:03 +00:00
|
|
|
|
var errorMessage = "The following "
|
2021-01-02 09:25:13 +00:00
|
|
|
|
+ (erroredPlugins.Count > 1 ? "plugins have " : "plugin has ")
|
|
|
|
|
|
+ "errored and cannot be loaded:";
|
2020-05-21 20:48:03 +00:00
|
|
|
|
|
2022-08-08 04:31:38 +00:00
|
|
|
|
_ = Task.Run(() =>
|
2020-05-20 22:47:09 +00:00
|
|
|
|
{
|
2020-05-26 04:11:20 +00:00
|
|
|
|
MessageBox.Show($"{errorMessage}{Environment.NewLine}{Environment.NewLine}" +
|
2021-01-02 09:25:13 +00:00
|
|
|
|
$"{errorPluginString}{Environment.NewLine}{Environment.NewLine}" +
|
|
|
|
|
|
$"Please refer to the logs for more information", "",
|
|
|
|
|
|
MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
2020-05-20 22:47:09 +00:00
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-05-05 00:57:03 +00:00
|
|
|
|
return plugins;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
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
|
2022-10-23 20:41:48 +00:00
|
|
|
|
.Where(o => o.Language.Equals(AllowedLanguage.Executable, StringComparison.OrdinalIgnoreCase))
|
2020-06-24 22:55:20 +00:00
|
|
|
|
.Select(metadata => new PluginPair
|
|
|
|
|
|
{
|
2021-06-07 11:17:46 +00:00
|
|
|
|
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
|
|
|
|
}
|
2021-06-19 09:09:09 +00:00
|
|
|
|
}
|