| | | 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 GenerateArgumentAccessors(IOptions<CSharpOptions> options) : INotificationHandler<EvaluatingCSharp> |
| | | 18 | | { |
| | | 19 | | /// <inheritdoc /> |
| | | 20 | | public Task HandleAsync(EvaluatingCSharp notification, CancellationToken cancellationToken) |
| | | 21 | | { |
| | 0 | 22 | | var arguments = notification.Options.Arguments; |
| | 0 | 23 | | var sb = new StringBuilder(); |
| | 0 | 24 | | sb.AppendLine("public partial class ArgumentsProxy {"); |
| | 0 | 25 | | sb.AppendLine("\tpublic ArgumentsProxy(IDictionary<string, object> arguments) => Arguments = arguments;"); |
| | 0 | 26 | | sb.AppendLine("\tpublic IDictionary<string, object> Arguments { get; }"); |
| | 0 | 27 | | sb.AppendLine(); |
| | 0 | 28 | | sb.AppendLine("\tpublic T? Get<T>(string name) => Arguments.TryGetValue(name, out var v) ? (T?)v : default;"); |
| | 0 | 29 | | sb.AppendLine(); |
| | | 30 | | |
| | 0 | 31 | | if (!options.Value.DisableWrappers) |
| | | 32 | | { |
| | 0 | 33 | | foreach (var argument in arguments.Where(x => x.Key.IsValidVariableName())) |
| | | 34 | | { |
| | 0 | 35 | | var argumentName = argument.Key; |
| | 0 | 36 | | var variableType = argument.Value.GetType(); |
| | 0 | 37 | | var friendlyTypeName = variableType.GetFriendlyTypeName(Brackets.Angle); |
| | 0 | 38 | | sb.AppendLine($"\tpublic {friendlyTypeName} {argumentName}"); |
| | 0 | 39 | | sb.AppendLine("\t{"); |
| | 0 | 40 | | sb.AppendLine($"\t\tget => Get<{friendlyTypeName}>(\"{argumentName}\");"); |
| | 0 | 41 | | sb.AppendLine("\t}"); |
| | | 42 | | } |
| | | 43 | | } |
| | | 44 | | |
| | 0 | 45 | | sb.AppendLine("}"); |
| | 0 | 46 | | sb.AppendLine("var Args = new ArgumentsProxy(Arguments);"); |
| | 0 | 47 | | notification.AppendScript(sb.ToString()); |
| | 0 | 48 | | return Task.CompletedTask; |
| | | 49 | | } |
| | | 50 | | } |