From 8d74174eb03087ebec877822dbb28beb757e56e5 Mon Sep 17 00:00:00 2001 From: That_One_Nerd Date: Fri, 17 Mar 2023 09:58:59 -0400 Subject: [PATCH] Moved the command to its own seperate module. --- SrcMod/Shell/Modules/BaseModule.cs | 96 ----------------------- SrcMod/Shell/Modules/CompressionModule.cs | 93 ++++++++++++++++++++++ 2 files changed, 93 insertions(+), 96 deletions(-) create mode 100644 SrcMod/Shell/Modules/CompressionModule.cs diff --git a/SrcMod/Shell/Modules/BaseModule.cs b/SrcMod/Shell/Modules/BaseModule.cs index 58b6b38..41d45ec 100644 --- a/SrcMod/Shell/Modules/BaseModule.cs +++ b/SrcMod/Shell/Modules/BaseModule.cs @@ -19,102 +19,6 @@ public static class BaseModule Console.Write("\x1b[3J"); } - [Command("compress")] - public static void CompressFile(CompressedFileType type, string source, string? destination = null, - CompressionLevel level = CompressionLevel.Optimal) - { - destination ??= Path.Combine(Path.GetDirectoryName(source)!, - $"{Path.GetFileNameWithoutExtension(source)}.{type.ToString().ToLower()}"); - - string absSource = Path.GetFullPath(source), - absDest = Path.GetFullPath(destination); - - switch (type) - { - case CompressedFileType.Zip: - if (File.Exists(source)) - { - if (File.Exists(destination)) throw new($"File already exists at \"{destination}\""); - string message = $"Compressing file at \"{source}\" into \"{destination}\"..."; - Write(message); - - Stream writer = new FileStream(absDest, FileMode.CreateNew); - ZipArchive archive = new(writer, ZipArchiveMode.Create); - - archive.CreateEntryFromFile(absSource, Path.GetFileName(absSource), level); - - archive.Dispose(); - writer.Dispose(); - - Console.CursorLeft = 0; - Console.CursorTop -= (message.Length / Console.BufferWidth) + 1; - Write(new string(' ', message.Length), newLine: false); - } - else if (Directory.Exists(source)) - { - if (File.Exists(destination)) throw new($"File already exists at \"{destination}\""); - - Write($"Compressing folder at \"{source}\" into \"{destination}\"..."); - - Stream writer = new FileStream(absDest, FileMode.CreateNew); - ZipArchive archive = new(writer, ZipArchiveMode.Create); - - List files = new(GetAllFiles(absSource)), - relative = new(); - foreach (string f in files) relative.Add(Path.GetRelativePath(absSource, f)); - - LoadingBarStart(); - for (int i = 0; i < files.Count; i++) - { - archive.CreateEntryFromFile(files[i], relative[i], level); - LoadingBarSet((i + 1) / (float)files.Count, ConsoleColor.DarkGreen); - Console.CursorLeft = 0; - string message = $"{relative[i]}"; - int remainder = Console.BufferWidth - message.Length; - if (remainder >= 0) message += new string(' ', remainder); - else message = $"...{message[(3 - remainder)..]}"; - - Write(message, newLine: false); - } - - archive.Dispose(); - writer.Dispose(); - - LoadingBarEnd(); - - Console.CursorLeft = 0; - Write(new string(' ', Console.BufferWidth), newLine: false); - Console.SetCursorPosition(0, Console.CursorTop - 2); - Write(new string(' ', Console.BufferWidth), newLine: false); - } - else throw new("No file or directory located at \"source\""); - break; - - default: throw new($"Unknown type: \"{type}\""); - } - - DateTime stamp = DateTime.Now; - - Program.Shell!.AddHistory(new() - { - action = delegate - { - if (!File.Exists(absDest)) - { - Write("Looks like the job is already completed Boss.", ConsoleColor.DarkYellow); - return; - } - - FileInfo info = new(absDest); - if ((info.LastWriteTime - stamp).TotalMilliseconds >= 10) - throw new("The archive has been modified and probably shouldn't be undone."); - - File.Delete(absDest); - }, - name = $"Compressed a file or folder into a {type} archive located at \"{destination}\"" - }); - } - [Command("copy")] public static void CopyFile(string source, string destination) { diff --git a/SrcMod/Shell/Modules/CompressionModule.cs b/SrcMod/Shell/Modules/CompressionModule.cs new file mode 100644 index 0000000..ee03f0b --- /dev/null +++ b/SrcMod/Shell/Modules/CompressionModule.cs @@ -0,0 +1,93 @@ +namespace SrcMod.Shell.Modules; + +[Module("compress")] +public static class CompressionModule +{ + public static void CompressZip(string source, string? destination = null, + CompressionLevel level = CompressionLevel.Optimal) + { + destination ??= Path.Combine(Path.GetDirectoryName(Path.GetFullPath(source))!, + $"{Path.GetFileNameWithoutExtension(source)}.zip"); + + string absSource = Path.GetFullPath(source), + localDest = Path.GetRelativePath(Program.Shell!.WorkingDirectory, destination); + + if (File.Exists(source)) + { + if (File.Exists(destination)) throw new($"File already exists at \"{localDest}\""); + string message = $"Compressing file at \"{source}\" into \"{localDest}\"..."; + Write(message); + + Stream writer = new FileStream(destination, FileMode.CreateNew); + ZipArchive archive = new(writer, ZipArchiveMode.Create); + + archive.CreateEntryFromFile(absSource, Path.GetFileName(absSource), level); + + archive.Dispose(); + writer.Dispose(); + + Console.CursorLeft = 0; + Console.CursorTop -= (message.Length / Console.BufferWidth) + 1; + Write(new string(' ', message.Length), newLine: false); + } + else if (Directory.Exists(source)) + { + if (File.Exists(destination)) throw new($"File already exists at \"{localDest}\""); + + Write($"Compressing folder at \"{source}\" into \"{localDest}\"..."); + + Stream writer = new FileStream(destination, FileMode.CreateNew); + ZipArchive archive = new(writer, ZipArchiveMode.Create); + + List files = new(GetAllFiles(absSource)), + relative = new(); + foreach (string f in files) relative.Add(Path.GetRelativePath(absSource, f)); + + LoadingBarStart(); + for (int i = 0; i < files.Count; i++) + { + archive.CreateEntryFromFile(files[i], relative[i], level); + LoadingBarSet((i + 1) / (float)files.Count, ConsoleColor.DarkGreen); + Console.CursorLeft = 0; + string message = $"{relative[i]}"; + int remainder = Console.BufferWidth - message.Length; + if (remainder >= 0) message += new string(' ', remainder); + else message = $"...{message[(3 - remainder)..]}"; + + Write(message, newLine: false); + } + + archive.Dispose(); + writer.Dispose(); + + LoadingBarEnd(); + + Console.CursorLeft = 0; + Write(new string(' ', Console.BufferWidth), newLine: false); + Console.SetCursorPosition(0, Console.CursorTop - 2); + Write(new string(' ', Console.BufferWidth), newLine: false); + } + else throw new("No file or directory located at \"source\""); + + DateTime stamp = DateTime.Now; + + Program.Shell!.AddHistory(new() + { + action = delegate + { + if (!File.Exists(absDest)) + { + Write("Looks like the job is already completed Boss.", ConsoleColor.DarkYellow); + return; + } + + FileInfo info = new(absDest); + if ((info.LastWriteTime - stamp).TotalMilliseconds >= 10) + throw new("The archive has been modified and probably shouldn't be undone."); + + File.Delete(absDest); + }, + name = $"Compressed a file or folder into a {type} archive located at \"{destination}\"" + }); + } +}