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