-
Notifications
You must be signed in to change notification settings - Fork 0
/
UpdateCheck.cs
71 lines (54 loc) · 2.24 KB
/
UpdateCheck.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using Squirrel;
using System;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Threading.Tasks;
namespace ProcessCpuUsageStatusWindow
{
public static class UpdateCheck
{
public enum UpdateStatus
{
Checking,
None,
Downloading,
Installing,
Restarting
}
public delegate void UpdateStatusDelegate(UpdateStatus updateStatus, string message);
public static Version LocalVersion => Assembly.GetEntryAssembly().GetName().Version;
public static async Task<bool> CheckUpdate(UpdateStatusDelegate onUpdateStatus)
{
try
{
onUpdateStatus.Invoke(UpdateStatus.Checking, Properties.Resources.CheckingForUpdate);
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
using (var updateManager = await UpdateManager.GitHubUpdateManager(App.UpdateUrl))
{
var updates = await updateManager.CheckForUpdate();
var lastVersion = updates?.ReleasesToApply?.OrderBy(releaseEntry => releaseEntry.Version).LastOrDefault();
if (lastVersion == null)
{
onUpdateStatus.Invoke(UpdateStatus.None, Properties.Resources.NoUpdate);
return false;
}
onUpdateStatus.Invoke(UpdateStatus.Downloading, Properties.Resources.DownloadingUpdate);
Common.Settings.Extensions.BackupSettings();
await updateManager.DownloadReleases(new[] { lastVersion });
onUpdateStatus.Invoke(UpdateStatus.Installing, Properties.Resources.InstallingUpdate);
await updateManager.ApplyReleases(updates);
await updateManager.UpdateApp();
}
onUpdateStatus.Invoke(UpdateStatus.Restarting, Properties.Resources.RestartingAfterUpdate);
UpdateManager.RestartApp();
return true;
}
catch (Exception exception)
{
Console.WriteLine(exception);
return false;
}
}
}
}