Skip to content

Commit

Permalink
option and cli parameter to preserve custom images
Browse files Browse the repository at this point in the history
summary includes firmware version
uppdaed project version
  • Loading branch information
mattpannella committed Oct 8, 2022
1 parent e326542 commit 8d96766
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 20 deletions.
24 changes: 18 additions & 6 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Options>(args)
.WithParsed<Options>(o =>
Expand All @@ -28,6 +29,9 @@ private static async Task Main(string[] args)
if(o.CoreSelector) {
coreSelector = true;
}
if(o.PreserveImages) {
preserveImages = true;
}
}
).WithNotParsed<Options>(o =>
{
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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:");
Expand All @@ -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");
Expand Down Expand Up @@ -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; }
}
55 changes: 42 additions & 13 deletions Updater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class PocketCoreUpdater
\k<q>
[^>]* >");
private bool _downloadAssets = false;
private bool _preserveImages = false;

private string _githubApiKey = "";

Expand Down Expand Up @@ -95,6 +96,15 @@ public void DownloadAssets(bool set)
_downloadAssets = set;
}

/// <summary>
/// Turn on/off preserving custom images
/// </summary>
/// <param name="set">Set to true to enable preserving custom images</param>
public void PreserveImages(bool set)
{
_preserveImages = set;
}

public void DownloadFirmware(bool set)
{
_downloadFirmare = set;
Expand All @@ -107,27 +117,36 @@ public async Task RunUpdates()
{
List<Dictionary<string, string>> installed = new List<Dictionary<string, string>>();
List<string> installedAssets = new List<string>();
bool firmwareDownloaded = false;
bool imagesBacked = false;
string firmwareDownloaded = "";
if(_cores == null) {
throw new Exception("Must initialize updater before running update process");
}

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<Github.Release>? releases = await _fetchReleases(repo.owner, repo.name, _githubApiKey);
if(releases == null) {
Expand Down Expand Up @@ -177,7 +196,7 @@ public async Task RunUpdates()
installedAssets.AddRange(list);
}
_writeMessage("Up to date. Skipping core");
_writeMessage("------------");
Divide();
continue;
}
} else {
Expand All @@ -203,20 +222,25 @@ public async Task RunUpdates()

if(!foundZip) {
_writeMessage("No zip file found for release. Skipping");
_writeMessage("------------");
Divide();
continue;
}
if(_assets.ContainsKey(core.identifier)) {
var list = await _DownloadAssets(_assets[core.identifier]);
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;
Expand Down Expand Up @@ -273,6 +297,11 @@ private async Task<List<string>> _DownloadAssets(Dependency assets)
return installed;
}

private void Divide()
{
_writeMessage("-------------");
}

private string BuildAssetUrl(DependencyFile asset)
{
string archive = _settingsManager.GetConfig().archive_name;
Expand Down Expand Up @@ -377,16 +406,16 @@ protected virtual void OnUpdateProcessComplete(UpdateProcessCompleteEventArgs e)
}
}

public async Task<bool> UpdateFirmware()
public async Task<string> 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();
Expand All @@ -395,7 +424,7 @@ public async Task<bool> 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");
Expand All @@ -410,8 +439,8 @@ public async Task<bool> UpdateFirmware()
} else {
_writeMessage("Firmware up to date.");
}

return flag;
Divide();
return version;
}

public void ExtractAll(bool value)
Expand Down Expand Up @@ -442,5 +471,5 @@ public class UpdateProcessCompleteEventArgs : EventArgs
public string Message { get; set; }
public List<Dictionary<string, string>> InstalledCores { get; set; }
public List<string> InstalledAssets { get; set; }
public bool FirmwareUpdated { get; set; } = false;
public string FirmwareUpdated { get; set; } = "";
}
72 changes: 72 additions & 0 deletions helpers/Util.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
}
2 changes: 2 additions & 0 deletions models/Settings/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ 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()
{
download_assets = true;
download_firmware = true;
archive_name = "openFPGA-Files";
core_selector = true;
preserve_images = false;
}
}
2 changes: 1 addition & 1 deletion pocket_updater.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>2.2.0</Version>
<Version>2.3.0</Version>
<Description>Keep your Analogue Pocket up to date</Description>
<Copyright>2022 Matt Pannella</Copyright>
<Authors>Matt Pannella</Authors>
Expand Down

0 comments on commit 8d96766

Please sign in to comment.