elsa-core/src/modules/Elsa.CSharp/Activities/RunCSharp/RunCSharp.cs

73 lines
2.6 KiB
C#
Raw Normal View History

using System.Runtime.CompilerServices;
using Elsa.CSharp.Contracts;
using Elsa.CSharp.Extensions;
using Elsa.CSharp.Models;
using Elsa.Expressions.Models;
using Elsa.Extensions;
using Elsa.Workflows.Core;
using Elsa.Workflows.Core.Attributes;
using Elsa.Workflows.Core.Models;
2022-03-07 14:42:12 +00:00
// ReSharper disable once CheckNamespace
namespace Elsa.CSharp.Activities;
2022-03-07 14:42:12 +00:00
2023-03-14 07:14:14 +00:00
/// <summary>
/// Executes C# code.
2023-03-14 07:14:14 +00:00
/// </summary>
[Activity("Elsa", "Scripting", "Executes C# code", DisplayName = "Run C#")]
public class RunCSharp : CodeActivity<object?>
2022-03-07 14:42:12 +00:00
{
2023-03-14 07:14:14 +00:00
/// <inheritdoc />
public RunCSharp([CallerFilePath] string? source = default, [CallerLineNumber] int? line = default) : base(source, line)
2022-03-07 14:42:12 +00:00
{
}
2023-03-14 07:14:14 +00:00
/// <inheritdoc />
public RunCSharp(string script, [CallerFilePath] string? source = default, [CallerLineNumber] int? line = default) : this(source, line)
2022-03-07 14:42:12 +00:00
{
Script = new Input<string>(script);
}
2023-11-03 21:01:19 +00:00
2023-03-14 07:14:14 +00:00
/// <summary>
/// The script to run.
/// </summary>
[Input(
Description = "The script to run.",
UIHint = InputUIHints.CodeEditor,
OptionsProvider = typeof(RunCSharpOptionsProvider)
2023-03-14 07:14:14 +00:00
)]
2022-03-07 14:42:12 +00:00
public Input<string> Script { get; set; } = new("");
/// <summary>
/// A list of possible outcomes. Use "SetOutcome(string)" to set the outcome. Use "SetOutcomes(params string[])" to set multiple outcomes.
/// </summary>
[Input(Description = "A list of possible outcomes.", UIHint = InputUIHints.DynamicOutcomes)]
public Input<ICollection<string>> PossibleOutcomes { get; set; } = default!;
2022-03-07 14:42:12 +00:00
2023-03-14 07:14:14 +00:00
/// <inheritdoc />
2022-03-07 14:42:12 +00:00
protected override async ValueTask ExecuteAsync(ActivityExecutionContext context)
{
var script = context.Get(Script);
// If no script was specified, there's nothing to do.
if (string.IsNullOrWhiteSpace(script))
return;
// Get a C# evaluator.
var evaluator = context.GetRequiredService<ICSharpEvaluator>();
2022-03-07 14:42:12 +00:00
// Run the script.
var options = new ExpressionEvaluatorOptions();
var result = await evaluator.EvaluateAsync(script, typeof(object), context.ExpressionExecutionContext, options, context.CancellationToken);
2022-03-07 14:42:12 +00:00
// Set the result as output, if any.
if (result is not null)
context.Set(Result, result);
// Get the outcome or outcomes set by the script, if any. If not set, use "Done".
2023-11-03 21:01:19 +00:00
var outcomes = context.ExpressionExecutionContext.TransientProperties.GetValueOrDefault(OutcomeProxy.OutcomePropertiesKey, () => new[] { "Done" })!;
// Complete the activity with the outcome.
await context.CompleteActivityWithOutcomesAsync(outcomes);
}
2022-03-07 14:42:12 +00:00
}