2023-08-06 18:55:24 +00:00
|
|
|
|
using System.Runtime.CompilerServices;
|
2023-11-03 19:28:10 +00:00
|
|
|
|
using Elsa.CSharp.Contracts;
|
|
|
|
|
|
using Elsa.CSharp.Extensions;
|
|
|
|
|
|
using Elsa.CSharp.Models;
|
2023-09-07 18:34:53 +00:00
|
|
|
|
using Elsa.Extensions;
|
2023-03-03 13:48:05 +00:00
|
|
|
|
using Elsa.Workflows.Core;
|
2022-05-26 10:47:31 +00:00
|
|
|
|
using Elsa.Workflows.Core.Attributes;
|
|
|
|
|
|
using Elsa.Workflows.Core.Models;
|
2022-03-07 14:42:12 +00:00
|
|
|
|
|
2023-09-07 18:34:53 +00:00
|
|
|
|
// ReSharper disable once CheckNamespace
|
2023-11-03 19:28:10 +00:00
|
|
|
|
namespace Elsa.CSharp.Activities;
|
2022-03-07 14:42:12 +00:00
|
|
|
|
|
2023-03-14 07:14:14 +00:00
|
|
|
|
/// <summary>
|
2023-11-03 19:28:10 +00:00
|
|
|
|
/// Executes C# code.
|
2023-03-14 07:14:14 +00:00
|
|
|
|
/// </summary>
|
2023-11-03 19:28:10 +00:00
|
|
|
|
[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 />
|
2023-11-03 19:28:10 +00:00
|
|
|
|
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 />
|
2023-11-03 19:28:10 +00:00
|
|
|
|
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-09-07 18:43:12 +00:00
|
|
|
|
/// <summary>
|
2023-11-03 19:28:10 +00:00
|
|
|
|
/// A list of possible outcomes. Use "SetOutcome(string)" to set the outcome. Use "SetOutcomes(params string[])" to set multiple outcomes.
|
2023-09-07 18:43:12 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
[Input(Description = "A list of possible outcomes.", UIHint = InputUIHints.DynamicOutcomes)]
|
|
|
|
|
|
public Input<ICollection<string>> PossibleOutcomes { get; set; } = default!;
|
2022-11-28 18:12:01 +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,
|
2023-11-03 19:28:10 +00:00
|
|
|
|
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("");
|
|
|
|
|
|
|
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 JavaScript evaluator.
|
2023-11-03 19:28:10 +00:00
|
|
|
|
var evaluator = context.GetRequiredService<ICSharpEvaluator>();
|
2022-11-28 18:12:01 +00:00
|
|
|
|
|
2022-03-07 14:42:12 +00:00
|
|
|
|
// Run the script.
|
2023-11-03 19:28:10 +00:00
|
|
|
|
var result = await evaluator.EvaluateAsync(script, typeof(object), context.ExpressionExecutionContext, context.CancellationToken);
|
2022-03-07 14:42:12 +00:00
|
|
|
|
|
|
|
|
|
|
// Set the result as output, if any.
|
2023-08-06 18:55:24 +00:00
|
|
|
|
if (result is not null)
|
2023-06-23 16:42:07 +00:00
|
|
|
|
context.Set(Result, result);
|
2023-09-07 18:34:53 +00:00
|
|
|
|
|
|
|
|
|
|
// 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" })!;
|
2023-09-07 18:34:53 +00:00
|
|
|
|
|
|
|
|
|
|
// Complete the activity with the outcome.
|
|
|
|
|
|
await context.CompleteActivityWithOutcomesAsync(outcomes);
|
|
|
|
|
|
}
|
2022-03-07 14:42:12 +00:00
|
|
|
|
}
|