mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-06 03:07:04 +02:00
Change plugin config file format (from ini to json)
This commit is contained in:
@@ -4,8 +4,8 @@ using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Windows.Forms;
|
||||
using Newtonsoft.Json;
|
||||
using Wox.Helper;
|
||||
using Wox.Infrastructure;
|
||||
using Wox.Plugin;
|
||||
using Wox.Plugin.System;
|
||||
|
||||
@@ -14,7 +14,7 @@ namespace Wox.PluginLoader
|
||||
public abstract class BasePluginLoader
|
||||
{
|
||||
private static string PluginPath = AppDomain.CurrentDomain.BaseDirectory + "Plugins";
|
||||
private static string PluginConfigName = "plugin.ini";
|
||||
private static string PluginConfigName = "plugin.json";
|
||||
protected static List<PluginMetadata> pluginMetadatas = new List<PluginMetadata>();
|
||||
public abstract List<PluginPair> LoadPlugin();
|
||||
|
||||
@@ -36,7 +36,6 @@ namespace Wox.PluginLoader
|
||||
metadata.PluginType = PluginType.System;
|
||||
metadata.ActionKeyword = "*";
|
||||
metadata.ExecuteFileName = "Wox.Plugin.System.dll";
|
||||
metadata.ExecuteFilePath = AppDomain.CurrentDomain.BaseDirectory + metadata.ExecuteFileName;
|
||||
metadata.PluginDirecotry = AppDomain.CurrentDomain.BaseDirectory;
|
||||
pluginMetadatas.Add(metadata);
|
||||
}
|
||||
@@ -49,84 +48,67 @@ namespace Wox.PluginLoader
|
||||
string[] directories = Directory.GetDirectories(PluginPath);
|
||||
foreach (string directory in directories)
|
||||
{
|
||||
PluginMetadata metadata = GetMetadataFromIni(directory);
|
||||
PluginMetadata metadata = GetMetadataFromJson(directory);
|
||||
if (metadata != null) pluginMetadatas.Add(metadata);
|
||||
}
|
||||
}
|
||||
|
||||
private static PluginMetadata GetMetadataFromIni(string directory)
|
||||
private static PluginMetadata GetMetadataFromJson(string pluginDirectory)
|
||||
{
|
||||
string iniPath = directory + "\\" + PluginConfigName;
|
||||
string configPath = Path.Combine(pluginDirectory, PluginConfigName);
|
||||
PluginMetadata metadata;
|
||||
|
||||
if (!File.Exists(iniPath))
|
||||
if (!File.Exists(configPath))
|
||||
{
|
||||
Log.Error(string.Format("parse plugin {0} failed: didn't find config file.", iniPath));
|
||||
Log.Error(string.Format("parse plugin {0} failed: didn't find config file.", configPath));
|
||||
return null;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
PluginMetadata metadata = new PluginMetadata();
|
||||
IniParser ini = new IniParser(iniPath);
|
||||
metadata.Name = ini.GetSetting("plugin", "Name");
|
||||
metadata.Author = ini.GetSetting("plugin", "Author");
|
||||
metadata.Description = ini.GetSetting("plugin", "Description");
|
||||
metadata.Language = ini.GetSetting("plugin", "Language");
|
||||
metadata.Version = ini.GetSetting("plugin", "Version");
|
||||
metadata = JsonConvert.DeserializeObject<PluginMetadata>(File.ReadAllText(configPath));
|
||||
metadata.PluginType = PluginType.ThirdParty;
|
||||
metadata.ActionKeyword = ini.GetSetting("plugin", "ActionKeyword");
|
||||
metadata.ExecuteFilePath = directory + "\\" + ini.GetSetting("plugin", "ExecuteFile");
|
||||
metadata.PluginDirecotry = directory + "\\";
|
||||
metadata.ExecuteFileName = ini.GetSetting("plugin", "ExecuteFile");
|
||||
|
||||
if (!AllowedLanguage.IsAllowed(metadata.Language))
|
||||
{
|
||||
string error = string.Format("Parse ini {0} failed: invalid language {1}", iniPath,
|
||||
metadata.Language);
|
||||
Log.Error(error);
|
||||
#if (DEBUG)
|
||||
{
|
||||
throw new WoxException(error);
|
||||
}
|
||||
#endif
|
||||
return null;
|
||||
}
|
||||
if (!File.Exists(metadata.ExecuteFilePath))
|
||||
{
|
||||
string error = string.Format("Parse ini {0} failed: ExecuteFilePath didn't exist {1}", iniPath,
|
||||
metadata.ExecuteFilePath);
|
||||
Log.Error(error);
|
||||
#if (DEBUG)
|
||||
{
|
||||
throw new WoxException(error);
|
||||
}
|
||||
#endif
|
||||
return null;
|
||||
}
|
||||
|
||||
return metadata;
|
||||
metadata.PluginDirecotry = pluginDirectory;
|
||||
}
|
||||
catch (Exception e)
|
||||
catch (Exception)
|
||||
{
|
||||
Log.Error(string.Format("Parse ini {0} failed: {1}", iniPath, e.Message));
|
||||
string error = string.Format("Parse plugin config {0} failed: json format is not valid", configPath);
|
||||
Log.Error(error);
|
||||
#if (DEBUG)
|
||||
{
|
||||
throw;
|
||||
throw new WoxException(error);
|
||||
}
|
||||
#endif
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
if (!AllowedLanguage.IsAllowed(metadata.Language))
|
||||
{
|
||||
string error = string.Format("Parse plugin config {0} failed: invalid language {1}", configPath,
|
||||
metadata.Language);
|
||||
Log.Error(error);
|
||||
#if (DEBUG)
|
||||
{
|
||||
throw new WoxException(error);
|
||||
}
|
||||
#endif
|
||||
return null;
|
||||
}
|
||||
if (!File.Exists(metadata.ExecuteFilePath))
|
||||
{
|
||||
string error = string.Format("Parse plugin config {0} failed: ExecuteFile {1} didn't exist", configPath,
|
||||
metadata.ExecuteFilePath);
|
||||
Log.Error(error);
|
||||
#if (DEBUG)
|
||||
{
|
||||
throw new WoxException(error);
|
||||
}
|
||||
#endif
|
||||
return null;
|
||||
}
|
||||
|
||||
return metadata;
|
||||
}
|
||||
|
||||
///// <summary>
|
||||
///// Change python execute file name to unique file name using GUID
|
||||
///// this is because if two pythong plugin use the same
|
||||
///// </summary>
|
||||
///// <param name="metadata"></param>
|
||||
///// <returns></returns>
|
||||
//private static PluginMetadata filterPythonMetadata(PluginMetadata metadata)
|
||||
//{
|
||||
|
||||
//}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user