Skip to content
This repository was archived by the owner on Jan 3, 2023. It is now read-only.

Commit 992bd21

Browse files
committed
Fixed UpdateService which was erroneously using the local update manager instead of the GitHub update manager for several parts of the update process
1 parent 7d8b32b commit 992bd21

File tree

5 files changed

+107
-62
lines changed

5 files changed

+107
-62
lines changed

Filtration/Enums/UpdateSource.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Filtration.Enums
8+
{
9+
internal enum UpdateSource
10+
{
11+
GitHub,
12+
Local
13+
}
14+
}

Filtration/Filtration.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@
203203
<Compile Include="Converters\HashSignRemovalConverter.cs" />
204204
<Compile Include="Converters\ItemRarityConverter.cs" />
205205
<Compile Include="Converters\TreeViewMarginConverter.cs" />
206+
<Compile Include="Enums\UpdateSource.cs" />
206207
<Compile Include="Models\UpdateData.cs" />
207208
<Compile Include="Properties\Annotations.cs" />
208209
<Compile Include="Repositories\ItemFilterScriptRepository.cs" />

Filtration/Services/Bootstrapper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public async Task GoAsync()
3939

4040
_mainWindow.Show();
4141

42-
await _updateService.CheckForUpdates();
42+
await _updateService.CheckForUpdatesAsync();
4343
}
4444

4545
private void CurrentDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs e)

Filtration/Services/UpdateService.cs

Lines changed: 66 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Linq;
33
using System.Text.RegularExpressions;
44
using System.Threading.Tasks;
5+
using Filtration.Enums;
56
using Filtration.Properties;
67
using NLog;
78
using Squirrel;
@@ -52,7 +53,7 @@ internal interface IUpdateService
5253

5354
string LatestReleaseVersion { get; }
5455

55-
Task CheckForUpdates();
56+
Task CheckForUpdatesAsync();
5657

5758
Task DownloadUpdatesAsync();
5859

@@ -63,13 +64,16 @@ internal interface IUpdateService
6364

6465
internal class UpdateService : IUpdateService
6566
{
66-
private readonly ISettingsService _settingsService;
6767
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
6868

6969
private const string _localUpdatePath = @"C:\Repos\Filtration\Releases";
70+
71+
private readonly ISettingsService _settingsService;
72+
private readonly UpdateSource _updateSource = UpdateSource.GitHub;
73+
7074
private ReleaseEntry _latestRelease;
7175
private UpdateInfo _updates;
72-
76+
private bool _downloadPrereleaseUpdates;
7377
private UpdateStatus _updateStatus;
7478

7579
public UpdateService(ISettingsService settingsService)
@@ -96,35 +100,42 @@ private set
96100

97101
public string LatestReleaseVersion { get; private set; }
98102

99-
public async Task CheckForUpdates()
103+
public async Task CheckForUpdatesAsync()
100104
{
101105
if (UpdateStatus != UpdateStatus.NoUpdateAvailable)
102106
{
103107
throw new InvalidOperationException();
104108
}
105109

106-
107110
Logger.Debug("Checking for update...");
108111
UpdateStatus = UpdateStatus.CheckingForUpdate;
109112

110113
try
111114
{
112-
bool downloadPrereleaseUpdates;
113-
downloadPrereleaseUpdates = Settings.Default.DownloadPrereleaseUpdates;
115+
_downloadPrereleaseUpdates = Settings.Default.DownloadPrereleaseUpdates;
114116
#if DEBUG
115-
downloadPrereleaseUpdates = true;
117+
_downloadPrereleaseUpdates = true;
116118
#endif
117-
using (var mgr = await UpdateManager.GitHubUpdateManager("https://github.com/ben-wallis/Filtration", prerelease: downloadPrereleaseUpdates))
119+
120+
async Task CheckForUpdatesAsync(IUpdateManager updateManager)
118121
{
119-
_updates = await mgr.CheckForUpdate(progress: progress => UpdateProgressChanged?.Invoke(this, new UpdateProgressChangedEventArgs(progress)));
122+
_updates = await updateManager.CheckForUpdate(progress: progress => UpdateProgressChanged?.Invoke(this, new UpdateProgressChangedEventArgs(progress)));
120123
}
121124

122-
// Local file update source for testing
123-
//using (var mgr = new UpdateManager(_localUpdatePath))
124-
//{
125-
126-
// _updates = await mgr.CheckForUpdate(progress: progress => UpdateProgressChanged?.Invoke(this, new UpdateProgressChangedEventArgs(progress)));
127-
//}
125+
if (_updateSource == UpdateSource.GitHub)
126+
{
127+
using (var updateManager = await UpdateManager.GitHubUpdateManager("https://github.com/ben-wallis/Filtration", prerelease: _downloadPrereleaseUpdates))
128+
{
129+
await CheckForUpdatesAsync(updateManager);
130+
}
131+
}
132+
else
133+
{
134+
using (var updateManager = new UpdateManager(_localUpdatePath))
135+
{
136+
await CheckForUpdatesAsync(updateManager);
137+
}
138+
}
128139
}
129140
catch (Exception e)
130141
{
@@ -133,26 +144,13 @@ public async Task CheckForUpdates()
133144
return;
134145
}
135146

136-
137147
if (_updates.ReleasesToApply.Any())
138148
{
139149
_latestRelease = _updates.ReleasesToApply.OrderBy(x => x.Version).Last();
140150
LatestReleaseVersion = _latestRelease.Version.ToString();
141151

142-
Logger.Debug($"Update found ({LatestReleaseVersion}), fetching release notes...");
143-
144-
try
145-
{
146-
var releaseNotes = _latestRelease.GetReleaseNotes(_localUpdatePath);
147-
LatestReleaseNotes = ProcessReleaseNotes(releaseNotes);
148-
}
149-
catch (Exception e)
150-
{
151-
Logger.Error(e);
152-
UpdateStatus = UpdateStatus.Error;
153-
return;
154-
}
155-
152+
Logger.Debug($"Update found ({LatestReleaseVersion})");
153+
156154
UpdateStatus = UpdateStatus.UpdateAvailable;
157155
}
158156
else
@@ -162,7 +160,7 @@ public async Task CheckForUpdates()
162160
}
163161
}
164162

