Skip to content

Commit

Permalink
Merge pull request #240 from NoxOrg/improve/file-folder-copy-functions
Browse files Browse the repository at this point in the history
- improved the file copy functions
  • Loading branch information
jan-schutte committed May 31, 2024
2 parents 1e742fc + 94f174d commit 323dc0c
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 61 deletions.
14 changes: 10 additions & 4 deletions src/Nox.Cli.Caching/NoxCliCacheManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,17 @@ internal INoxCliCacheManager Build()
Dictionary<string,string> yamlFiles = new(StringComparer.OrdinalIgnoreCase);
if (_isServer)
{
if (string.IsNullOrEmpty(_cache!.TenantId)) throw new NoxCliException("Tenant has not been configured!");
if (string.IsNullOrEmpty(_cache!.TenantId)) throw new NoxCliException("Tenant has not been configured!");

GetOnlineWorkflowsAndManifest(yamlFiles);

GetOnlineTemplates();
if (_forceOffline)
{
GetLocalWorkflowsAndManifest(yamlFiles);
}
else
{
GetOnlineWorkflowsAndManifest(yamlFiles);
GetOnlineTemplates();
}
}
else
{
Expand Down
24 changes: 6 additions & 18 deletions src/Nox.Cli.Plugins/Nox.Cli.Plugin.File/FileCopyFile_v1.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Nox.Cli.Abstractions;
using Nox.Cli.Abstractions.Extensions;
using Nox.Cli.Plugin.File.Helpers;

namespace Nox.Cli.Plugin.File;

Expand Down Expand Up @@ -73,29 +74,16 @@ public Task<IDictionary<string, object>> ProcessAsync(INoxWorkflowContext ctx)
}
else
{
var isValid = true;
var fullTargetPath = Path.Combine(Path.GetFullPath(_targetPath), filename);
Directory.CreateDirectory(Path.GetDirectoryName(fullTargetPath)!);
var createDate = DateTime.Now;
if (System.IO.File.Exists(fullTargetPath))
var result = CopyHelper.CopyFile(fullSourcePath, fullTargetPath, _isOverwrite!.Value);
if (!result)
{
if (_isOverwrite!.Value)
{
createDate = System.IO.File.GetCreationTime(fullTargetPath);
System.IO.File.Delete(fullTargetPath);
}
else
{
ctx.SetErrorMessage($"File: {fullTargetPath} already exists, and is-overwrite was not specified.");
isValid = false;
}

ctx.SetErrorMessage($"File: {fullTargetPath} already exists, and is-overwrite was not specified.");
}
if (isValid)
else
{
System.IO.File.Copy(fullSourcePath, fullTargetPath);
System.IO.File.SetCreationTime(fullTargetPath, createDate);
ctx.SetState(ActionState.Success);
ctx.SetState(ActionState.Success);
}
}

Expand Down
41 changes: 6 additions & 35 deletions src/Nox.Cli.Plugins/Nox.Cli.Plugin.File/FileCopyFolder_v1.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Nox.Cli.Abstractions;
using Nox.Cli.Abstractions.Extensions;
using Nox.Cli.Plugin.File.Helpers;

namespace Nox.Cli.Plugin.File;

Expand Down Expand Up @@ -62,8 +63,6 @@ public Task BeginAsync(IDictionary<string,object> inputs)
public Task<IDictionary<string, object>> ProcessAsync(INoxWorkflowContext ctx)
{
var outputs = new Dictionary<string, object>();
var isValid = true;

ctx.SetState(ActionState.Error);

if (string.IsNullOrEmpty(_sourcePath) ||
Expand All @@ -79,35 +78,29 @@ public Task<IDictionary<string, object>> ProcessAsync(INoxWorkflowContext ctx)
if (!Directory.Exists(fullSourcePath))
{
ctx.SetErrorMessage($"Folder {fullSourcePath} does not exist!");
isValid = false;
}
else
{
var fullTargetPath = Path.GetFullPath(_targetPath);
if (!Directory.Exists(fullTargetPath))
{
Directory.CreateDirectory(fullTargetPath);
CopyFiles(fullSourcePath, fullTargetPath);
ctx.SetState(ActionState.Success);
}
else
{
if (_isOverwrite!.Value)
{
PurgeFolder(fullTargetPath);
CopyFiles(fullSourcePath, fullTargetPath);
ctx.SetState(ActionState.Success);
}
else
{
ctx.SetErrorMessage($"Folder {fullTargetPath} already exists, and is-overwrite was not specified.");
isValid = false;
}
}

if (isValid)
{
CopyFiles(fullSourcePath, fullTargetPath);
ctx.SetState(ActionState.Success);
}
}

}
catch (Exception ex)
{
Expand All @@ -130,15 +123,7 @@ private void CopyFiles(string sourceFolder, string targetFolder)
foreach (var file in di.GetFiles())
{
var targetFilePath = Path.Combine(targetFolder, file.Name);
var createDate = DateTime.Now;
if (System.IO.File.Exists(targetFilePath))
{
createDate = System.IO.File.GetCreationTime(targetFilePath);
System.IO.File.Delete(targetFilePath);
}
Directory.CreateDirectory(targetFolder);
file.CopyTo(targetFilePath);
System.IO.File.SetCreationTime(targetFilePath, createDate);
CopyHelper.CopyFile(file.FullName, targetFilePath, true);
}

if (_isRecursive == true)
Expand All @@ -149,18 +134,4 @@ private void CopyFiles(string sourceFolder, string targetFolder)
}
}
}

