-
-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement auto update in Fieldworks Lite #1298
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
e5180c7
create api to redirect to the latest GitHub release of fw lite
hahn-kev 146856b
invalidate lexbox release cache when a new version is released
hahn-kev 2c8dfae
implement api to check for the latest version and queue an update
hahn-kev b36f9df
refactor TryUpdate and pull out apply update code
hahn-kev 56da30b
pass app version and a user agent identifying fw lite
hahn-kev c8a2f56
validate the time since last update check and ignore it if it's in th…
hahn-kev ae6ee8c
add comment about launch profiles
hahn-kev 88afbf5
ensure InformationalVersion is set
hahn-kev fa2e4a9
create a desktop shortcut on first launch
hahn-kev 64fb903
correct release invalidation url
hahn-kev 43355b1
move Windows-specific code into the Windows platform folder
hahn-kev 02e6313
fix space prefixed on header value
hahn-kev e01f0c0
refactor update checking code to delegate the update decision to the …
hahn-kev 8bba522
break out update logic into its own service to call from the release …
hahn-kev 87efa3d
write tests for release service
hahn-kev dd498f7
increase timeout on fw lite build and test job
hahn-kev 224388d
make app name consistent
hahn-kev 05fbec0
don't reference the install path when making shortcut since it will c…
hahn-kev da8ba2c
add a default value for GetLatestRelease cancellation token
hahn-kev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm fine merging this, but I think it deserves more thought.
Checking for updates is cheap.
Installing updates is not.
So, we're not really protecting from checking for updates, but maybe repeated failed updates?
If we release two updates within 24hrs then we probably have good reason to.
Maybe the logic should be more like:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right is is cheap to check. At this point I have no idea what a failed update would look like or how that would even happen considering we're not running any custom code during install. Not that I doubt it could happen. That said, tracking update attempts introduces complexity which I'd rather avoid, so maybe let's leave it as is for now and see what we learn with people actually using it and updating to new versions