Skip to content

Commit f7318e4

Browse files
committed
Update functionality improvements
1 parent a4a887d commit f7318e4

File tree

6 files changed

+80
-42
lines changed

6 files changed

+80
-42
lines changed

SmartImage/Core/Info.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ internal static void ShowInfo()
159159

160160
NConsole.OverrideForegroundColor = Interface.ColorVersion;
161161

162-
var versionsInfo = UpdateInfo.CheckForUpdates();
162+
var versionsInfo = UpdateInfo.GetUpdateInfo();
163163

164164
NConsole.WriteInfo("Current version: {0}", versionsInfo.Current);
165165
NConsole.WriteInfo("Latest version: {0}", versionsInfo.Latest.Version);

SmartImage/Core/Interface.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,13 +262,13 @@ private static string GetContextMenuString(bool added)
262262
Color = ColorUtility,
263263
Function = () =>
264264
{
265-
var v = UpdateInfo.CheckForUpdates();
265+
var v = UpdateInfo.GetUpdateInfo();
266266

267267
if (v.Status == VersionStatus.Available) {
268268
Console.WriteLine($"Updating to {v.Latest}...");
269269

270270
try {
271-
UpdateInfo.Update();
271+
UpdateInfo.AutoUpdate();
272272
}
273273
catch (Exception e) {
274274
Console.WriteLine(e);

SmartImage/Program.cs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,28 +35,33 @@ public static class Program
3535

3636
private static void Main(string[] args)
3737
{
38-
38+
3939
/*
4040
* Setup
4141
* Check compatibility
4242
*/
4343
Info.Setup();
4444

45-
45+
4646
/*
4747
* Set up console
4848
*/
4949

5050
Console.Title = Info.NAME;
5151

52-
53-
if (Console.LargestWindowWidth >= Interface.ConsoleWindowWidth && Console.LargestWindowHeight >= Interface.ConsoleWindowHeight) {
52+
53+
if (Console.LargestWindowWidth >= Interface.ConsoleWindowWidth &&
54+
Console.LargestWindowHeight >= Interface.ConsoleWindowHeight) {
5455
Console.SetWindowSize(Interface.ConsoleWindowWidth, Interface.ConsoleWindowHeight);
5556
}
5657

5758
Console.OutputEncoding = Encoding.Unicode;
5859
Console.Clear();
5960

61+
Console.WriteLine(Info.NAME_BANNER);
62+
NConsole.WriteInfo("Setting up...");
63+
64+
6065
/*
6166
* Set up NConsole
6267
*/
@@ -68,13 +73,19 @@ private static void Main(string[] args)
6873
/*
6974
* Check for any legacy integrations that need to be migrated
7075
*/
71-
bool ok = LegacyIntegration.LegacyCleanup();
76+
bool cleanupOk = LegacyIntegration.LegacyCleanup();
7277

73-
if (!ok)
74-
{
78+
if (!cleanupOk) {
7579
NConsole.WriteError("Could not migrate legacy features");
7680
}
7781

82+
/*
83+
* Check for updates
84+
*/
85+
86+
87+
UpdateInfo.AutoUpdate();
88+
7889
/*
7990
* Run search
8091
*/

SmartImage/Utilities/CrashReport.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public override string ToString()
4343
}
4444

4545
sb.AppendLine(Strings.CreateSeparator("Program Info"));
46-
var versionsInfo = UpdateInfo.CheckForUpdates();
46+
var versionsInfo = UpdateInfo.GetUpdateInfo();
4747
sb.AppendFormat("Version: {0}", versionsInfo.Current);
4848

4949
return sb.ToString();

SmartImage/Utilities/ReleaseInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public static ReleaseInfo GetLatestRelease()
8080

8181
public override string ToString()
8282
{
83-
return String.Format("{0} {1} {2}", TagName, Version, PublishedAt);
83+
return $"{TagName} ({Version}) @ {PublishedAt}";
8484
}
8585
}
8686
}

SmartImage/Utilities/UpdateInfo.cs

Lines changed: 57 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System;
2+
using System.Diagnostics.CodeAnalysis;
23
using System.IO;
34
using System.Net;
45
using Novus.Win32;
6+
using SimpleCore.Console.CommandLine;
57
using SmartImage.Core;
68

79
namespace 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

Comments
 (0)