Merge pull request #464 from Flow-Launcher/FixPythonEscapeIssue

Use JsonLibrary to Parse object, and replace ProcessStartInfo.Arguments with ArgumentList
This commit is contained in:
Jeremy Wu 2021-06-07 19:56:35 +10:00 committed by GitHub
commit c7358f0f4f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 29 deletions

View file

@ -17,6 +17,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Serialization;
using Flow.Launcher.Plugin;
using System.Text.Json;
namespace Flow.Launcher.Core.Plugin
{
@ -59,6 +60,8 @@ namespace Flow.Launcher.Core.Plugin
public override string ToString()
{
return JsonSerializer.Serialize(this);
string rpc = string.Empty;
if (Parameters != null && Parameters.Length > 0)
{
@ -78,9 +81,9 @@ namespace Flow.Launcher.Core.Plugin
=> parameter switch
{
null => "null",
string _ => $@"\""{ReplaceEscapes(parameter.ToString())}\""",
bool _ => $@"{parameter.ToString().ToLower()}",
_ => parameter.ToString()
string p => $@"\""{ReplaceEscapes(p)}\""",
bool p => $@"{p.ToString().ToLower()}",
_ => $@"\""{ReplaceEscapes(parameter.ToString())}\"""
};
@ -97,11 +100,7 @@ namespace Flow.Launcher.Core.Plugin
/// </summary>
public class JsonRPCServerRequestModel : JsonRPCRequestModel
{
public override string ToString()
{
string rpc = base.ToString();
return rpc + "}";
}
}
/// <summary>
@ -110,12 +109,6 @@ namespace Flow.Launcher.Core.Plugin
public class JsonRPCClientRequestModel : JsonRPCRequestModel
{
public bool DontHideAfterAction { get; set; }
public override string ToString()
{
string rpc = base.ToString();
return rpc + "}";
}
}
/// <summary>

View file

@ -46,7 +46,6 @@ namespace Flow.Launcher.Core.Plugin
}
private async Task<List<Result>> DeserializedResultAsync(Stream output)
{
if (output == Stream.Null) return null;
@ -71,7 +70,7 @@ namespace Flow.Launcher.Core.Plugin
var results = new List<Result>();
if (queryResponseModel.Result == null) return null;
if(!string.IsNullOrEmpty(queryResponseModel.DebugMessage))
if (!string.IsNullOrEmpty(queryResponseModel.DebugMessage))
{
context.API.ShowMsg(queryResponseModel.DebugMessage);
}
@ -92,6 +91,8 @@ namespace Flow.Launcher.Core.Plugin
else
{
string actionReponse = ExecuteCallback(result.JsonRPCAction);
if (string.IsNullOrEmpty(actionReponse))
return false;
JsonRPCRequestModel jsonRpcRequestModel =
JsonSerializer.Deserialize<JsonRPCRequestModel>(actionReponse);
if (jsonRpcRequestModel != null
@ -177,12 +178,11 @@ namespace Flow.Launcher.Core.Plugin
if (result.StartsWith("DEBUG:"))
{
MessageBox.Show(new Form { TopMost = true }, result.Substring(6));
MessageBox.Show(new Form {TopMost = true}, result.Substring(6));
return string.Empty;
}
return result;
}
catch (Exception e)
{
@ -248,10 +248,10 @@ namespace Flow.Launcher.Core.Plugin
}
}
public Task InitAsync(PluginInitContext context)
public virtual Task InitAsync(PluginInitContext context)
{
this.context = context;
return Task.CompletedTask;
}
}
}
}

View file

@ -28,17 +28,19 @@ namespace Flow.Launcher.Core.Plugin
var path = Path.Combine(Constant.ProgramDirectory, JsonRPC);
_startInfo.EnvironmentVariables["PYTHONPATH"] = path;
//Add -B flag to tell python don't write .py[co] files. Because .pyc contains location infos which will prevent python portable
_startInfo.ArgumentList.Add("-B");
}
protected override Task<Stream> ExecuteQueryAsync(Query query, CancellationToken token)
{
JsonRPCServerRequestModel request = new JsonRPCServerRequestModel
{
Method = "query",
Parameters = new object[] { query.Search },
Method = "query", Parameters = new object[] {query.Search},
};
//Add -B flag to tell python don't write .py[co] files. Because .pyc contains location infos which will prevent python portable
_startInfo.Arguments = $"-B \"{context.CurrentPluginMetadata.ExecuteFilePath}\" \"{request}\"";
_startInfo.ArgumentList[2] = request.ToString();
// todo happlebao why context can't be used in constructor
_startInfo.WorkingDirectory = context.CurrentPluginMetadata.PluginDirectory;
@ -47,16 +49,17 @@ namespace Flow.Launcher.Core.Plugin
protected override string ExecuteCallback(JsonRPCRequestModel rpcRequest)
{
_startInfo.Arguments = $"-B \"{context.CurrentPluginMetadata.ExecuteFilePath}\" \"{rpcRequest}\"";
_startInfo.ArgumentList[2] = rpcRequest.ToString();
_startInfo.WorkingDirectory = context.CurrentPluginMetadata.PluginDirectory;
// TODO: Async Action
return Execute(_startInfo);
}
protected override string ExecuteContextMenu(Result selectedResult) {
JsonRPCServerRequestModel request = new JsonRPCServerRequestModel {
Method = "context_menu",
Parameters = new object[] { selectedResult.ContextData },
protected override string ExecuteContextMenu(Result selectedResult)
{
JsonRPCServerRequestModel request = new JsonRPCServerRequestModel
{
Method = "context_menu", Parameters = new object[] {selectedResult.ContextData},
};
_startInfo.Arguments = $"-B \"{context.CurrentPluginMetadata.ExecuteFilePath}\" \"{request}\"";
_startInfo.WorkingDirectory = context.CurrentPluginMetadata.PluginDirectory;
@ -64,5 +67,13 @@ namespace Flow.Launcher.Core.Plugin
// TODO: Async Action
return Execute(_startInfo);
}
public override Task InitAsync(PluginInitContext context)
{
this.context = context;
_startInfo.ArgumentList.Add(context.CurrentPluginMetadata.ExecuteFilePath);
_startInfo.ArgumentList.Add("");
return Task.CompletedTask;
}
}
}