using System.ComponentModel;
using FileFlows.Plugin;
using FileFlows.Plugin.Attributes;
namespace FileFlows.BasicNodes.Scripting;
///
/// Base for a script
///
public abstract class ScriptBase : Node
{
///
public override int Inputs => 1;
///
public override FlowElementType Type => FlowElementType.Process;
///
public override bool FailureNode => true;
///
/// Gets or sets the number of outputs
///
[DefaultValue(1)]
[NumberInt(1)]
public new int Outputs { get; set; }
///
/// Gets the language of this script
///
protected abstract ScriptLanguage Language { get; }
///
/// Gets or sets the code of the script
///
public virtual string Code { get; set; }
///
public override int Execute(NodeParameters args)
{
if (string.IsNullOrEmpty(Code))
{
args.FailureReason = $"No code specified in {Language} script";
args.Logger?.ELog(args.FailureReason);
return -1; // no code, flow cannot continue doesn't know what to do
}
var result = args.ScriptExecutor.Execute(new()
{
Args = args,
Logger = args.Logger,
TempPath = args.TempPath,
Code = Language is ScriptLanguage.CSharp or ScriptLanguage.JavaScript ? Code : args.ReplaceVariables(Code),
ScriptType = ScriptType.Flow,
Language = Language,
NotificationCallback = args.NotificationCallback
});
if (result.Failed(out var error))
{
args.FailureReason = error;
args.Logger?.ELog(error);
return -1;
}
if (result.Value > Outputs)
{
args.FailureReason = "Unexpected output: " + result.Value;
args.Logger?.ELog(args.FailureReason);
return -1;
}
return result.Value;
}
}