22using System . Linq ;
33using System . Text . RegularExpressions ;
44using System . Threading . Tasks ;
5+ using Filtration . Enums ;
56using Filtration . Properties ;
67using NLog ;
78using 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
0 commit comments