2022-04-07 10:44:12 +00:00
|
|
|
|
using System.Text.Json.Serialization;
|
2023-03-03 13:48:05 +00:00
|
|
|
|
using Elsa.Workflows.Core;
|
2022-05-26 10:47:31 +00:00
|
|
|
|
using Elsa.Workflows.Core.Attributes;
|
|
|
|
|
|
using Elsa.Workflows.Core.Models;
|
2022-11-28 18:12:01 +00:00
|
|
|
|
using Elsa.Workflows.Management.Models;
|
2023-02-13 11:15:20 +00:00
|
|
|
|
using IJavaScriptEvaluator = Elsa.JavaScript.Contracts.IJavaScriptEvaluator;
|
2022-03-07 14:42:12 +00:00
|
|
|
|
|
2022-05-26 10:47:31 +00:00
|
|
|
|
namespace Elsa.JavaScript.Activities;
|
2022-03-07 14:42:12 +00:00
|
|
|
|
|
2023-02-21 20:15:09 +00:00
|
|
|
|
[Activity("Elsa", "Scripting", "Executes JavaScript code", DisplayName = "Run JavaScript")]
|
2023-01-19 20:49:24 +00:00
|
|
|
|
public class RunJavaScript : CodeActivity<object?>
|
2022-03-07 14:42:12 +00:00
|
|
|
|
{
|
2022-04-07 10:44:12 +00:00
|
|
|
|
[JsonConstructor]
|
2022-03-07 14:42:12 +00:00
|
|
|
|
public RunJavaScript()
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public RunJavaScript(string script)
|
|
|
|
|
|
{
|
|
|
|
|
|
Script = new Input<string>(script);
|
|
|
|
|
|
}
|
2022-11-28 18:12:01 +00:00
|
|
|
|
|
|
|
|
|
|
[Input(UIHint = InputUIHints.CodeEditor, OptionsProvider = typeof(RunJavaScriptOptionsProvider))]
|
2022-03-07 14:42:12 +00:00
|
|
|
|
public Input<string> Script { get; set; } = new("");
|
|
|
|
|
|
|
|
|
|
|
|
protected override async ValueTask ExecuteAsync(ActivityExecutionContext context)
|
|
|
|
|
|
{
|
|
|
|
|
|
var script = context.Get(Script);
|
|
|
|
|
|
|
|
|
|
|
|
// If no script was specified, there's nothing to do.
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(script))
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
// Get a JavaScript evaluator.
|
|
|
|
|
|
var javaScriptEvaluator = context.GetRequiredService<IJavaScriptEvaluator>();
|
2022-11-28 18:12:01 +00:00
|
|
|
|
|
2022-03-07 14:42:12 +00:00
|
|
|
|
// Run the script.
|
|
|
|
|
|
var result = await javaScriptEvaluator.EvaluateAsync(script, typeof(object), context.ExpressionExecutionContext, cancellationToken: context.CancellationToken);
|
|
|
|
|
|
|
|
|
|
|
|
// Set the result as output, if any.
|
|
|
|
|
|
context.Set(Result, result);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|