| | | 1 | | using System.Dynamic; |
| | | 2 | | using System.Text; |
| | | 3 | | using Elsa.Expressions.CSharp.Extensions; |
| | | 4 | | using Elsa.Expressions.CSharp.Notifications; |
| | | 5 | | using Elsa.Expressions.CSharp.Options; |
| | | 6 | | using Elsa.Expressions.Models; |
| | | 7 | | using Elsa.Extensions; |
| | | 8 | | using Elsa.Mediator.Contracts; |
| | | 9 | | using Humanizer; |
| | | 10 | | using JetBrains.Annotations; |
| | | 11 | | using Microsoft.Extensions.Options; |
| | | 12 | | |
| | | 13 | | namespace Elsa.Expressions.CSharp.Handlers; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Configures the C# evaluator with methods to access workflow variables. |
| | | 17 | | /// </summary> |
| | | 18 | | [UsedImplicitly] |
| | 319 | 19 | | public class GenerateWorkflowVariableAccessors(IOptions<CSharpOptions> options) : INotificationHandler<EvaluatingCSharp> |
| | | 20 | | { |
| | | 21 | | /// <inheritdoc /> |
| | | 22 | | public Task HandleAsync(EvaluatingCSharp notification, CancellationToken cancellationToken) |
| | | 23 | | { |
| | 0 | 24 | | var expressionExecutionContext = notification.Context; |
| | 0 | 25 | | var variables = expressionExecutionContext.GetVariablesInScope().ToList(); |
| | 0 | 26 | | var sb = new StringBuilder(); |
| | 0 | 27 | | sb.AppendLine("public partial class WorkflowVariablesProxy {"); |
| | 0 | 28 | | sb.AppendLine("\tpublic WorkflowVariablesProxy(ExecutionContextProxy executionContext) => ExecutionContext = exe |
| | 0 | 29 | | sb.AppendLine("\tpublic ExecutionContextProxy ExecutionContext { get; }"); |
| | 0 | 30 | | sb.AppendLine(); |
| | 0 | 31 | | sb.AppendLine("\tpublic T? Get<T>(string name) => ExecutionContext.GetVariable<T>(name);"); |
| | 0 | 32 | | sb.AppendLine("\tpublic object? Get(string name) => ExecutionContext.GetVariable(name);"); |
| | 0 | 33 | | sb.AppendLine("\tpublic void Set(string name, object? value) => ExecutionContext.SetVariable(name, value);"); |
| | 0 | 34 | | sb.AppendLine(); |
| | | 35 | | |
| | 0 | 36 | | if (!options.Value.DisableWrappers) |
| | | 37 | | { |
| | 0 | 38 | | foreach (var variable in variables.Where(x => x.Name.IsValidVariableName())) |
| | | 39 | | { |
| | 0 | 40 | | var variableName = variable.Name.Pascalize(); |
| | 0 | 41 | | var variableType = variable.GetVariableType(); |
| | | 42 | | |
| | | 43 | | // Check if the variable type is ExpandoObject |
| | 0 | 44 | | bool isExpandoObject = variableType == typeof(ExpandoObject); |
| | | 45 | | |
| | | 46 | | // Use dynamic type for ExpandoObject to enable dot notation but keep the original type for retrieval |
| | 0 | 47 | | var displayTypeName = isExpandoObject ? "dynamic" : variableType.GetFriendlyTypeName(Brackets.Angle); |
| | 0 | 48 | | var retrieveTypeName = isExpandoObject ? "ExpandoObject" : displayTypeName; |
| | | 49 | | |
| | 0 | 50 | | sb.AppendLine($"\tpublic {displayTypeName} {variableName}"); |
| | 0 | 51 | | sb.AppendLine("\t{"); |
| | 0 | 52 | | sb.AppendLine($"\t\tget => Get<{retrieveTypeName}>(\"{variableName}\");"); |
| | 0 | 53 | | sb.AppendLine($"\t\tset => Set(\"{variableName}\", value);"); |
| | 0 | 54 | | sb.AppendLine("\t}"); |
| | | 55 | | } |
| | | 56 | | } |
| | | 57 | | |
| | 0 | 58 | | sb.AppendLine("}"); |
| | 0 | 59 | | sb.AppendLine("var Variables = new WorkflowVariablesProxy(ExecutionContext);"); // Obsolete; use Variable instea |
| | 0 | 60 | | sb.AppendLine("var Variable = Variables;"); |
| | 0 | 61 | | notification.AppendScript(sb.ToString()); |
| | 0 | 62 | | return Task.CompletedTask; |
| | | 63 | | } |
| | | 64 | | } |