-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #222 from NoxOrg/feature/directory-rename
- added a Rename Folder plugin
- Loading branch information
Showing
6 changed files
with
160 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
src/Nox.Cli.Plugins/Nox.Cli.Plugin.File/FileRenameFolder_v1.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.