2023-08-06 18:55:24 +00:00
|
|
|
|
using System.Runtime.CompilerServices;
|
2023-12-14 09:45:37 +00:00
|
|
|
|
using Elsa.Expressions.Models;
|
2023-09-07 18:34:53 +00:00
|
|
|
|
using Elsa.Extensions;
|
|
|
|
|
|
using Elsa.JavaScript.Contracts;
|
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;
|
2023-09-07 18:34:53 +00:00
|
|
|
|
using Jint;
|
2022-03-07 14:42:12 +00:00
|
|
|
|
|
2023-09-07 18:34:53 +00:00
|
|
|
|
// ReSharper disable once CheckNamespace
|
2022-05-26 10:47:31 +00:00
|
|
|
|
namespace Elsa.JavaScript.Activities;
|
2022-03-07 14:42:12 +00:00
|
|
|
|
|
2023-03-14 07:14:14 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Executes JavaScript code.
|
|
|
|
|
|
/// </summary>
|
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
|
|
|
|
{
|
2023-03-14 07:14:14 +00:00
|
|
|
|
/// <inheritdoc />
|
2023-08-06 18:55:24 +00:00
|
|
|
|
public RunJavaScript([CallerFilePath] string? source = default, [CallerLineNumber] int? line = default) : base(source, line)
|
2022-03-07 14:42:12 +00:00
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-14 07:14:14 +00:00
|
|
|
|
/// <inheritdoc />
|
2023-08-06 18:55:24 +00:00
|
|
|
|
public RunJavaScript(string script, [CallerFilePath] string? source = default, [CallerLineNumber] int? line = default) : this(source, line)
|
2022-03-07 14:42:12 +00:00
|
|
|
|
{
|
|
|
|
|
|
Script = new Input<string>(script);
|
|
|
|
|
|
}
|
2022-11-28 18:12:01 +00:00
|
|
|
|
|
2023-03-14 07:14:14 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// The script to run.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[Input(
|
|
|
|
|
|
Description = "The script to run.",
|
|
|
|
|
|
UIHint = InputUIHints.CodeEditor,
|
|
|
|
|
|
OptionsProvider = typeof(RunJavaScriptOptionsProvider)
|
|
|
|
|
|
)]
|
2022-03-07 14:42:12 +00:00
|
|
|
|
public Input<string> Script { get; set; } = new("");
|
2023-12-14 09:45:37 +00:00
|
|
|
|
|
2023-11-04 17:15:56 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// A list of possible outcomes. Use "setOutcome()" to set the outcome. Use "setOutcomes" to set multiple outcomes.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[Input(Description = "A list of possible outcomes.", UIHint = InputUIHints.DynamicOutcomes)]
|
|
|
|
|
|
public Input<ICollection<string>> PossibleOutcomes { get; set; } = default!;
|
2022-03-07 14:42:12 +00:00
|
|
|
|
|
2023-03-14 07:14:14 +00:00
|
|
|
|
/// <inheritdoc />
|
2022-03-07 14:42:12 +00:00
|
|
|
|
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.
|
2023-12-14 09:45:37 +00:00
|
|
|
|
var result = await javaScriptEvaluator.EvaluateAsync(
|
|
|
|
|
|
script,
|
|
|
|
|
|
typeof(object),
|
|
|
|
|
|
context.ExpressionExecutionContext,
|
|
|
|
|
|
ExpressionEvaluatorOptions.Empty,
|
|
|
|
|
|
engine => ConfigureEngine(engine, context),
|
|
|
|
|
|
context.CancellationToken);
|
2022-03-07 14:42:12 +00:00
|
|
|
|
|
|
|
|
|
|
// Set the result as output, if any.
|
2023-08-06 18:55:24 +00:00
|
|
|
|
if (result is not null)
|
2023-06-23 16:42:07 +00:00
|
|
|
|
context.Set(Result, result);
|
2023-09-07 18:34:53 +00:00
|
|
|
|
|
|
|
|
|
|
// Get the outcome or outcomes set by the script, if any. If not set, use "Done".
|
|
|
|
|
|
var outcomes = context.TransientProperties.GetValueOrDefault("Outcomes", () => new[] { "Done" })!;
|
|
|
|
|
|
|
|
|
|
|
|
// Complete the activity with the outcome.
|
|
|
|
|
|
await context.CompleteActivityWithOutcomesAsync(outcomes);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static void ConfigureEngine(Engine engine, ActivityExecutionContext context)
|
|
|
|
|
|
{
|
|
|
|
|
|
engine.SetValue("setOutcome", (Action<string>)(value => context.TransientProperties["Outcomes"] = new[] { value }));
|
|
|
|
|
|
engine.SetValue("setOutcomes", (Action<string[]>)(value => context.TransientProperties["Outcomes"] = value));
|
2022-03-07 14:42:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|