165-
private string ProcessReleaseNotes(string rawReleaseNotes)
163+
private static string ProcessReleaseNotes(string rawReleaseNotes)
166164
{
167165
var regex = new Regex(@"<!\[CDATA\[(.*)]]>", RegexOptions.Singleline);
168166
var matches = regex.Match(rawReleaseNotes);
@@ -184,11 +182,31 @@ public async Task DownloadUpdatesAsync()
184182

185183
UpdateStatus = UpdateStatus.Downloading;
186184

185+
async Task DownloadUpdatesAsync(IUpdateManager updateManager)
186+
{
187+
Logger.Debug("Downloading update...");
188+
await updateManager.DownloadReleases(_updates.ReleasesToApply, OnProgressChanged);
189+
190+
Logger.Debug("Fetching release notes...");
191+
var releaseNotes = _updates.FetchReleaseNotes();
192+
LatestReleaseNotes = ProcessReleaseNotes(releaseNotes[_latestRelease]);
193+
}
194+
187195
try
188196
{
189-
using (var updateManager = new UpdateManager(_localUpdatePath))
197+
if (_updateSource == UpdateSource.GitHub)
198+
{
199+
using (var updateManager = await UpdateManager.GitHubUpdateManager("https://github.com/ben-wallis/Filtration", prerelease: _downloadPrereleaseUpdates))
200+
{
201+
await DownloadUpdatesAsync(updateManager);
202+
}
203+
}
204+
else
190205
{
191-
await updateManager.DownloadReleases(_updates.ReleasesToApply, OnProgressChanged);
206+
using (var updateManager = new UpdateManager(_localUpdatePath))
207+
{
208+
await DownloadUpdatesAsync(updateManager);
209+
}
192210
}
193211
}
194212
catch (Exception e)
@@ -215,11 +233,22 @@ public async Task ApplyUpdatesAsync()
215233
// wipes out user settings due to the application directory changing with each update
216234
_settingsService.BackupSettings();
217235

236+
Logger.Debug("Applying update...");
218237
try
219238
{
220-
using (var updateManager = new UpdateManager(_localUpdatePath))
239+
if (_updateSource == UpdateSource.GitHub)
240+
{
241+
using (var mgr = await UpdateManager.GitHubUpdateManager("https://github.com/ben-wallis/Filtration", prerelease: _downloadPrereleaseUpdates))
242+
{
243+
await mgr.ApplyReleases(_updates, OnProgressChanged);
244+
}
245+
}
246+
else
221247
{
222-
await updateManager.ApplyReleases(_updates, OnProgressChanged);
248+
using (var updateManager = new UpdateManager(_localUpdatePath))
249+
{
250+
await updateManager.ApplyReleases(_updates, OnProgressChanged);
251+
}
223252
}
224253
}
225254
catch (Exception e)
@@ -229,6 +258,7 @@ public async Task ApplyUpdatesAsync()
229258
return;
230259
}
231260

