diff --git a/src/Nox.Cli.Plugins/Nox.Cli.Plugin.File/FileRename_v1.cs b/src/Nox.Cli.Plugins/Nox.Cli.Plugin.File/FileRenameFile_v1.cs similarity index 98% rename from src/Nox.Cli.Plugins/Nox.Cli.Plugin.File/FileRename_v1.cs rename to src/Nox.Cli.Plugins/Nox.Cli.Plugin.File/FileRenameFile_v1.cs index b22c76d1..60c9bda2 100644 --- a/src/Nox.Cli.Plugins/Nox.Cli.Plugin.File/FileRename_v1.cs +++ b/src/Nox.Cli.Plugins/Nox.Cli.Plugin.File/FileRenameFile_v1.cs @@ -3,7 +3,7 @@ namespace Nox.Cli.Plugin.File; -public class FileRename_v1 : INoxCliAddin +public class FileRenameFile_v1 : INoxCliAddin { public NoxActionMetaData Discover() { diff --git a/src/Nox.Cli.Plugins/Nox.Cli.Plugin.File/FileRenameFolder_v1.cs b/src/Nox.Cli.Plugins/Nox.Cli.Plugin.File/FileRenameFolder_v1.cs new file mode 100644 index 00000000..fa22dc5d --- /dev/null +++ b/src/Nox.Cli.Plugins/Nox.Cli.Plugin.File/FileRenameFolder_v1.cs @@ -0,0 +1,86 @@ +using Nox.Cli.Abstractions; +using Nox.Cli.Abstractions.Extensions; + +namespace Nox.Cli.Plugin.File; + +public class FileRenameFolder_v1 : INoxCliAddin +{ + public NoxActionMetaData Discover() + { + return new NoxActionMetaData + { + Name = "file/rename-folder@v1", + Author = "Jan Schutte", + Description = "Rename a folder.", + + Inputs = + { + ["source-path"] = new NoxActionInput { + Id = "source-path", + Description = "The path to the folder to be renamed", + Default = string.Empty, + IsRequired = true + }, + + ["new-name"] = new NoxActionInput { + Id = "new-name", + Description = "The new name of the folder", + Default = string.Empty, + IsRequired = true + } + } + }; + } + + private string? _sourcePath; + private string? _newName; + + public Task BeginAsync(IDictionary inputs) + { + _sourcePath = inputs.Value("source-path"); + _newName = inputs.Value("new-name"); + return Task.CompletedTask; + } + + public Task> ProcessAsync(INoxWorkflowContext ctx) + { + var outputs = new Dictionary(); + + ctx.SetState(ActionState.Error); + + if (string.IsNullOrEmpty(_sourcePath) || + string.IsNullOrEmpty(_newName)) + { + ctx.SetErrorMessage("The File rename-folder action was not initialized"); + } + else + { + try + { + var fullSourcePath = Path.GetFullPath(_sourcePath); + if (!System.IO.Directory.Exists(fullSourcePath)) + { + ctx.SetErrorMessage($"Folder: {fullSourcePath} does not exist!"); + } + else + { + var sourcePath = Path.GetDirectoryName(fullSourcePath); + Directory.Move(fullSourcePath, Path.Combine(sourcePath!, _newName)); + ctx.SetState(ActionState.Success); + } + + } + catch (Exception ex) + { + ctx.SetErrorMessage(ex.Message); + } + } + + return Task.FromResult>(outputs); + } + + public Task EndAsync() + { + return Task.CompletedTask; + } +} \ No newline at end of file diff --git a/src/Plugin.File.Tests/FileRenameTests.cs b/src/Plugin.File.Tests/FileRenameTests.cs index 32d98f01..8bd1acc6 100644 --- a/src/Plugin.File.Tests/FileRenameTests.cs +++ b/src/Plugin.File.Tests/FileRenameTests.cs @@ -21,7 +21,7 @@ public async Task Can_Rename_a_File() System.IO.File.Delete(Path.Combine(path, "Sample-After.txt")); System.IO.File.Copy(Path.Combine(path, "Sample.txt"), Path.Combine(path, "Sample-Before.txt")); - var plugin = new FileRename_v1(); + var plugin = new FileRenameFile_v1(); var inputs = new Dictionary { {"source-path", "./files/rename/Sample-Before.txt"}, diff --git a/src/Plugin.File.Tests/FolderRenameTests.cs b/src/Plugin.File.Tests/FolderRenameTests.cs new file mode 100644 index 00000000..409b9c77 --- /dev/null +++ b/src/Plugin.File.Tests/FolderRenameTests.cs @@ -0,0 +1,69 @@ +using System.ServiceModel; +using Moq; +using Nox.Cli.Abstractions.Caching; +using Nox.Cli.Actions; +using Nox.Cli.Configuration; +using Nox.Cli.Plugin.File; +using Nox.Cli.Variables.Secrets; +using Nox.Secrets.Abstractions; +using Nox.Solution; +using Xunit; + +namespace Plugin.File.Tests; + +public class FolderRenameTests +{ + [Fact] + public async Task Can_Rename_a_Folder() + { + var path = "./files/rename"; + //Copy the sample folder for the test + if (Directory.Exists(Path.Combine(path, "Sample-Before"))) + { + Directory.Delete(Path.Combine(path, "Sample-Before")); + } + + if (Directory.Exists(Path.Combine(path, "Sample-After"))) + { + Directory.Delete(Path.Combine(path, "Sample-After")); + } + + CopyRecursively(Path.Combine(path, "Sample"), Path.Combine(path, "Sample-Before")); + + var plugin = new FileRenameFolder_v1(); + var inputs = new Dictionary + { + {"source-path", "./files/rename/Sample-Before"}, + {"new-name", "Sample-After"} + }; + await plugin.BeginAsync(inputs); + var wfConfig = new WorkflowConfiguration(); + var sln = Mock.Of(); + var orgResolver = Mock.Of(); + var cacheMan = Mock.Of(); + var lteConfig = Mock.Of(); + var secretsResolver = Mock.Of(); + var ctx = new NoxWorkflowContext(wfConfig, sln, orgResolver, cacheMan, lteConfig, secretsResolver); + await plugin.ProcessAsync(ctx); + Assert.True(System.IO.Directory.Exists(Path.Combine(path, "Sample-After"))); + Assert.False(System.IO.Directory.Exists(Path.Combine(path, "Sample-Before"))); + Assert.Single(Directory.GetFiles(Path.Combine(path, "Sample-After"))); + } + + private void CopyRecursively(string sourcePath, string targetPath) + { + //Now Create all the directories + foreach (string dirPath in Directory.GetDirectories(sourcePath, "*", SearchOption.AllDirectories)) + { + Directory.CreateDirectory(dirPath.Replace(sourcePath, targetPath)); + } + + Directory.CreateDirectory(targetPath); + + //Copy all the files & Replaces any files with the same name + foreach (string newPath in Directory.GetFiles(sourcePath, "*.*",SearchOption.AllDirectories)) + { + System.IO.File.Copy(newPath, newPath.Replace(sourcePath, targetPath), true); + } + } +} \ No newline at end of file diff --git a/src/Plugin.File.Tests/Plugin.File.Tests.csproj b/src/Plugin.File.Tests/Plugin.File.Tests.csproj index 47be36af..bd3e8ab3 100644 --- a/src/Plugin.File.Tests/Plugin.File.Tests.csproj +++ b/src/Plugin.File.Tests/Plugin.File.Tests.csproj @@ -24,6 +24,9 @@ Always + + Always + diff --git a/src/Plugin.File.Tests/files/rename/Sample/Sample.txt b/src/Plugin.File.Tests/files/rename/Sample/Sample.txt new file mode 100644 index 00000000..e69de29b