private void PurgeFolder(string path)
{
var di = new DirectoryInfo(path);
foreach (var file in di.GetFiles())
{
file.Delete();
}

foreach (var dir in di.GetDirectories())
{
dir.Delete(true);
}
}
}
25 changes: 25 additions & 0 deletions src/Nox.Cli.Plugins/Nox.Cli.Plugin.File/Helpers/CopyHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace Nox.Cli.Plugin.File.Helpers;

public static class CopyHelper
{
public static bool CopyFile(string sourcePath, string targetPath, bool isOverwrite)
{
if (System.IO.File.Exists(targetPath))
{
if (isOverwrite)
{
var createDate = System.IO.File.GetCreationTime(targetPath);
System.IO.File.Delete(targetPath);
System.IO.File.Copy(sourcePath, targetPath);
System.IO.File.SetCreationTime(targetPath, createDate);
}

return false;
}

var targetFolder = Path.GetDirectoryName(targetPath);
Directory.CreateDirectory(targetFolder!);
System.IO.File.Copy(sourcePath, targetPath);
return true;
}
}
16 changes: 14 additions & 2 deletions src/Nox.Cli.Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,17 @@

var builder = WebApplication.CreateBuilder(args);

var forceOffline = false;
var offlineArg = args.FirstOrDefault(arg => arg.StartsWith("--offline"));
if (offlineArg != null)
{
forceOffline = true;
}

// Add services to the container.
builder.Services.AddMicrosoftIdentityWebApiAuthentication(builder.Configuration, subscribeToJwtBearerMiddlewareDiagnosticsEvents: true);

var cacheManager = new NoxCliCacheBuilder(builder.Configuration["NoxScriptsUrl"]!, false)
var cacheManager = new NoxCliCacheBuilder(builder.Configuration["NoxScriptsUrl"]!, forceOffline)
.WithTenantId(builder.Configuration["AzureAd:TenantId"]!)
.ForServer()
.Build();
Expand Down Expand Up @@ -62,7 +69,12 @@
});

builder.Services.AddSingleton<IWorkflowContextFactory, WorkflowContextFactory>();
builder.Services.AddAllElasticApm();

if (!forceOffline)
{
builder.Services.AddAllElasticApm();
}


var app = builder.Build();

Expand Down
1 change: 1 addition & 0 deletions src/Nox.Cli.Server/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"commandLineArgs": "--offline",
"launchUrl": "swagger",
"applicationUrl": "http://localhost:8000",
"environmentVariables": {
Expand Down
4 changes: 2 additions & 2 deletions src/Nox.Cli/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"profiles": {
"Nox.Cli": {
"commandName": "Project",
"commandLineArgs": "sync tf --offline",
"workingDirectory": "/home/jan/demo/CliDemo",
"commandLineArgs": "sync chart --offline",
"workingDirectory": "/home/jan/Projects/IWG/Fno.MultiDeploy/src/Ingena.Fno.MultiDeploy.Api",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
Expand Down
41 changes: 41 additions & 0 deletions src/Plugin.File.Tests/CopyFolderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
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 CopyFolderTests
{
[Fact]
public async Task Can_copy_a_File_if_destination_folder_does_not_exist()
{
var path = "./files/copy-folder";
//Ensure the target folder does not exist
Helpers.PurgeFolderRecursive(Path.Combine(path, "target"), true);

var plugin = new FileCopyFolder_v1();
var inputs = new Dictionary<string, object>
{
{"source-path", Path.Combine(path, "source")},
{"target-path", Path.Combine(path, "target")},
{"is-recursive", true}
};
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, "target/Sample.txt")));
Assert.True(System.IO.File.Exists(Path.Combine(path, "target/child/Sample.txt")));
}
}
6 changes: 6 additions & 0 deletions src/Plugin.File.Tests/Plugin.File.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@
<None Update="files\copy-file\source\child\Sample.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="files\copy-folder\source\child\Sample.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="files\copy-folder\source\Sample.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
Expand Down
Empty file.
Empty file.

0 comments on commit 323dc0c

Please sign in to comment.