2019-07-15 07:53:48 +00:00
using System.IO ;
using System.Threading ;
using System.Threading.Tasks ;
2019-09-21 22:02:04 +00:00
using Elsa.Attributes ;
2019-07-15 07:53:48 +00:00
using Elsa.Services ;
using Elsa.Services.Models ;
namespace Elsa.Activities.Console.Activities
{
/// <summary>
/// Reads input from the console.
/// </summary>
2019-09-21 22:02:04 +00:00
[ ActivityDefinition (
Category = "Console" ,
2019-10-26 10:46:20 +00:00
Description = "Read text from standard in." ,
2019-11-09 12:57:00 +00:00
Icon = "fas fa-terminal" ,
2019-10-26 10:46:20 +00:00
RuntimeDescription = "a => !!a.state.variableName ? `Read text from standard in and store into <strong>${ a.state.variableName }</strong>.` : 'Read text from standard in.'" ,
2019-09-21 22:02:04 +00:00
Outcomes = new [ ] { OutcomeNames . Done }
) ]
2019-07-15 07:53:48 +00:00
public class ReadLine : Activity
{
private readonly TextReader input ;
2019-10-24 19:45:19 +00:00
public ReadLine ( )
2019-07-15 07:53:48 +00:00
{
}
public ReadLine ( TextReader input )
{
this . input = input ;
}
2019-07-25 20:00:44 +00:00
2019-09-21 22:02:04 +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
2019-07-15 07:53:48 +00:00
{
get = > GetState < string > ( ) ;
set = > SetState ( value ) ;
}
2019-07-25 20:00:44 +00:00
2019-11-24 14:25:16 +00:00
protected override async Task < IActivityExecutionResult > OnExecuteAsync ( WorkflowExecutionContext context , CancellationToken cancellationToken )
2019-07-15 07:53:48 +00:00
{
if ( input = = null )
return Halt ( ) ;
var receivedInput = await input . ReadLineAsync ( ) ;
return Execute ( context , receivedInput ) ;
}
2019-11-24 14:25:16 +00:00
protected override IActivityExecutionResult OnResume ( WorkflowExecutionContext context )
2019-07-15 07:53:48 +00:00
{
2019-11-17 19:43:16 +00:00
var receivedInput = context . Workflow . Input . GetVariable < string > ( "ReadLineInput" ) ;
2019-07-15 07:53:48 +00:00
return Execute ( context , receivedInput ) ;
}
2019-11-24 14:25:16 +00:00
private IActivityExecutionResult Execute ( WorkflowExecutionContext workflowContext , string receivedInput )
2019-07-15 07:53:48 +00:00
{
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
2019-11-24 14:25:16 +00:00
return Done ( receivedInput ) ;
2019-07-15 07:53:48 +00:00
}
}
2018-10-14 15:16:33 +00:00
}