11using System ;
2+ using System . Diagnostics . CodeAnalysis ;
23using System . IO ;
34using System . Net ;
45using Novus . Win32 ;
6+ using SimpleCore . Console . CommandLine ;
57using SmartImage . Core ;
68
79namespace SmartImage . Utilities
@@ -17,54 +19,79 @@ public readonly struct UpdateInfo
1719 private UpdateInfo ( Version current , ReleaseInfo info , VersionStatus status )
1820 {
1921 Current = current ;
20- Latest = info ;
21- Status = status ;
22+ Latest = info ;
23+ Status = status ;
2224 }
2325
24-
25- public static void Update ( )
26+ public static void Update ( UpdateInfo ui )
2627 {
27- var ui = CheckForUpdates ( ) ;
28+ const string NEW_EXE = "SmartImage-new.exe" ;
29+ const string UPDATE_BAT = "SmartImage_Updater.bat" ;
2830
2931
30- if ( ui . Status == VersionStatus . Available ) {
31- var dest = Path . Combine ( Info . AppFolder , "SmartImage- new.exe" ) ;
32+ var destNew = Path . Combine ( Info . AppFolder , NEW_EXE ) ;
33+ var wc = new WebClient ( ) ;
3234
33- var wc = new WebClient ( ) ;
34- wc . DownloadFile ( ui . Latest . AssetUrl , dest ) ;
35+ NConsole . WriteInfo ( "Downloading..." ) ;
36+
37+ wc . DownloadFile ( ui . Latest . AssetUrl , destNew ) ;
38+
39+
3540
36- string exeFileName = Info . ExeLocation ;
37- const string UPDATE_BAT = "SmartImage_Updater.bat" ;
41+ string exeFileName = Info . ExeLocation ;
3842
39- string [ ] commands =
40- {
41- "@echo off" ,
43+ //const string WAIT_4_SEC = "ping 127.0.0.1 > nul";
4244
43- /* Wait approximately 4 seconds (so that the process is already terminated) */
44- "ping 127.0.0.1 > nul" ,
45+ const string WAIT_4_SEC = "timeout /t 4 /nobreak >nul" ;
4546
46- /* Delete executable */
47- "echo y | del /F " + exeFileName ,
47+ string [ ] commands =
48+ {
49+ "@echo off" ,
4850
49- /* Rename */
50- $ "move /Y \" { dest } \" \" { exeFileName } \" > NUL" ,
51+ /* Wait approximately 4 seconds (so that the process is already terminated) */
52+ WAIT_4_SEC ,
5153
52- /* Delete this bat file */
53- "echo y | del " + UPDATE_BAT
54- } ;
55-
54+ /* Delete executable */
55+ "echo y | del /F " + exeFileName ,
5656
57- // Runs in background
58-
59- Command . RunBatch ( commands , false , UPDATE_BAT ) ;
60- }
57+ /* Rename */
58+ $ "move /Y \" { destNew } \" \" { exeFileName } \" > NUL",
59+
60+ /* Wait */
61+ WAIT_4_SEC ,
62+ WAIT_4_SEC ,
63+
64+ /* Open the new SmartImage version */
65+ $ "start /d \" { Info . AppFolder } \" { Info . NAME_EXE } ",
6166
67+ /* Delete this batch file */
68+ "echo y | del " + UPDATE_BAT ,
69+ } ;
70+
71+
72+ // Runs in background
73+ Command . RunBatch ( commands , false , UPDATE_BAT ) ;
74+ }
6275
76+
77+ // NOTE: Does not return if a new update is found and the user updates
78+ public static void AutoUpdate ( )
79+ {
80+ var ui = GetUpdateInfo ( ) ;
81+
82+ if ( ui . Status == VersionStatus . Available ) {
83+ NConsole . WriteSuccess ( $ "Update found: { ui . Latest } ") ;
84+
85+ if ( NConsoleIO . ReadConfirmation ( "Update?" ) ) {
86+ Update ( ui ) ;
87+ Environment . Exit ( 0 ) ;
88+ }
89+ }
6390 }
6491
65- public static UpdateInfo CheckForUpdates ( )
92+ public static UpdateInfo GetUpdateInfo ( )
6693 {
67- var asm = typeof ( Info ) . Assembly . GetName ( ) ;
94+ var asm = typeof ( Info ) . Assembly . GetName ( ) ;
6895 var currentVersion = asm . Version ;
6996
7097
0 commit comments