FxCopAnalyzer fix for Wox.Core (PR#2of3) - Removing unused JsonRPC files (#7211)

* Removed unused JsonRPC related files and references

- Files Removed: ExecutablePlugin.cs, JsonRPCClientRequestModel.cs, JsonRPCErrorModel.cs, JsonRPCModelBase.cs, JsonRPCPlugin.cs, JsonRPCQueryResponseModel.cs, JsonRPCRequestModel.cs, JsonRPCResponseModel.cs, JsonRPCResult.cs, JsonRPCServerRequestModel.cs
- Modified PluginsLoader.cs:
	- Removed method ExecutablePlugins
	- In method Plugins(): Removed call to ExecutablePlugins()

* Suppressed warning for catching general exception type

* Addressing comments: Replaced in source suppression with attribute and logged exception information
This commit is contained in:
Avneet Kaur
2020-10-19 19:48:51 -07:00
committed by GitHub
parent 280d1907d8
commit 701ccfdbcc
12 changed files with 6 additions and 588 deletions

View File

@@ -1,60 +0,0 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Diagnostics;
using Wox.Plugin;
namespace Wox.Core.Plugin
{
internal class ExecutablePlugin : JsonRPCPlugin
{
private readonly ProcessStartInfo _startInfo;
public override string SupportedLanguage { get; set; } = AllowedLanguage.Executable;
public ExecutablePlugin(string filename)
{
_startInfo = new ProcessStartInfo
{
FileName = filename,
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
};
}
protected override string ExecuteQuery(Query query)
{
JsonRPCServerRequestModel request = new JsonRPCServerRequestModel
{
Method = "query",
Parameters = new object[] { query.Search },
};
_startInfo.Arguments = $"\"{request}\"";
return Execute(_startInfo);
}
protected override string ExecuteCallback(JsonRPCRequestModel rpcRequest)
{
_startInfo.Arguments = $"\"{rpcRequest}\"";
return Execute(_startInfo);
}
protected override string ExecuteContextMenu(Result selectedResult)
{
JsonRPCServerRequestModel request = new JsonRPCServerRequestModel
{
Method = "contextmenu",
Parameters = new object[] { selectedResult.ContextData },
};
_startInfo.Arguments = $"\"{request}\"";
return Execute(_startInfo);
}
}
}

View File

@@ -1,34 +0,0 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
/* We basically follow the Json-RPC 2.0 spec (http://www.jsonrpc.org/specification) to invoke methods between Wox and other plugins,
* like python or other self-execute program. But, we added additional infos (proxy and so on) into rpc request. Also, we didn't use the
* "id" and "jsonrpc" in the request, since it's not so useful in our request model.
*
* When execute a query:
* Wox -------JsonRPCServerRequestModel--------> client
* Wox <------JsonRPCQueryResponseModel--------- client
*
* When execute a action (which mean user select an item in reulst item):
* Wox -------JsonRPCServerRequestModel--------> client
* Wox <------JsonRPCResponseModel-------------- client
*
*/
namespace Wox.Core.Plugin
{
/// <summary>
/// Json RPC Request(in query response) that client sent to Wox
/// </summary>
public class JsonRPCClientRequestModel : JsonRPCRequestModel
{
public bool DontHideAfterAction { get; set; }
public override string ToString()
{
string rpc = base.ToString();
return rpc + "}";
}
}
}

View File

@@ -1,29 +0,0 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
/* We basically follow the Json-RPC 2.0 spec (http://www.jsonrpc.org/specification) to invoke methods between Wox and other plugins,
* like python or other self-execute program. But, we added additional infos (proxy and so on) into rpc request. Also, we didn't use the
* "id" and "jsonrpc" in the request, since it's not so useful in our request model.
*
* When execute a query:
* Wox -------JsonRPCServerRequestModel--------> client
* Wox <------JsonRPCQueryResponseModel--------- client
*
* When execute a action (which mean user select an item in reulst item):
* Wox -------JsonRPCServerRequestModel--------> client
* Wox <------JsonRPCResponseModel-------------- client
*
*/
namespace Wox.Core.Plugin
{
public class JsonRPCErrorModel
{
public int Code { get; set; }
public string Message { get; set; }
public string Data { get; set; }
}
}

View File

@@ -1,25 +0,0 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
/* We basically follow the Json-RPC 2.0 spec (http://www.jsonrpc.org/specification) to invoke methods between Wox and other plugins,
* like python or other self-execute program. But, we added additional infos (proxy and so on) into rpc request. Also, we didn't use the
* "id" and "jsonrpc" in the request, since it's not so useful in our request model.
*
* When execute a query:
* Wox -------JsonRPCServerRequestModel--------> client
* Wox <------JsonRPCQueryResponseModel--------- client
*
* When execute a action (which mean user select an item in reulst item):
* Wox -------JsonRPCServerRequestModel--------> client
* Wox <------JsonRPCResponseModel-------------- client
*
*/
namespace Wox.Core.Plugin
{
public class JsonRPCModelBase
{
public int Id { get; set; }
}
}

View File

@@ -1,227 +0,0 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Windows.Forms;
using Newtonsoft.Json;
using Wox.Infrastructure.Logger;
using Wox.Plugin;
namespace Wox.Core.Plugin
{
/// <summary>
/// Represent the plugin that using JsonPRC
/// every JsonRPC plugin should has its own plugin instance
/// </summary>
internal abstract class JsonRPCPlugin : IPlugin, IContextMenu
{
protected PluginInitContext Context { get; set; }
public const string JsonRPC = "JsonRPC";
/// <summary>
/// Gets or sets the language this JsonRPCPlugin support
/// </summary>
public abstract string SupportedLanguage { get; set; }
protected abstract string ExecuteQuery(Query query);
protected abstract string ExecuteCallback(JsonRPCRequestModel rpcRequest);
protected abstract string ExecuteContextMenu(Result selectedResult);
public List<Result> Query(Query query)
{
string output = ExecuteQuery(query);
try
{
return DeserializedResult(output);
}
catch (Exception e)
{
Log.Exception($"Exception when query <{query}>", e, GetType());
return null;
}
}
public List<ContextMenuResult> LoadContextMenus(Result selectedResult)
{
string output = ExecuteContextMenu(selectedResult);
try
{
// This should not hit. If it does it's because Wox shares the same interface for querying context menu items as well as search results. In this case please file a bug.
// To my knowledge we aren't supporting this JSonRPC commands in Launcher, and am not able to repro this, but I will leave this here for the time being in case I'm proven wrong.
// We should remove this, or identify and test officially supported use cases and Deserialize this properly.
// return DeserializedResult(output);
throw new NotImplementedException();
}
catch (Exception e)
{
Log.Exception($"THIS IS A BUG - Exception on result <{selectedResult}>", e, GetType());
return null;
}
}
private List<Result> DeserializedResult(string output)
{
if (!string.IsNullOrEmpty(output))
{
List<Result> results = new List<Result>();
JsonRPCQueryResponseModel queryResponseModel = JsonConvert.DeserializeObject<JsonRPCQueryResponseModel>(output);
if (queryResponseModel.Result == null)
{
return null;
}
foreach (JsonRPCResult result in queryResponseModel.Result)
{
JsonRPCResult result1 = result;
result.Action = c =>
{
if (result1.JsonRPCAction == null)
{
return false;
}
if (!string.IsNullOrEmpty(result1.JsonRPCAction.Method))
{
if (result1.JsonRPCAction.Method.StartsWith("Wox."))
{
ExecuteWoxAPI(result1.JsonRPCAction.Method.Substring(4), result1.JsonRPCAction.Parameters);
}
else
{
string actionResponse = ExecuteCallback(result1.JsonRPCAction);
JsonRPCRequestModel jsonRpcRequestModel = JsonConvert.DeserializeObject<JsonRPCRequestModel>(actionResponse);
if (jsonRpcRequestModel != null
&& !string.IsNullOrEmpty(jsonRpcRequestModel.Method)
&& jsonRpcRequestModel.Method.StartsWith("Wox."))
{
ExecuteWoxAPI(jsonRpcRequestModel.Method.Substring(4), jsonRpcRequestModel.Parameters);
}
}
}
return !result1.JsonRPCAction.DontHideAfterAction;
};
results.Add(result);
}
return results;
}
else
{
return null;
}
}
private void ExecuteWoxAPI(string method, object[] parameters)
{
MethodInfo methodInfo = PluginManager.API.GetType().GetMethod(method);
if (methodInfo != null)
{
try
{
methodInfo.Invoke(PluginManager.API, parameters);
}
catch (Exception)
{
#if DEBUG
{
throw;
}
#endif
}
}
}
/// <summary>
/// Execute external program and return the output
/// </summary>
/// <param name="fileName">file to execute</param>
/// <param name="arguments">args to pass in to that exe</param>
/// <returns>results</returns>
protected string Execute(string fileName, string arguments)
{
ProcessStartInfo start = new ProcessStartInfo
{
FileName = fileName,
Arguments = arguments,
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
};
return Execute(start);
}
protected string Execute(ProcessStartInfo startInfo)
{
try
{
using (var process = Process.Start(startInfo))
{
if (process != null)
{
using (var standardOutput = process.StandardOutput)
{
var result = standardOutput.ReadToEnd();
if (string.IsNullOrEmpty(result))
{
using (var standardError = process.StandardError)
{
var error = standardError.ReadToEnd();
if (!string.IsNullOrEmpty(error))
{
Log.Error(error, GetType());
return string.Empty;
}
else
{
Log.Error("Empty standard output and standard error.", GetType());
return string.Empty;
}
}
}
else if (result.StartsWith("DEBUG:"))
{
MessageBox.Show(new Form { TopMost = true }, result.Substring(6));
return string.Empty;
}
else
{
return result;
}
}
}
else
{
Log.Error("Can't start new process", GetType());
return string.Empty;
}
}
}
catch (Exception e)
{
Log.Exception($"Exception for filename <{startInfo.FileName}> with argument <{startInfo.Arguments}>", e, GetType());
return string.Empty;
}
}
public void Init(PluginInitContext ctx)
{
Context = ctx;
}
}
}

View File

@@ -1,27 +0,0 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
/* We basically follow the Json-RPC 2.0 spec (http://www.jsonrpc.org/specification) to invoke methods between Wox and other plugins,
* like python or other self-execute program. But, we added additional infos (proxy and so on) into rpc request. Also, we didn't use the
* "id" and "jsonrpc" in the request, since it's not so useful in our request model.
*
* When execute a query:
* Wox -------JsonRPCServerRequestModel--------> client
* Wox <------JsonRPCQueryResponseModel--------- client
*
* When execute a action (which mean user select an item in reulst item):
* Wox -------JsonRPCServerRequestModel--------> client
* Wox <------JsonRPCResponseModel-------------- client
*
*/
using System.Collections.Generic;
namespace Wox.Core.Plugin
{
public class JsonRPCQueryResponseModel : JsonRPCResponseModel
{
public new List<JsonRPCResult> Result { get; set; }
}
}

View File

@@ -1,78 +0,0 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
/* We basically follow the Json-RPC 2.0 spec (http://www.jsonrpc.org/specification) to invoke methods between Wox and other plugins,
* like python or other self-execute program. But, we added additional infos (proxy and so on) into rpc request. Also, we didn't use the
* "id" and "jsonrpc" in the request, since it's not so useful in our request model.
*
* When execute a query:
* Wox -------JsonRPCServerRequestModel--------> client
* Wox <------JsonRPCQueryResponseModel--------- client
*
* When execute a action (which mean user select an item in reulst item):
* Wox -------JsonRPCServerRequestModel--------> client
* Wox <------JsonRPCResponseModel-------------- client
*
*/
using System.Linq;
namespace Wox.Core.Plugin
{
public class JsonRPCRequestModel : JsonRPCModelBase
{
public string Method { get; set; }
public object[] Parameters { get; set; }
public override string ToString()
{
string rpc = string.Empty;
if (Parameters != null && Parameters.Length > 0)
{
string parameters = Parameters.Aggregate("[", (current, o) => current + (GetParameterByType(o) + ","));
parameters = parameters.Substring(0, parameters.Length - 1) + "]";
rpc = string.Format(@"{{\""method\"":\""{0}\"",\""parameters\"":{1}", Method, parameters);
}
else
{
rpc = string.Format(@"{{\""method\"":\""{0}\"",\""parameters\"":[]", Method);
}
return rpc;
}
private string GetParameterByType(object parameter)
{
if (parameter == null)
{
return "null";
}
if (parameter is string)
{
return string.Format(@"\""{0}\""", ReplaceEscapes(parameter.ToString()));
}
if (parameter is int || parameter is float || parameter is double)
{
return string.Format(@"{0}", parameter);
}
if (parameter is bool)
{
return string.Format(@"{0}", parameter.ToString().ToLower());
}
return parameter.ToString();
}
private string ReplaceEscapes(string str)
{
return str.Replace(@"\", @"\\") // Escapes in ProcessStartInfo
.Replace(@"\", @"\\") // Escapes itself when passed to client
.Replace(@"""", @"\\""""");
}
}
}

View File

@@ -1,27 +0,0 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
/* We basically follow the Json-RPC 2.0 spec (http://www.jsonrpc.org/specification) to invoke methods between Wox and other plugins,
* like python or other self-execute program. But, we added additional infos (proxy and so on) into rpc request. Also, we didn't use the
* "id" and "jsonrpc" in the request, since it's not so useful in our request model.
*
* When execute a query:
* Wox -------JsonRPCServerRequestModel--------> client
* Wox <------JsonRPCQueryResponseModel--------- client
*
* When execute a action (which mean user select an item in reulst item):
* Wox -------JsonRPCServerRequestModel--------> client
* Wox <------JsonRPCResponseModel-------------- client
*
*/
namespace Wox.Core.Plugin
{
public class JsonRPCResponseModel : JsonRPCModelBase
{
public string Result { get; set; }
public JsonRPCErrorModel Error { get; set; }
}
}

View File

@@ -1,32 +0,0 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
/* We basically follow the Json-RPC 2.0 spec (http://www.jsonrpc.org/specification) to invoke methods between Wox and other plugins,
* like python or other self-execute program. But, we added additional infos (proxy and so on) into rpc request. Also, we didn't use the
* "id" and "jsonrpc" in the request, since it's not so useful in our request model.
*
* When execute a query:
* Wox -------JsonRPCServerRequestModel--------> client
* Wox <------JsonRPCQueryResponseModel--------- client
*
* When execute a action (which mean user select an item in reulst item):
* Wox -------JsonRPCServerRequestModel--------> client
* Wox <------JsonRPCResponseModel-------------- client
*
*/
using Wox.Plugin;
namespace Wox.Core.Plugin
{
/// <summary>
/// Represent the json-rpc result item that client send to Wox
/// Typically, we will send back this request model to client after user select the result item
/// But if the request method starts with "Wox.", we will invoke the public APIs we expose.
/// </summary>
public class JsonRPCResult : Result
{
public JsonRPCClientRequestModel JsonRPCAction { get; set; }
}
}

View File

@@ -1,32 +0,0 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
/* We basically follow the Json-RPC 2.0 spec (http://www.jsonrpc.org/specification) to invoke methods between Wox and other plugins,
* like python or other self-execute program. But, we added additional infos (proxy and so on) into rpc request. Also, we didn't use the
* "id" and "jsonrpc" in the request, since it's not so useful in our request model.
*
* When execute a query:
* Wox -------JsonRPCServerRequestModel--------> client
* Wox <------JsonRPCQueryResponseModel--------- client
*
* When execute a action (which mean user select an item in reulst item):
* Wox -------JsonRPCServerRequestModel--------> client
* Wox <------JsonRPCResponseModel-------------- client
*
*/
namespace Wox.Core.Plugin
{
/// <summary>
/// Json RPC Request that Wox sent to client
/// </summary>
public class JsonRPCServerRequestModel : JsonRPCRequestModel
{
public override string ToString()
{
string rpc = base.ToString();
return rpc + "}";
}
}
}

View File

@@ -4,9 +4,11 @@
using System;
using System.IO;
using System.Reflection;
using System.Windows;
using ICSharpCode.SharpZipLib.Zip;
using Newtonsoft.Json;
using Wox.Infrastructure.Logger;
using Wox.Plugin;
namespace Wox.Core.Plugin
@@ -96,6 +98,7 @@ namespace Wox.Core.Plugin
}
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "All exception information is being logged")]
private static PluginMetadata GetMetadataFromJson(string pluginDirectory)
{
string configPath = Path.Combine(pluginDirectory, "plugin.json");
@@ -111,9 +114,10 @@ namespace Wox.Core.Plugin
metadata = JsonConvert.DeserializeObject<PluginMetadata>(File.ReadAllText(configPath));
metadata.PluginDirectory = pluginDirectory;
}
catch (Exception)
catch (Exception e)
{
string error = $"Parse plugin config {configPath} failed: json format is not valid";
Log.Exception(error, e, MethodBase.GetCurrentMethod().DeclaringType);
#if DEBUG
{
throw new Exception(error);

View File

@@ -20,9 +20,7 @@ namespace Wox.Core.Plugin
public static List<PluginPair> Plugins(List<PluginMetadata> metadatas)
{
var csharpPlugins = CSharpPlugins(metadatas).ToList();
var executablePlugins = ExecutablePlugins(metadatas);
var plugins = csharpPlugins.Concat(executablePlugins).ToList();
return plugins;
return csharpPlugins;
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "All exception information is being logged")]
@@ -87,18 +85,5 @@ namespace Wox.Core.Plugin
return plugins;
}
public static IEnumerable<PluginPair> ExecutablePlugins(IEnumerable<PluginMetadata> source)
{
var metadatas = source.Where(o => o.Language.ToUpperInvariant() == AllowedLanguage.Executable);
var plugins = metadatas.Select(metadata => new PluginPair
{
Plugin = new ExecutablePlugin(metadata.ExecuteFilePath),
Metadata = metadata,
});
return plugins;
}
}
}