From 8cdb41f59b799ac6982226dd9f8bac17fe726f28 Mon Sep 17 00:00:00 2001 From: Patrick Fic Date: Thu, 30 Jan 2020 22:02:12 -0800 Subject: [PATCH] Base squirrel config. --- BodyshopUploader/BodyshopUploader.csproj | 38 ++++++ BodyshopUploader/Utils/UpdateHandler.cs | 112 ++++++++++++++++++ BodyshopUploader/ViewModels/MainViewModel.cs | 12 ++ .../ViewModels/MainViewModel.props.cs | 1 + BodyshopUploader/Views/Login.xaml.cs | 3 + BodyshopUploader/packages.config | 5 + 6 files changed, 171 insertions(+) create mode 100644 BodyshopUploader/Utils/UpdateHandler.cs diff --git a/BodyshopUploader/BodyshopUploader.csproj b/BodyshopUploader/BodyshopUploader.csproj index 3be3699..43b530d 100644 --- a/BodyshopUploader/BodyshopUploader.csproj +++ b/BodyshopUploader/BodyshopUploader.csproj @@ -1,5 +1,6 @@  + Debug @@ -55,6 +56,15 @@ favicon.ico + + ..\packages\DeltaCompressionDotNet.1.1.0\lib\net20\DeltaCompressionDotNet.dll + + + ..\packages\DeltaCompressionDotNet.1.1.0\lib\net20\DeltaCompressionDotNet.MsDelta.dll + + + ..\packages\DeltaCompressionDotNet.1.1.0\lib\net20\DeltaCompressionDotNet.PatchApi.dll + ..\packages\DotNetDBF.6.0.0.3\lib\net35\DotNetDBF.dll @@ -81,15 +91,39 @@ True True + + ..\packages\Mono.Cecil.0.9.6.1\lib\net45\Mono.Cecil.dll + + + ..\packages\Mono.Cecil.0.9.6.1\lib\net45\Mono.Cecil.Mdb.dll + + + ..\packages\Mono.Cecil.0.9.6.1\lib\net45\Mono.Cecil.Pdb.dll + + + ..\packages\Mono.Cecil.0.9.6.1\lib\net45\Mono.Cecil.Rocks.dll + ..\packages\Newtonsoft.Json.12.0.3\lib\net45\Newtonsoft.Json.dll ..\packages\NLog.4.6.8\lib\net45\NLog.dll + + ..\packages\squirrel.windows.1.9.1\lib\Net45\NuGet.Squirrel.dll + ..\packages\Ookii.Dialogs.1.0\lib\net35\Ookii.Dialogs.Wpf.dll + + ..\packages\SharpCompress.0.17.1\lib\net45\SharpCompress.dll + + + ..\packages\Splat.1.6.2\lib\Net45\Splat.dll + + + ..\packages\squirrel.windows.1.9.1\lib\Net45\Squirrel.dll + ..\packages\System.AppContext.4.3.0\lib\net463\System.AppContext.dll @@ -287,6 +321,7 @@ + @@ -380,6 +415,9 @@ + + + \ No newline at end of file diff --git a/BodyshopUploader/Utils/UpdateHandler.cs b/BodyshopUploader/Utils/UpdateHandler.cs new file mode 100644 index 0000000..dff3f3f --- /dev/null +++ b/BodyshopUploader/Utils/UpdateHandler.cs @@ -0,0 +1,112 @@ +using Squirrel; +using System; +using System.Collections.Generic; +using System.Configuration; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +namespace BodyshopUploader.Utils +{ + public static class UpdateHandler + { + private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); + + public const string UpdatePath = @"https://bodyshop.snapt.ca"; + + public static async Task AreUpdatesAvailable() + { + try + { + logger.Debug("Checking if updates are available."); + using (var updateManager = new UpdateManager(UpdatePath)) + { + var updateInfo = await updateManager.CheckForUpdate(); + if (updateInfo == null || !updateInfo.ReleasesToApply.Any()) + { + logger.Debug("No releases to apply."); + return false; + } + return true; + } + } + catch (Exception Ex) + { + logger.Error(Ex, "Unable to retrieve updates. "); + return false; + } + + } + + public static async Task ApplyUpdates(int DownloadProgress, int InstallProgress) + { + try + { + BackupSettings(); + } + catch (Exception ex) + { + logger.Warn(ex, "Unable to backup settings."); + } + + using (var updateManager = new UpdateManager(UpdatePath)) + { + var updateInfo = await updateManager.CheckForUpdate(); + var releases = updateInfo.ReleasesToApply; + await updateManager.DownloadReleases(releases, _ => { DownloadProgress = _; }); + await updateManager.ApplyReleases(updateInfo, _ => { InstallProgress = _; }); + } + + } + + public static void BackupSettings() + { + string settingsFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath; + string destination = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\..\\last.config"; + logger.Trace("Backup settings from: {0} to: {1}", settingsFile, destination); + File.Copy(settingsFile, destination, true); + } + + /// + /// Restore our settings backup if any. + /// Used to persist settings across updates. + /// + public static void RestoreSettings() + { + //Restore settings after application update + string destFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath; + string sourceFile = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\..\\last.config"; + // Check if we have settings that we need to restore + if (!File.Exists(sourceFile)) + { + // Nothing we need to do + return; + } + // Create directory as needed + try + { + Directory.CreateDirectory(Path.GetDirectoryName(destFile)); + } + catch (Exception) { } + + // Copy our backup file in place + try + { + logger.Trace("Restore settings from: {0} to: {1}", sourceFile, destFile); + File.Copy(sourceFile, destFile, true); + } + catch (Exception) { } + + // Delete backup files + try + { + File.Delete(sourceFile); + } + catch (Exception) { } + } + + + } +} diff --git a/BodyshopUploader/ViewModels/MainViewModel.cs b/BodyshopUploader/ViewModels/MainViewModel.cs index bb11fcc..ea68660 100644 --- a/BodyshopUploader/ViewModels/MainViewModel.cs +++ b/BodyshopUploader/ViewModels/MainViewModel.cs @@ -65,6 +65,11 @@ namespace BodyshopUploader.ViewModels _callingThread.ReportProgress(30); + _updateCheckTimer.Elapsed += _updateTimer_Elapsed; + _updateCheckTimer.AutoReset = true; + _updateCheckTimer.Start(); + + _callingThread.ReportProgress(50); //Cannot use await. LoadBodyshopData().Wait(); ; @@ -74,6 +79,13 @@ namespace BodyshopUploader.ViewModels _callingThread.ReportProgress(100); } + private async void _updateTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) + { + if(await Utils.UpdateHandler.AreUpdatesAvailable()) + { + await Utils.UpdateHandler.ApplyUpdates(Progress, Progress); + } + } private void MonitoringPathsChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { diff --git a/BodyshopUploader/ViewModels/MainViewModel.props.cs b/BodyshopUploader/ViewModels/MainViewModel.props.cs index 3891b60..c3745a9 100644 --- a/BodyshopUploader/ViewModels/MainViewModel.props.cs +++ b/BodyshopUploader/ViewModels/MainViewModel.props.cs @@ -12,6 +12,7 @@ namespace BodyshopUploader.ViewModels public partial class MainViewModel : BaseViewModel { public GrowlNotification Growler; + private System.Timers.Timer _updateCheckTimer = new System.Timers.Timer(60 * 1000); private ObservableCollection _monitoringPaths = new ObservableCollection(); public ObservableCollection MonitoringPaths diff --git a/BodyshopUploader/Views/Login.xaml.cs b/BodyshopUploader/Views/Login.xaml.cs index 43e3739..dd9d915 100644 --- a/BodyshopUploader/Views/Login.xaml.cs +++ b/BodyshopUploader/Views/Login.xaml.cs @@ -22,6 +22,9 @@ namespace BodyshopUploader.Views { public Login() { + Utils.UpdateHandler.RestoreSettings(); + Properties.Settings.Default.Reload(); + InitializeComponent(); Utils.ApplicationExceptionHandler.InitExceptionHandlers(); } diff --git a/BodyshopUploader/packages.config b/BodyshopUploader/packages.config index 7986984..d1c9bf3 100644 --- a/BodyshopUploader/packages.config +++ b/BodyshopUploader/packages.config @@ -1,5 +1,6 @@  + @@ -10,12 +11,16 @@ + + + +