mirror of
https://github.com/revenz/FileFlowsPlugins.git
synced 2025-12-21 13:09:32 -06:00
FF-1662: New flow element Write Text
This commit is contained in:
102
BasicNodes/File/WriteText.cs
Normal file
102
BasicNodes/File/WriteText.cs
Normal file
@@ -0,0 +1,102 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using FileFlows.BasicNodes;
|
||||
using FileFlows.Plugin;
|
||||
using FileFlows.Plugin.Attributes;
|
||||
|
||||
namespace BasicNodes.File;
|
||||
|
||||
/// <summary>
|
||||
/// A flow element that writes text to a file
|
||||
/// </summary>
|
||||
public class WriteText : Node
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public override int Inputs => 1;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override int Outputs => 1;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override FlowElementType Type => FlowElementType.Process;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string Icon => "fas fa-file-signature";
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string Group => "File";
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string HelpUrl => "https://fileflows.com/docs/plugins/basic-nodes/write-text";
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the output text location
|
||||
/// </summary>
|
||||
[TextVariable(1)]
|
||||
[Required]
|
||||
public string File { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the text to write, if blank writes the current file
|
||||
/// </summary>
|
||||
[TextVariable(2)]
|
||||
public string Text { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public override int Execute(NodeParameters args)
|
||||
{
|
||||
string file = args.ReplaceVariables(File, stripMissing: true);
|
||||
if (string.IsNullOrWhiteSpace(file))
|
||||
{
|
||||
args.FailureReason = "No file set";
|
||||
args.Logger?.ELog(args.FailureReason);
|
||||
return -1;
|
||||
}
|
||||
|
||||
string text = GetText(args, Text, file);
|
||||
|
||||
args.Logger?.ILog($"Text: {text}");
|
||||
args.Logger?.ILog($"File: {file}");
|
||||
|
||||
var result = args.FileService.FileAppendAllText(file, text);
|
||||
if (result.Failed(out var error))
|
||||
{
|
||||
args.FailureReason = "File writing file: " + error;
|
||||
args.Logger.ELog(args.FailureReason);
|
||||
return -1;
|
||||
}
|
||||
|
||||
args.Logger?.ILog($"Text written to file");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the text to write
|
||||
/// </summary>
|
||||
/// <param name="args">the node parameters</param>
|
||||
/// <param name="text">the original text</param>
|
||||
/// <param name="outputFile">the file to write to</param>
|
||||
/// <returns>the text to write</returns>
|
||||
public static string GetText(NodeParameters args, string text, string outputFile)
|
||||
{
|
||||
if (outputFile?.ToLowerInvariant().EndsWith(".csv") == true)
|
||||
{
|
||||
var parts = text?.Split([";", ","], StringSplitOptions.RemoveEmptyEntries) ?? [];
|
||||
if (parts.Length == 0)
|
||||
{
|
||||
return CsvEncode(args.WorkingFile);
|
||||
}
|
||||
|
||||
return string.Join(",", parts.Select(x => CsvEncode(args.ReplaceVariables(x, stripMissing: true))));
|
||||
}
|
||||
|
||||
return args.ReplaceVariables(text, stripMissing: true)?.EmptyAsNull() ?? args.WorkingFile;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// CSV encodes a string
|
||||
/// </summary>
|
||||
/// <param name="text">the text to encode</param>
|
||||
/// <returns>the CSV encoded string</returns>
|
||||
private static string CsvEncode(string text)
|
||||
=> $"\"{text.Replace("\"", "\"\"")}\"";
|
||||
}
|
||||
52
BasicNodes/Tests/WriteTextTests.cs
Normal file
52
BasicNodes/Tests/WriteTextTests.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
#if(DEBUG)
|
||||
|
||||
using BasicNodes.File;
|
||||
|
||||
namespace BasicNodes.Tests;
|
||||
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
|
||||
[TestClass]
|
||||
public class WriteTextTests : TestBase
|
||||
{
|
||||
[TestMethod]
|
||||
public void WorkingFile_Csv()
|
||||
{
|
||||
var args = new FileFlows.Plugin.NodeParameters(@"/test/file.mkv", new TestLogger(), false, string.Empty, new LocalFileService());
|
||||
|
||||
var output = WriteText.GetText(args, "", "file.csv");
|
||||
Assert.AreEqual("\"/test/file.mkv\"", output);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void WorkingFile_Text()
|
||||
{
|
||||
var args = new FileFlows.Plugin.NodeParameters(@"/test/file.mkv", new TestLogger(), false, string.Empty, new LocalFileService());
|
||||
|
||||
var output = WriteText.GetText(args, "", "file.txt");
|
||||
Assert.AreEqual("/test/file.mkv", output);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CsvArgs()
|
||||
{
|
||||
var args = new FileFlows.Plugin.NodeParameters(@"/test/file.mkv", new TestLogger(), false, string.Empty, new LocalFileService());
|
||||
args.Variables["file.Name"] = "file.mkv";
|
||||
args.Variables["ext"] = "mkv";
|
||||
|
||||
var output = WriteText.GetText(args, "{file.Name};{ext}", "file.csv");
|
||||
Assert.AreEqual("\"file.mkv\",\"mkv\"", output);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CsvArg()
|
||||
{
|
||||
var args = new FileFlows.Plugin.NodeParameters(@"/test/file.mkv", new TestLogger(), false, string.Empty, new LocalFileService());
|
||||
args.Variables["file.Name"] = "file.mkv";
|
||||
|
||||
var output = WriteText.GetText(args, "{file.Name}", "file.csv");
|
||||
Assert.AreEqual("\"file.mkv\"", output);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -501,6 +501,18 @@
|
||||
"Options": "Options",
|
||||
"Options-Help": "The string to match against."
|
||||
}
|
||||
},
|
||||
"WriteText": {
|
||||
"Description": "Writes text to a file",
|
||||
"Outputs": {
|
||||
"1": "Text written to file"
|
||||
},
|
||||
"Fields": {
|
||||
"File": "File",
|
||||
"File-Help": "The file to write the text to.",
|
||||
"Text": "Text",
|
||||
"Text-Help": "The text to write to the file. If left blank the current working file full path will be written."
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user