-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement auto update in Fieldworks Lite (#1298)
* create api to redirect to the latest GitHub release of fw lite * invalidate lexbox release cache when a new version is released * implement api to check for the latest version and queue an update * validate the time since last update check and ignore it if it's in the future * ensure InformationalVersion is set * create a desktop shortcut on first launch * move Windows-specific code into the Windows platform folder * refactor update checking code to delegate the update decision to the server, if the server returns a release, then the client will update to that version * don't reference the install path when making shortcut since it will change after an update * add a default value for GetLatestRelease cancellation token
- Loading branch information
Showing
18 changed files
with
668 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System.Reflection; | ||
|
||
namespace FwLiteDesktop; | ||
|
||
public class AppVersion | ||
{ | ||
public static readonly string Version = typeof(AppVersion).Assembly | ||
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion ?? "dev"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
backend/FwLite/FwLiteDesktop/Platforms/Windows/AppUpdateService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
using System.Buffers; | ||
using System.Net.Http.Json; | ||
using Windows.Management.Deployment; | ||
using LexCore.Entities; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace FwLiteDesktop; | ||
|
||
public class AppUpdateService( | ||
IHttpClientFactory httpClientFactory, | ||
ILogger<AppUpdateService> logger, | ||
IPreferences preferences, | ||
IConnectivity connectivity) : IMauiInitializeService | ||
{ | ||
private const string LastUpdateCheck = "lastUpdateChecked"; | ||
private const string FwliteUpdateUrlEnvVar = "FWLITE_UPDATE_URL"; | ||
private const string ForceUpdateCheckEnvVar = "FWLITE_FORCE_UPDATE_CHECK"; | ||
private const string PreventUpdateCheckEnvVar = "FWLITE_PREVENT_UPDATE"; | ||
|
||
private static readonly SearchValues<string> ValidPositiveEnvVarValues = | ||
SearchValues.Create(["1", "true", "yes"], StringComparison.OrdinalIgnoreCase); | ||
|
||
private static readonly string ShouldUpdateUrl = Environment.GetEnvironmentVariable(FwliteUpdateUrlEnvVar) ?? | ||
$"https://lexbox.org/api/fwlite-release/should-update?appVersion={AppVersion.Version}"; | ||
|
||
public void Initialize(IServiceProvider services) | ||
{ | ||
_ = Task.Run(TryUpdate); | ||
} | ||
|
||
private async Task TryUpdate() | ||
{ | ||
if (ValidPositiveEnvVarValues.Contains(Environment.GetEnvironmentVariable(PreventUpdateCheckEnvVar) ?? "")) | ||
{ | ||
logger.LogInformation("Update check prevented by env var {EnvVar}", PreventUpdateCheckEnvVar); | ||
return; | ||
} | ||
|
||
if (!ShouldCheckForUpdate()) return; | ||
var response = await ShouldUpdate(); | ||
if (!response.Update) return; | ||
|
||
await ApplyUpdate(response.Release); | ||
} | ||
|
||
private async Task ApplyUpdate(FwLiteRelease latestRelease) | ||
{ | ||
logger.LogInformation("New version available: {Version}", latestRelease.Version); | ||
var packageManager = new PackageManager(); | ||
var asyncOperation = packageManager.AddPackageAsync(new Uri(latestRelease.Url), [], DeploymentOptions.None); | ||
asyncOperation.Progress = (info, progressInfo) => | ||
{ | ||
logger.LogInformation("Downloading update: {ProgressPercentage}%", progressInfo.percentage); | ||
}; | ||
var result = await asyncOperation.AsTask(); | ||
if (!string.IsNullOrEmpty(result.ErrorText)) | ||
{ | ||
logger.LogError(result.ExtendedErrorCode, "Failed to download update: {ErrorText}", result.ErrorText); | ||
return; | ||
} | ||
|
||
logger.LogInformation("Update downloaded, will install on next restart"); | ||
} | ||
|
||
private async Task<ShouldUpdateResponse> ShouldUpdate() | ||
{ | ||
try | ||
{ | ||
var response = await httpClientFactory | ||
.CreateClient("Lexbox") | ||
.SendAsync(new HttpRequestMessage(HttpMethod.Get, ShouldUpdateUrl) | ||
{ | ||
Headers = { { "User-Agent", $"Fieldworks-Lite-Client/{AppVersion.Version}" } } | ||
}); | ||
if (!response.IsSuccessStatusCode) | ||
{ | ||
var responseContent = await response.Content.ReadAsStringAsync(); | ||
logger.LogError("Failed to get should update response lexbox: {StatusCode} {ResponseContent}", | ||
response.StatusCode, | ||
responseContent); | ||
return new ShouldUpdateResponse(null); | ||
} | ||
|
||
return await response.Content.ReadFromJsonAsync<ShouldUpdateResponse>() ?? new ShouldUpdateResponse(null); | ||
} | ||
catch (Exception e) | ||
{ | ||
if (connectivity.NetworkAccess == NetworkAccess.Internet) | ||
{ | ||
logger.LogError(e, "Failed to fetch latest release"); | ||
} | ||
else | ||
{ | ||
logger.LogInformation(e, "Failed to fetch latest release, no internet connection"); | ||
} | ||
|
||
return new ShouldUpdateResponse(null); | ||
} | ||
} | ||
|
||
private bool ShouldCheckForUpdate() | ||
{ | ||
if (ValidPositiveEnvVarValues.Contains(Environment.GetEnvironmentVariable(ForceUpdateCheckEnvVar) ?? "")) | ||
return true; | ||
var lastChecked = preferences.Get(LastUpdateCheck, DateTime.MinValue); | ||
var timeSinceLastCheck = DateTime.UtcNow - lastChecked; | ||
//if last checked is in the future (should never happen), then we want to reset the time and check again | ||
if (timeSinceLastCheck.Hours < -1) | ||
{ | ||
preferences.Set(LastUpdateCheck, DateTime.UtcNow); | ||
return true; | ||
} | ||
if (timeSinceLastCheck.Hours < 20) return false; | ||
preferences.Set(LastUpdateCheck, DateTime.UtcNow); | ||
return true; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
backend/FwLite/FwLiteDesktop/Platforms/Windows/WindowsKernel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System.Runtime.InteropServices; | ||
|
||
namespace FwLiteDesktop; | ||
|
||
public static class WindowsKernel | ||
{ | ||
public static void AddFwLiteWindows(this MauiAppBuilder builder) | ||
{ | ||
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) return; | ||
if (FwLiteDesktopKernel.IsPackagedApp) | ||
{ | ||
builder.Services.AddSingleton<IMauiInitializeService, AppUpdateService>(); | ||
builder.Services.AddSingleton<IMauiInitializeService, WindowsShortcutService>(); | ||
} | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
backend/FwLite/FwLiteDesktop/Platforms/Windows/WindowsShortcutService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using System.Runtime.InteropServices; | ||
using System.Runtime.InteropServices.ComTypes; | ||
using System.Text; | ||
using Windows.ApplicationModel; | ||
|
||
namespace FwLiteDesktop; | ||
|
||
public class WindowsShortcutService(IVersionTracking versionTracking) : IMauiInitializeService | ||
{ | ||
public void Initialize(IServiceProvider services) | ||
{ | ||
if (!FwLiteDesktopKernel.IsPackagedApp || !versionTracking.IsFirstLaunchEver || !RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) return; | ||
var package = Package.Current; | ||
IShellLink link = (IShellLink)new ShellLink(); | ||
link.SetPath($@"shell:AppsFolder\{package.Id.FamilyName}!App"); | ||
var file = (IPersistFile)link; | ||
file.Save(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "FieldWorks Lite.lnk"), | ||
false); | ||
} | ||
|
||
|
||
[ComImport] | ||
[Guid("00021401-0000-0000-C000-000000000046")] | ||
internal class ShellLink | ||
{ | ||
} | ||
|
||
[ComImport] | ||
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] | ||
[Guid("000214F9-0000-0000-C000-000000000046")] | ||
internal interface IShellLink | ||
{ | ||
void GetPath([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszFile, | ||
int cchMaxPath, | ||
out IntPtr pfd, | ||
int fFlags); | ||
|
||
void GetIDList(out IntPtr ppidl); | ||
void SetIDList(IntPtr pidl); | ||
void GetDescription([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszName, int cchMaxName); | ||
void SetDescription([MarshalAs(UnmanagedType.LPWStr)] string pszName); | ||
void GetWorkingDirectory([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszDir, int cchMaxPath); | ||
void SetWorkingDirectory([MarshalAs(UnmanagedType.LPWStr)] string pszDir); | ||
void GetArguments([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszArgs, int cchMaxPath); | ||
void SetArguments([MarshalAs(UnmanagedType.LPWStr)] string pszArgs); | ||
void GetHotkey(out short pwHotkey); | ||
void SetHotkey(short wHotkey); | ||
void GetShowCmd(out int piShowCmd); | ||
void SetShowCmd(int iShowCmd); | ||
|
||
void GetIconLocation([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszIconPath, | ||
int cchIconPath, | ||
out int piIcon); | ||
|
||
void SetIconLocation([MarshalAs(UnmanagedType.LPWStr)] string pszIconPath, int iIcon); | ||
void SetRelativePath([MarshalAs(UnmanagedType.LPWStr)] string pszPathRel, int dwReserved); | ||
void Resolve(IntPtr hwnd, int fFlags); | ||
void SetPath([MarshalAs(UnmanagedType.LPWStr)] string pszFile); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using System.Diagnostics; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using LexBoxApi.Otel; | ||
using LexBoxApi.Services.FwLiteReleases; | ||
using LexCore.Entities; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.Extensions.Caching.Hybrid; | ||
|
||
namespace LexBoxApi.Controllers; | ||
|
||
[ApiController] | ||
[Route("/api/fwlite-release")] | ||
[ApiExplorerSettings(GroupName = LexBoxKernel.OpenApiPublicDocumentName)] | ||
public class FwLiteReleaseController(FwLiteReleaseService releaseService) : ControllerBase | ||
{ | ||
|
||
[HttpGet("download-latest")] | ||
[AllowAnonymous] | ||
[ProducesResponseType(StatusCodes.Status404NotFound)] | ||
public async Task<ActionResult> DownloadLatest() | ||
{ | ||
using var activity = LexBoxActivitySource.Get().StartActivity(); | ||
var latestRelease = await releaseService.GetLatestRelease(); | ||
if (latestRelease is null) | ||
{ | ||
activity?.SetStatus(ActivityStatusCode.Error, "Latest release not found"); | ||
return NotFound(); | ||
} | ||
activity?.AddTag(FwLiteReleaseService.FwLiteReleaseVersionTag, latestRelease.Version); | ||
return Redirect(latestRelease.Url); | ||
} | ||
|
||
[HttpGet("latest")] | ||
[AllowAnonymous] | ||
[ProducesResponseType(StatusCodes.Status404NotFound)] | ||
[ProducesResponseType(StatusCodes.Status200OK)] | ||
[ProducesDefaultResponseType] | ||
public async ValueTask<ActionResult<FwLiteRelease>> LatestRelease(string? appVersion = null) | ||
{ | ||
using var activity = LexBoxActivitySource.Get().StartActivity(); | ||
activity?.AddTag(FwLiteReleaseService.FwLiteClientVersionTag, appVersion ?? "unknown"); | ||
var latestRelease = await releaseService.GetLatestRelease(); | ||
activity?.AddTag(FwLiteReleaseService.FwLiteReleaseVersionTag, latestRelease?.Version); | ||
if (latestRelease is null) return NotFound(); | ||
return latestRelease; | ||
} | ||
|
||
[HttpGet("should-update")] | ||
public async Task<ActionResult<ShouldUpdateResponse>> ShouldUpdate([FromQuery] string appVersion) | ||
{ | ||
using var activity = LexBoxActivitySource.Get().StartActivity(); | ||
activity?.AddTag(FwLiteReleaseService.FwLiteClientVersionTag, appVersion); | ||
var response = await releaseService.ShouldUpdate(appVersion); | ||
activity?.AddTag(FwLiteReleaseService.FwLiteReleaseVersionTag, response.Release?.Version); | ||
return response; | ||
} | ||
|
||
[HttpPost("new-release")] | ||
[AllowAnonymous] | ||
public async Task<OkResult> NewRelease() | ||
{ | ||
await releaseService.InvalidateReleaseCache(); | ||
return Ok(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.