Skip to content

Commit

Permalink
Allow pre-release versions in Updater
Browse files Browse the repository at this point in the history
  • Loading branch information
stevencohn committed Dec 27, 2024
1 parent 0ef02e7 commit 2dfda51
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
2 changes: 2 additions & 0 deletions OneMore/Commands/Tools/Updater/GitRelease.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ internal class GitRelease

public string name { get; set; }

public bool prerelease { get; set; }

public string published_at { get; set; }

public List<GitAsset> assets { get; set; }
Expand Down
51 changes: 43 additions & 8 deletions OneMore/Commands/Tools/Updater/Updater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
//************************************************************************************************

#pragma warning disable S1075 // URIs should not be hardcoded
#define xDebugUpdater

// uncomment this to enable interactive interception of the installer
//#define Interactive

namespace River.OneMoreAddIn.Commands.Tools.Updater
{
Expand All @@ -19,7 +21,9 @@ namespace River.OneMoreAddIn.Commands.Tools.Updater

internal class Updater : Loggable, IUpdateReport
{
private const string LatestUrl = "https://api.github.com/repos/stevencohn/onemore/releases/latest";
private const string LatestUrl = "https://api.github.com/repos/stevencohn/onemore/releases";
private const string Latest = "/latest";
private const string LatestN = "?per_page=5";
private const string TagUrl = "https://github.com/stevencohn/OneMore/releases/tag";

private GitRelease release;
Expand Down Expand Up @@ -52,7 +56,7 @@ public Updater()
.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);

using var root = hive.OpenSubKey(path);
if (root != null)
if (root is not null)
{
foreach (var subName in root.GetSubKeyNames())
{
Expand Down Expand Up @@ -90,16 +94,18 @@ public Updater()
public async Task<bool> FetchLatestRelease()
{
var client = HttpClientFactory.Create();
var uri = $"{LatestUrl}{LatestN}";
GitRelease[] releases;

try
{
using var response = await client.GetAsync(LatestUrl);
using var response = await client.GetAsync(uri);
var body = await response.Content.ReadAsStringAsync();

// use the .NET Framework serializer;
// it's not great but I didn't want to pull in a nuget if I didn't need to
var serializer = new JavaScriptSerializer();
release = serializer.Deserialize<GitRelease>(body);
releases = serializer.Deserialize<GitRelease[]>(body);
}
catch (AggregateException exc)
{
Expand All @@ -121,6 +127,35 @@ public async Task<bool> FetchLatestRelease()
return false;
}

// find latest released-release, allowing prereleases to be published
release = null;
foreach (var r in releases)
{
// dump them out for debugging
var name = r.prerelease ? $"{r.name} PRERELEASE" : r.name;
logger.WriteLine($"fetched {r.tag_name}, {r.published_at} - \"{name}\"");
if (!r.prerelease && release is null)
{
release = r;
}
}

// either no releases fetched or only prerelease versions!
if (release is null)
{
logger.Write($"no official release found; ");
if (releases.Length == 0)
{
logger.WriteLine("empty release list returned");
}
else
{
logger.WriteLine($"latest release is [{releases[0].tag_name}]");
}

return false;
}

// allow semantic version e.g. "v1.2.3" or suffixed e.g. "v1.2.3-beta"
var plainver = release.tag_name;
if (plainver is not null)
Expand Down Expand Up @@ -161,7 +196,7 @@ public async Task<bool> Update()
var key = Environment.Is64BitOperatingSystem ? "x64" : "x86";

var asset = release.assets.Find(a => a.browser_download_url.Contains(key));
if (asset == null)
if (asset is null)
{
logger.WriteLine($"did not find installer asset for {key}");
return false;
Expand Down Expand Up @@ -203,7 +238,7 @@ public async Task<bool> Update()
using var writer = new StreamWriter(path, false);
await writer.WriteLineAsync(shutdown);
await writer.WriteLineAsync(msi);
#if DebugUpdater
#if Interactive
writer.WriteLine("set /p \"continue: \""); // for debugging
#endif
await writer.FlushAsync();
Expand All @@ -217,7 +252,7 @@ public async Task<bool> Update()
FileName = Environment.ExpandEnvironmentVariables("%ComSpec%"),
Arguments = $"/c {path}",
UseShellExecute = true,
#if DebugUpdater
#if Interactive
WindowStyle = ProcessWindowStyle.Normal
#else
WindowStyle = ProcessWindowStyle.Hidden
Expand Down

0 comments on commit 2dfda51

Please sign in to comment.