Add option to register scripts via options
This commit is contained in:
parent
d1051cafb4
commit
2efd04cf53
|
|
@ -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 =>
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
||||
/// <summary>
|
||||
/// This handler configures the Python engine based on the <see cref="PythonOptions"/>.
|
||||
/// </summary>
|
||||
[UsedImplicitly]
|
||||
public class ConfigurePythonFromOptions : INotificationHandler<EvaluatingPython>
|
||||
{
|
||||
private readonly PythonOptions _options;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ConfigurePythonFromOptions"/> class.
|
||||
/// </summary>
|
||||
public ConfigurePythonFromOptions(IOptions<PythonOptions> options)
|
||||
{
|
||||
_options = options.Value;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task HandleAsync(EvaluatingPython notification, CancellationToken cancellationToken)
|
||||
{
|
||||
foreach (var script in _options.Scripts)
|
||||
notification.AppendScript(script);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,7 +1,6 @@
|
|||
using Elsa.Expressions.Helpers;
|
||||
using Elsa.Expressions.Models;
|
||||
using Elsa.Extensions;
|
||||
using Humanizer;
|
||||
using JetBrains.Annotations;
|
||||
|
||||
namespace Elsa.Python.Models;
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
using System.Text;
|
||||
|
||||
namespace Elsa.Python.Options;
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -5,5 +7,28 @@ namespace Elsa.Python.Options;
|
|||
/// </summary>
|
||||
public class PythonOptions
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the Python script files to load.
|
||||
/// </summary>
|
||||
public ICollection<string> Scripts { get; } = new List<string>();
|
||||
|
||||
/// <summary>
|
||||
/// Appends a script to the Python engine.
|
||||
/// </summary>
|
||||
/// <param name="builder">A builder that builds the script to append.</param>
|
||||
public void AddScript(Action<StringBuilder> builder)
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
builder(sb);
|
||||
AddScript(sb.ToString());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Appends a script to the Python engine.
|
||||
/// </summary>
|
||||
/// <param name="script">The script to append.</param>
|
||||
public void AddScript(string script)
|
||||
{
|
||||
Scripts.Add(script);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue