11using System ;
22using System . IO ;
3+ using System . Linq ;
34using System . Reflection ;
45using System . Threading . Tasks ;
6+ using System . Windows . Forms ;
57using DataCommander . Update . Events ;
68using Foundation ;
79using Foundation . Deployment ;
@@ -10,29 +12,33 @@ namespace DataCommander.Update
1012{
1113 public sealed class Updater
1214 {
13- private const string ApplicationName = "DataCommander" ;
1415 private readonly Action < Event > _eventPublisher ;
1516 private bool _updateStarted ;
1617 public bool UpdateStarted => _updateStarted ;
1718 public Updater ( Action < Event > eventHandler ) => _eventPublisher = eventHandler ;
1819
1920 public Task Update ( )
2021 {
21- var command = DeploymentCommandRepository . Get ( ApplicationName ) ;
22+ var entryAsembly = Assembly . GetEntryAssembly ( ) ;
23+ var title = entryAsembly . GetCustomAttributes ( ) . OfType < AssemblyTitleAttribute > ( ) . First ( ) . Title ;
24+ var applicationName = title ;
25+
26+ var command = DeploymentCommandRepository . Get ( applicationName ) ;
2227 return Handle ( ( dynamic ) command ) ;
2328 }
2429
2530 private async Task Handle ( CheckForUpdates checkForUpdates )
2631 {
2732 if ( checkForUpdates . When <= UniversalTime . Default . UtcNow )
2833 {
29- var localVersion = Assembly . GetEntryAssembly ( ) . GetName ( ) . Version ;
34+ var entryAssembly = Assembly . GetEntryAssembly ( ) ;
35+ var localVersion = entryAssembly . GetName ( ) . Version ;
3036 var remoteVersionUri = new Uri ( "https://raw.githubusercontent.com/csbernath/DataCommander/master/Version.txt" ) ;
3137 _eventPublisher ( new CheckForUpdatesStarted ( ) ) ;
3238 var remoteVersion = await DeploymentHelper . GetRemoteVersion ( remoteVersionUri ) ;
3339 if ( localVersion < remoteVersion )
3440 {
35- _eventPublisher ( new DownloadingNewVersionStarted ( ) ) ;
41+ _eventPublisher ( new DownloadingNewVersionStarted ( remoteVersion ) ) ;
3642 var address = new Uri ( $ "https://github.com/csbernath/DataCommander/releases/download/{ remoteVersion } /DataCommander.Updater.zip") ;
3743 var guid = Guid . NewGuid ( ) ;
3844 var updaterDirectory = Path . Combine ( Path . GetTempPath ( ) , guid . ToString ( ) ) ;
@@ -43,24 +49,37 @@ await DeploymentHelper.DownloadUpdater(address, updaterDirectory, zipFileName,
4349 DeploymentHelper . ExtractZip ( zipFileName , updaterDirectory ) ;
4450
4551 var updaterExeFileName = Path . Combine ( updaterDirectory , "DataCommander.Updater.exe" ) ;
46- var applicationExeFileName = Assembly . GetEntryAssembly ( ) . Location ;
52+ var applicationExeFileName = entryAssembly . Location ;
4753 DeploymentHelper . StartUpdater ( updaterExeFileName , applicationExeFileName ) ;
4854 _updateStarted = true ;
4955 }
5056 else
51- {
52- var now = UniversalTime . Default . UtcNow ;
53- var when = now . AddDays ( 1 ) ;
54- DeploymentCommandRepository . Save ( ApplicationName , new CheckForUpdates { When = when } ) ;
55- }
57+ ScheduleCheckForUpdates ( ) ;
5658 }
59+ }
5760
58- _eventPublisher ( new CheckForUpdateCompleted ( ) ) ;
61+ private static void ScheduleCheckForUpdates ( )
62+ {
63+ var entryAsembly = Assembly . GetEntryAssembly ( ) ;
64+ var title = entryAsembly . GetCustomAttributes ( ) . OfType < AssemblyTitleAttribute > ( ) . First ( ) . Title ;
65+ var applicationName = title ;
66+ var now = UniversalTime . Default . UtcNow ;
67+ var tomorrow = now . AddDays ( 1 ) ;
68+ DeploymentCommandRepository . Save ( applicationName , new CheckForUpdates ( tomorrow ) ) ;
5969 }
6070
6171 private Task Handle ( DeleteUpdater deleteUpdater )
6272 {
63- DeploymentHelper . DeleteUpdater ( deleteUpdater . Directory ) ;
73+ try
74+ {
75+ DeploymentHelper . DeleteUpdater ( deleteUpdater . Directory ) ;
76+ }
77+ catch ( Exception e )
78+ {
79+ MessageBox . Show ( e . ToString ( ) ) ;
80+ }
81+
82+ ScheduleCheckForUpdates ( ) ;
6483 return Task . CompletedTask ;
6584 }
6685 }
0 commit comments