-
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 #221 from NoxOrg/feature/rename-file-addin
- added a file rename plugin
- Loading branch information
Showing
5 changed files
with
170 additions
and
0 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
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 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<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-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<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,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<string, object> | ||
{ | ||
{"source-path", "./files/rename/Sample-Before.txt"}, | ||
{"new-name", "Sample-After.txt"} | ||
}; | ||
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.File.Exists(Path.Combine(path, "Sample-After.txt"))); | ||
Assert.False(System.IO.File.Exists(Path.Combine(path, "Sample-Before.txt"))); | ||
} | ||
} |
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,35 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0"/> | ||
<PackageReference Include="Moq" Version="4.20.70" /> | ||
<PackageReference Include="xunit" Version="2.8.0" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.0"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="coverlet.collector" Version="6.0.2"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Update="files\rename\Sample.txt"> | ||
<CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
</None> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Nox.Cli.Plugins\Nox.Cli.Plugin.File\Nox.Cli.Plugin.File.csproj" /> | ||
<ProjectReference Include="..\Nox.Cli.Variables\Nox.Cli.Variables.csproj" /> | ||
<ProjectReference Include="..\Nox.Cli\Nox.Cli.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Empty file.