Skip to content

Commit

Permalink
Merge pull request #222 from NoxOrg/feature/directory-rename
Browse files Browse the repository at this point in the history
- added a Rename Folder plugin
  • Loading branch information
jan-schutte authored May 7, 2024
2 parents ee92855 + 9b74265 commit 9cb2602
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Nox.Cli.Plugin.File;

public class FileRename_v1 : INoxCliAddin
public class FileRenameFile_v1 : INoxCliAddin
{
public NoxActionMetaData Discover()
{
Expand Down
86 changes: 86 additions & 0 deletions src/Nox.Cli.Plugins/Nox.Cli.Plugin.File/FileRenameFolder_v1.cs
Original file line number Diff line number Diff line change
@@ -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<string,object> inputs)
{
_sourcePath = inputs.Value<string>("source-path");
_newName = inputs.Value<string>("new-name");
return Task.CompletedTask;
}

public Task<IDictionary<string, object>> ProcessAsync(INoxWorkflowContext ctx)
{
var outputs = new Dictionary<string, object>();

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<IDictionary<string, object>>(outputs);
}

public Task EndAsync()
{
return Task.CompletedTask;
}
}
2 changes: 1 addition & 1 deletion src/Plugin.File.Tests/FileRenameTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, object>
{
{"source-path", "./files/rename/Sample-Before.txt"},
Expand Down
69 changes: 69 additions & 0 deletions src/Plugin.File.Tests/FolderRenameTests.cs
Original file line number Diff line number Diff line change
@@ -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<string, object>
{
{"source-path", "./files/rename/Sample-Before"},
{"new-name", "Sample-After"}
};
await plugin.BeginAsync(inputs);
var wfConfig = new WorkflowConfiguration();
var sln = Mock.Of<NoxSolution>();
var orgResolver = Mock.Of<IOrgSecretResolver>();
var cacheMan = Mock.Of<INoxCliCacheManager>();
var lteConfig = Mock.Of<LocalTaskExecutorConfiguration>();
var secretsResolver = Mock.Of<INoxSecretsResolver>();
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);
}
}
}
3 changes: 3 additions & 0 deletions src/Plugin.File.Tests/Plugin.File.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
<None Update="files\rename\Sample.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="files\rename\Sample\Sample.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
Expand Down
Empty file.

0 comments on commit 9cb2602

Please sign in to comment.