Skip to content

Commit

Permalink
Merge pull request #221 from NoxOrg/feature/rename-file-addin
Browse files Browse the repository at this point in the history
- added a file rename plugin
  • Loading branch information
jan-schutte authored May 7, 2024
2 parents f717553 + 309ff80 commit ee92855
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 0 deletions.
86 changes: 86 additions & 0 deletions src/Nox.Cli.Plugins/Nox.Cli.Plugin.File/FileRename_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 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;
}
}
7 changes: 7 additions & 0 deletions src/Nox.Cli.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}
Expand Down
42 changes: 42 additions & 0 deletions src/Plugin.File.Tests/FileRenameTests.cs
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")));
}
}
35 changes: 35 additions & 0 deletions src/Plugin.File.Tests/Plugin.File.Tests.csproj
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.

0 comments on commit ee92855

Please sign in to comment.