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