< Summary

Information
Class: Elsa.Expressions.CSharp.Options.CSharpOptions
Assembly: Elsa.Expressions.CSharp
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Expressions.CSharp/Options/CSharpOptions.cs
Line coverage
77%
Covered lines: 28
Uncovered lines: 8
Coverable lines: 36
Total lines: 110
Line coverage: 77.7%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_AllowHostCodeExecution()100%11100%
get_ConfigureScriptOptionsCallbacks()100%11100%
get_ConfigureScriptCallbacks()100%11100%
get_Assemblies()100%11100%
.ctor()100%11100%
get_Namespaces()100%11100%
get_ScriptCacheTimeout()100%11100%
get_DisableWrappers()100%11100%
ConfigureScriptOptions(...)100%210%
AppendScript(...)100%11100%
AppendScript(...)100%210%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Expressions.CSharp/Options/CSharpOptions.cs

#LineLine coverage
 1using System.Dynamic;
 2using System.Reflection;
 3using System.Text.Json;
 4using System.Text.Json.Nodes;
 5using System.Text.Json.Serialization;
 6using Microsoft.CSharp.RuntimeBinder;
 7using Elsa.Expressions.CSharp.Contexts;
 8using Elsa.Expressions.CSharp.Models;
 9using Elsa.Expressions.Models;
 10using Microsoft.CodeAnalysis.Scripting;
 11
 12namespace Elsa.Expressions.CSharp.Options;
 13
 14/// <summary>
 15/// Options for the C# expression evaluator.
 16/// </summary>
 17public 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>
 4223    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>
 328    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>
 933    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>
 338    public ISet<Assembly> Assemblies { get; } = new HashSet<Assembly>(new[]
 339    {
 340        typeof(Globals).Assembly, // Elsa.Expressions.CSharp
 341        typeof(Enumerable).Assembly, // System.Linq
 342        typeof(Guid).Assembly, // System.Runtime
 343        typeof(JsonSerializer).Assembly, // System.Text.Json
 344        typeof(IDictionary<string, object>).Assembly, // System.Collections
 345        typeof(ExpandoObject).Assembly, // System.Linq.Expressions assembly — hosts System.Dynamic.ExpandoObject in .NET
 346        typeof(CSharpArgumentInfo).Assembly, // Microsoft.CSharp (required for dynamic dispatch)
 347    });
 48
 49    /// <summary>
 50    /// A list of namespaces that will be imported when evaluating a C# expression.
 51    /// </summary>
 352    public ISet<string> Namespaces { get; } = new HashSet<string>(new[]
 353    {
 354        typeof(Globals).Namespace!, // Elsa.Expressions.CSharp
 355        typeof(Enumerable).Namespace!, // System.Linq
 356        typeof(Guid).Namespace!, // System
 357        typeof(JsonSerializer).Namespace!, // System.Text.Json
 358        typeof(JsonConverter).Namespace!, // System.Text.Json.Serialization
 359        typeof(JsonNode).Namespace!, // System.Text.Json.Nodes
 360        typeof(IDictionary<string, object>).Namespace!, // System.Collections.Generic
 361        typeof(ExpandoObject).Namespace!, // System.Dynamic
 362    });
 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>
 971    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>
 977    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    {
 085        ConfigureScriptOptionsCallbacks.Add((scriptOptions, context) =>
 086        {
 087            configurator(new ScriptOptionsConfigurationContext(scriptOptions, context));
 088            return scriptOptions;
 089        });
 090        return this;
 91    }
 92
 93    /// <summary>
 94    /// Appends a C# script to the current script.
 95    /// </summary>
 96    public CSharpOptions AppendScript(string script)
 97    {
 698        ConfigureScriptCallbacks.Add((s, _) => s.ContinueWith(script));
 699        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    {
 0107        ConfigureScriptCallbacks.Add((s, c) => s.ContinueWith(script(c)));
 0108        return this;
 109    }
 110}