From 2efd04cf5379727a30ab216c3e868b5001f41085 Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Sat, 4 Nov 2023 18:08:56 +0100 Subject: [PATCH] Add option to register scripts via options --- .../Elsa.WorkflowServer.Web/Program.cs | 16 +++++++-- .../{RunCSharp => RunPython}/RunPython.cs | 0 .../RunPythonOptionsProvider.cs | 0 .../Handlers/ConfigureScriptsFromOptions.cs | 33 +++++++++++++++++++ .../Models/ExecutionContextProxy.cs | 1 - .../Elsa.Python/Options/PythonOptions.cs | 25 ++++++++++++++ 6 files changed, 72 insertions(+), 3 deletions(-) rename src/modules/Elsa.Python/Activities/{RunCSharp => RunPython}/RunPython.cs (100%) rename src/modules/Elsa.Python/Activities/{RunCSharp => RunPython}/RunPythonOptionsProvider.cs (100%) create mode 100644 src/modules/Elsa.Python/Handlers/ConfigureScriptsFromOptions.cs diff --git a/src/bundles/Elsa.WorkflowServer.Web/Program.cs b/src/bundles/Elsa.WorkflowServer.Web/Program.cs index cc2a8e786..22fe348bb 100644 --- a/src/bundles/Elsa.WorkflowServer.Web/Program.cs +++ b/src/bundles/Elsa.WorkflowServer.Web/Program.cs @@ -151,8 +151,20 @@ services options.AppendScript("string Greet(string name) => $\"Hello {name}!\";"); options.AppendScript("string SayHelloWorld() => Greet(\"World\");"); }) - .UseJavaScript(options => options.AllowClrAccess = true) - .UsePython() + .UseJavaScript(options => + { + options.AllowClrAccess = true; + options.ConfigureEngine(engine => + { + engine.Execute("function greet(name) { return `Hello ${name}!`; }"); + engine.Execute("function sayHelloWorld() { return greet('World'); }"); + }); + }) + .UsePython(options => + { + options.AddScript("def greet(name): return f\"Hello {name}!\";"); + options.AddScript("def say_hello_world(): return greet(\"World\");"); + }) .UseLiquid(liquid => liquid.FluidOptions = options => options.Encoder = HtmlEncoder.Default) .UseHttp(http => { diff --git a/src/modules/Elsa.Python/Activities/RunCSharp/RunPython.cs b/src/modules/Elsa.Python/Activities/RunPython/RunPython.cs similarity index 100% rename from src/modules/Elsa.Python/Activities/RunCSharp/RunPython.cs rename to src/modules/Elsa.Python/Activities/RunPython/RunPython.cs diff --git a/src/modules/Elsa.Python/Activities/RunCSharp/RunPythonOptionsProvider.cs b/src/modules/Elsa.Python/Activities/RunPython/RunPythonOptionsProvider.cs similarity index 100% rename from src/modules/Elsa.Python/Activities/RunCSharp/RunPythonOptionsProvider.cs rename to src/modules/Elsa.Python/Activities/RunPython/RunPythonOptionsProvider.cs diff --git a/src/modules/Elsa.Python/Handlers/ConfigureScriptsFromOptions.cs b/src/modules/Elsa.Python/Handlers/ConfigureScriptsFromOptions.cs new file mode 100644 index 000000000..4dfcabb6b --- /dev/null +++ b/src/modules/Elsa.Python/Handlers/ConfigureScriptsFromOptions.cs @@ -0,0 +1,33 @@ +using Elsa.Mediator.Contracts; +using Elsa.Python.Notifications; +using Elsa.Python.Options; +using JetBrains.Annotations; +using Microsoft.Extensions.Options; + +namespace Elsa.Python.Handlers; + +/// +/// This handler configures the Python engine based on the . +/// +[UsedImplicitly] +public class ConfigurePythonFromOptions : INotificationHandler +{ + private readonly PythonOptions _options; + + /// + /// Initializes a new instance of the class. + /// + public ConfigurePythonFromOptions(IOptions options) + { + _options = options.Value; + } + + /// + public Task HandleAsync(EvaluatingPython notification, CancellationToken cancellationToken) + { + foreach (var script in _options.Scripts) + notification.AppendScript(script); + + return Task.CompletedTask; + } +} \ No newline at end of file diff --git a/src/modules/Elsa.Python/Models/ExecutionContextProxy.cs b/src/modules/Elsa.Python/Models/ExecutionContextProxy.cs index 909e91360..591db80e9 100644 --- a/src/modules/Elsa.Python/Models/ExecutionContextProxy.cs +++ b/src/modules/Elsa.Python/Models/ExecutionContextProxy.cs @@ -1,7 +1,6 @@ using Elsa.Expressions.Helpers; using Elsa.Expressions.Models; using Elsa.Extensions; -using Humanizer; using JetBrains.Annotations; namespace Elsa.Python.Models; diff --git a/src/modules/Elsa.Python/Options/PythonOptions.cs b/src/modules/Elsa.Python/Options/PythonOptions.cs index 595bc2f3f..48942d9d7 100644 --- a/src/modules/Elsa.Python/Options/PythonOptions.cs +++ b/src/modules/Elsa.Python/Options/PythonOptions.cs @@ -1,3 +1,5 @@ +using System.Text; + namespace Elsa.Python.Options; /// @@ -5,5 +7,28 @@ namespace Elsa.Python.Options; /// public class PythonOptions { + /// + /// Gets or sets the Python script files to load. + /// + public ICollection Scripts { get; } = new List(); + /// + /// Appends a script to the Python engine. + /// + /// A builder that builds the script to append. + public void AddScript(Action builder) + { + var sb = new StringBuilder(); + builder(sb); + AddScript(sb.ToString()); + } + + /// + /// Appends a script to the Python engine. + /// + /// The script to append. + public void AddScript(string script) + { + Scripts.Add(script); + } } \ No newline at end of file