From f5b1b4f830eb423be029f6024530f80808ad8940 Mon Sep 17 00:00:00 2001 From: Hongtao Zhang Date: Fri, 3 Nov 2023 21:16:51 -0500 Subject: [PATCH] rename and add js/ts v2 --- .../Environments/JavaScriptV2Environment.cs | 14 ++++++ .../Environments/TypeScriptV2Environment.cs | 44 +++++++++++++++++++ .../Plugin/JsonRPCModelContext.cs | 20 --------- .../Plugin/JsonRPCPluginBase.cs | 4 +- ...leSettings.cs => JsonRPCPluginSettings.cs} | 2 +- Flow.Launcher.Core/Plugin/NodePlugin.cs | 8 ++-- Flow.Launcher.Core/Plugin/NodePluginV2.cs | 38 ++++++++++++++++ Flow.Launcher.Core/Plugin/PluginsLoader.cs | 40 ++++++++++++----- .../Plugin/ProcessStreamPluginV2.cs | 2 +- Flow.Launcher.Core/Plugin/PythonPluginV2.cs | 2 - Flow.Launcher.Plugin/AllowedLanguage.cs | 33 +++++++++++--- 11 files changed, 159 insertions(+), 48 deletions(-) create mode 100644 Flow.Launcher.Core/ExternalPlugins/Environments/JavaScriptV2Environment.cs create mode 100644 Flow.Launcher.Core/ExternalPlugins/Environments/TypeScriptV2Environment.cs delete mode 100644 Flow.Launcher.Core/Plugin/JsonRPCModelContext.cs rename Flow.Launcher.Core/Plugin/{PortableSettings.cs => JsonRPCPluginSettings.cs} (99%) create mode 100644 Flow.Launcher.Core/Plugin/NodePluginV2.cs diff --git a/Flow.Launcher.Core/ExternalPlugins/Environments/JavaScriptV2Environment.cs b/Flow.Launcher.Core/ExternalPlugins/Environments/JavaScriptV2Environment.cs new file mode 100644 index 000000000..6c8c5aa57 --- /dev/null +++ b/Flow.Launcher.Core/ExternalPlugins/Environments/JavaScriptV2Environment.cs @@ -0,0 +1,14 @@ +using System.Collections.Generic; +using Flow.Launcher.Infrastructure.UserSettings; +using Flow.Launcher.Plugin; + +namespace Flow.Launcher.Core.ExternalPlugins.Environments +{ + + internal class JavaScriptV2Environment : TypeScriptV2Environment + { + internal override string Language => AllowedLanguage.JavaScriptV2; + + internal JavaScriptV2Environment(List pluginMetadataList, PluginsSettings pluginSettings) : base(pluginMetadataList, pluginSettings) { } + } +} diff --git a/Flow.Launcher.Core/ExternalPlugins/Environments/TypeScriptV2Environment.cs b/Flow.Launcher.Core/ExternalPlugins/Environments/TypeScriptV2Environment.cs new file mode 100644 index 000000000..11ed94d3f --- /dev/null +++ b/Flow.Launcher.Core/ExternalPlugins/Environments/TypeScriptV2Environment.cs @@ -0,0 +1,44 @@ +using System.Collections.Generic; +using Droplex; +using Flow.Launcher.Infrastructure.UserSettings; +using Flow.Launcher.Plugin.SharedCommands; +using Flow.Launcher.Plugin; +using System.IO; +using Flow.Launcher.Core.Plugin; + +namespace Flow.Launcher.Core.ExternalPlugins.Environments +{ + internal class TypeScriptV2Environment : AbstractPluginEnvironment + { + internal override string Language => AllowedLanguage.TypeScriptV2; + + internal override string EnvName => DataLocation.NodeEnvironmentName; + + internal override string EnvPath => Path.Combine(DataLocation.PluginEnvironmentsPath, EnvName); + + internal override string InstallPath => Path.Combine(EnvPath, "Node-v16.18.0"); + internal override string ExecutablePath => Path.Combine(InstallPath, "node-v16.18.0-win-x64\\node.exe"); + + internal override string PluginsSettingsFilePath { get => PluginSettings.NodeExecutablePath; set => PluginSettings.NodeExecutablePath = value; } + + internal TypeScriptV2Environment(List pluginMetadataList, PluginsSettings pluginSettings) : base(pluginMetadataList, pluginSettings) { } + + internal override void InstallEnvironment() + { + FilesFolders.RemoveFolderIfExists(InstallPath); + + DroplexPackage.Drop(App.nodejs_16_18_0, InstallPath).Wait(); + + PluginsSettingsFilePath = ExecutablePath; + } + + internal override PluginPair CreatePluginPair(string filePath, PluginMetadata metadata) + { + return new PluginPair + { + Plugin = new NodePluginV2(filePath), + Metadata = metadata + }; + } + } +} diff --git a/Flow.Launcher.Core/Plugin/JsonRPCModelContext.cs b/Flow.Launcher.Core/Plugin/JsonRPCModelContext.cs deleted file mode 100644 index b84801578..000000000 --- a/Flow.Launcher.Core/Plugin/JsonRPCModelContext.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System.Text.Json.Serialization; - -namespace Flow.Launcher.Core.Plugin -{ - - [JsonSerializable(typeof(JsonRPCQueryResponseModel))] - public partial class JsonRPCQueryResponseModelContext : JsonSerializerContext - { - } - - [JsonSerializable(typeof(JsonRPCRequestModel))] - public partial class JsonRPCRequestModelContext : JsonSerializerContext - { - } - - [JsonSerializable(typeof(JsonRPCClientRequestModel))] - public partial class JsonRPCClientRequestModelContext : JsonSerializerContext - { - } -} diff --git a/Flow.Launcher.Core/Plugin/JsonRPCPluginBase.cs b/Flow.Launcher.Core/Plugin/JsonRPCPluginBase.cs index 18f787018..330120c12 100644 --- a/Flow.Launcher.Core/Plugin/JsonRPCPluginBase.cs +++ b/Flow.Launcher.Core/Plugin/JsonRPCPluginBase.cs @@ -69,7 +69,7 @@ namespace Flow.Launcher.Core.Plugin protected abstract Task ExecuteResultAsync(JsonRPCResult result); - protected PortableSettings Settings { get; set; } + protected JsonRPCPluginSettings Settings { get; set; } protected List ParseResults(JsonRPCQueryResponseModel queryResponseModel) { @@ -135,7 +135,7 @@ namespace Flow.Launcher.Core.Plugin deserializer.Deserialize( await File.ReadAllTextAsync(SettingConfigurationPath)); - Settings ??= new PortableSettings + Settings ??= new JsonRPCPluginSettings { Configuration = configuration, SettingPath = SettingPath, API = Context.API }; diff --git a/Flow.Launcher.Core/Plugin/PortableSettings.cs b/Flow.Launcher.Core/Plugin/JsonRPCPluginSettings.cs similarity index 99% rename from Flow.Launcher.Core/Plugin/PortableSettings.cs rename to Flow.Launcher.Core/Plugin/JsonRPCPluginSettings.cs index 542460877..b87623c56 100644 --- a/Flow.Launcher.Core/Plugin/PortableSettings.cs +++ b/Flow.Launcher.Core/Plugin/JsonRPCPluginSettings.cs @@ -8,7 +8,7 @@ using Flow.Launcher.Plugin; namespace Flow.Launcher.Core.Plugin { - public class PortableSettings + public class JsonRPCPluginSettings { public required JsonRpcConfigurationModel Configuration { get; init; } diff --git a/Flow.Launcher.Core/Plugin/NodePlugin.cs b/Flow.Launcher.Core/Plugin/NodePlugin.cs index 8ea5c4b78..40eb057cb 100644 --- a/Flow.Launcher.Core/Plugin/NodePlugin.cs +++ b/Flow.Launcher.Core/Plugin/NodePlugin.cs @@ -1,5 +1,6 @@ using System.Diagnostics; using System.IO; +using System.Text.Json; using System.Threading; using System.Threading.Tasks; using Flow.Launcher.Plugin; @@ -27,23 +28,22 @@ namespace Flow.Launcher.Core.Plugin protected override Task RequestAsync(JsonRPCRequestModel request, CancellationToken token = default) { - _startInfo.ArgumentList[1] = request.ToString(); + _startInfo.ArgumentList[1] = JsonSerializer.Serialize(request); return ExecuteAsync(_startInfo, token); } protected override string Request(JsonRPCRequestModel rpcRequest, CancellationToken token = default) { // since this is not static, request strings will build up in ArgumentList if index is not specified - _startInfo.ArgumentList[1] = rpcRequest.ToString(); + _startInfo.ArgumentList[1] = JsonSerializer.Serialize(rpcRequest); return Execute(_startInfo); } public override async Task InitAsync(PluginInitContext context) { _startInfo.ArgumentList.Add(context.CurrentPluginMetadata.ExecuteFilePath); - _startInfo.ArgumentList.Add(string.Empty); - await base.InitAsync(context); _startInfo.WorkingDirectory = context.CurrentPluginMetadata.PluginDirectory; + await base.InitAsync(context); } } } diff --git a/Flow.Launcher.Core/Plugin/NodePluginV2.cs b/Flow.Launcher.Core/Plugin/NodePluginV2.cs new file mode 100644 index 000000000..6c95777f0 --- /dev/null +++ b/Flow.Launcher.Core/Plugin/NodePluginV2.cs @@ -0,0 +1,38 @@ +using System.Diagnostics; +using System.IO; +using System.IO.Pipelines; +using System.Threading; +using System.Threading.Tasks; +using Flow.Launcher.Plugin; + +namespace Flow.Launcher.Core.Plugin +{ + /// + /// Execution of JavaScript & TypeScript plugins + /// + internal class NodePluginV2 : ProcessStreamPluginV2 + { + public NodePluginV2(string filename) + { + StartInfo = new ProcessStartInfo + { + FileName = filename, + UseShellExecute = false, + CreateNoWindow = true, + RedirectStandardOutput = true, + RedirectStandardError = true + }; + } + + public override string SupportedLanguage { get; set; } + protected override ProcessStartInfo StartInfo { get; set; } + + public override async Task InitAsync(PluginInitContext context) + { + StartInfo.ArgumentList.Add(context.CurrentPluginMetadata.ExecuteFilePath); + StartInfo.ArgumentList.Add(string.Empty); + StartInfo.WorkingDirectory = context.CurrentPluginMetadata.PluginDirectory; + await base.InitAsync(context); + } + } +} diff --git a/Flow.Launcher.Core/Plugin/PluginsLoader.cs b/Flow.Launcher.Core/Plugin/PluginsLoader.cs index e6018d800..0f2e4f996 100644 --- a/Flow.Launcher.Core/Plugin/PluginsLoader.cs +++ b/Flow.Launcher.Core/Plugin/PluginsLoader.cs @@ -19,25 +19,33 @@ namespace Flow.Launcher.Core.Plugin public static List Plugins(List metadatas, PluginsSettings settings) { var dotnetPlugins = DotNetPlugins(metadatas); - + var pythonEnv = new PythonEnvironment(metadatas, settings); var pythonV2Env = new PythonV2Environment(metadatas, settings); var tsEnv = new TypeScriptEnvironment(metadatas, settings); var jsEnv = new JavaScriptEnvironment(metadatas, settings); + var tsV2Env = new TypeScriptV2Environment(metadatas, settings); + var jsV2Env = new JavaScriptV2Environment(metadatas, settings); var pythonPlugins = pythonEnv.Setup(); var pythonV2Plugins = pythonV2Env.Setup(); var tsPlugins = tsEnv.Setup(); var jsPlugins = jsEnv.Setup(); - + var tsV2Plugins = tsV2Env.Setup(); + var jsV2Plugins = jsV2Env.Setup(); + var executablePlugins = ExecutablePlugins(metadatas); - + var executableV2Plugins = ExecutableV2Plugins(metadatas); + var plugins = dotnetPlugins - .Concat(pythonPlugins) - .Concat(pythonV2Plugins) - .Concat(tsPlugins) - .Concat(jsPlugins) - .Concat(executablePlugins) - .ToList(); + .Concat(pythonPlugins) + .Concat(pythonV2Plugins) + .Concat(tsPlugins) + .Concat(jsPlugins) + .Concat(tsV2Plugins) + .Concat(jsV2Plugins) + .Concat(executablePlugins) + .Concat(executableV2Plugins) + .ToList(); return plugins; } @@ -96,7 +104,7 @@ namespace Flow.Launcher.Core.Plugin return; } - plugins.Add(new PluginPair {Plugin = plugin, Metadata = metadata}); + plugins.Add(new PluginPair { Plugin = plugin, Metadata = metadata }); }); metadata.InitTime += milliseconds; } @@ -121,7 +129,7 @@ namespace Flow.Launcher.Core.Plugin return plugins; } - public static IEnumerable ExecutablePlugins(IEnumerable source) + public static IEnumerable ExecutablePlugins(IEnumerable source) { return source .Where(o => o.Language.Equals(AllowedLanguage.Executable, StringComparison.OrdinalIgnoreCase)) @@ -130,5 +138,15 @@ namespace Flow.Launcher.Core.Plugin Plugin = new ExecutablePlugin(metadata.ExecuteFilePath), Metadata = metadata }); } + + public static IEnumerable ExecutableV2Plugins(IEnumerable source) + { + return source + .Where(o => o.Language.Equals(AllowedLanguage.ExecutableV2, StringComparison.OrdinalIgnoreCase)) + .Select(metadata => new PluginPair + { + Plugin = new ExecutablePluginV2(metadata.ExecuteFilePath), Metadata = metadata + }); + } } } diff --git a/Flow.Launcher.Core/Plugin/ProcessStreamPluginV2.cs b/Flow.Launcher.Core/Plugin/ProcessStreamPluginV2.cs index be35d481c..24d06d975 100644 --- a/Flow.Launcher.Core/Plugin/ProcessStreamPluginV2.cs +++ b/Flow.Launcher.Core/Plugin/ProcessStreamPluginV2.cs @@ -13,7 +13,7 @@ namespace Flow.Launcher.Core.Plugin { public override string SupportedLanguage { get; set; } - protected override IDuplexPipe ClientPipe { get; set; } + protected sealed override IDuplexPipe ClientPipe { get; set; } protected abstract ProcessStartInfo StartInfo { get; set; } diff --git a/Flow.Launcher.Core/Plugin/PythonPluginV2.cs b/Flow.Launcher.Core/Plugin/PythonPluginV2.cs index 842dc5cad..4a8d8d7de 100644 --- a/Flow.Launcher.Core/Plugin/PythonPluginV2.cs +++ b/Flow.Launcher.Core/Plugin/PythonPluginV2.cs @@ -19,8 +19,6 @@ namespace Flow.Launcher.Core.Plugin internal sealed class PythonPluginV2 : ProcessStreamPluginV2 { public override string SupportedLanguage { get; set; } = AllowedLanguage.Python; - - protected override IDuplexPipe ClientPipe { get; set; } protected override ProcessStartInfo StartInfo { get; set; } public PythonPluginV2(string filename) diff --git a/Flow.Launcher.Plugin/AllowedLanguage.cs b/Flow.Launcher.Plugin/AllowedLanguage.cs index 96f90e093..619a94deb 100644 --- a/Flow.Launcher.Plugin/AllowedLanguage.cs +++ b/Flow.Launcher.Plugin/AllowedLanguage.cs @@ -11,7 +11,7 @@ namespace Flow.Launcher.Plugin /// Python /// public const string Python = "Python"; - + /// /// Python V2 /// @@ -32,16 +32,31 @@ namespace Flow.Launcher.Plugin /// public const string Executable = "Executable"; + /// + /// Standard .exe + /// + public const string ExecutableV2 = "Executable_V2"; + /// /// TypeScript /// public const string TypeScript = "TypeScript"; + /// + /// TypeScript + /// + public const string TypeScriptV2 = "TypeScript_V2"; + /// /// JavaScript /// public const string JavaScript = "JavaScript"; + /// + /// JavaScript + /// + public const string JavaScriptV2 = "JavaScript_V2"; + /// /// Determines if this language is a .NET language /// @@ -50,7 +65,7 @@ namespace Flow.Launcher.Plugin public static bool IsDotNet(string language) { return language.Equals(CSharp, StringComparison.OrdinalIgnoreCase) - || language.Equals(FSharp, StringComparison.OrdinalIgnoreCase); + || language.Equals(FSharp, StringComparison.OrdinalIgnoreCase); } /// @@ -61,11 +76,15 @@ namespace Flow.Launcher.Plugin public static bool IsAllowed(string language) { return IsDotNet(language) - || language.Equals(Python, StringComparison.OrdinalIgnoreCase) - || language.Equals(PythonV2, StringComparison.OrdinalIgnoreCase) - || language.Equals(Executable, StringComparison.OrdinalIgnoreCase) - || language.Equals(TypeScript, StringComparison.OrdinalIgnoreCase) - || language.Equals(JavaScript, StringComparison.OrdinalIgnoreCase); + || language.Equals(Python, StringComparison.OrdinalIgnoreCase) + || language.Equals(PythonV2, StringComparison.OrdinalIgnoreCase) + || language.Equals(Executable, StringComparison.OrdinalIgnoreCase) + || language.Equals(TypeScript, StringComparison.OrdinalIgnoreCase) + || language.Equals(JavaScript, StringComparison.OrdinalIgnoreCase) + || language.Equals(ExecutableV2, StringComparison.OrdinalIgnoreCase) + || language.Equals(TypeScriptV2, StringComparison.OrdinalIgnoreCase) + || language.Equals(JavaScriptV2, StringComparison.OrdinalIgnoreCase); + ; } } }