* Refactor directory structure and add variable wrappers option Renamed TypeDefinitions/Providers to Providers for better clarity. Added a new configuration option to disable generating variable wrappers for scenarios requiring invalid JavaScript variable names. * Add variable name validation and wrapper control Introduced methods to validate variable names and filter invalid ones. Integrated `DisableWrappers` option to control the generation of variable wrappers, enhancing flexibility in variable management. * Remove unused item group from project file The item group containing the "TypeDefinitions\Providers\" folder was removed from the Elsa.JavaScript.csproj file. This folder was not serving any purpose and its removal helps in cleaning up the project structure.
50 lines
1.9 KiB
C#
50 lines
1.9 KiB
C#
using System.Text;
|
|
using Elsa.CSharp.Extensions;
|
|
using Elsa.CSharp.Notifications;
|
|
using Elsa.CSharp.Options;
|
|
using Elsa.Expressions.Models;
|
|
using Elsa.Extensions;
|
|
using Elsa.Mediator.Contracts;
|
|
using JetBrains.Annotations;
|
|
using Microsoft.Extensions.Options;
|
|
|
|
namespace Elsa.CSharp.Handlers;
|
|
|
|
/// <summary>
|
|
/// Configures the C# evaluator with methods to access workflow variables.
|
|
/// </summary>
|
|
[UsedImplicitly]
|
|
public class GenerateArgumentAccessors(IOptions<CSharpOptions> options) : INotificationHandler<EvaluatingCSharp>
|
|
{
|
|
/// <inheritdoc />
|
|
public Task HandleAsync(EvaluatingCSharp notification, CancellationToken cancellationToken)
|
|
{
|
|
var arguments = notification.Options.Arguments;
|
|
var sb = new StringBuilder();
|
|
sb.AppendLine("public partial class ArgumentsProxy {");
|
|
sb.AppendLine("\tpublic ArgumentsProxy(IDictionary<string, object> arguments) => Arguments = arguments;");
|
|
sb.AppendLine("\tpublic IDictionary<string, object> Arguments { get; }");
|
|
sb.AppendLine();
|
|
sb.AppendLine("\tpublic T? Get<T>(string name) => Arguments.TryGetValue(name, out var v) ? (T?)v : default;");
|
|
sb.AppendLine();
|
|
|
|
if (!options.Value.DisableWrappers)
|
|
{
|
|
foreach (var argument in arguments.Where(x => x.Key.IsValidVariableName()))
|
|
{
|
|
var argumentName = argument.Key;
|
|
var variableType = argument.Value.GetType();
|
|
var friendlyTypeName = variableType.GetFriendlyTypeName(Brackets.Angle);
|
|
sb.AppendLine($"\tpublic {friendlyTypeName} {argumentName}");
|
|
sb.AppendLine("\t{");
|
|
sb.AppendLine($"\t\tget => Get<{friendlyTypeName}>(\"{argumentName}\");");
|
|
sb.AppendLine("\t}");
|
|
}
|
|
}
|
|
|
|
sb.AppendLine("}");
|
|
sb.AppendLine("var Args = new ArgumentsProxy(Arguments);");
|
|
notification.AppendScript(sb.ToString());
|
|
return Task.CompletedTask;
|
|
}
|
|
} |