mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-06 03:07:04 +02:00
Refactoring [WIP]
This commit is contained in:
@@ -2,7 +2,6 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using Python.Runtime;
|
||||
using Wox.Helper;
|
||||
using Wox.Infrastructure.Storage.UserSettings;
|
||||
using Wox.Plugin;
|
||||
@@ -12,9 +11,6 @@ namespace Wox.Commands
|
||||
{
|
||||
public class PluginCommand : BaseCommand
|
||||
{
|
||||
private string currentPythonModulePath = string.Empty;
|
||||
private IntPtr GIL;
|
||||
|
||||
public override void Dispatch(Query query)
|
||||
{
|
||||
PluginPair thirdPlugin = Plugins.AllPlugins.FirstOrDefault(o => o.Metadata.ActionKeyword == query.ActionName);
|
||||
@@ -28,10 +24,6 @@ namespace Wox.Commands
|
||||
return;
|
||||
}
|
||||
|
||||
//if (thirdPlugin.Metadata.Language == AllowedLanguage.Python)
|
||||
//{
|
||||
// SwitchPythonEnv(thirdPlugin);
|
||||
//}
|
||||
ThreadPool.QueueUserWorkItem(t =>
|
||||
{
|
||||
try
|
||||
@@ -52,24 +44,5 @@ namespace Wox.Commands
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private void SwitchPythonEnv(PluginPair thirdPlugin)
|
||||
{
|
||||
if (currentPythonModulePath != thirdPlugin.Metadata.PluginDirecotry)
|
||||
{
|
||||
currentPythonModulePath = thirdPlugin.Metadata.PluginDirecotry;
|
||||
|
||||
if (GIL != IntPtr.Zero)
|
||||
{
|
||||
Runtime.PyEval_RestoreThread(GIL);
|
||||
PythonEngine.Shutdown();
|
||||
}
|
||||
PythonEngine.Initialize();
|
||||
IntPtr pyStrPtr = Runtime.PyString_FromString(thirdPlugin.Metadata.PluginDirecotry);
|
||||
IntPtr sysDotPath = Runtime.PySys_GetObject("path");
|
||||
Runtime.PyList_Append(sysDotPath, pyStrPtr);
|
||||
GIL = PythonEngine.BeginAllowThreads();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,9 +7,9 @@ using Wox.RPC;
|
||||
|
||||
namespace Wox.PluginLoader
|
||||
{
|
||||
public class BasePluginLoader<T> where T :BasePluginWrapper,new()
|
||||
public class BasePluginLoader<T> : IPluginLoader where T : BasePluginWrapper, new()
|
||||
{
|
||||
public List<PluginPair> LoadPlugin(List<PluginMetadata> pluginMetadatas)
|
||||
public virtual List<PluginPair> LoadPlugin(List<PluginMetadata> pluginMetadatas)
|
||||
{
|
||||
List<PluginPair> plugins = new List<PluginPair>();
|
||||
|
||||
|
||||
@@ -15,33 +15,27 @@ namespace Wox.PluginLoader
|
||||
protected PluginInitContext context;
|
||||
|
||||
public abstract List<string> GetAllowedLanguages();
|
||||
protected abstract string GetFileName();
|
||||
|
||||
protected abstract string GetQueryArguments(Query query);
|
||||
|
||||
protected abstract string GetActionJsonRPCArguments(ActionJsonRPCResult result);
|
||||
protected abstract string ExecuteQuery(Query query);
|
||||
protected abstract string ExecuteAction(string rpcRequest);
|
||||
|
||||
public List<Result> Query(Query query)
|
||||
{
|
||||
string fileName = GetFileName();
|
||||
string arguments = GetQueryArguments(query);
|
||||
string output = Execute(fileName, arguments);
|
||||
string output = ExecuteQuery(query);
|
||||
if (!string.IsNullOrEmpty(output))
|
||||
{
|
||||
try
|
||||
{
|
||||
JsonPRCModel rpc = JsonConvert.DeserializeObject<JsonPRCModel>(output);
|
||||
List<ActionJsonRPCResult> rpcresults =
|
||||
JsonConvert.DeserializeObject<List<ActionJsonRPCResult>>(rpc.result);
|
||||
List<Result> results = new List<Result>();
|
||||
foreach (ActionJsonRPCResult result in rpcresults)
|
||||
|
||||
JsonRPCQueryResponseModel queryResponseModel = JsonConvert.DeserializeObject<JsonRPCQueryResponseModel>(output);
|
||||
foreach (JsonRPCResult result in queryResponseModel.QueryResults)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(result.ActionJSONRPC))
|
||||
if (result.JSONRPCActionModel != null)
|
||||
{
|
||||
ActionJsonRPCResult resultCopy = result;
|
||||
result.Action = (c) =>
|
||||
{
|
||||
Execute(fileName, GetActionJsonRPCArguments(resultCopy));
|
||||
ExecuteAction(result.JSONRPCAction);
|
||||
return true;
|
||||
};
|
||||
}
|
||||
@@ -56,7 +50,13 @@ namespace Wox.PluginLoader
|
||||
return null;
|
||||
}
|
||||
|
||||
private string Execute(string fileName, string arguments)
|
||||
/// <summary>
|
||||
/// Execute external program and return the output
|
||||
/// </summary>
|
||||
/// <param name="fileName"></param>
|
||||
/// <param name="arguments"></param>
|
||||
/// <returns></returns>
|
||||
protected string Execute(string fileName, string arguments)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
@@ -8,7 +8,7 @@ using Wox.Plugin.SystemPlugins;
|
||||
|
||||
namespace Wox.PluginLoader {
|
||||
|
||||
public class CSharpPluginConfigLoader
|
||||
public class CSharpPluginConfigLoader : IPluginLoader
|
||||
{
|
||||
public List<PluginPair> LoadPlugin(List<PluginMetadata> pluginMetadatas)
|
||||
{
|
||||
|
||||
13
Wox/PluginLoader/IPluginLoader.cs
Normal file
13
Wox/PluginLoader/IPluginLoader.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Wox.Plugin;
|
||||
|
||||
namespace Wox.PluginLoader
|
||||
{
|
||||
public interface IPluginLoader
|
||||
{
|
||||
List<PluginPair> LoadPlugin(List<PluginMetadata> pluginMetadatas);
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using Python.Runtime;
|
||||
using Wox.Plugin;
|
||||
using Wox.Helper;
|
||||
using Wox.RPC;
|
||||
|
||||
namespace Wox.PluginLoader
|
||||
@@ -22,22 +17,20 @@ namespace Wox.PluginLoader
|
||||
};
|
||||
}
|
||||
|
||||
protected override string GetFileName()
|
||||
protected override string ExecuteQuery(Query query)
|
||||
{
|
||||
return Path.Combine(woxDirectory, "PYTHONTHOME\\Scripts\\python.exe");
|
||||
string fileName = Path.Combine(woxDirectory, "PYTHONTHOME\\Scripts\\python.exe");
|
||||
string parameters = string.Format("{0} \"{1}\"", context.CurrentPluginMetadata.ExecuteFilePath,
|
||||
JsonRPC.Send("query", query.GetAllRemainingParameter()));
|
||||
return Execute(fileName, parameters);
|
||||
}
|
||||
|
||||
protected override string GetQueryArguments(Query query)
|
||||
protected override string ExecuteAction(string rpcRequest)
|
||||
{
|
||||
return string.Format("{0} \"{1}\"",
|
||||
context.CurrentPluginMetadata.ExecuteFilePath,
|
||||
JsonRPC.GetRPC("query", query.GetAllRemainingParameter()));
|
||||
}
|
||||
|
||||
protected override string GetActionJsonRPCArguments(ActionJsonRPCResult result)
|
||||
{
|
||||
return string.Format("{0} \"{1}\"", context.CurrentPluginMetadata.ExecuteFilePath,
|
||||
result.ActionJSONRPC);
|
||||
string fileName = Path.Combine(woxDirectory, "PYTHONTHOME\\Scripts\\python.exe");
|
||||
string parameters = string.Format("{0} \"{1}\"", context.CurrentPluginMetadata.ExecuteFilePath,
|
||||
rpcRequest);
|
||||
return Execute(fileName, parameters);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,17 +1,62 @@
|
||||
using Wox.Plugin;
|
||||
using System.Collections.Generic;
|
||||
using System.Windows.Documents;
|
||||
using Newtonsoft.Json;
|
||||
using Wox.Plugin;
|
||||
|
||||
namespace Wox.RPC
|
||||
{
|
||||
public class JsonPRCModel
|
||||
public class JsonRPCErrorModel
|
||||
{
|
||||
public int id { get; set; }
|
||||
public string jsonrpc { get; set; }
|
||||
public int Code { get; set; }
|
||||
|
||||
public string result { get; set; }
|
||||
public string Message { get; set; }
|
||||
|
||||
public string Data { get; set; }
|
||||
}
|
||||
|
||||
public class ActionJsonRPCResult : Result
|
||||
public class JsonRPCModelBase
|
||||
{
|
||||
public string ActionJSONRPC { get; set; }
|
||||
public int Id { get; set; }
|
||||
|
||||
public string JsonRPC { get; set; }
|
||||
}
|
||||
|
||||
public class JsonRPCResponseModel : JsonRPCModelBase
|
||||
{
|
||||
public string Result { get; set; }
|
||||
|
||||
public JsonRPCErrorModel Error { get; set; }
|
||||
}
|
||||
|
||||
public class JsonRPCQueryResponseModel : JsonRPCResponseModel
|
||||
{
|
||||
public List<JsonRPCResult> QueryResults
|
||||
{
|
||||
get
|
||||
{
|
||||
return JsonConvert.DeserializeObject<List<JsonRPCResult>>(Result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class JsonRPCRequestModel : JsonRPCModelBase
|
||||
{
|
||||
public string Method { get; set; }
|
||||
|
||||
/*
|
||||
* 1. c# can't use params as the variable name
|
||||
* 2. all prarmeter should be string type
|
||||
*/
|
||||
public List<string> Parameters { get; set; }
|
||||
}
|
||||
|
||||
public class JsonRPCResult : Result
|
||||
{
|
||||
public string JSONRPCAction { get; set; }
|
||||
|
||||
public JsonRPCRequestModel JSONRPCActionModel
|
||||
{
|
||||
get { return null; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,16 +5,16 @@ namespace Wox.RPC
|
||||
{
|
||||
public class JsonRPC
|
||||
{
|
||||
public static string GetRPC(string method, List<string> paras)
|
||||
public static string Send(string method, List<string> paras)
|
||||
{
|
||||
var list = paras.Select(s => string.Format(@"\""{0}\""", s));
|
||||
return string.Format(@"{{\""jsonrpc\"": \""2.0\"",\""method\"": \""{0}\"", \""params\"": [{1}], \""id\"": 1}}",
|
||||
method, string.Join(",", list.ToArray()));
|
||||
}
|
||||
|
||||
public static string GetRPC(string method, string para)
|
||||
public static string Send(string method, string para)
|
||||
{
|
||||
return GetRPC(method, new List<string>() { para });
|
||||
return Send(method, new List<string>() { para });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -138,6 +138,7 @@
|
||||
<DependentUpon>HotkeyControl.xaml</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Converters\ImagePathConverter.cs" />
|
||||
<Compile Include="PluginLoader\IPluginLoader.cs" />
|
||||
<Compile Include="RPC\JsonRPC.cs" />
|
||||
<Compile Include="Msg.xaml.cs">
|
||||
<DependentUpon>Msg.xaml</DependentUpon>
|
||||
@@ -263,10 +264,6 @@
|
||||
<AppDesigner Include="Properties\" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Pythonnet.Runtime\Python.Runtime.csproj">
|
||||
<Project>{097b4ac0-74e9-4c58-bcf8-c69746ec8271}</Project>
|
||||
<Name>Python.Runtime</Name>
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="..\Wox.Infrastructure\Wox.Infrastructure.csproj">
|
||||
<Project>{4fd29318-a8ab-4d8f-aa47-60bc241b8da3}</Project>
|
||||
<Name>Wox.Infrastructure</Name>
|
||||
|
||||
Reference in New Issue
Block a user