261+
Logger.Debug("Update complete");
232262
UpdateStatus = UpdateStatus.UpdateComplete;
233263
}
234264

Filtration/ViewModels/UpdateViewModel.cs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public UpdateViewModel(IAvalonDockWorkspaceViewModel avalonDockWorkspaceViewMode
5050
updateService.UpdateProgressChanged += UpdateServiceOnUpdateProgressChanged;
5151
updateService.UpdateStatusChanged += UpdateServiceOnUpdateStatusChanged;
5252

53-
53+
5454
HideUpdateWindowCommand = new RelayCommand(OnHideUpdateWindowCommand, () => UpdateStatus == UpdateStatus.UpdateAvailable || UpdateStatus == UpdateStatus.Error);
5555
NextStepCommand = new RelayCommand(async () => await OnNextStepCommandAsync(), () => NextStepCommandEnabled);
5656

@@ -100,39 +100,39 @@ public string NextStepButtonText
100100
}
101101

102102
private bool NextStepCommandEnabled => UpdateStatus == UpdateStatus.UpdateAvailable || UpdateStatus == UpdateStatus.ReadyToApplyUpdate || UpdateStatus == UpdateStatus.UpdateComplete;
103-
103+
104104
private async Task OnNextStepCommandAsync()
105105
{
106106
switch (UpdateStatus)
107107
{
108108
case UpdateStatus.UpdateAvailable:
109-
{
110-
await _updateService.DownloadUpdatesAsync();
111-
break;
112-
}
113-
case UpdateStatus.ReadyToApplyUpdate:
114-
{
115-
if (!_updateTabShown)
116109
{
117-
// When the update has downloaded and is ready to apply, clicking the button
118-
// closes the update popup and shows the update tab.
119-
_avalonDockWorkspaceViewModel.AddDocument(this);
120-
Visible = false;
121-
_updateTabShown = true;
122-
NextStepButtonText = "Update";
110+
await _updateService.DownloadUpdatesAsync();
111+
break;
123112
}
124-
else
113+
case UpdateStatus.ReadyToApplyUpdate:
125114
{
126-
await _updateService.ApplyUpdatesAsync();
115+
if (!_updateTabShown)
116+
{
117+
// When the update has downloaded and is ready to apply, clicking the button
118+
// closes the update popup and shows the update tab.
119+
_avalonDockWorkspaceViewModel.AddDocument(this);
120+
Visible = false;
121+
_updateTabShown = true;
122+
NextStepButtonText = "Update";
123+
}
124+
else
125+
{
126+
await _updateService.ApplyUpdatesAsync();
127+
}
128+
129+
break;
127130
}
128-
129-
break;
130-
}
131131
case UpdateStatus.UpdateComplete:
132-
{
133-
_updateService.RestartAfterUpdate();
134-
break;
135-
}
132+
{
133+
_updateService.RestartAfterUpdate();
134+
break;
135+
}
136136
}
137137
}
138138

@@ -144,7 +144,6 @@ private void UpdateServiceOnUpdateStatusChanged(object sender, UpdateStatusChang
144144
{
145145
Visible = true;
146146
NextStepButtonText = "Download";
147-
RaisePropertyChanged(nameof(ReleaseNotes));
148147
RaisePropertyChanged(nameof(Version));
149148
break;
150149
}
@@ -156,6 +155,7 @@ private void UpdateServiceOnUpdateStatusChanged(object sender, UpdateStatusChang
156155
}
157156
case UpdateStatus.ReadyToApplyUpdate:
158157
{
158+
RaisePropertyChanged(nameof(ReleaseNotes));
159159
NextStepButtonText = "Update Ready";
160160
break;
161161
}

0 commit comments

Comments
 (0)