From 8d96766272188351a17fbf3947feada4ce07cbb6 Mon Sep 17 00:00:00 2001 From: Matt Pannella Date: Sat, 8 Oct 2022 11:44:00 -0400 Subject: [PATCH] option and cli parameter to preserve custom images summary includes firmware version uppdaed project version --- Program.cs | 24 +++++++++---- Updater.cs | 55 +++++++++++++++++++++++------- helpers/Util.cs | 72 +++++++++++++++++++++++++++++++++++++++ models/Settings/Config.cs | 2 ++ pocket_updater.csproj | 2 +- 5 files changed, 135 insertions(+), 20 deletions(-) create mode 100644 helpers/Util.cs diff --git a/Program.cs b/Program.cs index ca55c7f7..fcfd5e16 100644 --- a/Program.cs +++ b/Program.cs @@ -14,6 +14,7 @@ private static async Task Main(string[] args) string? path = Path.GetDirectoryName(location); bool extractAll = false; bool coreSelector = false; + bool preserveImages = false; Parser.Default.ParseArguments(args) .WithParsed(o => @@ -28,6 +29,9 @@ private static async Task Main(string[] args) if(o.CoreSelector) { coreSelector = true; } + if(o.PreserveImages) { + preserveImages = true; + } } ).WithNotParsed(o => { @@ -64,6 +68,10 @@ private static async Task Main(string[] args) RunCoreSelector(settings, cores); } + if(preserveImages || settings.GetConfig().preserve_images) { + updater.PreserveImages(true); + } + updater.ExtractAll(extractAll); updater.SetGithubApiKey(settings.GetConfig().github_token); @@ -105,7 +113,7 @@ static void updater_StatusUpdated(object sender, StatusUpdatedEventArgs e) static void updater_UpdateProcessComplete(object sender, UpdateProcessCompleteEventArgs e) { - Console.WriteLine("------------"); + Console.WriteLine("-------------"); Console.WriteLine(e.Message); if(e.InstalledCores.Count > 0) { Console.WriteLine("Cores Updated:"); @@ -121,8 +129,9 @@ static void updater_UpdateProcessComplete(object sender, UpdateProcessCompleteEv } Console.WriteLine(""); } - if(e.FirmwareUpdated) { + if(e.FirmwareUpdated != "") { Console.WriteLine("New Firmware was downloaded. Restart your Pocket to install"); + Console.WriteLine(e.FirmwareUpdated); Console.WriteLine(""); } Console.WriteLine("we did it, come again soon"); @@ -177,9 +186,12 @@ public class Options [Option('p', "path", HelpText = "Absolute path to install location", Required = false)] public string? InstallPath { get; set; } - [Option ('a', "all", Required = false, HelpText = "Extract all release assets, instead of just ones containing openFPGA cores.")] - public bool ExtractAll { get; set; } + [Option ('a', "all", Required = false, HelpText = "Extract all release assets, instead of just ones containing openFPGA cores.")] + public bool ExtractAll { get; set; } + + [Option ('c', "coreselector", Required = false, HelpText = "Run the core selector.")] + public bool CoreSelector { get; set; } - [Option ('c', "coreselector", Required = false, HelpText = "Run the core selector.")] - public bool CoreSelector { get; set; } + [Option ('i', "images", Required = false, HelpText = "Preserve the images directory, so custom images aren't overwritten by updates.")] + public bool PreserveImages { get; set; } } \ No newline at end of file diff --git a/Updater.cs b/Updater.cs index b6c07256..8d6a981f 100644 --- a/Updater.cs +++ b/Updater.cs @@ -20,6 +20,7 @@ public class PocketCoreUpdater \k [^>]* >"); private bool _downloadAssets = false; + private bool _preserveImages = false; private string _githubApiKey = ""; @@ -95,6 +96,15 @@ public void DownloadAssets(bool set) _downloadAssets = set; } + /// + /// Turn on/off preserving custom images + /// + /// Set to true to enable preserving custom images + public void PreserveImages(bool set) + { + _preserveImages = set; + } + public void DownloadFirmware(bool set) { _downloadFirmare = set; @@ -107,7 +117,8 @@ public async Task RunUpdates() { List> installed = new List>(); List installedAssets = new List(); - bool firmwareDownloaded = false; + bool imagesBacked = false; + string firmwareDownloaded = ""; if(_cores == null) { throw new Exception("Must initialize updater before running update process"); } @@ -115,19 +126,27 @@ public async Task RunUpdates() if(_downloadFirmare) { firmwareDownloaded = await UpdateFirmware(); } + + if(_preserveImages) { + _writeMessage("Backing up images"); + Util.BackupImagesDirectory(UpdateDirectory); + _writeMessage("Finished backing up images"); + Divide(); + imagesBacked = true; + } string json; foreach(Core core in _cores) { try { if(_settingsManager.GetCoreSettings(core.identifier).skip) { continue; } - Repo? repo = core.repository; - _writeMessage("Starting Repo: " + repo.name); string name = core.identifier; if(name == null) { _writeMessage("Core Name is required. Skipping."); continue; } + Repo? repo = core.repository; + _writeMessage("Checking Core: " + name); bool allowPrerelease = _settingsManager.GetCoreSettings(core.identifier).allowPrerelease; List? releases = await _fetchReleases(repo.owner, repo.name, _githubApiKey); if(releases == null) { @@ -177,7 +196,7 @@ public async Task RunUpdates() installedAssets.AddRange(list); } _writeMessage("Up to date. Skipping core"); - _writeMessage("------------"); + Divide(); continue; } } else { @@ -203,7 +222,7 @@ public async Task RunUpdates() if(!foundZip) { _writeMessage("No zip file found for release. Skipping"); - _writeMessage("------------"); + Divide(); continue; } if(_assets.ContainsKey(core.identifier)) { @@ -211,12 +230,17 @@ public async Task RunUpdates() installedAssets.AddRange(list); } _writeMessage("Installation complete."); - _writeMessage("------------"); + Divide(); } catch(Exception e) { _writeMessage("Uh oh something went wrong."); _writeMessage(e.Message); } } + if(imagesBacked) { + _writeMessage("Restoring images"); + Util.RestoreImagesDirectory(UpdateDirectory); + Divide(); + } UpdateProcessCompleteEventArgs args = new UpdateProcessCompleteEventArgs(); args.Message = "Update Process Complete"; args.InstalledCores = installed; @@ -273,6 +297,11 @@ private async Task> _DownloadAssets(Dependency assets) return installed; } + private void Divide() + { + _writeMessage("-------------"); + } + private string BuildAssetUrl(DependencyFile asset) { string archive = _settingsManager.GetConfig().archive_name; @@ -377,16 +406,16 @@ protected virtual void OnUpdateProcessComplete(UpdateProcessCompleteEventArgs e) } } - public async Task UpdateFirmware() + public async Task UpdateFirmware() { - bool flag = false; + string version = ""; _writeMessage("Checking for firmware updates..."); string html = await HttpHelper.GetHTML(FIRMWARE_URL); MatchCollection matches = BIN_REGEX.Matches(html); if(matches.Count != 1) { _writeMessage("cant find it"); - return false; + return version; } string firmwareUrl = matches[0].Groups["url"].ToString(); @@ -395,7 +424,7 @@ public async Task UpdateFirmware() Firmware current = _settingsManager.GetCurrentFirmware(); if(current.version != filename) { - flag = true; + version = filename; _writeMessage("Firmware update found. Downloading..."); await HttpHelper.DownloadFileAsync(firmwareUrl, Path.Combine(UpdateDirectory, filename)); _writeMessage("Download Complete"); @@ -410,8 +439,8 @@ public async Task UpdateFirmware() } else { _writeMessage("Firmware up to date."); } - - return flag; + Divide(); + return version; } public void ExtractAll(bool value) @@ -442,5 +471,5 @@ public class UpdateProcessCompleteEventArgs : EventArgs public string Message { get; set; } public List> InstalledCores { get; set; } public List InstalledAssets { get; set; } - public bool FirmwareUpdated { get; set; } = false; + public string FirmwareUpdated { get; set; } = ""; } diff --git a/helpers/Util.cs b/helpers/Util.cs new file mode 100644 index 00000000..7dba681a --- /dev/null +++ b/helpers/Util.cs @@ -0,0 +1,72 @@ +namespace pannella.analoguepocket; + +public class Util +{ + private static string _imagesDirectory = Path.Combine("Platforms", "_images"); + private static string _temp = "imagesbackup"; + public static bool BackupImagesDirectory(string rootPath) + { + string fullPath = Path.Combine(rootPath, _imagesDirectory); + if(!Directory.Exists(fullPath)) { + return false; + } + + string tempPath = Path.Combine(rootPath, _temp); + try { + Directory.CreateDirectory(tempPath); + CopyDirectory(fullPath, tempPath, false, true); + } catch (Exception) { + return false; + } + return true; + } + + public static bool RestoreImagesDirectory(string rootPath) + { + string fullPath = Path.Combine(rootPath, _imagesDirectory); + if(!Directory.Exists(fullPath)) { + return false; + } + string tempPath = Path.Combine(rootPath, _temp); + if(!Directory.Exists(tempPath)) { + return false; + } + CopyDirectory(tempPath, fullPath, false, true); + Directory.Delete(tempPath, true); + + return true; + } + + static void CopyDirectory(string sourceDir, string destinationDir, bool recursive, bool overwrite) + { + // Get information about the source directory + var dir = new DirectoryInfo(sourceDir); + + // Check if the source directory exists + if (!dir.Exists) + throw new DirectoryNotFoundException($"Source directory not found: {dir.FullName}"); + + // Cache directories before we start copying + DirectoryInfo[] dirs = dir.GetDirectories(); + + // Create the destination directory + Directory.CreateDirectory(destinationDir); + + // Get the files in the source directory and copy to the destination directory + foreach (FileInfo file in dir.GetFiles()) + { + string targetFilePath = Path.Combine(destinationDir, file.Name); + file.CopyTo(targetFilePath, overwrite); + } + + // If recursive and copying subdirectories, recursively call this method + if (recursive) + { + foreach (DirectoryInfo subDir in dirs) + { + string newDestinationDir = Path.Combine(destinationDir, subDir.Name); + CopyDirectory(subDir.FullName, newDestinationDir, true, overwrite); + } + } + } +} \ No newline at end of file diff --git a/models/Settings/Config.cs b/models/Settings/Config.cs index 6dcdacf5..7e6bc50f 100644 --- a/models/Settings/Config.cs +++ b/models/Settings/Config.cs @@ -7,6 +7,7 @@ public class Config public string? github_token { get; set; } public bool download_firmware { get; set; } public bool core_selector { get; set; } + public bool preserve_images { get; set; } public Config() { @@ -14,5 +15,6 @@ public Config() download_firmware = true; archive_name = "openFPGA-Files"; core_selector = true; + preserve_images = false; } } \ No newline at end of file diff --git a/pocket_updater.csproj b/pocket_updater.csproj index 37f63c73..c111a486 100644 --- a/pocket_updater.csproj +++ b/pocket_updater.csproj @@ -5,7 +5,7 @@ net6.0 enable enable - 2.2.0 + 2.3.0 Keep your Analogue Pocket up to date 2022 Matt Pannella Matt Pannella