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;
|
2022-11-21 22:20:27 +00:00
|
|
|
|
using Flow.Launcher.Core.ExternalPlugins.Environments;
|
2023-04-25 12:04:08 +00:00
|
|
|
|
#pragma warning disable IDE0005
|
2020-04-21 09:12:17 +00:00
|
|
|
|
using Flow.Launcher.Infrastructure.Logger;
|
2023-04-25 12:04:08 +00:00
|
|
|
|
#pragma warning restore IDE0005
|
2020-04-21 09:12:17 +00:00
|
|
|
|
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
|
|
|
|
{
|
2016-05-05 00:57:03 +00:00
|
|
|
|
public static class PluginsLoader
|
2014-12-26 11:36:43 +00:00
|
|
|
|
{
|
2025-04-08 13:53:00 +00:00
|
|
|
|
private static readonly string ClassName = nameof(PluginsLoader);
|
|
|
|
|
|
|
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);
|
2023-11-04 02:16:51 +00:00
|
|
|
|
|
2022-11-21 22:20:27 +00:00
|
|
|
|
var pythonEnv = new PythonEnvironment(metadatas, settings);
|
2023-03-26 06:12:21 +00:00
|
|
|
|
var pythonV2Env = new PythonV2Environment(metadatas, settings);
|
2022-11-21 22:20:27 +00:00
|
|
|
|
var tsEnv = new TypeScriptEnvironment(metadatas, settings);
|
|
|
|
|
|
var jsEnv = new JavaScriptEnvironment(metadatas, settings);
|
2023-11-04 02:16:51 +00:00
|
|
|
|
var tsV2Env = new TypeScriptV2Environment(metadatas, settings);
|
|
|
|
|
|
var jsV2Env = new JavaScriptV2Environment(metadatas, settings);
|
2022-11-21 22:20:27 +00:00
|
|
|
|
var pythonPlugins = pythonEnv.Setup();
|
2023-03-26 06:12:21 +00:00
|
|
|
|
var pythonV2Plugins = pythonV2Env.Setup();
|
2022-11-21 22:20:27 +00:00
|
|
|
|
var tsPlugins = tsEnv.Setup();
|
|
|
|
|
|
var jsPlugins = jsEnv.Setup();
|
2023-11-04 02:16:51 +00:00
|
|
|
|
var tsV2Plugins = tsV2Env.Setup();
|
|
|
|
|
|
var jsV2Plugins = jsV2Env.Setup();
|
|
|
|
|
|
|
2016-05-10 00:08:54 +00:00
|
|
|
|
var executablePlugins = ExecutablePlugins(metadatas);
|
2023-11-04 02:16:51 +00:00
|
|
|
|
var executableV2Plugins = ExecutableV2Plugins(metadatas);
|
|
|
|
|
|
|
2022-10-23 09:05:12 +00:00
|
|
|
|
var plugins = dotnetPlugins
|
2023-11-04 02:16:51 +00:00
|
|
|
|
.Concat(pythonPlugins)
|
|
|
|
|
|
.Concat(pythonV2Plugins)
|
|
|
|
|
|
.Concat(tsPlugins)
|
|
|
|
|
|
.Concat(jsPlugins)
|
|
|
|
|
|
.Concat(tsV2Plugins)
|
|
|
|
|
|
.Concat(jsV2Plugins)
|
|
|
|
|
|
.Concat(executablePlugins)
|
|
|
|
|
|
.Concat(executableV2Plugins)
|
|
|
|
|
|
.ToList();
|
2016-05-10 00:08:54 +00:00
|
|
|
|
return plugins;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-21 13:11:48 +00:00
|
|
|
|
private static List<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
|
|
|
|
{
|
2025-09-23 09:40:54 +00:00
|
|
|
|
var milliseconds = PublicApi.Instance.StopwatchLogDebug(ClassName, $"Constructor init cost for {metadata.Name}", () =>
|
2025-07-21 13:11:48 +00:00
|
|
|
|
{
|
|
|
|
|
|
Assembly assembly = null;
|
|
|
|
|
|
IAsyncPlugin plugin = null;
|
2021-01-02 09:25:13 +00:00
|
|
|
|
|
2025-07-21 13:11:48 +00:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var assemblyLoader = new PluginAssemblyLoader(metadata.ExecuteFilePath);
|
|
|
|
|
|
assembly = assemblyLoader.LoadAssemblyAndDependencies();
|
2021-01-02 09:25:13 +00:00
|
|
|
|
|
2025-07-21 13:11:48 +00:00
|
|
|
|
var type = assemblyLoader.FromAssemblyGetTypeOfInterface(assembly,
|
|
|
|
|
|
typeof(IAsyncPlugin));
|
2021-01-02 09:25:13 +00:00
|
|
|
|
|
2025-07-21 13:11:48 +00:00
|
|
|
|
plugin = Activator.CreateInstance(type) as IAsyncPlugin;
|
2025-02-23 13:01:53 +00:00
|
|
|
|
|
2025-07-21 13:11:48 +00:00
|
|
|
|
metadata.AssemblyName = assembly.GetName().Name;
|
|
|
|
|
|
}
|
2022-08-09 23:31:29 +00:00
|
|
|
|
#if DEBUG
|
2025-07-21 13:11:48 +00:00
|
|
|
|
catch (Exception)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw;
|
|
|
|
|
|
}
|
2022-08-09 23:31:29 +00:00
|
|
|
|
#else
|
2025-07-21 13:11:48 +00:00
|
|
|
|
catch (Exception e) when (assembly == null)
|
|
|
|
|
|
{
|
2025-10-02 12:22:20 +00:00
|
|
|
|
PublicApi.Instance.LogException(ClassName, $"Couldn't load assembly for the plugin: {metadata.Name}", e);
|
2025-07-21 13:11:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
catch (InvalidOperationException e)
|
|
|
|
|
|
{
|
2025-10-02 12:22:20 +00:00
|
|
|
|
PublicApi.Instance.LogException(ClassName, $"Can't find the required IPlugin interface for the plugin: <{metadata.Name}>", e);
|
2025-07-21 13:11:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
catch (ReflectionTypeLoadException e)
|
|
|
|
|
|
{
|
2025-10-02 12:22:20 +00:00
|
|
|
|
PublicApi.Instance.LogException(ClassName, $"The GetTypes method was unable to load assembly types for the plugin: <{metadata.Name}>", e);
|
2025-07-21 13:11:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
2025-10-02 12:22:20 +00:00
|
|
|
|
PublicApi.Instance.LogException(ClassName, $"The following plugin has errored and can not be loaded: <{metadata.Name}>", e);
|
2025-07-21 13:11:48 +00:00
|
|
|
|
}
|
2021-03-23 09:25:46 +00:00
|
|
|
|
#endif
|
2022-08-09 23:31:29 +00:00
|
|
|
|
|
2025-07-21 13:11:48 +00:00
|
|
|
|
if (plugin == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
erroredPlugins.Add(metadata.Name);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2021-03-23 09:25:46 +00:00
|
|
|
|
|
2025-07-21 13:11:48 +00:00
|
|
|
|
plugins.Add(new PluginPair { Plugin = plugin, Metadata = metadata });
|
|
|
|
|
|
});
|
2025-07-21 13:17:30 +00:00
|
|
|
|
|
2016-11-30 01:08:29 +00:00
|
|
|
|
metadata.InitTime += milliseconds;
|
2025-10-16 10:43:19 +00:00
|
|
|
|
PublicApi.Instance.LogDebug(ClassName, $"Constructor cost for <{metadata.Name}> is <{metadata.InitTime}ms>");
|
2016-05-05 00:57:03 +00:00
|
|
|
|
}
|
2020-05-20 22:47:09 +00:00
|
|
|
|
|
|
|
|
|
|
if (erroredPlugins.Count > 0)
|
|
|
|
|
|
{
|
2025-02-24 03:35:41 +00:00
|
|
|
|
var errorPluginString = string.Join(Environment.NewLine, erroredPlugins);
|
2020-05-20 22:47:09 +00:00
|
|
|
|
|
2025-07-20 04:07:19 +00:00
|
|
|
|
var errorMessage = erroredPlugins.Count > 1 ?
|
2025-09-23 09:14:30 +00:00
|
|
|
|
Localize.pluginsHaveErrored():
|
|
|
|
|
|
Localize.pluginHasErrored();
|
2020-05-21 20:48:03 +00:00
|
|
|
|
|
2025-09-23 09:40:54 +00:00
|
|
|
|
PublicApi.Instance.ShowMsgError($"{errorMessage}{Environment.NewLine}{Environment.NewLine}" +
|
2025-08-14 11:22:03 +00:00
|
|
|
|
$"{errorPluginString}{Environment.NewLine}{Environment.NewLine}" +
|
2025-09-23 09:14:30 +00:00
|
|
|
|
Localize.referToLogs());
|
2020-05-20 22:47:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-05-05 00:57:03 +00:00
|
|
|
|
return plugins;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-30 05:57:35 +00:00
|
|
|
|
private 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))
|
2025-02-24 04:37:08 +00:00
|
|
|
|
.Select(metadata =>
|
2020-06-24 22:55:20 +00:00
|
|
|
|
{
|
2025-03-23 04:47:57 +00:00
|
|
|
|
return new PluginPair
|
2025-02-24 04:37:08 +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
|
|
|
|
}
|
2023-11-04 02:16:51 +00:00
|
|
|
|
|
2025-03-30 05:57:35 +00:00
|
|
|
|
private static IEnumerable<PluginPair> ExecutableV2Plugins(IEnumerable<PluginMetadata> source)
|
2023-11-04 02:16:51 +00:00
|
|
|
|
{
|
|
|
|
|
|
return source
|
|
|
|
|
|
.Where(o => o.Language.Equals(AllowedLanguage.ExecutableV2, StringComparison.OrdinalIgnoreCase))
|
2025-02-24 04:37:08 +00:00
|
|
|
|
.Select(metadata =>
|
2023-11-04 02:16:51 +00:00
|
|
|
|
{
|
2025-03-23 04:47:57 +00:00
|
|
|
|
return new PluginPair
|
2025-02-24 04:37:08 +00:00
|
|
|
|
{
|
|
|
|
|
|
Plugin = new ExecutablePlugin(metadata.ExecuteFilePath),
|
|
|
|
|
|
Metadata = metadata
|
|
|
|
|
|
};
|
2023-11-04 02:16:51 +00:00
|
|
|
|
});
|
|
|
|
|
|
}
|
2014-12-26 11:36:43 +00:00
|
|
|
|
}
|
2021-06-19 09:09:09 +00:00
|
|
|
|
}
|