elsa-core/src/activities/Elsa.Activities.Console/Activities/ReadLine.cs

63 lines
2.1 KiB
C#
Raw Normal View History

using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Elsa.Attributes;
using Elsa.Services;
using Elsa.Services.Models;
namespace Elsa.Activities.Console.Activities
{
/// <summary>
/// Reads input from the console.
/// </summary>
[ActivityDefinition(
Category = "Console",
Description = "Read text from standard in.",
2019-11-09 12:57:00 +00:00
Icon = "fas fa-terminal",
RuntimeDescription = "a => !!a.state.variableName ? `Read text from standard in and store into <strong>${ a.state.variableName }</strong>.` : 'Read text from standard in.'",
Outcomes = new[] { OutcomeNames.Done }
)]
public class ReadLine : Activity
{
private readonly TextReader input;
2019-10-24 19:45:19 +00:00
public ReadLine()
{
}
public ReadLine(TextReader input)
{
this.input = input;
}
2019-07-25 20:00:44 +00:00
[ActivityProperty(Hint = "The name of the variable to store the value into.")]
2019-07-25 20:00:44 +00:00
public string VariableName
{
get => GetState<string>();
set => SetState(value);
}
2019-07-25 20:00:44 +00:00
protected override async Task<IActivityExecutionResult> OnExecuteAsync(WorkflowExecutionContext context, CancellationToken cancellationToken)
{
if (input == null)
return Halt();
var receivedInput = await input.ReadLineAsync();
return Execute(context, receivedInput);
}
protected override IActivityExecutionResult OnResume(WorkflowExecutionContext context)
{
var receivedInput = context.Workflow.Input.GetVariable<string>("ReadLineInput");
return Execute(context, receivedInput);
}
private IActivityExecutionResult Execute(WorkflowExecutionContext workflowContext, string receivedInput)
{
2019-07-25 20:00:44 +00:00
if (!string.IsNullOrWhiteSpace(VariableName))
workflowContext.CurrentScope.SetVariable(VariableName, receivedInput);
2019-11-09 12:57:00 +00:00
return Done(receivedInput);
}
}
}