| | | 1 | | using System.Text; |
| | | 2 | | using Elsa.Expressions.CSharp.Extensions; |
| | | 3 | | using Elsa.Expressions.CSharp.Notifications; |
| | | 4 | | using Elsa.Expressions.CSharp.Options; |
| | | 5 | | using Elsa.Expressions.Models; |
| | | 6 | | using Elsa.Extensions; |
| | | 7 | | using Elsa.Mediator.Contracts; |
| | | 8 | | using JetBrains.Annotations; |
| | | 9 | | using Microsoft.Extensions.Options; |
| | | 10 | | |
| | | 11 | | namespace Elsa.Expressions.CSharp.Handlers; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Configures the C# evaluator with methods to access workflow variables. |
| | | 15 | | /// </summary> |
| | | 16 | | [UsedImplicitly] |
| | 319 | 17 | | public class GenerateWorkflowInputAccessors(IOptions<CSharpOptions> options) : INotificationHandler<EvaluatingCSharp> |
| | | 18 | | { |
| | | 19 | | /// <inheritdoc /> |
| | | 20 | | public Task HandleAsync(EvaluatingCSharp notification, CancellationToken cancellationToken) |
| | | 21 | | { |
| | 0 | 22 | | var expressionExecutionContext = notification.Context; |
| | | 23 | | |
| | 0 | 24 | | if (!expressionExecutionContext.TryGetWorkflowExecutionContext(out var workflowExecutionContext)) |
| | 0 | 25 | | return Task.CompletedTask; |
| | | 26 | | |
| | 0 | 27 | | var workflow = workflowExecutionContext.Workflow; |
| | 0 | 28 | | var inputDefinitions = workflow.Inputs.ToList(); |
| | 0 | 29 | | var sb = new StringBuilder(); |
| | 0 | 30 | | sb.AppendLine("public partial class WorkflowInputsProxy {"); |
| | 0 | 31 | | sb.AppendLine("\tpublic WorkflowInputsProxy(ExecutionContextProxy executionContext) => ExecutionContext = execut |
| | 0 | 32 | | sb.AppendLine("\tpublic ExecutionContextProxy ExecutionContext { get; }"); |
| | 0 | 33 | | sb.AppendLine(); |
| | 0 | 34 | | sb.AppendLine("\tpublic T? Get<T>(string name) => ExecutionContext.GetInput<T>(name);"); |
| | 0 | 35 | | sb.AppendLine(); |
| | | 36 | | |
| | 0 | 37 | | if (!options.Value.DisableWrappers) |
| | | 38 | | { |
| | 0 | 39 | | foreach (var inputDefinition in inputDefinitions.Where(x => x.Name.IsValidVariableName())) |
| | | 40 | | { |
| | 0 | 41 | | var inputName = inputDefinition.Name; |
| | 0 | 42 | | var variableType = inputDefinition.Type; |
| | 0 | 43 | | var friendlyTypeName = variableType.GetFriendlyTypeName(Brackets.Angle); |
| | 0 | 44 | | sb.AppendLine($"\tpublic {friendlyTypeName} {inputName}"); |
| | 0 | 45 | | sb.AppendLine("\t{"); |
| | 0 | 46 | | sb.AppendLine($"\t\tget => Get<{friendlyTypeName}>(\"{inputName}\");"); |
| | 0 | 47 | | sb.AppendLine("\t}"); |
| | | 48 | | } |
| | | 49 | | } |
| | | 50 | | |
| | 0 | 51 | | sb.AppendLine("}"); |
| | 0 | 52 | | sb.AppendLine("var Inputs = new WorkflowInputsProxy(ExecutionContext);"); // Obsolete; use Input instead. |
| | 0 | 53 | | sb.AppendLine("var Input = Inputs;"); |
| | 0 | 54 | | notification.AppendScript(sb.ToString()); |
| | 0 | 55 | | return Task.CompletedTask; |
| | | 56 | | } |
| | | 57 | | } |