2016-01-07 05:34:42 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2014-12-26 19:36:43 +08:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using Newtonsoft.Json;
|
2015-01-05 22:41:17 +08:00
|
|
|
|
using Wox.Core.UserSettings;
|
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 List<PluginMetadata> pluginMetadatas = new List<PluginMetadata>();
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Parse plugin metadata in giving directories
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="pluginDirectories"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static List<PluginMetadata> Parse(List<string> pluginDirectories)
|
|
|
|
|
{
|
|
|
|
|
pluginMetadatas.Clear();
|
|
|
|
|
foreach (string pluginDirectory in pluginDirectories)
|
|
|
|
|
{
|
2015-01-03 15:20:34 +08:00
|
|
|
|
ParsePluginConfigs(pluginDirectory);
|
2014-12-26 19:36:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return pluginMetadatas;
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-03 15:20:34 +08:00
|
|
|
|
private static void ParsePluginConfigs(string pluginDirectory)
|
2014-12-26 19:36:43 +08:00
|
|
|
|
{
|
2014-12-29 22:09:54 +08:00
|
|
|
|
if (!Directory.Exists(pluginDirectory)) return;
|
2014-12-26 19:36:43 +08:00
|
|
|
|
|
|
|
|
|
string[] directories = Directory.GetDirectories(pluginDirectory);
|
|
|
|
|
foreach (string directory in directories)
|
|
|
|
|
{
|
|
|
|
|
if (File.Exists((Path.Combine(directory, "NeedDelete.txt"))))
|
|
|
|
|
{
|
2014-12-29 23:02:50 +08:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Directory.Delete(directory, true);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2016-01-07 05:34:42 +08:00
|
|
|
|
catch (Exception e)
|
2014-12-29 23:02:50 +08:00
|
|
|
|
{
|
2015-11-08 01:32:58 +08:00
|
|
|
|
Log.Fatal(e);
|
2014-12-29 23:02:50 +08:00
|
|
|
|
}
|
2014-12-26 19:36:43 +08:00
|
|
|
|
}
|
|
|
|
|
PluginMetadata metadata = GetPluginMetadata(directory);
|
|
|
|
|
if (metadata != null)
|
|
|
|
|
{
|
|
|
|
|
pluginMetadatas.Add(metadata);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static PluginMetadata GetPluginMetadata(string pluginDirectory)
|
|
|
|
|
{
|
|
|
|
|
string configPath = Path.Combine(pluginDirectory, pluginConfigName);
|
|
|
|
|
if (!File.Exists(configPath))
|
|
|
|
|
{
|
2015-11-08 01:32:58 +08:00
|
|
|
|
Log.Warn($"parse plugin {configPath} failed: didn't find config file.");
|
2014-12-26 19:36:43 +08:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PluginMetadata metadata;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
metadata = JsonConvert.DeserializeObject<PluginMetadata>(File.ReadAllText(configPath));
|
|
|
|
|
metadata.PluginDirectory = pluginDirectory;
|
2015-11-05 06:49:40 +08:00
|
|
|
|
// for plugins which doesn't has ActionKeywords key
|
2015-11-06 10:29:32 +08:00
|
|
|
|
metadata.ActionKeywords = metadata.ActionKeywords ?? new List<string> {metadata.ActionKeyword};
|
2015-11-05 06:49:40 +08:00
|
|
|
|
// 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
|
|
|
|
{
|
2015-11-08 01:32:58 +08:00
|
|
|
|
string msg = $"Parse plugin config {configPath} failed: json format is not valid";
|
|
|
|
|
Log.Error(new WoxException(msg));
|
2014-12-26 19:36:43 +08:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!AllowedLanguage.IsAllowed(metadata.Language))
|
|
|
|
|
{
|
2015-11-08 01:32:58 +08:00
|
|
|
|
string msg = $"Parse plugin config {configPath} failed: invalid language {metadata.Language}";
|
|
|
|
|
Log.Error(new WoxException(msg));
|
2014-12-26 19:36:43 +08:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!File.Exists(metadata.ExecuteFilePath))
|
|
|
|
|
{
|
2015-11-08 01:32:58 +08:00
|
|
|
|
string msg = $"Parse plugin config {configPath} failed: ExecuteFile {metadata.ExecuteFilePath} didn't exist";
|
|
|
|
|
Log.Error(new WoxException(msg));
|
2014-12-26 19:36:43 +08:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//replace action keyword if user customized it.
|
|
|
|
|
var customizedPluginConfig = UserSettingStorage.Instance.CustomizedPluginConfigs.FirstOrDefault(o => o.ID == metadata.ID);
|
2015-11-06 10:29:32 +08:00
|
|
|
|
if (customizedPluginConfig?.ActionKeywords?.Count > 0)
|
2014-12-26 19:36:43 +08:00
|
|
|
|
{
|
2015-11-05 06:49:40 +08:00
|
|
|
|
metadata.ActionKeywords = customizedPluginConfig.ActionKeywords;
|
2015-11-06 06:47:28 +08:00
|
|
|
|
metadata.ActionKeyword = customizedPluginConfig.ActionKeywords[0];
|
2014-12-26 19:36:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return metadata;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-12-29 22:09:54 +08:00
|
|
|
|
}
|