PowerToys/Wox.Core/Plugin/PluginConfig.cs

100 lines
3.5 KiB
C#
Raw Normal View History

2016-01-07 05:34:42 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
2014-12-26 19:36:43 +08:00
using System.IO;
using System.Threading.Tasks;
2014-12-26 19:36:43 +08:00
using Newtonsoft.Json;
2015-11-09 09:32:33 +08:00
using Wox.Infrastructure.Exception;
2014-12-26 19:36:43 +08:00
using Wox.Infrastructure.Logger;
using Wox.Plugin;
namespace Wox.Core.Plugin
{
internal abstract class PluginConfig
{
private const string PluginConfigName = "plugin.json";
private static readonly List<PluginMetadata> PluginMetadatas = new List<PluginMetadata>();
2014-12-26 19:36:43 +08:00
/// <summary>
/// Parse plugin metadata in giving directories
/// </summary>
/// <param name="pluginDirectories"></param>
/// <returns></returns>
public static List<PluginMetadata> Parse(string[] pluginDirectories)
2014-12-26 19:36:43 +08:00
{
PluginMetadatas.Clear();
var directories = pluginDirectories.SelectMany(Directory.GetDirectories);
ParsePluginConfigs(directories);
return PluginMetadatas;
2014-12-26 19:36:43 +08:00
}
private static void ParsePluginConfigs(IEnumerable<string> directories)
2014-12-26 19:36:43 +08:00
{
// todo use linq when diable plugin is implmented since parallel.foreach + list is not thread saft
foreach (var directory in directories)
2014-12-26 19:36:43 +08:00
{
if (File.Exists(Path.Combine(directory, "NeedDelete.txt")))
2014-12-26 19:36:43 +08:00
{
2014-12-29 23:02:50 +08:00
try
{
Directory.Delete(directory, true);
}
2016-01-07 05:34:42 +08:00
catch (Exception e)
2014-12-29 23:02:50 +08:00
{
2017-01-24 08:24:20 +08:00
Log.Exception($"|PluginConfig.ParsePLuginConfigs|Can't delete <{directory}>", e);
2014-12-29 23:02:50 +08:00
}
2014-12-26 19:36:43 +08:00
}
else
2014-12-26 19:36:43 +08:00
{
PluginMetadata metadata = GetPluginMetadata(directory);
if (metadata != null)
{
PluginMetadatas.Add(metadata);
}
2014-12-26 19:36:43 +08:00
}
}
2014-12-26 19:36:43 +08:00
}
private static PluginMetadata GetPluginMetadata(string pluginDirectory)
{
string configPath = Path.Combine(pluginDirectory, PluginConfigName);
2014-12-26 19:36:43 +08:00
if (!File.Exists(configPath))
{
2017-01-24 08:24:20 +08:00
Log.Error($"|PluginConfig.GetPluginMetadata|Didn't find config file <{configPath}>");
2014-12-26 19:36:43 +08:00
return null;
}
PluginMetadata metadata;
try
{
metadata = JsonConvert.DeserializeObject<PluginMetadata>(File.ReadAllText(configPath));
metadata.PluginDirectory = pluginDirectory;
// for plugins which doesn't has ActionKeywords key
metadata.ActionKeywords = metadata.ActionKeywords ?? new List<string> { metadata.ActionKeyword };
// for plugin still use old ActionKeyword
metadata.ActionKeyword = metadata.ActionKeywords?[0];
2014-12-26 19:36:43 +08:00
}
2016-01-07 05:34:42 +08:00
catch (Exception e)
2014-12-26 19:36:43 +08:00
{
2017-01-24 08:24:20 +08:00
Log.Exception($"|PluginConfig.GetPluginMetadata|invalid json for config <{configPath}>", e);
2014-12-26 19:36:43 +08:00
return null;
}
if (!AllowedLanguage.IsAllowed(metadata.Language))
{
2017-01-24 08:24:20 +08:00
Log.Error($"|PluginConfig.GetPluginMetadata|Invalid language <{metadata.Language}> for config <{configPath}>");
2014-12-26 19:36:43 +08:00
return null;
}
if (!File.Exists(metadata.ExecuteFilePath))
{
2017-01-24 08:24:20 +08:00
Log.Error($"|PluginConfig.GetPluginMetadata|execute file path didn't exist <{metadata.ExecuteFilePath}> for conifg <{configPath}");
2014-12-26 19:36:43 +08:00
return null;
}
return metadata;
}
}
}