Fixed UpdateService which was erroneously using the local update manager instead of the GitHub update manager for several parts of the update process

This commit is contained in:
Ben Wallis 2018-09-28 20:24:09 +01:00
parent 7d8b32b2e7
commit 992bd21570
5 changed files with 108 additions and 63 deletions

View File

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Filtration.Enums
{
internal enum UpdateSource
{
GitHub,
Local
}
}

View File

@ -203,6 +203,7 @@
<Compile Include="Converters\HashSignRemovalConverter.cs" /> <Compile Include="Converters\HashSignRemovalConverter.cs" />
<Compile Include="Converters\ItemRarityConverter.cs" /> <Compile Include="Converters\ItemRarityConverter.cs" />
<Compile Include="Converters\TreeViewMarginConverter.cs" /> <Compile Include="Converters\TreeViewMarginConverter.cs" />
<Compile Include="Enums\UpdateSource.cs" />
<Compile Include="Models\UpdateData.cs" /> <Compile Include="Models\UpdateData.cs" />
<Compile Include="Properties\Annotations.cs" /> <Compile Include="Properties\Annotations.cs" />
<Compile Include="Repositories\ItemFilterScriptRepository.cs" /> <Compile Include="Repositories\ItemFilterScriptRepository.cs" />

View File

@ -39,7 +39,7 @@ namespace Filtration.Services
_mainWindow.Show(); _mainWindow.Show();
await _updateService.CheckForUpdates(); await _updateService.CheckForUpdatesAsync();
} }
private void CurrentDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs e) private void CurrentDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs e)

View File

