Moved the command to its own seperate module.
This commit is contained in:
parent
5cb9442e34
commit
8d74174eb0
@ -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<string> 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)
|
||||
{
|
||||
|
||||
93
SrcMod/Shell/Modules/CompressionModule.cs
Normal file
93
SrcMod/Shell/Modules/CompressionModule.cs
Normal file
@ -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<string> 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}\""
|
||||
});
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user