Skip to content

Commit

Permalink
Fix Squirrel Auto updater(#32)
Browse files Browse the repository at this point in the history
* Since standard behavior is fine remove Squirrel awareness

* Handle firstrun without Squirrel

* Downgraded SharpCompress and Cecil

See Squirrel/Squirrel.Windows#1253

* Restart after updater disposed

This should fix leaked mutex errror
  • Loading branch information
wolkesson authored Dec 16, 2018
1 parent 0d8f051 commit 22dd936
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 84 deletions.
5 changes: 4 additions & 1 deletion Sample Crunch/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="SharpCompress" publicKeyToken="afb0a02973931d96" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-0.22.0.0" newVersion="0.22.0.0" />
<bindingRedirect oldVersion="0.0.0.0-0.17.1.0" newVersion="0.17.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="CommonServiceLocator" publicKeyToken="489b6accfaf20ef0" culture="neutral" />
Expand All @@ -36,6 +36,9 @@
<setting name="AppTelemetryUID" serializeAs="String">
<value />
</setting>
<setting name="FirstRun" serializeAs="String">
<value>True</value>
</setting>
</Sample_Crunch.Properties.Settings>
</userSettings>
</configuration>
7 changes: 2 additions & 5 deletions Sample Crunch/AppTelemetry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,8 @@ internal static async Task<string> 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;
}

Expand Down
9 changes: 7 additions & 2 deletions Sample Crunch/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ViewModel.UpdateViewModel>().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)
Expand All @@ -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
{ }
Expand Down
2 changes: 1 addition & 1 deletion Sample Crunch/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions Sample Crunch/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Sample Crunch/Properties/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@
<Setting Name="AppTelemetryUID" Description="Unique ID for App Telemetry" Type="System.String" Scope="User">
<Value Profile="(Default)" />
</Setting>
<Setting Name="FirstRun" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">True</Value>
</Setting>
</Settings>
</SettingsFile>
20 changes: 10 additions & 10 deletions Sample Crunch/SampleCrunch.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -102,23 +102,23 @@
<Reference Include="GalaSoft.MvvmLight.Platform, Version=5.4.1.0, Culture=neutral, PublicKeyToken=5f873c45e98af8a1, processorArchitecture=MSIL">
<HintPath>..\packages\MvvmLightLibs.5.4.1.1\lib\net45\GalaSoft.MvvmLight.Platform.dll</HintPath>
</Reference>
<Reference Include="Mono.Cecil, Version=0.10.1.0, Culture=neutral, PublicKeyToken=50cebf1cceb9d05e, processorArchitecture=MSIL">
<HintPath>..\packages\Mono.Cecil.0.10.1\lib\net40\Mono.Cecil.dll</HintPath>
<Reference Include="Mono.Cecil, Version=0.9.6.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756, processorArchitecture=MSIL">
<HintPath>..\packages\Mono.Cecil.0.9.6.4\lib\net45\Mono.Cecil.dll</HintPath>
</Reference>
<Reference Include="Mono.Cecil.Mdb, Version=0.10.1.0, Culture=neutral, PublicKeyToken=50cebf1cceb9d05e, processorArchitecture=MSIL">
<HintPath>..\packages\Mono.Cecil.0.10.1\lib\net40\Mono.Cecil.Mdb.dll</HintPath>
<Reference Include="Mono.Cecil.Mdb, Version=0.9.6.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756, processorArchitecture=MSIL">
<HintPath>..\packages\Mono.Cecil.0.9.6.4\lib\net45\Mono.Cecil.Mdb.dll</HintPath>
</Reference>
<Reference Include="Mono.Cecil.Pdb, Version=0.10.1.0, Culture=neutral, PublicKeyToken=50cebf1cceb9d05e, processorArchitecture=MSIL">
<HintPath>..\packages\Mono.Cecil.0.10.1\lib\net40\Mono.Cecil.Pdb.dll</HintPath>
<Reference Include="Mono.Cecil.Pdb, Version=0.9.6.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756, processorArchitecture=MSIL">
<HintPath>..\packages\Mono.Cecil.0.9.6.4\lib\net45\Mono.Cecil.Pdb.dll</HintPath>
</Reference>
<Reference Include="Mono.Cecil.Rocks, Version=0.10.1.0, Culture=neutral, PublicKeyToken=50cebf1cceb9d05e, processorArchitecture=MSIL">
<HintPath>..\packages\Mono.Cecil.0.10.1\lib\net40\Mono.Cecil.Rocks.dll</HintPath>
<Reference Include="Mono.Cecil.Rocks, Version=0.9.6.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756, processorArchitecture=MSIL">
<HintPath>..\packages\Mono.Cecil.0.9.6.4\lib\net45\Mono.Cecil.Rocks.dll</HintPath>
</Reference>
<Reference Include="NuGet.Squirrel, Version=3.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\squirrel.windows.1.9.0\lib\Net45\NuGet.Squirrel.dll</HintPath>
</Reference>
<Reference Include="SharpCompress, Version=0.22.0.0, Culture=neutral, PublicKeyToken=afb0a02973931d96, processorArchitecture=MSIL">
<HintPath>..\packages\SharpCompress.0.22.0\lib\net45\SharpCompress.dll</HintPath>
<Reference Include="SharpCompress, Version=0.17.1.0, Culture=neutral, PublicKeyToken=afb0a02973931d96, processorArchitecture=MSIL">
<HintPath>..\packages\SharpCompress.0.17.1\lib\net45\SharpCompress.dll</HintPath>
</Reference>
<Reference Include="Splat, Version=5.1.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Splat.5.1.4\lib\net461\Splat.dll</HintPath>
Expand Down
2 changes: 1 addition & 1 deletion Sample Crunch/SampleCrunch.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
<iconUrl>https://raw.githubusercontent.com/wolkesson/SampleCrunch/master/Sample%20Crunch/Resources/app.ico</iconUrl>
</metadata>
<files>
<file src="*.*" target="lib\net45\" exclude="*.pdb;*.nupkg;*.vshost.*"/>
<file src="*.*" target="lib\net461\" exclude="*.pdb;*.nupkg;*.vshost.*"/>
</files>
</package>
6 changes: 6 additions & 0 deletions Sample Crunch/SplashScreen.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -17,6 +18,10 @@
<Setter Property="Foreground" Value="White" />
<Setter Property="Margin" Value="10,0"/>
</Style>
<Style TargetType="CheckBox">
<Setter Property="Foreground" Value="White" />
<Setter Property="Margin" Value="10,0"/>
</Style>
</Window.Resources>