@ -2,6 +2,7 @@
using System.Linq; using System.Linq;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
using Filtration.Enums;
using Filtration.Properties; using Filtration.Properties;
using NLog; using NLog;
using Squirrel; using Squirrel;
@ -52,7 +53,7 @@ namespace Filtration.Services
string LatestReleaseVersion { get; } string LatestReleaseVersion { get; }
Task CheckForUpdates(); Task CheckForUpdatesAsync();
Task DownloadUpdatesAsync(); Task DownloadUpdatesAsync();
@ -63,13 +64,16 @@ namespace Filtration.Services
internal class UpdateService : IUpdateService internal class UpdateService : IUpdateService
{ {
private readonly ISettingsService _settingsService;
private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private const string _localUpdatePath = @"C:\Repos\Filtration\Releases"; private const string _localUpdatePath = @"C:\Repos\Filtration\Releases";
private readonly ISettingsService _settingsService;
private readonly UpdateSource _updateSource = UpdateSource.GitHub;
private ReleaseEntry _latestRelease; private ReleaseEntry _latestRelease;
private UpdateInfo _updates; private UpdateInfo _updates;
private bool _downloadPrereleaseUpdates;
private UpdateStatus _updateStatus; private UpdateStatus _updateStatus;
public UpdateService(ISettingsService settingsService) public UpdateService(ISettingsService settingsService)
@ -96,35 +100,42 @@ namespace Filtration.Services
public string LatestReleaseVersion { get; private set; } public string LatestReleaseVersion { get; private set; }
public async Task CheckForUpdates() public async Task CheckForUpdatesAsync()
{ {
if (UpdateStatus != UpdateStatus.NoUpdateAvailable) if (UpdateStatus != UpdateStatus.NoUpdateAvailable)
{ {
throw new InvalidOperationException(); throw new InvalidOperationException();
} }
Logger.Debug("Checking for update..."); Logger.Debug("Checking for update...");
UpdateStatus = UpdateStatus.CheckingForUpdate; UpdateStatus = UpdateStatus.CheckingForUpdate;
try try
{ {
bool downloadPrereleaseUpdates; _downloadPrereleaseUpdates = Settings.Default.DownloadPrereleaseUpdates;
downloadPrereleaseUpdates = Settings.Default.DownloadPrereleaseUpdates;
#if DEBUG #if DEBUG
downloadPrereleaseUpdates = true; _downloadPrereleaseUpdates = true;
#endif #endif
using (var mgr = await UpdateManager.GitHubUpdateManager("https://github.com/ben-wallis/Filtration", prerelease: downloadPrereleaseUpdates))
async Task CheckForUpdatesAsync(IUpdateManager updateManager)
{ {
_updates = await mgr.CheckForUpdate(progress: progress => UpdateProgressChanged?.Invoke(this, new UpdateProgressChangedEventArgs(progress))); _updates = await updateManager.CheckForUpdate(progress: progress => UpdateProgressChanged?.Invoke(this, new UpdateProgressChangedEventArgs(progress)));
} }
// Local file update source for testing if (_updateSource == UpdateSource.GitHub)
//using (var mgr = new UpdateManager(_localUpdatePath)) {
//{ using (var updateManager = await UpdateManager.GitHubUpdateManager("https://github.com/ben-wallis/Filtration", prerelease: _downloadPrereleaseUpdates))
{
// _updates = await mgr.CheckForUpdate(progress: progress => UpdateProgressChanged?.Invoke(this, new UpdateProgressChangedEventArgs(progress))); await CheckForUpdatesAsync(updateManager);
//} }
}
else
{
using (var updateManager = new UpdateManager(_localUpdatePath))
{
await CheckForUpdatesAsync(updateManager);
}
}
} }
catch (Exception e) catch (Exception e)
{ {
@ -133,26 +144,13 @@ namespace Filtration.Services
return; return;
} }
if (_updates.ReleasesToApply.Any()) if (_updates.ReleasesToApply.Any())
{ {
_latestRelease = _updates.ReleasesToApply.OrderBy(x => x.Version).Last(); _latestRelease = _updates.ReleasesToApply.OrderBy(x => x.Version).Last();
LatestReleaseVersion = _latestRelease.Version.ToString(); LatestReleaseVersion = _latestRelease.Version.ToString();
Logger.Debug($"Update found ({LatestReleaseVersion}), fetching release notes..."); Logger.Debug($"Update found ({LatestReleaseVersion})");
try
{
var releaseNotes = _latestRelease.GetReleaseNotes(_localUpdatePath);
LatestReleaseNotes = ProcessReleaseNotes(releaseNotes);
}
catch (Exception e)
{
Logger.Error(e);
UpdateStatus = UpdateStatus.Error;
return;
}
UpdateStatus = UpdateStatus.UpdateAvailable; UpdateStatus = UpdateStatus.UpdateAvailable;
} }
else else
@ -162,7 +160,7 @@ namespace Filtration.Services
} }
} }
private string ProcessReleaseNotes(string rawReleaseNotes) private static string ProcessReleaseNotes(string rawReleaseNotes)
{ {
var regex = new Regex(@"<!\[CDATA\[(.*)]]>", RegexOptions.Singleline); var regex = new Regex(@"<!\[CDATA\[(.*)]]>", RegexOptions.Singleline);
var matches = regex.Match(rawReleaseNotes); var matches = regex.Match(rawReleaseNotes);
@ -184,11 +182,31 @@ namespace Filtration.Services
UpdateStatus = UpdateStatus.Downloading; UpdateStatus = UpdateStatus.Downloading;
async Task DownloadUpdatesAsync(IUpdateManager updateManager)
{
Logger.Debug("Downloading update...");
await updateManager.DownloadReleases(_updates.ReleasesToApply, OnProgressChanged);
Logger.Debug("Fetching release notes...");
var releaseNotes = _updates.FetchReleaseNotes();
LatestReleaseNotes = ProcessReleaseNotes(releaseNotes[_latestRelease]);
}
try try
{ {
using (var updateManager = new UpdateManager(_localUpdatePath)) if (_updateSource == UpdateSource.GitHub)
{ {
await updateManager.DownloadReleases(_updates.ReleasesToApply, OnProgressChanged); using (var updateManager = await UpdateManager.GitHubUpdateManager("https://github.com/ben-wallis/Filtration", prerelease: _downloadPrereleaseUpdates))
{
await DownloadUpdatesAsync(updateManager);
}
}
else
{
using (var updateManager = new UpdateManager(_localUpdatePath))
{
await DownloadUpdatesAsync(updateManager);
}
} }
} }
catch (Exception e) catch (Exception e)
@ -215,11 +233,22 @@ namespace Filtration.Services
// wipes out user settings due to the application directory changing with each update // wipes out user settings due to the application directory changing with each update
_settingsService.BackupSettings(); _settingsService.BackupSettings();
Logger.Debug("Applying update...");
try try
{ {
using (var updateManager = new UpdateManager(_localUpdatePath)) if (_updateSource == UpdateSource.GitHub)
{ {
await updateManager.ApplyReleases(_updates, OnProgressChanged); using (var mgr = await UpdateManager.GitHubUpdateManager("https://github.com/ben-wallis/Filtration", prerelease: _downloadPrereleaseUpdates))
{
await mgr.ApplyReleases(_updates, OnProgressChanged);
}
}
else
{
using (var updateManager = new UpdateManager(_localUpdatePath))
{
await updateManager.ApplyReleases(_updates, OnProgressChanged);
}
} }
} }
catch (Exception e) catch (Exception e)
@ -229,6 +258,7 @@ namespace Filtration.Services
return; return;
} }
Logger.Debug("Update complete");
UpdateStatus = UpdateStatus.UpdateComplete; UpdateStatus = UpdateStatus.UpdateComplete;
} }

