diff --git a/Program.cs b/Program.cs index 470106b..13846c3 100644 --- a/Program.cs +++ b/Program.cs @@ -1,8 +1,9 @@ public class Program { static void Main(string[] args) { - string result = Update.copyEverthingToBackup(); + string result = Update.copyEverthingBeforeUpdateToBackupLocation(); Console.WriteLine(result); - Update.startUpdate(); + string result2 = Update.copyEverthingFromBackupLocationToFinalDestination(); + Console.WriteLine(result2); } } \ No newline at end of file diff --git a/README.md b/README.md index 63d34e5..8c68107 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ # update-c# A rewrite of the legendary update script in C# + +This time with file handling entirely in C# while keeping all scripting parts in bash. ## Authors - [@ProfessionalUwU](http://192.168.0.69:3000/ProfessionalUwU) @@ -46,11 +48,20 @@ Run executable ```bash ./update ``` -## Roadmap +## Roadmap/ToDo - Figure out how to do options/arguments -- Backup important files -- Figure out how to make a single executable +- Backup all necessary files +- Shrink size of the executable +- Potentially speed up file handling +- Color output according to state (success = green, failure = red, info = yellow) +- Backup pacman database +- Compress all files to single archive + +## Sites I used to help make this project +- [dotnetperls](https://dotnetperls.com) +- [stackoverflow](https://stackoverflow.com/questions/tagged/c%23) +- [c-sharpcorner](https://www.c-sharpcorner.com) ## Contributing Contributions are always welcome! diff --git a/Update.cs b/Update.cs index 18a4f77..79495ba 100644 --- a/Update.cs +++ b/Update.cs @@ -11,33 +11,53 @@ public class Update { throw new ApplicationException("This script doesn't support your operating system."); } } + public static string copyEverthingBeforeUpdateToBackupLocation() { + string targetPath = getHomePath() + "/backup/"; + + string[] systemFilesToCopy = {"/etc/fstab", "/etc/makepkg.conf"}; + List filesToCopy = new List(systemFilesToCopy); - public static void startUpdate() { - Process process = new Process(); + string pacmanPackageListBeforeUpdate = getHomePath() + "/pacman-pre.txt"; + filesToCopy.Add(pacmanPackageListBeforeUpdate); + string flatpakListBeforeUpdate = getHomePath() + "/flatpak-pre.txt"; + filesToCopy.Add(flatpakListBeforeUpdate); - ProcessStartInfo processStartInfo = new ProcessStartInfo(); - //processStartInfo.WindowStyle = ProcessWindowStyle.Hidden; - processStartInfo.FileName = @"yay"; - processStartInfo.WorkingDirectory = getHomePath(); - //processStartInfo.Arguments = "--color"; - processStartInfo.RedirectStandardOutput = true; - processStartInfo.RedirectStandardError = true; - processStartInfo.UseShellExecute = false; + if (!Directory.Exists(targetPath)) { + Directory.CreateDirectory(targetPath); + } else { + foreach (string file in filesToCopy) { + FileInfo info = new FileInfo(file); + string destFile = Path.Combine(targetPath, info.Name); + File.Copy(info.FullName, destFile, true); + } + } - process.StartInfo = processStartInfo; - process.Start(); - - // Read the output of the "yay" command - string output = process.StandardOutput.ReadLine(); - string error = process.StandardError.ReadToEnd(); - process.WaitForExit(); - - Console.WriteLine(output); - Console.WriteLine(error); + string copiedFiles = string.Join(", ", filesToCopy); + Console.ForegroundColor = ConsoleColor.Green; + return $"Copied {copiedFiles} to {targetPath}"; } - // using System; + public static string copyEverthingFromBackupLocationToFinalDestination() { + string targetPath = "/artemis/test/"; + string sourcePath = getHomePath() + "/backup/"; + string[] intermediateBackupLocation = Directory.GetFiles(sourcePath); + + if (!Directory.Exists(targetPath)) { + throw new DirectoryNotFoundException("Target directory not found"); + } else { + foreach (string file in intermediateBackupLocation) { + FileInfo info = new FileInfo(file); + string destFile = Path.Combine(targetPath, info.Name); + File.Copy(info.FullName, destFile, true); + } + } + return $"Copied everything successfully to {targetPath}"; + } + +// Commented code below was written by ChatGPT + +// using System; // using System.Diagnostics; // namespace UpdateLinux @@ -73,21 +93,4 @@ public class Update { // } // } // } - - public static string copyEverthingToBackup() { - string fileName = "fstab"; - string sourcePath = @"/etc/"; - string targetPath = getHomePath(); - - string backupPath = targetPath + "/backup/"; - - string sourceFile = Path.Combine(sourcePath, fileName); - string destFile = Path.Combine(backupPath, fileName); - - Directory.CreateDirectory(backupPath); - - File.Copy(sourceFile, destFile, true); - - return $"Copied {fileName} to {backupPath}"; - } } \ No newline at end of file