Skip to content

Commit

Permalink
Merge pull request #65 from mattpannella/develop
Browse files Browse the repository at this point in the history
merge develop into master
  • Loading branch information
mattpannella authored Nov 30, 2022
2 parents 9c9db2a + 9b254b8 commit 3c8e1ac
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 141 deletions.
34 changes: 34 additions & 0 deletions Base.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
namespace pannella.analoguepocket;

public class Base
{
public event EventHandler<StatusUpdatedEventArgs>? StatusUpdated;
protected void Divide()
{
_writeMessage("-------------");
}

protected void _writeMessage(string message)
{
StatusUpdatedEventArgs args = new StatusUpdatedEventArgs();
args.Message = message;
OnStatusUpdated(args);
}

protected virtual void OnStatusUpdated(StatusUpdatedEventArgs e)
{
EventHandler<StatusUpdatedEventArgs> handler = StatusUpdated;
if(handler != null)
{
handler(this, e);
}
}
}

public class StatusUpdatedEventArgs : EventArgs
{
/// <summary>
/// Contains the message from the updater
/// </summary>
public string Message { get; set; }
}
7 changes: 4 additions & 3 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using pannella.analoguepocket;
using System.Runtime.InteropServices;
using CommandLine;
using System.IO.Compression;

internal class Program
{
Expand Down Expand Up @@ -50,7 +49,7 @@ private static async Task Main(string[] args)
}
);

//path = "/Users/mattpannella/pocket-test";
// path = "/Users/mattpannella/pocket-test";

ConsoleKey response;

Expand Down Expand Up @@ -84,6 +83,7 @@ private static async Task Main(string[] args)
if(coreSelector || settings.GetConfig().core_selector) {
List<Core> cores = await CoresService.GetCores();
RunCoreSelector(settings, cores);
updater.LoadSettings();
}

if(!forceUpdate) {
Expand All @@ -99,6 +99,7 @@ private static async Task Main(string[] args)
case 2:
List<Core> cores = await CoresService.GetCores();
RunCoreSelector(settings, cores);
updater.LoadSettings();
Pause();
break;
case 3:
Expand Down Expand Up @@ -297,7 +298,7 @@ private static void Pause()
"Update All",
"Update Firmware",
"Select Cores",
"Download Image Packs",
"Download Platform Image Packs",
"Exit"
};
}
Expand Down
153 changes: 18 additions & 135 deletions Updater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@

namespace pannella.analoguepocket;

public class PocketCoreUpdater
public class PocketCoreUpdater : Base
{
private const string ARCHIVE_BASE_URL = "https://archive.org/download";
private static readonly string[] ZIP_TYPES = {"application/x-zip-compressed", "application/zip"};
private const string FIRMWARE_FILENAME_PATTERN = "pocket_firmware_*.bin";
private const string FIRMWARE_URL = "https://www.analogue.co/support/pocket/firmware/latest";
private const string ZIP_FILE_NAME = "core.zip";
private static readonly Regex BIN_REGEX = new Regex(@"(?inx)
<a \s [^>]*
href \s* = \s*
Expand Down Expand Up @@ -72,6 +70,9 @@ public async Task LoadDependencies()
public async Task LoadCores()
{
_cores = await CoresService.GetCores();
foreach(Core core in _cores) {
core.StatusUpdated += updater_StatusUpdated; //attach handler to bubble event up
}
// await LoadNonAPICores();
}

Expand Down Expand Up @@ -155,7 +156,6 @@ public async Task RunUpdates()
_writeMessage("Core Name is required. Skipping.");
continue;
}
Repo? repo = core.repository;
_writeMessage("Checking Core: " + name);

var mostRecentRelease = core.release;
Expand Down Expand Up @@ -197,7 +197,7 @@ public async Task RunUpdates()
_writeMessage("Downloading core");
}

await _fetchCustomRelease(core);
await core.Install(UpdateDirectory, date.ToString("yyyyMMdd"));
Dictionary<string, string> summary = new Dictionary<string, string>();
summary.Add("version", date.ToString("yyyyMMdd"));
summary.Add("core", core.identifier);
Expand All @@ -217,10 +217,9 @@ public async Task RunUpdates()
_writeMessage("Core Name is required. Skipping.");
continue;
}
Repo? repo = core.repository;

_writeMessage("Checking Core: " + name);
bool allowPrerelease = _settingsManager.GetCoreSettings(core.identifier).allowPrerelease;

var mostRecentRelease = core.release;

