diff --git a/ADO2024/PI4/Helper-2024-12-17.cs b/ADO2024/PI4/Helper-2024-12-17.cs index 3b3492b..6bcf1ff 100644 --- a/ADO2024/PI4/Helper-2024-12-17.cs +++ b/ADO2024/PI4/Helper-2024-12-17.cs @@ -54,13 +54,20 @@ internal static partial class Helper20241217 Job jobNew; string path; string? json; + string asidePath; bool areTheyTheSame; string directoryName; + logger.LogInformation(args[0]); + logger.LogInformation(args[1]); + logger.LogInformation(args[2]); + logger.LogInformation(args[3]); + logger.LogInformation(args[4]); ReadOnlyCollection files; string searchPattern = args[2]; string[] ignoreFileNames = args[3].Split('~'); + string destination = Path.GetFullPath(args[4]); string sourceDirectory = Path.GetFullPath(args[0]); - char destinationDriveLetter = args[4].Split(':')[0][0]; + char destinationDriveLetter = destination.Split(':')[0][0]; logger.LogInformation("Searching <{sourceDirectory}> with search pattern {searchPattern}", args[0], searchPattern); if (Debugger.IsAttached) Verify(searchPattern, ignoreFileNames); @@ -80,10 +87,15 @@ internal static partial class Helper20241217 continue; } directoryName = Path.GetFileName(record.Directory); - path = Path.Combine(record.Directory, $"{directoryName}-{DateTime.Now:yyyy-MM-dd-HH-mm-ss-fff}{record.Job.Extension}"); - logger.LogInformation("Writing <{directory}> extension", record.Directory); - WritePassedExtension(destinationDriveLetter, record, files, path); + asidePath = Path.Combine(record.Directory, $"{directoryName}-{DateTime.Now:yyyy-MM-dd-HH-mm-ss-fff}{record.Job.Extension}"); + path = $"{destinationDriveLetter}{asidePath[1..]}"; + logger.LogInformation("Writing <{path}> extension", path); + WritePassedExtension(record, files, directoryName, path); + logger.LogInformation("Wrote <{path}> extension", path); + MovePassedExtension(destination, path); + logger.LogInformation("Moved <{path}> extension", path); WriteAllText(record.Path, json); + Helpers.HelperDeleteEmptyDirectories.DeleteEmptyDirectories(logger, $"{destinationDriveLetter}{record.Directory[1..]}"); } if (Debugger.IsAttached && records.Count() == 0) { @@ -245,8 +257,11 @@ internal static partial class Helper20241217 else { result = false; - WriteAllText(Path.Combine(Environment.CurrentDirectory, ".vscode", "helper", "old.json"), jsonOld); - WriteAllText(Path.Combine(Environment.CurrentDirectory, ".vscode", "helper", "new.json"), jsonNew); + if (Debugger.IsAttached) + { + WriteAllText(Path.Combine(Environment.CurrentDirectory, ".vscode", "helper", "old.json"), jsonOld); + WriteAllText(Path.Combine(Environment.CurrentDirectory, ".vscode", "helper", "new.json"), jsonNew); + } logger.LogInformation("<{directory}> file serialized are different {filesTotalLengthNew} != {filesTotalLengthOld}", record.Directory, filesTotalLengthNew, filesTotalLengthOld); } } @@ -261,36 +276,44 @@ internal static partial class Helper20241217 System.IO.File.WriteAllText(path, text); } - private static void WritePassedExtension(char destinationDriveLetter, Record record, ReadOnlyCollection files, string path) + private static void WritePassedExtension(Record record, ReadOnlyCollection files, string directoryName, string path) { - string directoryName = Path.GetFileName(record.Directory); if (record.Job.Extension.Equals(".iso", StringComparison.OrdinalIgnoreCase)) - WriteISO(destinationDriveLetter, record, files, path, directoryName); + WriteISO(record, files, path, directoryName); else if (record.Job.Extension.Equals(".zip", StringComparison.OrdinalIgnoreCase)) - WriteZIP(destinationDriveLetter, record, files, path); + WriteZIP(record, files, path); else throw new NotImplementedException(); } - private static void WriteISO(char destinationDriveLetter, Record record, ReadOnlyCollection files, string path, string directoryName) + private static void MovePassedExtension(string destination, string path) { - string checkFile = $"{destinationDriveLetter}{path[1..]}"; - string checkDirectory = Path.GetDirectoryName(checkFile) ?? throw new Exception(); + string checkPath = $"{destination}{path[2..]}"; + string checkDirectory = Path.GetDirectoryName(checkPath) ?? throw new Exception(); + if (!Directory.Exists(checkDirectory)) + _ = Directory.CreateDirectory(checkDirectory); + if (System.IO.File.Exists(checkPath)) + throw new NotImplementedException($"<{checkPath}> already exists!"); + System.IO.File.Move(path, checkPath); + } + + private static void WriteISO(Record record, ReadOnlyCollection files, string path, string directoryName) + { + string checkDirectory = Path.GetDirectoryName(path) ?? throw new Exception(); if (!Directory.Exists(checkDirectory)) _ = Directory.CreateDirectory(checkDirectory); CDBuilder builder = new() { UseJoliet = true, VolumeIdentifier = directoryName.Length < 25 ? directoryName : directoryName[..25] }; foreach (File file in files) _ = builder.AddFile(file.RelativePath, Path.Combine(record.Directory, file.RelativePath)); - builder.Build(checkFile); + builder.Build(path); } - private static void WriteZIP(char destinationDriveLetter, Record record, ReadOnlyCollection files, string path) + private static void WriteZIP(Record record, ReadOnlyCollection files, string path) { - string checkFile = $"{destinationDriveLetter}{path[1..]}"; - string checkDirectory = Path.GetDirectoryName(checkFile) ?? throw new Exception(); + string checkDirectory = Path.GetDirectoryName(path) ?? throw new Exception(); if (!Directory.Exists(checkDirectory)) _ = Directory.CreateDirectory(checkDirectory); - using ZipArchive zip = ZipFile.Open(checkFile, ZipArchiveMode.Create); + using ZipArchive zip = ZipFile.Open(path, ZipArchiveMode.Create); string directoryEntry; List directoryEntries = []; foreach (File file in files)