< Summary

Information
Class: Elsa.Expressions.CSharp.Activities.RunCSharp
Assembly: Elsa.Expressions.CSharp
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Expressions.CSharp/Activities/RunCSharp/RunCSharp.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 18
Coverable lines: 18
Total lines: 75
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 4
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
.ctor(...)100%210%
get_Script()100%210%
get_PossibleOutcomes()100%210%
ExecuteAsync()0%2040%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Expressions.CSharp/Activities/RunCSharp/RunCSharp.cs

#LineLine coverage
 1using System.Runtime.CompilerServices;
 2using Elsa.Expressions.CSharp.Contracts;
 3using Elsa.Expressions.CSharp.Extensions;
 4using Elsa.Expressions.CSharp.Models;
 5using Elsa.Expressions.Models;
 6using Elsa.Extensions;
 7using Elsa.Workflows;
 8using Elsa.Workflows.Attributes;
 9using Elsa.Workflows.UIHints;
 10using Elsa.Workflows.Models;
 11
 12// ReSharper disable once CheckNamespace
 13namespace Elsa.Expressions.CSharp.Activities;
 14
 15/// <summary>
 16/// Executes C# code.
 17/// </summary>
 18[Activity("Elsa", "Scripting", "Executes C# code", DisplayName = "Run C#")]
 19public class RunCSharp : CodeActivity<object?>
 20{
 21    /// <inheritdoc />
 022    public RunCSharp([CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : base(source, line)
 23    {
 024    }
 25
 26    /// <inheritdoc />
 027    public RunCSharp(string script, [CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : this(
 28    {
 029        Script = new Input<string>(script);
 030    }
 31
 32    /// <summary>
 33    /// The script to run.
 34    /// </summary>
 35    [Input(
 36        Description = "The script to run.",
 37        DefaultSyntax = "CSharp",
 38        UIHint = InputUIHints.CodeEditor,
 39        UIHandler = typeof(RunCSharpOptionsProvider)
 40    )]
 041    public Input<string> Script { get; set; } = new("");
 42
 43    /// <summary>
 44    /// A list of possible outcomes. Use "SetOutcome(string)" to set the outcome. Use "SetOutcomes(params string[])" to 
 45    /// </summary>
 46    [Input(Description = "A list of possible outcomes.", UIHint = InputUIHints.DynamicOutcomes)]
 047    public Input<ICollection<string>> PossibleOutcomes { get; set; } = null!;
 48
 49    /// <inheritdoc />
 50    protected override async ValueTask ExecuteAsync(ActivityExecutionContext context)
 51    {
 052        var script = context.Get(Script);
 53
 54        // If no script was specified, there's nothing to do.
 055        if (string.IsNullOrWhiteSpace(script))
 056            return;
 57
 58        // Get a C# evaluator.
 059        var evaluator = context.GetRequiredService<ICSharpEvaluator>();
 60
 61        // Run the script.
 062        var options = new ExpressionEvaluatorOptions();
 063        var result = await evaluator.EvaluateAsync(script, typeof(object), context.ExpressionExecutionContext, options, 
 64
 65        // Set the result as output, if any.
 066        if (result is not null)
 067            context.Set(Result, result);
 68
 69        // Get the outcome or outcomes set by the script, if any. If not set, use "Done".
 070        var outcomes = context.ExpressionExecutionContext.TransientProperties.GetValueOrDefault(OutcomeProxy.OutcomeProp
 71
 72        // Complete the activity with the outcome.
 073        await context.CompleteActivityWithOutcomesAsync(outcomes);
 074    }
 75}