From 22dd9364b718b8578029ec1894834d58b81e7cb8 Mon Sep 17 00:00:00 2001 From: wolkesson <5307773+wolkesson@users.noreply.github.com> Date: Sun, 16 Dec 2018 02:49:57 +0100 Subject: [PATCH] Fix Squirrel Auto updater(#32) * Since standard behavior is fine remove Squirrel awareness * Handle firstrun without Squirrel * Downgraded SharpCompress and Cecil See https://github.com/Squirrel/Squirrel.Windows/issues/1253 * Restart after updater disposed This should fix leaked mutex errror --- Sample Crunch/App.config | 5 +- Sample Crunch/AppTelemetry.cs | 7 +- Sample Crunch/MainWindow.xaml.cs | 9 +- Sample Crunch/Properties/AssemblyInfo.cs | 2 +- Sample Crunch/Properties/Settings.Designer.cs | 12 ++ Sample Crunch/Properties/Settings.settings | 3 + Sample Crunch/SampleCrunch.csproj | 20 ++-- Sample Crunch/SampleCrunch.nuspec | 2 +- Sample Crunch/SplashScreen.xaml | 6 + Sample Crunch/ViewModel/UpdateViewModel.cs | 109 ++++++++---------- Sample Crunch/packages.config | 4 +- 11 files changed, 95 insertions(+), 84 deletions(-) diff --git a/Sample Crunch/App.config b/Sample Crunch/App.config index d7bd2cf..955872a 100644 --- a/Sample Crunch/App.config +++ b/Sample Crunch/App.config @@ -20,7 +20,7 @@ - + @@ -36,6 +36,9 @@ + + True + diff --git a/Sample Crunch/AppTelemetry.cs b/Sample Crunch/AppTelemetry.cs index 965d007..4226d6a 100644 --- a/Sample Crunch/AppTelemetry.cs +++ b/Sample Crunch/AppTelemetry.cs @@ -40,11 +40,8 @@ internal static async Task RegisterUser(string locale, string version) string uid = Encoding.UTF8.GetString(resp); // Save UID for future calls - if (!string.IsNullOrWhiteSpace(uid)) - { - Properties.Settings.Default.AppTelemetryUID = uid; - Properties.Settings.Default.Save(); - } + Properties.Settings.Default.AppTelemetryUID = uid; + Properties.Settings.Default.Save(); return uid; } diff --git a/Sample Crunch/MainWindow.xaml.cs b/Sample Crunch/MainWindow.xaml.cs index 5dcb6f2..fde0fdd 100644 --- a/Sample Crunch/MainWindow.xaml.cs +++ b/Sample Crunch/MainWindow.xaml.cs @@ -107,13 +107,15 @@ private async void Window_Loaded(object sender, RoutedEventArgs e) // If this is first run show news try { - bool firstrun = SimpleIoc.Default.GetInstance().IsFirstRun; + bool firstrun = Properties.Settings.Default.FirstRun; if (firstrun) { MainViewModel.ShowWebPageCommand.Execute(@"https://wolkesson.github.io/SampleCrunch/getting-started"); Properties.Settings.Default.AppTelemetry = (MessageBox.Show( "Sample Crunch uses volentary telemetry to track usage and find bugs. Do you approve to send annonumous data?", "Allow telemetry?", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.Yes) == MessageBoxResult.Yes); + Properties.Settings.Default.FirstRun = false; + Properties.Settings.Default.Save(); } } catch (System.Deployment.Application.InvalidDeploymentException) @@ -125,7 +127,10 @@ private async void Window_Loaded(object sender, RoutedEventArgs e) { // Block App telemetry if user has disapproved it AppTelemetry.DoNotSend = !Properties.Settings.Default.AppTelemetry; - AppTelemetry.RegisterUser(CultureInfo.InstalledUICulture.EnglishName, MainViewModel.Version); + if (string.IsNullOrEmpty(Properties.Settings.Default.AppTelemetryUID)) + { + AppTelemetry.RegisterUser(CultureInfo.InstalledUICulture.EnglishName, MainViewModel.Version); + } } catch { } diff --git a/Sample Crunch/Properties/AssemblyInfo.cs b/Sample Crunch/Properties/AssemblyInfo.cs index f0d66c0..3e74366 100644 --- a/Sample Crunch/Properties/AssemblyInfo.cs +++ b/Sample Crunch/Properties/AssemblyInfo.cs @@ -31,7 +31,7 @@ //[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)] // Squirrel to handle command line arguments during installation -[assembly: AssemblyMetadata("SquirrelAwareVersion", "1")] +//[assembly: AssemblyMetadata("SquirrelAwareVersion", "1")] [assembly: ThemeInfo( ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located diff --git a/Sample Crunch/Properties/Settings.Designer.cs b/Sample Crunch/Properties/Settings.Designer.cs index ffafc39..0ffee98 100644 --- a/Sample Crunch/Properties/Settings.Designer.cs +++ b/Sample Crunch/Properties/Settings.Designer.cs @@ -54,5 +54,17 @@ public string AppTelemetryUID { this["AppTelemetryUID"] = value; } } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("True")] + public bool FirstRun { + get { + return ((bool)(this["FirstRun"])); + } + set { + this["FirstRun"] = value; + } + } } } diff --git a/Sample Crunch/Properties/Settings.settings b/Sample Crunch/Properties/Settings.settings index 40be196..738ea0a 100644 --- a/Sample Crunch/Properties/Settings.settings +++ b/Sample Crunch/Properties/Settings.settings @@ -8,5 +8,8 @@ + + True + \ No newline at end of file diff --git a/Sample Crunch/SampleCrunch.csproj b/Sample Crunch/SampleCrunch.csproj index afada48..f43baf8 100644 --- a/Sample Crunch/SampleCrunch.csproj +++ b/Sample Crunch/SampleCrunch.csproj @@ -102,23 +102,23 @@ ..\packages\MvvmLightLibs.5.4.1.1\lib\net45\GalaSoft.MvvmLight.Platform.dll - - ..\packages\Mono.Cecil.0.10.1\lib\net40\Mono.Cecil.dll + + ..\packages\Mono.Cecil.0.9.6.4\lib\net45\Mono.Cecil.dll - - ..\packages\Mono.Cecil.0.10.1\lib\net40\Mono.Cecil.Mdb.dll + + ..\packages\Mono.Cecil.0.9.6.4\lib\net45\Mono.Cecil.Mdb.dll - - ..\packages\Mono.Cecil.0.10.1\lib\net40\Mono.Cecil.Pdb.dll + + ..\packages\Mono.Cecil.0.9.6.4\lib\net45\Mono.Cecil.Pdb.dll - - ..\packages\Mono.Cecil.0.10.1\lib\net40\Mono.Cecil.Rocks.dll + + ..\packages\Mono.Cecil.0.9.6.4\lib\net45\Mono.Cecil.Rocks.dll ..\packages\squirrel.windows.1.9.0\lib\Net45\NuGet.Squirrel.dll - - ..\packages\SharpCompress.0.22.0\lib\net45\SharpCompress.dll + + ..\packages\SharpCompress.0.17.1\lib\net45\SharpCompress.dll ..\packages\Splat.5.1.4\lib\net461\Splat.dll diff --git a/Sample Crunch/SampleCrunch.nuspec b/Sample Crunch/SampleCrunch.nuspec index bd51430..af5d018 100644 --- a/Sample Crunch/SampleCrunch.nuspec +++ b/Sample Crunch/SampleCrunch.nuspec @@ -11,6 +11,6 @@ https://raw.githubusercontent.com/wolkesson/SampleCrunch/master/Sample%20Crunch/Resources/app.ico - + \ No newline at end of file diff --git a/Sample Crunch/SplashScreen.xaml b/Sample Crunch/SplashScreen.xaml index 4fbd1bf..5c7571c 100644 --- a/Sample Crunch/SplashScreen.xaml +++ b/Sample Crunch/SplashScreen.xaml @@ -2,6 +2,7 @@ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:sc="clr-namespace:Sample_Crunch" + xmlns:properties="clr-namespace:Sample_Crunch.Properties" Title="SplashScreen" Height="200" Width="500" ResizeMode="NoResize" WindowStyle="None" WindowStartupLocation="CenterScreen" BorderThickness="5" BorderBrush="AliceBlue" @@ -17,6 +18,10 @@ + @@ -107,5 +112,6 @@ + diff --git a/Sample Crunch/ViewModel/UpdateViewModel.cs b/Sample Crunch/ViewModel/UpdateViewModel.cs index d4478bc..10b7479 100644 --- a/Sample Crunch/ViewModel/UpdateViewModel.cs +++ b/Sample Crunch/ViewModel/UpdateViewModel.cs @@ -3,43 +3,29 @@ using GalaSoft.MvvmLight.Ioc; using Squirrel; using System; -using System.Collections.Generic; using System.Diagnostics; using System.Linq; -using System.Text; using System.Threading.Tasks; using System.Windows.Input; namespace Sample_Crunch.ViewModel { - public class UpdateViewModel:ViewModelBase + public class UpdateViewModel : ViewModelBase { - UpdateManager manager; - bool firstRun = false; - public UpdateViewModel() { - // Note, in most of these scenarios, the app exits after this method completes! - SquirrelAwareApp.HandleEvents( - //onInitialInstall: v => mgr.CreateShortcutForThisExe(), - //onAppUpdate: v => mgr.CreateShortcutForThisExe(), - //onAppUninstall: v => mgr.RemoveShortcutForThisExe(), - onFirstRun: () => firstRun = true); + } public override void Cleanup() { base.Cleanup(); - if (manager != null) - { - manager.Dispose(); - } } public Task CheckForUpdates(int timeout) { var task = CheckForUpdate(); - + return Task.Run(async () => { if (await Task.WhenAny(task, Task.Delay(timeout)) != task) @@ -86,11 +72,6 @@ private set } } - public bool IsFirstRun - { - get { return this.firstRun; } - } - public string AvailableVersion { get { return (lastVersion == null ? "Checking..." : lastVersion.Version.ToString()); } @@ -103,7 +84,7 @@ public ICommand UpdateCommand { get { - return updateCommand ?? (updateCommand = new RelayCommand(Execute_UpdateCommand, ()=> { return this.UpdateAvailable && !this.updating; })); + return updateCommand ?? (updateCommand = new RelayCommand(Execute_UpdateCommand, () => { return this.UpdateAvailable && !this.updating; })); } } private bool updating = false; @@ -127,36 +108,38 @@ private async void Execute_UpdateCommand() try { Stopwatch watch = Stopwatch.StartNew(); - - var updates = await manager.CheckForUpdate(); - var lastVersion = updates?.ReleasesToApply?.OrderBy(x => x.Version).LastOrDefault(); - CurrentState = State.Downloading; - await manager.DownloadReleases(new[] { lastVersion }); + using (var manager = await UpdateManager.GitHubUpdateManager("https://github.com/wolkesson/SampleCrunch", null, null, null, true)) + { + var updates = await manager.CheckForUpdate(); + var lastVersion = updates?.ReleasesToApply?.OrderBy(x => x.Version).LastOrDefault(); + CurrentState = State.Downloading; + await manager.DownloadReleases(new[] { lastVersion }); #if DEBUG System.Windows.Forms.MessageBox.Show("DEBUG: Don't actually perform the update in debug mode"); #else - CurrentState = State.Installing; - await manager.ApplyReleases(updates); - await manager.UpdateApp(); + CurrentState = State.Installing; - manager.CreateShortcutForThisExe(); + //manager.CreateShortcutForThisExe(); - MainViewModel main = SimpleIoc.Default.GetInstance(); + MainViewModel main = SimpleIoc.Default.GetInstance(); - // Send Telemetry - System.Collections.Specialized.NameValueCollection data = new System.Collections.Specialized.NameValueCollection - { - { "from", main.Version }, - { "to", this.lastVersion.Version.ToString() }, - { "elapse", watch.ElapsedMilliseconds.ToString() } - }; - AppTelemetry.ReportEvent("Updating", data); - - CurrentState = State.Installed; - //System.Windows.Forms.MessageBox.Show("The application has been updated - please restart the app."); - UpdateManager.RestartApp(); + // Send Telemetry + System.Collections.Specialized.NameValueCollection data = new System.Collections.Specialized.NameValueCollection + { + { "from", main.Version }, + { "to", this.lastVersion.Version.ToString() }, + { "elapse", watch.ElapsedMilliseconds.ToString() } + }; + AppTelemetry.ReportEvent("Updating", data); + + //System.Windows.Forms.MessageBox.Show("The application has been updated - please restart the app."); + await manager.ApplyReleases(updates); + await manager.UpdateApp(); + + CurrentState = State.Installed; #endif + } } catch (Exception e) { @@ -165,6 +148,10 @@ private async void Execute_UpdateCommand() } finally { + if (CurrentState == State.Installed) + { + UpdateManager.RestartApp(); + } Updating = false; } } @@ -174,24 +161,22 @@ private async Task CheckForUpdate() try { CurrentState = State.Checking; - if (manager == null) - { - this.manager = await UpdateManager.GitHubUpdateManager("https://github.com/wolkesson/SampleCrunch", null, null, null, true); - } - - var updates = await manager.CheckForUpdate(); - this.lastVersion = updates?.ReleasesToApply?.OrderBy(x => x.Version).LastOrDefault(); - - if (this.lastVersion == null) - { - CurrentState = State.NoUpdateAvailable; - UpdateAvailable = false; - } - else + using (var manager = await UpdateManager.GitHubUpdateManager("https://github.com/wolkesson/SampleCrunch", null, null, null, true)) { - UpdateAvailable = true; - CurrentState = State.UpdateAvailable; - RaisePropertyChanged(nameof(AvailableVersion)); + var updates = await manager.CheckForUpdate(); + this.lastVersion = updates?.ReleasesToApply?.OrderBy(x => x.Version).LastOrDefault(); + + if (this.lastVersion == null) + { + CurrentState = State.NoUpdateAvailable; + UpdateAvailable = false; + } + else + { + UpdateAvailable = true; + CurrentState = State.UpdateAvailable; + RaisePropertyChanged(nameof(AvailableVersion)); + } } } catch (Exception e) diff --git a/Sample Crunch/packages.config b/Sample Crunch/packages.config index 9151be5..736439c 100644 --- a/Sample Crunch/packages.config +++ b/Sample Crunch/packages.config @@ -3,11 +3,11 @@ - + - +