| | | 1 | | using System.Runtime.CompilerServices; |
| | | 2 | | using Elsa.Expressions.Models; |
| | | 3 | | using Elsa.Extensions; |
| | | 4 | | using Elsa.Expressions.JavaScript.Contracts; |
| | | 5 | | using Elsa.Workflows; |
| | | 6 | | using Elsa.Workflows.Attributes; |
| | | 7 | | using Elsa.Workflows.Models; |
| | | 8 | | using Elsa.Workflows.UIHints; |
| | | 9 | | using Jint; |
| | | 10 | | |
| | | 11 | | // ReSharper disable once CheckNamespace |
| | | 12 | | namespace Elsa.Expressions.JavaScript.Activities; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Executes JavaScript code. |
| | | 16 | | /// </summary> |
| | | 17 | | [Activity("Elsa", "Scripting", "Executes JavaScript code", DisplayName = "Run JavaScript")] |
| | | 18 | | public class RunJavaScript : CodeActivity<object?> |
| | | 19 | | { |
| | | 20 | | /// <inheritdoc /> |
| | 88 | 21 | | public RunJavaScript([CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : base(source, lin |
| | | 22 | | { |
| | 88 | 23 | | } |
| | | 24 | | |
| | | 25 | | /// <inheritdoc /> |
| | 54 | 26 | | public RunJavaScript(string script, [CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : t |
| | | 27 | | { |
| | 54 | 28 | | Script = new(script); |
| | 54 | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// The script to run. |
| | | 33 | | /// </summary> |
| | | 34 | | [Input( |
| | | 35 | | Description = "The script to run.", |
| | | 36 | | DefaultSyntax = "JavaScript", |
| | | 37 | | UIHint = InputUIHints.CodeEditor, |
| | | 38 | | UIHandler = typeof(RunJavaScriptOptionsProvider) |
| | | 39 | | )] |
| | 398 | 40 | | public Input<string> Script { get; set; } = new(""); |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// A list of possible outcomes. Use "setOutcome()" to set the outcome. Use "setOutcomes" to set multiple outcomes. |
| | | 44 | | /// </summary> |
| | | 45 | | [Input(Description = "A list of possible outcomes.", UIHint = InputUIHints.DynamicOutcomes)] |
| | 219 | 46 | | public Input<ICollection<string>> PossibleOutcomes { get; set; } = null!; |
| | | 47 | | |
| | | 48 | | /// <inheritdoc /> |
| | | 49 | | protected override async ValueTask ExecuteAsync(ActivityExecutionContext context) |
| | | 50 | | { |
| | 21 | 51 | | var script = context.Get(Script); |
| | | 52 | | |
| | | 53 | | // If no script was specified, there's nothing to do. |
| | 21 | 54 | | if (string.IsNullOrWhiteSpace(script)) |
| | 2 | 55 | | return; |
| | | 56 | | |
| | | 57 | | // Get a JavaScript evaluator. |
| | 19 | 58 | | var javaScriptEvaluator = context.GetRequiredService<IJavaScriptEvaluator>(); |
| | | 59 | | |
| | | 60 | | // Run the script. |
| | 19 | 61 | | var result = await javaScriptEvaluator.EvaluateAsync( |
| | 19 | 62 | | script, |
| | 19 | 63 | | typeof(object), |
| | 19 | 64 | | context.ExpressionExecutionContext, |
| | 19 | 65 | | ExpressionEvaluatorOptions.Empty, |
| | 19 | 66 | | engine => ConfigureEngine(engine, context), |
| | 19 | 67 | | context.CancellationToken); |
| | | 68 | | |
| | | 69 | | // Set the result as output, if any. |
| | 16 | 70 | | if (result is not null) |
| | 11 | 71 | | context.Set(Result, result); |
| | | 72 | | |
| | | 73 | | // Get the outcome or outcomes set by the script, if any. If not set, use "Done". |
| | 29 | 74 | | var outcomes = context.TransientProperties.GetValueOrDefault("Outcomes", () => new[] { "Done" })!; |
| | | 75 | | |
| | | 76 | | // Complete the activity with the outcome. |
| | 16 | 77 | | await context.CompleteActivityWithOutcomesAsync(outcomes); |
| | 18 | 78 | | } |
| | | 79 | | |
| | | 80 | | private static void ConfigureEngine(Engine engine, ActivityExecutionContext context) |
| | | 81 | | { |
| | 21 | 82 | | engine.SetValue("setOutcome", (Action<string>)(value => context.TransientProperties["Outcomes"] = new[] { value |
| | 20 | 83 | | engine.SetValue("setOutcomes", (Action<string[]>)(value => context.TransientProperties["Outcomes"] = value)); |
| | 19 | 84 | | } |
| | | 85 | | } |