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