From 9eeb62fecd7aa43e20966c2ab15cd04b83ca9879 Mon Sep 17 00:00:00 2001 From: Zoltanar Date: Fri, 16 Aug 2024 22:38:37 +0100 Subject: [PATCH] Remove DatabaseDumpReader namespace and change class name. --- Happy Reader/View/Tabs/SettingsTab.xaml.cs | 2 +- .../DumpReader/DumpReader.cs | 14 +- .../{Program.cs => DumpReaderStarter.cs} | 3 +- .../Happy_Apps_Core.csproj | 2 +- HappySearchObjectClasses/RSync.cs | 270 +++++++++--------- IthVnrSharpLib | 2 +- 6 files changed, 145 insertions(+), 148 deletions(-) rename HappySearchObjectClasses/DumpReader/{Program.cs => DumpReaderStarter.cs} (99%) diff --git a/Happy Reader/View/Tabs/SettingsTab.xaml.cs b/Happy Reader/View/Tabs/SettingsTab.xaml.cs index bc24a93..bdee5b0 100644 --- a/Happy Reader/View/Tabs/SettingsTab.xaml.cs +++ b/Happy Reader/View/Tabs/SettingsTab.xaml.cs @@ -202,7 +202,7 @@ private async void UpdateVndbData(object sender, RoutedEventArgs e) UpdateVndbButton.Content = $"{UpdateVndbText} (in progress)"; UpdateVndbButton.IsEnabled = false; MultiLogger.PreviousLogTime = null; - var updateResult = await Happy_Apps_Core.DumpReader.Program.Execute(UpdateLoggingAction); + var updateResult = await Happy_Apps_Core.DumpReader.DumpReaderStarter.Execute(UpdateLoggingAction); if (updateResult.Success) { diff --git a/HappySearchObjectClasses/DumpReader/DumpReader.cs b/HappySearchObjectClasses/DumpReader/DumpReader.cs index 84ad736..17cccd4 100644 --- a/HappySearchObjectClasses/DumpReader/DumpReader.cs +++ b/HappySearchObjectClasses/DumpReader/DumpReader.cs @@ -44,7 +44,7 @@ public DumpReader(string dumpFolder, string currentDatabaseFilePath, int userId, File.Copy(currentDatabaseFilePath, inProgressDbFile); //DRB = dump reader backup var backupPath = GetDatabaseFile(currentDbFile, "-DRB"); - Program.PrintLogLine([$"Backing up database to {backupPath}"]); + DumpReaderStarter.PrintLogLine([$"Backing up database to {backupPath}"]); File.Copy(currentDbFile.FullName, backupPath); UserId = userId; DumpFolder = dumpFolder; @@ -65,8 +65,8 @@ private string GetDatabaseFile(FileInfo currentDatabaseFile, string suffix) } public void Run(DateTime dumpDate, int[] previousVnIds, int[] previousCharacterIds) { - Program.PrintLogLine(["Starting Dump Reader..."]); - Program.PrintLogLine(["Loading Tag/Trait Dump files..."]); + DumpReaderStarter.PrintLogLine(["Starting Dump Reader..."]); + DumpReaderStarter.PrintLogLine(["Loading Tag/Trait Dump files..."]); DumpFiles.Load(); var votesFilePath = FindVotesFile(); Load((i, t) => Database.Producers.Add(i, false, true, t), "db\\producers"); @@ -102,9 +102,9 @@ public void Run(DateTime dumpDate, int[] previousVnIds, int[] previousCharacterI Database.VisualNovels.Add(i, false, true, t); ResolveUserVnForVn(i, t); }, "db\\vn"); - Program.PrintLogLine([$"Added {newTitleCount} new titles."]); + DumpReaderStarter.PrintLogLine([$"Added {newTitleCount} new titles."]); Database.SaveLatestDumpUpdate(dumpDate); - Program.PrintLogLine(["Completed."]); + DumpReaderStarter.PrintLogLine(["Completed."]); } private void LoadStaff() @@ -266,7 +266,7 @@ private void LoadAndResolveTags() if (i.Ignore) return; VnTags.Add(i); }, "db\\tags_vn"); - Program.PrintLogLine(["Resolving Tags..."]); + DumpReaderStarter.PrintLogLine(["Resolving Tags..."]); var groupedTags = VnTags.GroupBy(t => (t.TagId, t.VnId)).ToArray(); WrapInTransaction(trans => { @@ -289,7 +289,7 @@ private void LoadAndResolveTags() private void Load(Action addToList, string filePath, bool useHeaderFile = true) where T : DumpItem, new() { - Program.PrintLogLine([$"Loading for {typeof(T).Name}..."]); + DumpReaderStarter.PrintLogLine([$"Loading for {typeof(T).Name}..."]); new T().SetDumpHeaders((useHeaderFile ? File.ReadAllLines(Path.Combine(DumpFolder, filePath + ".header")).Single() : string.Empty).Split('\t')); diff --git a/HappySearchObjectClasses/DumpReader/Program.cs b/HappySearchObjectClasses/DumpReader/DumpReaderStarter.cs similarity index 99% rename from HappySearchObjectClasses/DumpReader/Program.cs rename to HappySearchObjectClasses/DumpReader/DumpReaderStarter.cs index ae6e041..3e6560c 100644 --- a/HappySearchObjectClasses/DumpReader/Program.cs +++ b/HappySearchObjectClasses/DumpReader/DumpReaderStarter.cs @@ -6,11 +6,10 @@ using System.Text; using System.Threading.Tasks; using System.Windows; -using DatabaseDumpReader; namespace Happy_Apps_Core.DumpReader; -public static class Program +public static class DumpReaderStarter { private static readonly string DumpFolder = Path.Combine(StaticHelpers.AppDataFolder, "Database Dumps"); private const string LatestDbDumpUrl = "https://dl.vndb.org/dump/vndb-db-latest.tar.zst"; diff --git a/HappySearchObjectClasses/Happy_Apps_Core.csproj b/HappySearchObjectClasses/Happy_Apps_Core.csproj index 82fd9ac..f841171 100644 --- a/HappySearchObjectClasses/Happy_Apps_Core.csproj +++ b/HappySearchObjectClasses/Happy_Apps_Core.csproj @@ -150,7 +150,7 @@ - + diff --git a/HappySearchObjectClasses/RSync.cs b/HappySearchObjectClasses/RSync.cs index 8d51ed4..c45b0f9 100644 --- a/HappySearchObjectClasses/RSync.cs +++ b/HappySearchObjectClasses/RSync.cs @@ -3,147 +3,145 @@ using System.Diagnostics; using System.IO; using System.Reflection; -using Happy_Apps_Core; -namespace DatabaseDumpReader +namespace Happy_Apps_Core; + +internal static class RSync { - internal static class RSync - { - private const string RsyncExecutableName = @"rsync.exe"; - private const string Switches = @"-rptv --del"; + private const string RsyncExecutableName = @"rsync.exe"; + private const string Switches = @"-rptv --del"; - public static bool Run(string rsyncUrl, string destinationFolderPath, out string errorMessage) - { - var filesCopied = new List(); - try - { - if (!ValidatePathsAndFiles(destinationFolderPath, out errorMessage, out var originalRsyncFile, out var destinationFolder)) return false; - if (!CopyRSyncFiles(ref errorMessage, destinationFolder, originalRsyncFile, filesCopied)) return false; - Debug.Assert(destinationFolder.Parent != null, "destinationFolder.Parent != null"); - var psi = new ProcessStartInfo("cmd.exe") - { - Arguments = $"/C \"\"rsync\" {Switches} \"{rsyncUrl}\" \"{destinationFolder.Name}\"\"", - CreateNoWindow = true, - RedirectStandardOutput = true, - RedirectStandardInput = true, - RedirectStandardError = true, - UseShellExecute = false, - WorkingDirectory = destinationFolder.Parent.FullName - }; - StaticHelpers.Logger.ToFile("Starting Rsync and waiting for completion..."); - var process = Process.Start(psi); - if (process == null) - { - errorMessage = "Failed to start rsync"; - return false; - } - if (process.HasExited) - { - errorMessage = "rsync process exited immediately."; - return false; - } - process.OutputDataReceived += Process_OutputDataReceived; - process.ErrorDataReceived += Process_OutputDataReceived; - process.BeginOutputReadLine(); - process.BeginErrorReadLine(); - process.WaitForExit(); - errorMessage = "No errors."; - return true; - } - catch (Exception ex) - { - errorMessage = ex.ToString(); - return false; - } - finally - { - DeleteFiles(filesCopied); - } - } + public static bool Run(string rsyncUrl, string destinationFolderPath, out string errorMessage) + { + var filesCopied = new List(); + try + { + if (!ValidatePathsAndFiles(destinationFolderPath, out errorMessage, out var originalRsyncFile, out var destinationFolder)) return false; + if (!CopyRSyncFiles(ref errorMessage, destinationFolder, originalRsyncFile, filesCopied)) return false; + Debug.Assert(destinationFolder.Parent != null, "destinationFolder.Parent != null"); + var psi = new ProcessStartInfo("cmd.exe") + { + Arguments = $"/C \"\"rsync\" {Switches} \"{rsyncUrl}\" \"{destinationFolder.Name}\"\"", + CreateNoWindow = true, + RedirectStandardOutput = true, + RedirectStandardInput = true, + RedirectStandardError = true, + UseShellExecute = false, + WorkingDirectory = destinationFolder.Parent.FullName + }; + StaticHelpers.Logger.ToFile("Starting Rsync and waiting for completion..."); + var process = Process.Start(psi); + if (process == null) + { + errorMessage = "Failed to start rsync"; + return false; + } + if (process.HasExited) + { + errorMessage = "rsync process exited immediately."; + return false; + } + process.OutputDataReceived += Process_OutputDataReceived; + process.ErrorDataReceived += Process_OutputDataReceived; + process.BeginOutputReadLine(); + process.BeginErrorReadLine(); + process.WaitForExit(); + errorMessage = "No errors."; + return true; + } + catch (Exception ex) + { + errorMessage = ex.ToString(); + return false; + } + finally + { + DeleteFiles(filesCopied); + } + } - private static bool CopyRSyncFiles( - ref string errorMessage, - DirectoryInfo destinationFolder, - FileInfo originalRsyncFile, - ICollection filesCopied) - { - try - { - var rsyncDirectory = destinationFolder.Parent; - // ReSharper disable once PossibleNullReferenceException - foreach (var file in originalRsyncFile.Directory.GetFiles()) - { - // ReSharper disable once PossibleNullReferenceException - var copyDestination = Path.Combine(rsyncDirectory.FullName, file.Name); - if (File.Exists(copyDestination)) continue; - file.CopyTo(copyDestination); - filesCopied.Add(copyDestination); - } - return true; - } - catch (Exception ex) - { - errorMessage = $"Failed to temporarily copy rsync files to destination path: {ex}"; - return false; - } - } + private static bool CopyRSyncFiles( + ref string errorMessage, + DirectoryInfo destinationFolder, + FileInfo originalRsyncFile, + ICollection filesCopied) + { + try + { + var rsyncDirectory = destinationFolder.Parent; + // ReSharper disable once PossibleNullReferenceException + foreach (var file in originalRsyncFile.Directory.GetFiles()) + { + // ReSharper disable once PossibleNullReferenceException + var copyDestination = Path.Combine(rsyncDirectory.FullName, file.Name); + if (File.Exists(copyDestination)) continue; + file.CopyTo(copyDestination); + filesCopied.Add(copyDestination); + } + return true; + } + catch (Exception ex) + { + errorMessage = $"Failed to temporarily copy rsync files to destination path: {ex}"; + return false; + } + } - private static bool ValidatePathsAndFiles( - string destinationFolderPath, - out string errorMessage, - out FileInfo originalRsyncFile, - out DirectoryInfo destinationFolder) - { - var assemblyDirectory = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory; - // ReSharper disable once PossibleNullReferenceException - originalRsyncFile = new FileInfo(Path.Combine(assemblyDirectory.FullName, "Rsync", RsyncExecutableName)); - destinationFolder = new DirectoryInfo(Path.GetFullPath(destinationFolderPath)); - if (destinationFolder.Parent == null) - { - errorMessage = "Destination folder cannot be a drive (or root path)."; - return false; - } - if (!destinationFolder.Exists) - { - try - { - destinationFolder.Create(); - } - catch (IOException ex) - { - errorMessage = $"Failed to create destination folder '{destinationFolder.FullName}': {ex}"; - return false; - } - } - if (!originalRsyncFile.Exists) - { - errorMessage = $"rsync executable not found: {originalRsyncFile}"; - return false; - } + private static bool ValidatePathsAndFiles( + string destinationFolderPath, + out string errorMessage, + out FileInfo originalRsyncFile, + out DirectoryInfo destinationFolder) + { + var assemblyDirectory = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory; + // ReSharper disable once PossibleNullReferenceException + originalRsyncFile = new FileInfo(Path.Combine(assemblyDirectory.FullName, "Rsync", RsyncExecutableName)); + destinationFolder = new DirectoryInfo(Path.GetFullPath(destinationFolderPath)); + if (destinationFolder.Parent == null) + { + errorMessage = "Destination folder cannot be a drive (or root path)."; + return false; + } + if (!destinationFolder.Exists) + { + try + { + destinationFolder.Create(); + } + catch (IOException ex) + { + errorMessage = $"Failed to create destination folder '{destinationFolder.FullName}': {ex}"; + return false; + } + } + if (!originalRsyncFile.Exists) + { + errorMessage = $"rsync executable not found: {originalRsyncFile}"; + return false; + } - errorMessage = "No errors."; - return true; - } + errorMessage = "No errors."; + return true; + } - private static void DeleteFiles(IEnumerable filesCopied) - { - foreach (var file in filesCopied) - { - try - { - new FileInfo(file).Delete(); - } - catch (Exception ex) - { - StaticHelpers.Logger.ToFile($"Failed to delete file '{file}': {ex}"); - } - } - } + private static void DeleteFiles(IEnumerable filesCopied) + { + foreach (var file in filesCopied) + { + try + { + new FileInfo(file).Delete(); + } + catch (Exception ex) + { + StaticHelpers.Logger.ToFile($"Failed to delete file '{file}': {ex}"); + } + } + } - private static void Process_OutputDataReceived(object sender, DataReceivedEventArgs e) - { - if (string.IsNullOrEmpty(e.Data)) return; - Debug.WriteLine(e.Data); - } - } -} + private static void Process_OutputDataReceived(object sender, DataReceivedEventArgs e) + { + if (string.IsNullOrEmpty(e.Data)) return; + Debug.WriteLine(e.Data); + } +} \ No newline at end of file diff --git a/IthVnrSharpLib b/IthVnrSharpLib index 5657c8e..30e26cf 160000 --- a/IthVnrSharpLib +++ b/IthVnrSharpLib @@ -1 +1 @@ -Subproject commit 5657c8e5ae87a77534c8f4968c7c42ca616f50fb +Subproject commit 30e26cfba8769e9fb08ee5460e52ebce25f5bb75