if(allowPrerelease && core.prerelease != null) {
Expand All @@ -239,8 +238,6 @@ public async Task RunUpdates()
string localCoreFile = Path.Combine(UpdateDirectory, "Cores/"+name+"/core.json");
bool fileExists = File.Exists(localCoreFile);

bool foundZip = false;

if (fileExists) {
json = File.ReadAllText(localCoreFile);

Expand Down Expand Up @@ -277,28 +274,12 @@ public async Task RunUpdates()
_writeMessage("Downloading core");
}

//iterate through assets to find the zip release
var release = await _fetchRelease(repo.owner, repo.name, tag_name, _githubApiKey);
List<Github.Asset> assets = release.assets;
foreach(Github.Asset asset in assets) {
if(!ZIP_TYPES.Contains(asset.content_type)) {
//not a zip file. move on
continue;
}
foundZip = true;
if(await _getAsset(asset.browser_download_url, core.identifier)) {
Dictionary<string, string> summary = new Dictionary<string, string>();
summary.Add("version", releaseSemver);
summary.Add("core", core.identifier);
summary.Add("platform", core.platform);
installed.Add(summary);
}
}

if(!foundZip) {
_writeMessage("No zip file found for release. Skipping");
Divide();
continue;
if(await core.Install(UpdateDirectory, tag_name, _githubApiKey, _extractAll)) {
Dictionary<string, string> summary = new Dictionary<string, string>();
summary.Add("version", releaseSemver);
summary.Add("core", core.identifier);
summary.Add("platform", core.platform);
installed.Add(summary);
}
if(_assets.ContainsKey(core.identifier)) {
var list = await _DownloadAssets(_assets[core.identifier]);
Expand Down Expand Up @@ -436,94 +417,11 @@ private string BuildAssetUrlNew(string filename)
return ARCHIVE_BASE_URL + "/" + archive + "/" + filename;
}

private async Task<Github.Release>? _fetchRelease(string user, string repository, string tag_name, string token = "")
{
try {
var release = await GithubApi.GetRelease(user, repository, tag_name, token);
return release;
} catch (HttpRequestException e) {
_writeMessage("Error communicating with Github API.");
_writeMessage(e.Message);
return null;
}
}

private async Task<bool> _getAsset(string downloadLink, string coreName)
{
bool updated = false;
_writeMessage("Downloading file " + downloadLink + "...");
string zipPath = Path.Combine(UpdateDirectory, ZIP_FILE_NAME);
string extractPath = UpdateDirectory;
await HttpHelper.DownloadFileAsync(downloadLink, zipPath);

bool isCore = _extractAll;

if(!isCore) {
var zip = ZipFile.OpenRead(zipPath);
foreach(ZipArchiveEntry entry in zip.Entries) {
string[] parts = entry.FullName.Split('/');
if(parts.Contains("Cores")) {
isCore = true;
break;
}
}
zip.Dispose();
}

if(!isCore) {
_writeMessage("Zip does not contain openFPGA core. Skipping. Please use -a if you'd like to extract all zips.");
} else {
_writeMessage("Extracting...");
ZipFile.ExtractToDirectory(zipPath, extractPath, true);
updated = true;
}
File.Delete(zipPath);

return updated;
}

private async Task<bool> _fetchCustomRelease(Core core)
{
string zip_name = core.identifier + "_" + core.release.tag_name + ".zip";
Github.File file = await GithubApi.GetFile(core.repository.owner, core.repository.name, core.release_path + "/" + zip_name, _githubApiKey);

_writeMessage("Downloading file " + file.download_url + "...");
string zipPath = Path.Combine(UpdateDirectory, ZIP_FILE_NAME);
string extractPath = UpdateDirectory;
await HttpHelper.DownloadFileAsync(file.download_url, zipPath);

_writeMessage("Extracting...");
ZipFile.ExtractToDirectory(zipPath, extractPath, true);

File.Delete(zipPath);

return true;
}

private void _writeMessage(string message)
{
if(_useConsole) {
Console.WriteLine(message);
}
StatusUpdatedEventArgs args = new StatusUpdatedEventArgs();
args.Message = message;
OnStatusUpdated(args);
}

public void SetGithubApiKey(string key)
{
_githubApiKey = key;
}

protected virtual void OnStatusUpdated(StatusUpdatedEventArgs e)
{
EventHandler<StatusUpdatedEventArgs> handler = StatusUpdated;
if(handler != null)
{
handler(this, e);
}
}

protected virtual void OnUpdateProcessComplete(UpdateProcessCompleteEventArgs e)
{
EventHandler<UpdateProcessCompleteEventArgs> handler = UpdateProcessComplete;
Expand Down Expand Up @@ -587,32 +485,17 @@ private void _DeleteCore(Core core)
if(!_deleteSkippedCores) {
return;
}
List<string> folders = new List<string>{"Cores", "Presets"};
foreach(string folder in folders) {
string path = Path.Combine(UpdateDirectory, folder, core.identifier);
if(Directory.Exists(path)) {
_writeMessage("Uninstalling " + path);
Directory.Delete(path, true);
Divide();
}
}

core.Uninstall(UpdateDirectory);
}

/// <summary>
/// Event is raised every time the updater prints a progress update
/// </summary>
public event EventHandler<StatusUpdatedEventArgs>? StatusUpdated;
private void updater_StatusUpdated(object sender, StatusUpdatedEventArgs e)
{
this.OnStatusUpdated(e);
}
public event EventHandler<UpdateProcessCompleteEventArgs>? UpdateProcessComplete;
}

public class StatusUpdatedEventArgs : EventArgs
{
/// <summary>
/// Contains the message from the updater
/// </summary>
public string Message { get; set; }
}

public class UpdateProcessCompleteEventArgs : EventArgs
{
/// <summary>
Expand Down
Loading

0 comments on commit 3c8e1ac

Please sign in to comment.