tweak plugin settings (#9522)

This commit is contained in:
Mykhailo Pylyp
2021-02-10 15:12:42 +02:00
committed by GitHub
parent ec6b9acad9
commit d92ff6d45d
24 changed files with 240 additions and 398 deletions

View File

@@ -81,12 +81,6 @@ namespace PowerLauncher.Plugin
{
metadata = JsonConvert.DeserializeObject<PluginMetadata>(File.ReadAllText(configPath));
metadata.PluginDirectory = pluginDirectory;
// for plugins which doesn't has ActionKeywords key
metadata.SetActionKeywords(metadata.GetActionKeywords() ?? new List<string> { metadata.ActionKeyword });
// for plugin still use old ActionKeyword
metadata.ActionKeyword = metadata.GetActionKeywords()?[0];
}
catch (Exception e)
{

View File

@@ -28,22 +28,54 @@ namespace PowerLauncher.Plugin
private static IEnumerable<PluginPair> _contextMenuPlugins = new List<PluginPair>();
private static List<PluginPair> _allPlugins;
// should be only used in tests
public static void SetAllPlugins(List<PluginPair> plugins)
{
_allPlugins = plugins;
}
/// <summary>
/// Gets directories that will hold Wox plugin directory
/// </summary>
public static List<PluginPair> AllPlugins { get; private set; } = new List<PluginPair>();
public static List<PluginPair> AllPlugins
{
get
{
if (_allPlugins == null)
{
_allPlugins = PluginsLoader.Plugins(PluginConfig.Parse(Directories));
}
return _allPlugins;
}
}
public static IPublicAPI API { get; private set; }
public static readonly List<PluginPair> GlobalPlugins = new List<PluginPair>();
public static readonly Dictionary<string, PluginPair> NonGlobalPlugins = new Dictionary<string, PluginPair>();
public static List<PluginPair> GlobalPlugins
{
get
{
return AllPlugins.Where(x => x.Metadata.IsGlobal).ToList();
}
}
public static Dictionary<string, PluginPair> NonGlobalPlugins
{
get
{
return AllPlugins
.Where(x => !string.IsNullOrWhiteSpace(x.Metadata.ActionKeyword))
.GroupBy(x => x.Metadata.ActionKeyword)
.Select(x => x.First())
.ToDictionary(x => x.Metadata.ActionKeyword);
}
}
private static readonly string[] Directories = { Constant.PreinstalledDirectory, Constant.PluginsDirectory };
// todo happlebao, this should not be public, the indicator function should be embedded
public static PluginSettings Settings { get; set; }
private static List<PluginMetadata> _metadatas;
private static void ValidateUserDirectory()
{
if (!Directory.Exists(Constant.PluginsDirectory))
@@ -75,19 +107,6 @@ namespace PowerLauncher.Plugin
ValidateUserDirectory();
}
/// <summary>
/// because InitializePlugins needs API, so LoadPlugins needs to be called first
/// todo happlebao The API should be removed
/// </summary>
/// <param name="settings">Plugin settings</param>
public static void LoadPlugins(PluginSettings settings)
{
_metadatas = PluginConfig.Parse(Directories);
Settings = settings ?? throw new ArgumentNullException(nameof(settings));
Settings.UpdatePluginSettings(_metadatas);
AllPlugins = PluginsLoader.Plugins(_metadatas);
}
/// <summary>
/// Call initialize for all plugins
/// </summary>
@@ -120,18 +139,6 @@ namespace PowerLauncher.Plugin
});
_contextMenuPlugins = GetPluginsForInterface<IContextMenu>();
foreach (var plugin in AllPlugins)
{
if (IsGlobalPlugin(plugin.Metadata))
{
GlobalPlugins.Add(plugin);
}
// Plugins may have multiple ActionKeywords, eg. WebSearch
plugin.Metadata.GetActionKeywords().Where(x => x != Query.GlobalPluginWildcardSign)
.ToList()
.ForEach(x => NonGlobalPlugins[x] = plugin);
}
if (failedPlugins.Any())
{
@@ -145,24 +152,6 @@ namespace PowerLauncher.Plugin
PluginInstaller.Install(path);
}
public static List<PluginPair> ValidPluginsForQuery(Query query)
{
if (query == null)
{
throw new ArgumentNullException(nameof(query));
}
if (NonGlobalPlugins.ContainsKey(query.ActionKeyword))
{
var plugin = NonGlobalPlugins[query.ActionKeyword];
return new List<PluginPair> { plugin };
}
else
{
return GlobalPlugins;
}
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "Suppressing this to enable FxCop. We are logging the exception, and going forward general exceptions should not be caught")]
public static List<Result> QueryForPlugin(PluginPair pair, Query query, bool delayedExecution = false)
{
@@ -245,11 +234,6 @@ namespace PowerLauncher.Plugin
}
}
private static bool IsGlobalPlugin(PluginMetadata metadata)
{
return metadata.GetActionKeywords().Contains(Query.GlobalPluginWildcardSign);
}
/// <summary>
/// get specified plugin, return null if not found
/// </summary>
@@ -293,72 +277,6 @@ namespace PowerLauncher.Plugin
}
}
public static bool ActionKeywordRegistered(string actionKeyword)
{
if (actionKeyword != Query.GlobalPluginWildcardSign &&
NonGlobalPlugins.ContainsKey(actionKeyword))
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// used to add action keyword for multiple action keyword plugin
/// e.g. web search
/// </summary>
public static void AddActionKeyword(string id, string newActionKeyword)
{
var plugin = GetPluginForId(id);
if (newActionKeyword == Query.GlobalPluginWildcardSign)
{
GlobalPlugins.Add(plugin);
}
else
{
NonGlobalPlugins[newActionKeyword] = plugin;
}
plugin.Metadata.GetActionKeywords().Add(newActionKeyword);
}
/// <summary>
/// used to add action keyword for multiple action keyword plugin
/// e.g. web search
/// </summary>
public static void RemoveActionKeyword(string id, string oldActionkeyword)
{
var plugin = GetPluginForId(id);
if (oldActionkeyword == Query.GlobalPluginWildcardSign
&& // Plugins may have multiple ActionKeywords that are global, eg. WebSearch
plugin.Metadata.GetActionKeywords()
.Where(x => x == Query.GlobalPluginWildcardSign)
.ToList()
.Count == 1)
{
GlobalPlugins.Remove(plugin);
}
if (oldActionkeyword != Query.GlobalPluginWildcardSign)
{
NonGlobalPlugins.Remove(oldActionkeyword);
}
plugin.Metadata.GetActionKeywords().Remove(oldActionkeyword);
}
public static void ReplaceActionKeyword(string id, string oldActionKeyword, string newActionKeyword)
{
if (oldActionKeyword != newActionKeyword)
{
AddActionKeyword(id, newActionKeyword);
RemoveActionKeyword(id, oldActionKeyword);
}
}
public static void Dispose()
{
foreach (var plugin in AllPlugins)

View File

@@ -12,18 +12,13 @@ namespace PowerLauncher.Plugin
{
public static class QueryBuilder
{
public static Dictionary<PluginPair, Query> Build(ref string text, Dictionary<string, PluginPair> nonGlobalPlugins)
public static Dictionary<PluginPair, Query> Build(ref string text)
{
if (text == null)
{
throw new ArgumentNullException(nameof(text));
}
if (nonGlobalPlugins == null)
{
throw new ArgumentNullException(nameof(nonGlobalPlugins));
}
// replace multiple white spaces with one white space
var terms = text.Split(new[] { Query.TermSeparator }, StringSplitOptions.RemoveEmptyEntries);
if (terms.Length == 0)
@@ -41,12 +36,12 @@ namespace PowerLauncher.Plugin
string possibleActionKeyword = terms[0];
foreach (string pluginActionKeyword in nonGlobalPlugins.Keys)
foreach (string pluginActionKeyword in PluginManager.NonGlobalPlugins.Keys)
{
// Using Ordinal since this is used internally
if (possibleActionKeyword.StartsWith(pluginActionKeyword, StringComparison.Ordinal))
{
if (nonGlobalPlugins.TryGetValue(pluginActionKeyword, out var pluginPair) && !pluginPair.Metadata.Disabled)
if (PluginManager.NonGlobalPlugins.TryGetValue(pluginActionKeyword, out var pluginPair) && !pluginPair.Metadata.Disabled)
{
// The search string is the raw query excluding the action keyword
string search = rawQuery.Substring(pluginActionKeyword.Length).Trim();
@@ -75,8 +70,6 @@ namespace PowerLauncher.Plugin
// add the global plugins to the list.
if (pluginQueryPair.Count == 0)
{
var globalplugins = PluginManager.GlobalPlugins;
foreach (PluginPair globalPlugin in PluginManager.GlobalPlugins)
{
if (!pluginQueryPair.ContainsKey(globalPlugin))