From 6ce6c28db139f019e81f950abc099819fdd726d6 Mon Sep 17 00:00:00 2001 From: Mike Phares Date: Sat, 28 Jun 2025 11:02:19 -0700 Subject: [PATCH] Including .json and .html after creating .iso (Day-Helper-2024-12-17) --- .vscode/launch.json | 8 +++ ADO2024/PI4/Helper-2024-12-17.cs | 111 +++++++++++++++++++------------ 2 files changed, 75 insertions(+), 44 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 67b6786..9e138e9 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -11,6 +11,14 @@ "preLaunchTask": "build", "program": "${workspaceFolder}/bin/Debug/net8.0/win-x64/File-Folder-Helper.dll", "args": [ + "s", + "X", + "D:/5-Other-Small", + "Day-Helper-2024-12-17", + ".job.json", + "thumbs.db~sync.ffs_db~verify.json~.html", + "D:/0-ISO-A", + "D:/5-Other-Small/Proxmox/Snap2HTML/Snap2HTML.exe", "s", "M", "D:/5-Other-Small/Notes/EC-Documentation", diff --git a/ADO2024/PI4/Helper-2024-12-17.cs b/ADO2024/PI4/Helper-2024-12-17.cs index df0af8f..9d930b7 100644 --- a/ADO2024/PI4/Helper-2024-12-17.cs +++ b/ADO2024/PI4/Helper-2024-12-17.cs @@ -24,9 +24,12 @@ internal static partial class Helper20241217 long Length, string RelativePath); - private record Record(string Directory, + private record Record(string DestinationDirectory, + string DirectoryName, Job Job, - string Path); + string Path, + string Snap2HyperTextMarkupLanguage, + string SourceDirectory); private record Job(string? AlternatePath, string Directory, @@ -49,14 +52,16 @@ internal static partial class Helper20241217 { } + private static ReadOnlyCollection GetFiles(string searchPattern, string[] ignoreFileNames, Record record) => + GetFiles(record.SourceDirectory, searchPattern, ignoreFileNames); + internal static void Backup(ILogger logger, List args) { Job jobNew; string path; string? json; - string asidePath; + string fileName; bool areTheyTheSame; - string directoryName; IEnumerable records; logger.LogInformation(args[0]); logger.LogInformation(args[1]); @@ -69,7 +74,7 @@ internal static partial class Helper20241217 string[] ignoreFileNames = args[3].Split('~'); string destination = Path.GetFullPath(args[4]); string sourceDirectory = Path.GetFullPath(args[0]); - char destinationDriveLetter = destination.Split(':')[0][0]; + string? snap2HyperTextMarkupLanguage = args.Count < 6 ? null : Path.GetFullPath(args[5]); logger.LogInformation("Searching <{sourceDirectory}> with search pattern {searchPattern}", args[0], searchPattern); if (Debugger.IsAttached) Verify(searchPattern, ignoreFileNames); @@ -85,12 +90,14 @@ internal static partial class Helper20241217 } else throw new NotImplementedException(); - records = GetRecords(sourceDirectory, searchPatternFiles); + records = GetRecords(sourceDirectory, destination, searchPatternFiles); foreach (Record record in records) { if (record.Job is null || string.IsNullOrEmpty(record.Job.Extension)) continue; - logger.LogInformation("Searching <{directory}>", record.Directory); + logger.LogInformation("Searching <{directory}>", record.SourceDirectory); + if (snap2HyperTextMarkupLanguage is not null && System.IO.File.Exists(snap2HyperTextMarkupLanguage)) + WriteSnap2HyperTextMarkupLanguage(logger, snap2HyperTextMarkupLanguage, record); files = GetFiles(searchPattern, ignoreFileNames, record); jobNew = GetJob(searchPattern, ignoreFileNames, record, files); json = JsonSerializer.Serialize(jobNew, JobSourceGenerationContext.Default.Job); @@ -100,16 +107,13 @@ internal static partial class Helper20241217 WriteAllText(record.Path, json); continue; } - directoryName = Path.GetFileName(record.Directory); - asidePath = Path.Combine(record.Directory, $"{directoryName}-{DateTime.Now:yyyy-MM-dd-HH-mm-ss-fff}{record.Job.Extension}"); - path = $"{destinationDriveLetter}{asidePath[1..]}"; + fileName = $"{record.DirectoryName}-{DateTime.Now:yyyy-MM-dd-HH-mm-ss-fff}{record.Job.Extension}"; + path = Path.Combine(record.DestinationDirectory, fileName); logger.LogInformation("Writing <{path}> extension", path); - WritePassedExtension(record, files, directoryName, path); + WritePassedExtension(record, files, record.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..]}"); + WriteAllText(record, json, path); } } } @@ -159,23 +163,28 @@ internal static partial class Helper20241217 return results.AsReadOnly(); } - private static IEnumerable GetRecords(string directory, IEnumerable files) + private static IEnumerable GetRecords(string directory, string destination, IEnumerable files) { Job? job; string json; Record record; string fileName; string directoryName; + string sourceDirectory; + string destinationDirectory; + string snap2HyperTextMarkupLanguage; foreach (string file in files) { fileName = Path.GetFileName(file); - directoryName = Path.GetDirectoryName(file) ?? throw new Exception(); + sourceDirectory = Path.GetDirectoryName(file) ?? throw new Exception(); + directoryName = Path.GetFileName(sourceDirectory); if (!fileName.StartsWith('.')) { - System.IO.File.Move(file, Path.Combine(directoryName, $".{fileName}")); + System.IO.File.Move(file, Path.Combine(sourceDirectory, $".{fileName}")); continue; } json = System.IO.File.ReadAllText(file); + snap2HyperTextMarkupLanguage = Path.Combine(sourceDirectory, ".html"); if (string.IsNullOrEmpty(json) || json is "{}" or "[]") job = null; else @@ -188,11 +197,36 @@ internal static partial class Helper20241217 FilesTotalLength: 0, Keep: 3, Targets: []); - record = new(Directory: directoryName, Job: job, Path: file); + destinationDirectory = $"{destination}{sourceDirectory[2..]}"; + if (!Directory.Exists(destinationDirectory)) + _ = Directory.CreateDirectory(destinationDirectory); + record = new(DestinationDirectory: destinationDirectory, + DirectoryName: directoryName, + Job: job, + Path: file, + Snap2HyperTextMarkupLanguage: snap2HyperTextMarkupLanguage, + SourceDirectory: sourceDirectory); yield return record; } } + private static void WriteSnap2HyperTextMarkupLanguage(ILogger logger, string snap2HyperTextMarkupLanguage, Record record) + { + string title = Path.GetFileName(record.SourceDirectory); + ProcessStartInfo processStartInfo = new() + { + Arguments = $"-path:\"{record.SourceDirectory}\" -outfile:\"{record.Snap2HyperTextMarkupLanguage}\" -title:\"{title}\" -hidden -silent", + FileName = snap2HyperTextMarkupLanguage, + RedirectStandardError = true, + RedirectStandardInput = true, + RedirectStandardOutput = true, + WorkingDirectory = record.SourceDirectory + }; + Process? process = Process.Start(processStartInfo) ?? throw new Exception("Process should not be null."); + process.WaitForExit(); + logger.LogInformation($"Snap2HyperTextMarkupLanguage: {process.StandardOutput.ReadToEnd()}{Environment.NewLine}{process.StandardError.ReadToEnd()}{Environment.NewLine}{process.ExitCode}"); + } + private static ReadOnlyCollection GetFiles(string directory, string searchPattern, string[] ignoreFileNames) { List results = []; @@ -217,16 +251,13 @@ internal static partial class Helper20241217 return results.AsReadOnly(); } - private static ReadOnlyCollection GetFiles(string searchPattern, string[] ignoreFileNames, Record record) => - GetFiles(record.Directory, searchPattern, ignoreFileNames); - private static Job GetJob(string searchPattern, string[] ignoreFileNames, Record record, ReadOnlyCollection files) { Job result; ReadOnlyCollection collection = GetFilteredFiles(searchPattern, ignoreFileNames, files); double filesTotalLengthNew = collection.Select(l => l.Length).Sum(); result = new(AlternatePath: record.Job.AlternatePath, - Directory: record.Directory, + Directory: record.SourceDirectory, Extension: record.Job.Extension, Files: collection.ToArray(), FilesCount: collection.Count, @@ -245,7 +276,7 @@ internal static partial class Helper20241217 if (filesCountNew != filesCountOld) { result = false; - logger.LogInformation("<{directory}> file count has changed {filesCountNew} != {filesCountOld}", record.Directory, filesCountNew, filesCountOld); + logger.LogInformation("<{directory}> file count has changed {filesCountNew} != {filesCountOld}", record.SourceDirectory, filesCountNew, filesCountOld); } else { @@ -254,7 +285,7 @@ internal static partial class Helper20241217 if (filesTotalLengthNew != filesTotalLengthOld) { result = false; - logger.LogInformation("<{directory}> file length has changed {filesTotalLengthNew} != {filesTotalLengthOld}", record.Directory, filesTotalLengthNew, filesTotalLengthOld); + logger.LogInformation("<{directory}> file length has changed {filesTotalLengthNew} != {filesTotalLengthOld}", record.SourceDirectory, filesTotalLengthNew, filesTotalLengthOld); } else { @@ -270,7 +301,7 @@ internal static partial class Helper20241217 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); + logger.LogInformation("<{directory}> file serialized are different {filesTotalLengthNew} != {filesTotalLengthOld}", record.SourceDirectory, filesTotalLengthNew, filesTotalLengthOld); } } } @@ -294,33 +325,16 @@ internal static partial class Helper20241217 throw new NotImplementedException(); } - private static void MovePassedExtension(string destination, string path) - { - 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.AddFile(file.RelativePath, Path.Combine(record.SourceDirectory, file.RelativePath)); builder.Build(path); } private static void WriteZIP(Record record, ReadOnlyCollection files, string path) { - string checkDirectory = Path.GetDirectoryName(path) ?? throw new Exception(); - if (!Directory.Exists(checkDirectory)) - _ = Directory.CreateDirectory(checkDirectory); using ZipArchive zip = ZipFile.Open(path, ZipArchiveMode.Create); string directoryEntry; List directoryEntries = []; @@ -333,7 +347,16 @@ internal static partial class Helper20241217 _ = zip.CreateEntry(file.RelativePath); } foreach (File file in files) - _ = zip.CreateEntryFromFile(Path.Combine(record.Directory, file.RelativePath), file.RelativePath); + _ = zip.CreateEntryFromFile(Path.Combine(record.SourceDirectory, file.RelativePath), file.RelativePath); + } + + private static void WriteAllText(Record record, string text, string path) + { + WriteAllText(record.Path, text); + System.IO.File.Copy(record.Path, $"{path}.json"); + string checkFile = Path.Combine(record.SourceDirectory, ".html"); + if (System.IO.File.Exists(checkFile)) + System.IO.File.Copy(checkFile, $"{path}.html"); } } \ No newline at end of file