Skip to content

Commit

Permalink
Retain settings on upgrade (#38)
Browse files Browse the repository at this point in the history
Backup and restore setting and plugins when application is auto-upgraded.
Overwrite old StandardPanels.dll after update to replace with new version.
  • Loading branch information
wolkesson authored Dec 22, 2018
1 parent edea235 commit 51f0d0a
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 30 deletions.
3 changes: 3 additions & 0 deletions Sample Crunch/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ public partial class App : System.Windows.Application

protected override void OnStartup(System.Windows.StartupEventArgs e)
{
ViewModel.UpdateViewModel.RestoreSettings();
Sample_Crunch.Properties.Settings.Default.Reload();

// Show splash screen
SplashScreen splash = new SplashScreen();
splash.Show();
Expand Down
12 changes: 6 additions & 6 deletions Sample Crunch/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,24 @@ private async void Window_Loaded(object sender, RoutedEventArgs e)
{
try
{
// Load plugins from plugin directory
string pluginPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Plugins");
var pluginManager = SimpleIoc.Default.GetInstance<ViewModel.PluginManagerViewModel>();

// Create plugin directory
if (!Directory.Exists(pluginPath))
if (!Directory.Exists(pluginManager.PluginPath))
{
Directory.CreateDirectory(pluginPath);
Directory.CreateDirectory(pluginManager.PluginPath);
}

// Move standard panels dll to plugin directory
string standardPanelsTargetPath = Path.Combine(pluginPath, "StandardPanels.dll");
string standardPanelsTargetPath = Path.Combine(pluginManager.PluginPath, "StandardPanels.dll");
if (!File.Exists(standardPanelsTargetPath))
{
string standardPanelsSourcePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "StandardPanels.dll");
File.Copy(standardPanelsSourcePath, standardPanelsTargetPath, true);
}

List<Exception> errors = PluginFactory.LoadPlugins(pluginPath);
// Load plugins from plugin directory
List<Exception> errors = PluginFactory.LoadPlugins(pluginManager.PluginPath);

foreach (var ex in errors)
{
Expand Down
1 change: 1 addition & 0 deletions Sample Crunch/SampleCrunch.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.Composition" />
<Reference Include="System.Configuration" />
<Reference Include="System.Data" />
<Reference Include="System.Deployment" />
<Reference Include="System.Drawing" />
Expand Down
17 changes: 9 additions & 8 deletions Sample Crunch/ViewModel/PluginManagerViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

public class PluginManagerViewModel
{
private string pluginPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Plugins");
public string PluginPath { get { return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Plugins"); } }

public IDialogServiceExt DialogService
{
Expand Down Expand Up @@ -44,20 +44,21 @@ private void Execute_ImportPluginCommand()
// Show open file dialog box
Nullable<bool> result = dlg.ShowDialog();

// Create directory if it does not exist
if (!Directory.Exists(PluginPath)) {
Directory.CreateDirectory(PluginPath);
}

// Process open file dialog box results
if (result == true)
{
// All files
foreach (var filename in dlg.FileNames)
{
try
{
// Create directory if it does not exist
if (!Directory.Exists(pluginPath)) {
Directory.CreateDirectory(pluginPath);
}

File.Copy(filename, Path.Combine(pluginPath, Path.GetFileName(filename)));
{
// Copy file to Plugin path
File.Copy(filename, Path.Combine(PluginPath, Path.GetFileName(filename)));
}

catch (Exception ex)
Expand Down
85 changes: 69 additions & 16 deletions Sample Crunch/ViewModel/UpdateViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
using GalaSoft.MvvmLight.Ioc;
using Squirrel;
using System;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Input;
Expand All @@ -14,6 +16,7 @@ public class UpdateViewModel : ViewModelBase
{
public UpdateViewModel()
{

}

public Task CheckForUpdates(int timeout)
Expand Down Expand Up @@ -65,17 +68,6 @@ private set
}
}

private bool updateAvailable = false;
public bool UpdateAvailable
{
get { return updateAvailable; }
private set
{
this.updateAvailable = value;
RaisePropertyChanged<bool>(nameof(UpdateAvailable));
}
}

public string AvailableVersion
{
get { return (lastVersion == null ? "Checking..." : lastVersion.Version.ToString()); }
Expand All @@ -88,7 +80,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.CurrentState == State.UpdateAvailable && !this.Updating; }));
}
}
private bool updating = false;
Expand All @@ -112,14 +104,14 @@ private async void Execute_UpdateCommand()
try
{
Stopwatch watch = Stopwatch.StartNew();
using (var manager = await UpdateManager.GitHubUpdateManager("https://github.com/wolkesson/SampleCrunch", null, null, null, true))
using (var manager = await UpdateManager.GitHubUpdateManager("https://github.com/wolkesson/SampleCrunch", null, null, null, Properties.Settings.Default.PreRelease))
{
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");
System.Windows.Forms.MessageBox.Show("DEBUG: Don't actually perform the update in debug mode");

#else
CurrentState = State.Installing;
Expand All @@ -140,6 +132,7 @@ private async void Execute_UpdateCommand()
//System.Windows.Forms.MessageBox.Show("The application has been updated - please restart the app.");
await manager.ApplyReleases(updates);
await manager.UpdateApp();
BackupSettings();

CurrentState = State.Installed;
#endif
Expand Down Expand Up @@ -173,11 +166,9 @@ private async Task CheckForUpdate()
if (this.lastVersion == null)
{
CurrentState = State.NoUpdateAvailable;
UpdateAvailable = false;
}
else
{
UpdateAvailable = true;
CurrentState = State.UpdateAvailable;
RaisePropertyChanged<string>(nameof(AvailableVersion));
}
Expand All @@ -189,5 +180,67 @@ private async Task CheckForUpdate()
CurrentState = State.Failed;
}
}

/// <summary>
/// Make a backup of our settings.
/// Used to persist settings across updates.
/// </summary>
public static void BackupSettings()
{
// Backup settings
string appDir = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
string settingsFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath;
string settingsBackup = Path.Combine(appDir, "..\\last.config");
File.Copy(settingsFile, settingsBackup, true);

// Backup plugins
var pluginManager = SimpleIoc.Default.GetInstance<ViewModel.PluginManagerViewModel>();
string destDir = Path.Combine(appDir, "..\\Plugin_backup");
Directory.Move(pluginManager.PluginPath, destDir);
}

/// <summary>
/// Restore our settings backup if any.
/// Used to persist settings across updates.
/// </summary>
public static void RestoreSettings()
{
//Restore settings after application update
string appDir = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
string settingsFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath;
string settingsBackup = Path.Combine(appDir, "..\\last.config");

// Check if we have settings that we need to restore
if (File.Exists(settingsBackup))
{
try
{
// Create directory as needed
Directory.CreateDirectory(Path.GetDirectoryName(settingsFile));

// Copy our backup file in place
File.Copy(settingsBackup, settingsFile, true);
File.Delete(settingsBackup);
}
catch (Exception) { }
}

// Move plugins to plugin path
string srcDir = Path.Combine(appDir, "..\\Plugin_backup");
string dstDir = Path.Combine(appDir, "Plugins"); // pluginManager not available yet
string stdFile = Path.Combine(srcDir, "StandardPanels.dll");

// We don't want to overwrite the StandardPlugins.dll from this release.
if (File.Exists(stdFile)) File.Delete(stdFile);

try
{
if (Directory.Exists(srcDir) && !Directory.Exists(dstDir))
{
Directory.Move(srcDir, dstDir);
}
}
catch (Exception) { }
}
}
}

0 comments on commit 51f0d0a

Please # to comment.