Files
OpenNest/OpenNest/UpdaterService.cs
T
ajandClaude Opus 5.5 7fe4de4b6d feat(release): ship self-updating Velopack packages on GitHub Releases
Publish-Windows.ps1 now packs the smoke-tested folder with the pinned vpk
CLI (.config/dotnet-tools.json, 1.2.0 to match the Velopack package) into
velopack/: Setup.exe, Portable.zip, the full nupkg, releases.win.json and
RELEASES, checked for the full package and the portable layout and hashed
into OpenNest.vX.Y.Z.velopack.sha256. The release workflow uploads them
with the candidate.

With no feed URL set, installed copies now check GitHub Releases (full
releases only) instead of skipping update checks; a URL in Options still
selects a self-hosted feed. Deploy-OpenNest.ps1 uses the pinned vpk.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 17:52:30 -04:00

88 lines
3.1 KiB
C#

using System;
using System.Threading;
using System.Threading.Tasks;
using OpenNest.Properties;
using Velopack;
using Velopack.Sources;
namespace OpenNest
{
/// <summary>
/// Periodically checks a Velopack release feed for updates and downloads
/// them in the background. Never restarts the app on its own -- an open,
/// unsaved nest must not be discarded out from under the user -- so
/// callers must observe <see cref="UpdateReady"/> and call
/// <see cref="ApplyAndRestart"/> once the user agrees to restart.
/// </summary>
/// <remarks>
/// A blank <see cref="Settings.UpdateFeedUrl"/> (the default) checks the
/// public GitHub Releases for full, non-pre-release versions. A URL set in
/// the Options dialog points this machine at a self-hosted feed instead;
/// that URL is operator infrastructure and never lives in source control.
/// </remarks>
public sealed class UpdaterService : IDisposable
{
private static readonly TimeSpan CheckInterval = TimeSpan.FromMinutes(30);
private const string GitHubRepositoryUrl = "https://github.com/ajisaacs/OpenNest";
private readonly UpdateManager _manager;
private Timer _timer;
private UpdateInfo _pendingUpdate;
/// <summary>Raised on the thread pool once an update has finished downloading.</summary>
public event Action<string> UpdateReady;
public UpdaterService()
: this(Settings.Default.UpdateFeedUrl) { }
public UpdaterService(string feedUrl)
{
IUpdateSource source = string.IsNullOrWhiteSpace(feedUrl)
? new GithubSource(GitHubRepositoryUrl, null, false)
: new SimpleWebSource(feedUrl);
_manager = new UpdateManager(source);
}
/// <summary>True once a downloaded update is waiting for <see cref="ApplyAndRestart"/>.</summary>
public bool HasPendingUpdate => _pendingUpdate != null;
public void Start()
{
if (_manager is null || !_manager.IsInstalled)
return;
_timer = new Timer(_ => _ = TickAsync(), null, CheckInterval, CheckInterval);
}
/// <summary>Applies the downloaded update and restarts the process. Call only after the user agrees.</summary>
public void ApplyAndRestart()
{
if (_manager != null && _pendingUpdate != null)
_manager.ApplyUpdatesAndRestart(_pendingUpdate);
}
public void Dispose() => _timer?.Dispose();
private async Task TickAsync()
{
if (_manager is null || _pendingUpdate != null)
return;
try
{
var newVersion = await _manager.CheckForUpdatesAsync();
if (newVersion == null)
return;
await _manager.DownloadUpdatesAsync(newVersion);
_pendingUpdate = newVersion;
UpdateReady?.Invoke(newVersion.TargetFullRelease.Version.ToString());
}
catch
{
// Network hiccup or feed unreachable -- retry next tick.
}
}
}
}