From 94f174de38644bc05d423c1e19cd60a558514b9c Mon Sep 17 00:00:00 2001 From: jan Date: Fri, 31 May 2024 12:27:10 +0200 Subject: [PATCH] - improved the file copy functions --- src/Nox.Cli.Caching/NoxCliCacheManager.cs | 14 +++++-- .../Nox.Cli.Plugin.File/FileCopyFile_v1.cs | 24 +++-------- .../Nox.Cli.Plugin.File/FileCopyFolder_v1.cs | 41 +++---------------- .../Nox.Cli.Plugin.File/Helpers/CopyHelper.cs | 25 +++++++++++ src/Nox.Cli.Server/Program.cs | 16 +++++++- .../Properties/launchSettings.json | 1 + src/Nox.Cli/Properties/launchSettings.json | 4 +- src/Plugin.File.Tests/CopyFolderTests.cs | 41 +++++++++++++++++++ .../Plugin.File.Tests.csproj | 6 +++ .../files/copy-folder/source/Sample.txt | 0 .../files/copy-folder/source/child/Sample.txt | 0 11 files changed, 111 insertions(+), 61 deletions(-) create mode 100644 src/Nox.Cli.Plugins/Nox.Cli.Plugin.File/Helpers/CopyHelper.cs create mode 100644 src/Plugin.File.Tests/CopyFolderTests.cs create mode 100644 src/Plugin.File.Tests/files/copy-folder/source/Sample.txt create mode 100644 src/Plugin.File.Tests/files/copy-folder/source/child/Sample.txt diff --git a/src/Nox.Cli.Caching/NoxCliCacheManager.cs b/src/Nox.Cli.Caching/NoxCliCacheManager.cs index aebffca1..b6bb230d 100755 --- a/src/Nox.Cli.Caching/NoxCliCacheManager.cs +++ b/src/Nox.Cli.Caching/NoxCliCacheManager.cs @@ -172,11 +172,17 @@ internal INoxCliCacheManager Build() Dictionary 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 { diff --git a/src/Nox.Cli.Plugins/Nox.Cli.Plugin.File/FileCopyFile_v1.cs b/src/Nox.Cli.Plugins/Nox.Cli.Plugin.File/FileCopyFile_v1.cs index 6765a1ed..1b59410e 100644 --- a/src/Nox.Cli.Plugins/Nox.Cli.Plugin.File/FileCopyFile_v1.cs +++ b/src/Nox.Cli.Plugins/Nox.Cli.Plugin.File/FileCopyFile_v1.cs @@ -1,5 +1,6 @@ using Nox.Cli.Abstractions; using Nox.Cli.Abstractions.Extensions; +using Nox.Cli.Plugin.File.Helpers; namespace Nox.Cli.Plugin.File; @@ -73,29 +74,16 @@ public Task> 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); } } diff --git a/src/Nox.Cli.Plugins/Nox.Cli.Plugin.File/FileCopyFolder_v1.cs b/src/Nox.Cli.Plugins/Nox.Cli.Plugin.File/FileCopyFolder_v1.cs index ac556a13..aaae8303 100755 --- a/src/Nox.Cli.Plugins/Nox.Cli.Plugin.File/FileCopyFolder_v1.cs +++ b/src/Nox.Cli.Plugins/Nox.Cli.Plugin.File/FileCopyFolder_v1.cs @@ -1,5 +1,6 @@ using Nox.Cli.Abstractions; using Nox.Cli.Abstractions.Extensions; +using Nox.Cli.Plugin.File.Helpers; namespace Nox.Cli.Plugin.File; @@ -62,8 +63,6 @@ public Task BeginAsync(IDictionary inputs) public Task> ProcessAsync(INoxWorkflowContext ctx) { var outputs = new Dictionary(); - var isValid = true; - ctx.SetState(ActionState.Error); if (string.IsNullOrEmpty(_sourcePath) || @@ -79,7 +78,6 @@ public Task> ProcessAsync(INoxWorkflowContext ctx) if (!Directory.Exists(fullSourcePath)) { ctx.SetErrorMessage($"Folder {fullSourcePath} does not exist!"); - isValid = false; } else { @@ -87,27 +85,22 @@ public Task> ProcessAsync(INoxWorkflowContext ctx) 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) { @@ -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) @@ -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); - } - } } diff --git a/src/Nox.Cli.Plugins/Nox.Cli.Plugin.File/Helpers/CopyHelper.cs b/src/Nox.Cli.Plugins/Nox.Cli.Plugin.File/Helpers/CopyHelper.cs new file mode 100644 index 00000000..2031b34c --- /dev/null +++ b/src/Nox.Cli.Plugins/Nox.Cli.Plugin.File/Helpers/CopyHelper.cs @@ -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; + } +} \ No newline at end of file diff --git a/src/Nox.Cli.Server/Program.cs b/src/Nox.Cli.Server/Program.cs index d9ba92f5..14b24ee0 100755 --- a/src/Nox.Cli.Server/Program.cs +++ b/src/Nox.Cli.Server/Program.cs @@ -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(); @@ -62,7 +69,12 @@ }); builder.Services.AddSingleton(); -builder.Services.AddAllElasticApm(); + +if (!forceOffline) +{ + builder.Services.AddAllElasticApm(); +} + var app = builder.Build(); diff --git a/src/Nox.Cli.Server/Properties/launchSettings.json b/src/Nox.Cli.Server/Properties/launchSettings.json index 6f0a2191..5d260e0a 100755 --- a/src/Nox.Cli.Server/Properties/launchSettings.json +++ b/src/Nox.Cli.Server/Properties/launchSettings.json @@ -5,6 +5,7 @@ "commandName": "Project", "dotnetRunMessages": true, "launchBrowser": true, + "commandLineArgs": "--offline", "launchUrl": "swagger", "applicationUrl": "http://localhost:8000", "environmentVariables": { diff --git a/src/Nox.Cli/Properties/launchSettings.json b/src/Nox.Cli/Properties/launchSettings.json index 4b08d386..2fa440c5 100755 --- a/src/Nox.Cli/Properties/launchSettings.json +++ b/src/Nox.Cli/Properties/launchSettings.json @@ -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" } diff --git a/src/Plugin.File.Tests/CopyFolderTests.cs b/src/Plugin.File.Tests/CopyFolderTests.cs new file mode 100644 index 00000000..23820d03 --- /dev/null +++ b/src/Plugin.File.Tests/CopyFolderTests.cs @@ -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 + { + {"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(); + var orgResolver = Mock.Of(); + var cacheMan = Mock.Of(); + var lteConfig = Mock.Of(); + var secretsResolver = Mock.Of(); + 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"))); + } +} \ No newline at end of file diff --git a/src/Plugin.File.Tests/Plugin.File.Tests.csproj b/src/Plugin.File.Tests/Plugin.File.Tests.csproj index 292fcb88..61923591 100644 --- a/src/Plugin.File.Tests/Plugin.File.Tests.csproj +++ b/src/Plugin.File.Tests/Plugin.File.Tests.csproj @@ -33,6 +33,12 @@ Always + + Always + + + Always + diff --git a/src/Plugin.File.Tests/files/copy-folder/source/Sample.txt b/src/Plugin.File.Tests/files/copy-folder/source/Sample.txt new file mode 100644 index 00000000..e69de29b diff --git a/src/Plugin.File.Tests/files/copy-folder/source/child/Sample.txt b/src/Plugin.File.Tests/files/copy-folder/source/child/Sample.txt new file mode 100644 index 00000000..e69de29b