View File

@ -50,7 +50,7 @@ namespace Filtration.ViewModels
updateService.UpdateProgressChanged += UpdateServiceOnUpdateProgressChanged; updateService.UpdateProgressChanged += UpdateServiceOnUpdateProgressChanged;
updateService.UpdateStatusChanged += UpdateServiceOnUpdateStatusChanged; updateService.UpdateStatusChanged += UpdateServiceOnUpdateStatusChanged;
HideUpdateWindowCommand = new RelayCommand(OnHideUpdateWindowCommand, () => UpdateStatus == UpdateStatus.UpdateAvailable || UpdateStatus == UpdateStatus.Error); HideUpdateWindowCommand = new RelayCommand(OnHideUpdateWindowCommand, () => UpdateStatus == UpdateStatus.UpdateAvailable || UpdateStatus == UpdateStatus.Error);
NextStepCommand = new RelayCommand(async () => await OnNextStepCommandAsync(), () => NextStepCommandEnabled); NextStepCommand = new RelayCommand(async () => await OnNextStepCommandAsync(), () => NextStepCommandEnabled);
@ -100,39 +100,39 @@ namespace Filtration.ViewModels
} }
private bool NextStepCommandEnabled => UpdateStatus == UpdateStatus.UpdateAvailable || UpdateStatus == UpdateStatus.ReadyToApplyUpdate || UpdateStatus == UpdateStatus.UpdateComplete; private bool NextStepCommandEnabled => UpdateStatus == UpdateStatus.UpdateAvailable || UpdateStatus == UpdateStatus.ReadyToApplyUpdate || UpdateStatus == UpdateStatus.UpdateComplete;
private async Task OnNextStepCommandAsync() private async Task OnNextStepCommandAsync()
{ {
switch (UpdateStatus) switch (UpdateStatus)
{ {
case UpdateStatus.UpdateAvailable: case UpdateStatus.UpdateAvailable:
{ {
await _updateService.DownloadUpdatesAsync(); await _updateService.DownloadUpdatesAsync();
break; break;
} }
case UpdateStatus.ReadyToApplyUpdate: case UpdateStatus.ReadyToApplyUpdate:
{
if (!_updateTabShown)
{ {
// When the update has downloaded and is ready to apply, clicking the button if (!_updateTabShown)
// closes the update popup and shows the update tab. {
_avalonDockWorkspaceViewModel.AddDocument(this); // When the update has downloaded and is ready to apply, clicking the button
Visible = false; // closes the update popup and shows the update tab.
_updateTabShown = true; _avalonDockWorkspaceViewModel.AddDocument(this);
NextStepButtonText = "Update"; Visible = false;
} _updateTabShown = true;
else NextStepButtonText = "Update";
{ }
await _updateService.ApplyUpdatesAsync(); else
} {
await _updateService.ApplyUpdatesAsync();
}
break; break;
} }
case UpdateStatus.UpdateComplete: case UpdateStatus.UpdateComplete:
{ {
_updateService.RestartAfterUpdate(); _updateService.RestartAfterUpdate();
break; break;
} }
} }
} }
@ -144,7 +144,6 @@ namespace Filtration.ViewModels
{ {
Visible = true; Visible = true;
NextStepButtonText = "Download"; NextStepButtonText = "Download";
RaisePropertyChanged(nameof(ReleaseNotes));
RaisePropertyChanged(nameof(Version)); RaisePropertyChanged(nameof(Version));
break; break;
} }
@ -156,6 +155,7 @@ namespace Filtration.ViewModels
} }
case UpdateStatus.ReadyToApplyUpdate: case UpdateStatus.ReadyToApplyUpdate:
{ {
RaisePropertyChanged(nameof(ReleaseNotes));
NextStepButtonText = "Update Ready"; NextStepButtonText = "Update Ready";
break; break;
} }