-
Notifications
You must be signed in to change notification settings - Fork 5
feat: add auto updater #117
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
Open
deansheather
wants to merge
9
commits into
main
Choose a base branch
from
dean/updater
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2f8f3a1
feat: add auto updater
deansheather c92ba0b
small changes
deansheather 5072e24
Merge branch 'main' into dean/updater
deansheather 26706bb
chore: appcast generation
deansheather 199a436
improvements
deansheather e0867e4
improvements
deansheather 6ea247d
use real URL
deansheather f3e5a67
cache control
deansheather 7d35551
actions fix
deansheather 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 hidden or 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 hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 |
---|---|---|
|
@@ -21,28 +21,38 @@ | |
using Microsoft.Win32; | ||
using Microsoft.Windows.AppLifecycle; | ||
using Microsoft.Windows.AppNotifications; | ||
using NetSparkleUpdater.Interfaces; | ||
using Serilog; | ||
using LaunchActivatedEventArgs = Microsoft.UI.Xaml.LaunchActivatedEventArgs; | ||
|
||
namespace Coder.Desktop.App; | ||
|
||
public partial class App : Application | ||
{ | ||
private readonly IServiceProvider _services; | ||
|
||
private bool _handleWindowClosed = true; | ||
private const string MutagenControllerConfigSection = "MutagenController"; | ||
private const string UpdaterConfigSection = "Updater"; | ||
|
||
#if !DEBUG | ||
private const string ConfigSubKey = @"SOFTWARE\Coder Desktop\App"; | ||
private const string logFilename = "app.log"; | ||
private const string LogFilename = "app.log"; | ||
private const string DefaultLogLevel = "Information"; | ||
#else | ||
private const string ConfigSubKey = @"SOFTWARE\Coder Desktop\DebugApp"; | ||
private const string logFilename = "debug-app.log"; | ||
private const string LogFilename = "debug-app.log"; | ||
private const string DefaultLogLevel = "Debug"; | ||
#endif | ||
|
||
// HACK: This is exposed for dispatcher queue access. The notifier uses | ||
// this to ensure action callbacks run in the UI thread (as | ||
// activation events aren't in the main thread). | ||
public TrayWindow? TrayWindow; | ||
|
||
private readonly IServiceProvider _services; | ||
private readonly ILogger<App> _logger; | ||
private readonly IUriHandler _uriHandler; | ||
private readonly IUserNotifier _userNotifier; | ||
|
||
private bool _handleWindowClosed = true; | ||
|
||
public App() | ||
{ | ||
|
@@ -55,7 +65,17 @@ public App() | |
configBuilder.Add( | ||
new RegistryConfigurationSource(Registry.LocalMachine, ConfigSubKey)); | ||
configBuilder.Add( | ||
new RegistryConfigurationSource(Registry.CurrentUser, ConfigSubKey)); | ||
new RegistryConfigurationSource( | ||
Registry.CurrentUser, | ||
ConfigSubKey, | ||
// Block "Updater:" configuration from HKCU, so that updater | ||
// settings can only be set at the HKLM level. | ||
// | ||
// HACK: This isn't super robust, but the security risk is | ||
// minor anyway. Malicious apps running as the user could | ||
// likely override this setting by altering the memory of | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One way to remove this risk is by always treating registry as a source of truth instead of relying on the in-memory value to block this. |
||
// this app. | ||
UpdaterConfigSection + ":")); | ||
|
||
var services = builder.Services; | ||
|
||
|
@@ -81,6 +101,12 @@ public App() | |
services.AddSingleton<IRdpConnector, RdpConnector>(); | ||
services.AddSingleton<IUriHandler, UriHandler>(); | ||
|
||
services.AddOptions<UpdaterConfig>() | ||
.Bind(builder.Configuration.GetSection(UpdaterConfigSection)); | ||
services.AddSingleton<IUpdaterUpdateAvailableViewModelFactory, UpdaterUpdateAvailableViewModelFactory>(); | ||
services.AddSingleton<IUIFactory, CoderSparkleUIFactory>(); | ||
services.AddSingleton<IUpdateController, SparkleUpdateController>(); | ||
|
||
// SignInWindow views and view models | ||
services.AddTransient<SignInViewModel>(); | ||
services.AddTransient<SignInWindow>(); | ||
|
@@ -107,8 +133,9 @@ public App() | |
services.AddTransient<TrayWindow>(); | ||
|
||
_services = services.BuildServiceProvider(); | ||
_logger = (ILogger<App>)_services.GetService(typeof(ILogger<App>))!; | ||
_uriHandler = (IUriHandler)_services.GetService(typeof(IUriHandler))!; | ||
_logger = _services.GetRequiredService<ILogger<App>>(); | ||
_uriHandler = _services.GetRequiredService<IUriHandler>(); | ||
_userNotifier = _services.GetRequiredService<IUserNotifier>(); | ||
|
||
InitializeComponent(); | ||
} | ||
|
@@ -129,6 +156,18 @@ public async Task ExitApplication() | |
protected override void OnLaunched(LaunchActivatedEventArgs args) | ||
{ | ||
_logger.LogInformation("new instance launched"); | ||
|
||
// Prevent the TrayWindow from closing, just hide it. | ||
if (TrayWindow != null) | ||
throw new InvalidOperationException("OnLaunched was called multiple times? TrayWindow is already set"); | ||
TrayWindow = _services.GetRequiredService<TrayWindow>(); | ||
TrayWindow.Closed += (_, closedArgs) => | ||
{ | ||
if (!_handleWindowClosed) return; | ||
closedArgs.Handled = true; | ||
TrayWindow.AppWindow.Hide(); | ||
}; | ||
|
||
// Start connecting to the manager in the background. | ||
var rpcController = _services.GetRequiredService<IRpcController>(); | ||
if (rpcController.GetState().RpcLifecycle == RpcLifecycle.Disconnected) | ||
|
@@ -166,7 +205,6 @@ protected override void OnLaunched(LaunchActivatedEventArgs args) | |
|
||
// Initialize file sync. | ||
// We're adding a 5s delay here to avoid race conditions when loading the mutagen binary. | ||
|
||
_ = Task.Delay(5000).ContinueWith((_) => | ||
{ | ||
var syncSessionCts = new CancellationTokenSource(TimeSpan.FromSeconds(10)); | ||
|
@@ -181,15 +219,6 @@ protected override void OnLaunched(LaunchActivatedEventArgs args) | |
syncSessionCts.Dispose(); | ||
}, CancellationToken.None); | ||
}); | ||
|
||
// Prevent the TrayWindow from closing, just hide it. | ||
var trayWindow = _services.GetRequiredService<TrayWindow>(); | ||
trayWindow.Closed += (_, closedArgs) => | ||
{ | ||
if (!_handleWindowClosed) return; | ||
closedArgs.Handled = true; | ||
trayWindow.AppWindow.Hide(); | ||
}; | ||
} | ||
|
||
public void OnActivated(object? sender, AppActivationArguments args) | ||
|
@@ -231,27 +260,36 @@ public void OnActivated(object? sender, AppActivationArguments args) | |
|
||
public void HandleNotification(AppNotificationManager? sender, AppNotificationActivatedEventArgs args) | ||
{ | ||
// right now, we don't do anything other than log | ||
_logger.LogInformation("handled notification activation"); | ||
_logger.LogInformation("handled notification activation: {Argument}", args.Argument); | ||
_userNotifier.HandleActivation(args); | ||
} | ||
|
||
private static void AddDefaultConfig(IConfigurationBuilder builder) | ||
{ | ||
var logPath = Path.Combine( | ||
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), | ||
"CoderDesktop", | ||
logFilename); | ||
LogFilename); | ||
builder.AddInMemoryCollection(new Dictionary<string, string?> | ||
{ | ||
[MutagenControllerConfigSection + ":MutagenExecutablePath"] = @"C:\mutagen.exe", | ||
|
||
["Serilog:Using:0"] = "Serilog.Sinks.File", | ||
["Serilog:MinimumLevel"] = "Information", | ||
["Serilog:MinimumLevel"] = DefaultLogLevel, | ||
["Serilog:Enrich:0"] = "FromLogContext", | ||
["Serilog:WriteTo:0:Name"] = "File", | ||
["Serilog:WriteTo:0:Args:path"] = logPath, | ||
["Serilog:WriteTo:0:Args:outputTemplate"] = | ||
"{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {SourceContext} - {Message:lj}{NewLine}{Exception}", | ||
["Serilog:WriteTo:0:Args:rollingInterval"] = "Day", | ||
|
||
#if DEBUG | ||
["Serilog:Using:1"] = "Serilog.Sinks.Debug", | ||
["Serilog:Enrich:1"] = "FromLogContext", | ||
["Serilog:WriteTo:1:Name"] = "Debug", | ||
["Serilog:WriteTo:1:Args:outputTemplate"] = | ||
"{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {SourceContext} - {Message:lj}{NewLine}{Exception}", | ||
#endif | ||
}); | ||
} | ||
} |
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.
considering the update will fail if we don't "bump" the version number (ie. previous is lower than new) - do we want to check this here within the script?