-
-
Notifications
You must be signed in to change notification settings - Fork 61
/
Update.cs
98 lines (94 loc) · 4.29 KB
/
Update.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Windows;
using Community.PowerToys.Run.Plugin.Everything.Properties;
namespace Community.PowerToys.Run.Plugin.Everything
{
internal sealed class Update
{
private readonly CompositeFormat updateAvailable = CompositeFormat.Parse(Resources.UpdateAvailable);
internal async Task UpdateAsync(Version v, Settings s)
{
string apiUrl = "https://api.github.com/repos/lin-ycv/EverythingPowerToys/releases/latest";
#if DEBUG
if (s.Log > LogLevel.None)
Debugger.Write("1.Checking Update...");
#endif
try
{
using HttpClient httpClient = new();
httpClient.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0");
HttpResponseMessage response = await httpClient.GetAsync(apiUrl);
#if DEBUG
if (s.Log == LogLevel.Verbose) Debugger.Write($"\tResponse: {response.StatusCode}");
#endif
if (response.IsSuccessStatusCode)
{
using JsonDocument jsonDocument = await JsonDocument.ParseAsync(await response.Content.ReadAsStreamAsync());
JsonElement root = jsonDocument.RootElement;
Version latest = Version.TryParse(root.GetProperty("tag_name").GetString().AsSpan(1), out var vNumber)
? vNumber
: Version.Parse(root.GetProperty("tag_name").GetString());
if (latest > v && latest.ToString() != s.Skip)
{
MessageBoxResult mbox = MessageBox.Show(string.Format(CultureInfo.InvariantCulture, updateAvailable, v, latest), "Updater", MessageBoxButton.YesNoCancel);
if (mbox == MessageBoxResult.Yes && root.TryGetProperty("assets", out JsonElement assets))
{
string[] nameUrl = [string.Empty, string.Empty];
foreach (JsonElement asset in assets.EnumerateArray())
{
#if X64
if (asset.TryGetProperty("browser_download_url", out JsonElement downUrl) && downUrl.ToString().EndsWith("x64.exe", StringComparison.OrdinalIgnoreCase))
#elif ARM64
if (asset.TryGetProperty("browser_download_url", out JsonElement downUrl) && downUrl.ToString().EndsWith("ARM64.exe", StringComparison.OrdinalIgnoreCase))
#endif
{
nameUrl[0] = asset.GetProperty("name").ToString();
nameUrl[1] = downUrl.ToString();
}
}
if (nameUrl[0].Length > 0)
{
byte[] fileContent = await httpClient.GetByteArrayAsync(nameUrl[1]);
string fileName = Path.Combine(Path.GetTempPath(), nameUrl[0]);
File.WriteAllBytes(fileName, fileContent);
Process.Start(fileName);
}
else
{
ProcessStartInfo p = new("https://github.com/lin-ycv/EverythingPowerToys/releases/latest")
{
UseShellExecute = true,
Verb = "Open",
};
Process.Start(p);
}
}
else if (mbox == MessageBoxResult.No)
{
s.Skip = latest.ToString();
}
}
}
}
#if RELEASE
catch
{ }
#else
catch (Exception e)
{
if (s.Log > LogLevel.None)
Debugger.Write($"\r\nERROR: {e.Message}\r\n{e.StackTrace}\r\n");
}
if (s.Log > LogLevel.None)
Debugger.Write(" Checking Update...Done");
#endif
}
}
}