| | | 1 | | using System.Dynamic; |
| | | 2 | | using System.Reflection; |
| | | 3 | | using System.Text.Json; |
| | | 4 | | using System.Text.Json.Nodes; |
| | | 5 | | using System.Text.Json.Serialization; |
| | | 6 | | using Microsoft.CSharp.RuntimeBinder; |
| | | 7 | | using Elsa.Expressions.CSharp.Contexts; |
| | | 8 | | using Elsa.Expressions.CSharp.Models; |
| | | 9 | | using Elsa.Expressions.Models; |
| | | 10 | | using Microsoft.CodeAnalysis.Scripting; |
| | | 11 | | |
| | | 12 | | namespace Elsa.Expressions.CSharp.Options; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Options for the C# expression evaluator. |
| | | 16 | | /// </summary> |
| | | 17 | | public class CSharpOptions |
| | | 18 | | { |
| | | 19 | | /// <summary> |
| | | 20 | | /// Gets or sets whether workflow-authored Roslyn C# host-code execution is allowed. |
| | | 21 | | /// Roslyn scripting is not a sandbox and can access host process capabilities. |
| | | 22 | | /// </summary> |
| | 42 | 23 | | public bool AllowHostCodeExecution { get; set; } |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// A list of callbacks that is invoked when a C# expression is evaluated. Use this to configure the <see cref="Scri |
| | | 27 | | /// </summary> |
| | 3 | 28 | | public ICollection<Func<ScriptOptions, ExpressionExecutionContext, ScriptOptions>> ConfigureScriptOptionsCallbacks { |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// A list of callbacks that is invoked when a C# expression is evaluated. Use this to configure the <see cref="Scri |
| | | 32 | | /// </summary> |
| | 9 | 33 | | public ICollection<Func<Script, ExpressionExecutionContext, Script>> ConfigureScriptCallbacks { get; } = new List<Fu |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// A list of assemblies that will be referenced when evaluating a C# expression. |
| | | 37 | | /// </summary> |
| | 3 | 38 | | public ISet<Assembly> Assemblies { get; } = new HashSet<Assembly>(new[] |
| | 3 | 39 | | { |
| | 3 | 40 | | typeof(Globals).Assembly, // Elsa.Expressions.CSharp |
| | 3 | 41 | | typeof(Enumerable).Assembly, // System.Linq |
| | 3 | 42 | | typeof(Guid).Assembly, // System.Runtime |
| | 3 | 43 | | typeof(JsonSerializer).Assembly, // System.Text.Json |
| | 3 | 44 | | typeof(IDictionary<string, object>).Assembly, // System.Collections |
| | 3 | 45 | | typeof(ExpandoObject).Assembly, // System.Linq.Expressions assembly — hosts System.Dynamic.ExpandoObject in .NET |
| | 3 | 46 | | typeof(CSharpArgumentInfo).Assembly, // Microsoft.CSharp (required for dynamic dispatch) |
| | 3 | 47 | | }); |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// A list of namespaces that will be imported when evaluating a C# expression. |
| | | 51 | | /// </summary> |
| | 3 | 52 | | public ISet<string> Namespaces { get; } = new HashSet<string>(new[] |
| | 3 | 53 | | { |
| | 3 | 54 | | typeof(Globals).Namespace!, // Elsa.Expressions.CSharp |
| | 3 | 55 | | typeof(Enumerable).Namespace!, // System.Linq |
| | 3 | 56 | | typeof(Guid).Namespace!, // System |
| | 3 | 57 | | typeof(JsonSerializer).Namespace!, // System.Text.Json |
| | 3 | 58 | | typeof(JsonConverter).Namespace!, // System.Text.Json.Serialization |
| | 3 | 59 | | typeof(JsonNode).Namespace!, // System.Text.Json.Nodes |
| | 3 | 60 | | typeof(IDictionary<string, object>).Namespace!, // System.Collections.Generic |
| | 3 | 61 | | typeof(ExpandoObject).Namespace!, // System.Dynamic |
| | 3 | 62 | | }); |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// The timeout for script caching. |
| | | 66 | | /// </summary> |
| | | 67 | | /// <remarks> |
| | | 68 | | /// The <c>ScriptCacheTimeout</c> property specifies the duration for which the scripts are cached in the C# engine. |
| | | 69 | | /// If the value of <c>ScriptCacheTimeout</c> is <c>null</c>, the scripts are cached indefinitely. If a time value i |
| | | 70 | | /// </remarks> |
| | 9 | 71 | | public TimeSpan? ScriptCacheTimeout { get; set; } = TimeSpan.FromDays(1); |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Disables the generation of variable wrappers. E.g. <c>Variables.MyVariable</c> will no longer be available for v |
| | | 75 | | /// This is useful if your application requires the use of invalid JavaScript variable names. |
| | | 76 | | /// </summary> |
| | 9 | 77 | | public bool DisableWrappers { get; set; } |
| | | 78 | | |
| | | 79 | | /// <summary> |
| | | 80 | | /// Configures the <see cref="ScriptOptions"/>. |
| | | 81 | | /// </summary> |
| | | 82 | | /// <param name="configurator">A callback that is invoked before a C# expression is evaluated. Use this to configure |
| | | 83 | | public CSharpOptions ConfigureScriptOptions(Action<ScriptOptionsConfigurationContext> configurator) |
| | | 84 | | { |
| | 0 | 85 | | ConfigureScriptOptionsCallbacks.Add((scriptOptions, context) => |
| | 0 | 86 | | { |
| | 0 | 87 | | configurator(new ScriptOptionsConfigurationContext(scriptOptions, context)); |
| | 0 | 88 | | return scriptOptions; |
| | 0 | 89 | | }); |
| | 0 | 90 | | return this; |
| | | 91 | | } |
| | | 92 | | |
| | | 93 | | /// <summary> |
| | | 94 | | /// Appends a C# script to the current script. |
| | | 95 | | /// </summary> |
| | | 96 | | public CSharpOptions AppendScript(string script) |
| | | 97 | | { |
| | 6 | 98 | | ConfigureScriptCallbacks.Add((s, _) => s.ContinueWith(script)); |
| | 6 | 99 | | return this; |
| | | 100 | | } |
| | | 101 | | |
| | | 102 | | /// <summary> |
| | | 103 | | /// Appends a C# script to the current script. |
| | | 104 | | /// </summary> |
| | | 105 | | public CSharpOptions AppendScript(Func<ExpressionExecutionContext, string> script) |
| | | 106 | | { |
| | 0 | 107 | | ConfigureScriptCallbacks.Add((s, c) => s.ContinueWith(script(c))); |
| | 0 | 108 | | return this; |
| | | 109 | | } |
| | | 110 | | } |