diff --git a/src/Nox.Cli.Plugins/Nox.Cli.Plugin.File/FileRename_v1.cs b/src/Nox.Cli.Plugins/Nox.Cli.Plugin.File/FileRename_v1.cs new file mode 100644 index 0000000..b22c76d --- /dev/null +++ b/src/Nox.Cli.Plugins/Nox.Cli.Plugin.File/FileRename_v1.cs @@ -0,0 +1,86 @@ +using Nox.Cli.Abstractions; +using Nox.Cli.Abstractions.Extensions; + +namespace Nox.Cli.Plugin.File; + +public class FileRename_v1 : INoxCliAddin +{ + public NoxActionMetaData Discover() + { + return new NoxActionMetaData + { + Name = "file/rename-file@v1", + Author = "Jan Schutte", + Description = "Rename a file.", + + Inputs = + { + ["source-path"] = new NoxActionInput { + Id = "source-path", + Description = "The path to the file to be renamed", + Default = string.Empty, + IsRequired = true + }, + + ["new-name"] = new NoxActionInput { + Id = "new-name", + Description = "The new name of the file", + 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-file action was not initialized"); + } + else + { + try + { + var fullSourcePath = Path.GetFullPath(_sourcePath); + if (!System.IO.File.Exists(fullSourcePath)) + { + ctx.SetErrorMessage($"File: {fullSourcePath} does not exist!"); + } + else + { + var sourcePath = Path.GetDirectoryName(fullSourcePath); + System.IO.File.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/Nox.Cli.sln b/src/Nox.Cli.sln index 598b66b..36b95d6 100755 --- a/src/Nox.Cli.sln +++ b/src/Nox.Cli.sln @@ -73,6 +73,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "plugins", "plugins", "{BDFC EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Plugin.Core.Tests", "..\tests\Plugin.Core.Tests\Plugin.Core.Tests.csproj", "{BD914F56-8686-40BB-996E-0F89DC613A35}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Plugin.File.Tests", "Plugin.File.Tests\Plugin.File.Tests.csproj", "{9530C383-D53C-4434-87E6-D6665977F378}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -191,6 +193,10 @@ Global {BD914F56-8686-40BB-996E-0F89DC613A35}.Debug|Any CPU.Build.0 = Debug|Any CPU {BD914F56-8686-40BB-996E-0F89DC613A35}.Release|Any CPU.ActiveCfg = Release|Any CPU {BD914F56-8686-40BB-996E-0F89DC613A35}.Release|Any CPU.Build.0 = Release|Any CPU + {9530C383-D53C-4434-87E6-D6665977F378}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9530C383-D53C-4434-87E6-D6665977F378}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9530C383-D53C-4434-87E6-D6665977F378}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9530C383-D53C-4434-87E6-D6665977F378}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -225,6 +231,7 @@ Global {F57A3836-FEEF-4E4A-A36A-73B827C0CCC9} = {6F597C08-39F3-47E6-B43A-777A876E9AB0} {BDFC35C7-EFDC-4502-B69B-45ECB3C805CE} = {70B359E7-710C-4583-99A0-1F59333C3AD1} {BD914F56-8686-40BB-996E-0F89DC613A35} = {BDFC35C7-EFDC-4502-B69B-45ECB3C805CE} + {9530C383-D53C-4434-87E6-D6665977F378} = {BDFC35C7-EFDC-4502-B69B-45ECB3C805CE} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {B440C24E-F11D-4F28-9EE5-F7AB214679E6} diff --git a/src/Plugin.File.Tests/FileRenameTests.cs b/src/Plugin.File.Tests/FileRenameTests.cs new file mode 100644 index 0000000..32d98f0 --- /dev/null +++ b/src/Plugin.File.Tests/FileRenameTests.cs @@ -0,0 +1,42 @@ +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 FileRenameTests +{ + [Fact] + public async Task Can_Rename_a_File() + { + var path = "./files/rename"; + //Copy the sample file for the test + System.IO.File.Delete(Path.Combine(path, "Sample-Before.txt")); + 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 inputs = new Dictionary + { + {"source-path", "./files/rename/Sample-Before.txt"}, + {"new-name", "Sample-After.txt"} + }; + 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.File.Exists(Path.Combine(path, "Sample-After.txt"))); + Assert.False(System.IO.File.Exists(Path.Combine(path, "Sample-Before.txt"))); + } +} \ 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 new file mode 100644 index 0000000..47be36a --- /dev/null +++ b/src/Plugin.File.Tests/Plugin.File.Tests.csproj @@ -0,0 +1,35 @@ + + + + net8.0 + enable + enable + + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + Always + + + + + + + + + + diff --git a/src/Plugin.File.Tests/files/rename/Sample.txt b/src/Plugin.File.Tests/files/rename/Sample.txt new file mode 100644 index 0000000..e69de29