<Grid>
Expand Down Expand Up @@ -107,5 +112,6 @@
</Style>
</Button.Style>
</Button>
<CheckBox x:Name="checkBox" Content="Allow App Telemetry" IsChecked="{Binding Source={x:Static properties:Settings.Default}, Path=AppTelemetry}" Grid.Column="1" Margin="10,10,0,0" Grid.Row="4" HorizontalAlignment="Left" VerticalAlignment="Bottom" Visibility="Hidden"/>
</Grid>
</Window>
109 changes: 47 additions & 62 deletions Sample Crunch/ViewModel/UpdateViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -86,11 +72,6 @@ private set
}
}

public bool IsFirstRun
{
get { return this.firstRun; }
}

public string AvailableVersion
{
get { return (lastVersion == null ? "Checking..." : lastVersion.Version.ToString()); }
Expand All @@ -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;
Expand All @@ -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>();
MainViewModel main = SimpleIoc.Default.GetInstance<MainViewModel>();

// 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)
{
Expand All @@ -165,6 +148,10 @@ private async void Execute_UpdateCommand()
}
finally
{
if (CurrentState == State.Installed)
{
UpdateManager.RestartApp();
}
Updating = false;
}
}
Expand All @@ -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<string>(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<string>(nameof(AvailableVersion));
}
}
}
catch (Exception e)
Expand Down
4 changes: 2 additions & 2 deletions Sample Crunch/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
<package id="CommonServiceLocator" version="2.0.4" targetFramework="net461" />
<package id="DeltaCompressionDotNet" version="1.1.0" targetFramework="net45" />
<package id="Extended.Wpf.Toolkit" version="3.4.0" targetFramework="net45" />
<package id="Mono.Cecil" version="0.10.1" targetFramework="net45" />
<package id="Mono.Cecil" version="0.9.6.4" targetFramework="net461" />
<package id="MvvmLight" version="5.4.1.1" targetFramework="net461" />
<package id="MvvmLightLibs" version="5.4.1.1" targetFramework="net461" />
<package id="NuGet.CommandLine" version="4.7.1" targetFramework="net45" developmentDependency="true" />
<package id="SharpCompress" version="0.22.0" targetFramework="net45" />
<package id="SharpCompress" version="0.17.1" targetFramework="net461" />
<package id="Splat" version="5.1.4" targetFramework="net461" />
<package id="squirrel.windows" version="1.9.0" targetFramework="net45" />
<package id="System.Drawing.Primitives" version="4.3.0" targetFramework="net45" />
Expand Down

0 comments on commit 22dd936

Please # to comment.