PowerToys/Wox.Core/Plugin/PluginsLoader.cs

139 lines
4.9 KiB
C#
Raw Normal View History

2014-12-26 19:36:43 +08:00
using System;
using System.Collections.Generic;
using System.IO;
2014-12-26 19:36:43 +08:00
using System.Linq;
using System.Reflection;
2016-07-20 09:01:47 +08:00
using Wox.Infrastructure;
2015-11-09 09:32:33 +08:00
using Wox.Infrastructure.Exception;
2014-12-26 19:36:43 +08:00
using Wox.Infrastructure.Logger;
2016-06-19 23:18:43 +08:00
using Wox.Infrastructure.UserSettings;
2014-12-26 19:36:43 +08:00
using Wox.Plugin;
namespace Wox.Core.Plugin
{
public static class PluginsLoader
2014-12-26 19:36:43 +08:00
{
public const string PATH = "PATH";
public const string Python = "python";
public const string PythonExecutable = "pythonw.exe";
2016-05-12 09:45:35 +08:00
public static List<PluginPair> Plugins(List<PluginMetadata> metadatas, PluginsSettings settings)
{
var csharpPlugins = CSharpPlugins(metadatas).ToList();
var pythonPlugins = PythonPlugins(metadatas, settings.PythonDirectory);
var executablePlugins = ExecutablePlugins(metadatas);
var plugins = csharpPlugins.Concat(pythonPlugins).Concat(executablePlugins).ToList();
return plugins;
}
public static IEnumerable<PluginPair> CSharpPlugins(List<PluginMetadata> source)
2014-12-26 19:36:43 +08:00
{
var plugins = new List<PluginPair>();
var metadatas = source.Where(o => o.Language.ToUpper() == AllowedLanguage.CSharp);
2014-12-26 19:36:43 +08:00
foreach (var metadata in metadatas)
2014-12-26 19:36:43 +08:00
{
Assembly assembly;
try
{
assembly = Assembly.Load(AssemblyName.GetAssemblyName(metadata.ExecuteFilePath));
}
catch (Exception e)
{
2016-05-16 00:03:06 +08:00
Log.Exception(new WoxPluginException(metadata.Name, "Couldn't load assembly", e));
continue;
}
var types = assembly.GetTypes();
Type type;
try
{
type = types.First(o => o.IsClass && !o.IsAbstract && o.GetInterfaces().Contains(typeof(IPlugin)));
}
catch (InvalidOperationException e)
{
2016-05-16 00:03:06 +08:00
Log.Exception(new WoxPluginException(metadata.Name, "Can't find class implement IPlugin", e));
continue;
}
IPlugin plugin;
2014-12-26 19:36:43 +08:00
try
{
plugin = (IPlugin)Activator.CreateInstance(type);
}
catch (Exception e)
{
2016-05-16 00:03:06 +08:00
Log.Exception(new WoxPluginException(metadata.Name, "Can't create instance", e));
continue;
}
PluginPair pair = new PluginPair
{
Plugin = plugin,
Metadata = metadata
};
plugins.Add(pair);
}
return plugins;
}
public static IEnumerable<PluginPair> PythonPlugins(List<PluginMetadata> source, string pythonDirecotry)
{
var metadatas = source.Where(o => o.Language.ToUpper() == AllowedLanguage.Python);
string filename;
if (string.IsNullOrEmpty(pythonDirecotry))
{
var paths = Environment.GetEnvironmentVariable(PATH);
if (paths != null)
{
var pythonPaths = paths.Split(';').Where(p => p.ToLower().Contains(Python));
if (pythonPaths.Any())
2014-12-26 19:36:43 +08:00
{
filename = PythonExecutable;
2014-12-26 19:36:43 +08:00
}
else
2014-12-26 19:36:43 +08:00
{
2016-05-16 00:03:06 +08:00
Log.Exception(new WoxException("Python can't be found in PATH."));
return new List<PluginPair>();
2014-12-26 19:36:43 +08:00
}
}
else
2014-12-26 19:36:43 +08:00
{
2016-05-16 00:03:06 +08:00
Log.Exception(new WoxException("Path variable is not set."));
return new List<PluginPair>();
2014-12-26 19:36:43 +08:00
}
}
else
{
var path = Path.Combine(pythonDirecotry, PythonExecutable);
if (File.Exists(path))
{
filename = path;
}
else
{
2016-05-16 00:03:06 +08:00
Log.Exception(new WoxException("Can't find python executable in python directory"));
return new List<PluginPair>();
}
}
2016-07-20 09:01:47 +08:00
Constant.PythonPath = filename;
var plugins = metadatas.Select(metadata => new PluginPair
{
Plugin = new PythonPlugin(filename),
Metadata = metadata
});
2014-12-26 19:36:43 +08:00
return plugins;
}
2016-05-07 23:56:29 +08:00
public static IEnumerable<PluginPair> ExecutablePlugins(IEnumerable<PluginMetadata> source)
{
var metadatas = source.Where(o => o.Language.ToUpper() == AllowedLanguage.Executable);
var plugins = metadatas.Select(metadata => new PluginPair
{
Plugin = new ExecutablePlugin(metadata.ExecuteFilePath),
Metadata = metadata
});
return plugins;
}
2014-12-26 19:36:43 +08:00
}
}