-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGitHubReleaseUpdateBackend.cs
More file actions
277 lines (238 loc) · 10.4 KB
/
Copy pathGitHubReleaseUpdateBackend.cs
File metadata and controls
277 lines (238 loc) · 10.4 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
using System.Globalization;
using System.Net.Http.Headers;
using System.Text.Json;
using System.Text.RegularExpressions;
using SwitchifyPc.Core.Storage;
namespace SwitchifyPc.Core.Updates;
public sealed class GitHubReleaseUpdateBackend : IUpdateBackend
{
private const string InstallerAssetPrefix = "Switchify-PC-Setup-";
private const string InstallerAssetSuffix = "-x64.exe";
private readonly HttpClient httpClient;
private readonly string owner;
private readonly string repository;
private readonly string cacheDirectory;
public GitHubReleaseUpdateBackend(HttpClient httpClient, string owner, string repository, string cacheDirectory)
{
this.httpClient = httpClient;
this.owner = owner;
this.repository = repository;
this.cacheDirectory = cacheDirectory;
if (!this.httpClient.DefaultRequestHeaders.UserAgent.Any())
{
this.httpClient.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("Switchify-PC", "0.2"));
}
}
public async Task<UpdateCheckOutcome> CheckForUpdatesAsync(string currentVersion, CancellationToken cancellationToken = default)
{
try
{
bool includePrerelease = SemVersion.Parse(currentVersion).IsPrerelease;
using HttpResponseMessage response = await httpClient.GetAsync(
$"https://api.github.com/repos/{owner}/{repository}/releases",
cancellationToken).ConfigureAwait(false);
if (!response.IsSuccessStatusCode)
{
return UpdateCheckOutcome.Failed(UpdateFailureReason.NetworkError);
}
await using Stream stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
IReadOnlyList<GitHubRelease> releases = await JsonSerializer.DeserializeAsync<List<GitHubRelease>>(
stream,
JsonOptions,
cancellationToken).ConfigureAwait(false) ?? [];
foreach (GitHubRelease release in releases)
{
if (release.Draft) continue;
if (release.Prerelease && !includePrerelease) continue;
string version = NormalizeVersion(release.TagName);
if (!SemVersion.IsNewer(version, currentVersion)) continue;
GitHubReleaseAsset? installer = release.Assets.FirstOrDefault(asset =>
string.Equals(asset.Name, InstallerAssetName(version), StringComparison.OrdinalIgnoreCase) &&
!string.IsNullOrWhiteSpace(asset.BrowserDownloadUrl));
if (installer is null) continue;
return UpdateCheckOutcome.Available(new AvailableUpdate(
Version: version,
ReleaseName: release.Name,
ReleaseNotes: release.Body,
InstallerAssetName: installer.Name,
InstallerDownloadUrl: installer.BrowserDownloadUrl));
}
return UpdateCheckOutcome.UpToDate();
}
catch (OperationCanceledException)
{
throw;
}
catch
{
return UpdateCheckOutcome.Failed(UpdateFailureReason.NetworkError);
}
}
public async Task<UpdateDownloadOutcome> DownloadUpdateAsync(
AvailableUpdate update,
IProgress<UpdateDownloadSnapshot> progress,
CancellationToken cancellationToken = default)
{
if (string.IsNullOrWhiteSpace(update.InstallerAssetName) || string.IsNullOrWhiteSpace(update.InstallerDownloadUrl))
{
return UpdateDownloadOutcome.Failed(UpdateFailureReason.InvalidUpdate);
}
try
{
using HttpResponseMessage response = await httpClient.GetAsync(
update.InstallerDownloadUrl,
HttpCompletionOption.ResponseHeadersRead,
cancellationToken).ConfigureAwait(false);
if (!response.IsSuccessStatusCode)
{
return UpdateDownloadOutcome.Failed(UpdateFailureReason.NetworkError);
}
Directory.CreateDirectory(cacheDirectory);
string outputPath = Path.Combine(cacheDirectory, SafeInstallerAssetName(update.InstallerAssetName));
string tempPath = $"{outputPath}.{Environment.ProcessId}.{DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()}.{Guid.NewGuid():N}.tmp";
long? totalBytes = response.Content.Headers.ContentLength;
long downloadedBytes = 0;
var renamed = false;
try
{
{
await using Stream input = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
await using FileStream output = new(tempPath, FileMode.Create, FileAccess.Write, FileShare.None, 81920, FileOptions.Asynchronous | FileOptions.WriteThrough);
byte[] buffer = new byte[81920];
while (true)
{
int read = await input.ReadAsync(buffer, cancellationToken).ConfigureAwait(false);
if (read == 0) break;
await output.WriteAsync(buffer.AsMemory(0, read), cancellationToken).ConfigureAwait(false);
downloadedBytes += read;
progress.Report(new UpdateDownloadSnapshot(downloadedBytes, totalBytes, Percent(downloadedBytes, totalBytes)));
}
await output.FlushAsync(cancellationToken).ConfigureAwait(false);
output.Flush(flushToDisk: true);
}
File.Move(tempPath, outputPath, overwrite: true);
renamed = true;
}
finally
{
if (!renamed)
{
TryDelete(tempPath);
}
}
progress.Report(new UpdateDownloadSnapshot(downloadedBytes, totalBytes, 100));
return UpdateDownloadOutcome.Downloaded(outputPath);
}
catch (OperationCanceledException)
{
throw;
}
catch
{
return UpdateDownloadOutcome.Failed(UpdateFailureReason.NetworkError);
}
}
public static string InstallerAssetName(string version) => $"{InstallerAssetPrefix}{version}{InstallerAssetSuffix}";
private static string NormalizeVersion(string tagName)
{
return tagName.StartsWith('v') || tagName.StartsWith('V') ? tagName[1..] : tagName;
}
private static int? Percent(long downloadedBytes, long? totalBytes)
{
if (totalBytes is null or <= 0) return null;
return Math.Clamp((int)Math.Round(downloadedBytes / (double)totalBytes * 100, MidpointRounding.AwayFromZero), 0, 100);
}
private static string SafeInstallerAssetName(string assetName)
{
string fileName = Path.GetFileName(assetName);
return string.IsNullOrWhiteSpace(fileName) ? InstallerAssetName("unknown") : fileName;
}
private static void TryDelete(string path)
{
try
{
File.Delete(path);
}
catch
{
// Best effort cleanup.
}
}
private static readonly JsonSerializerOptions JsonOptions = new()
{
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower,
PropertyNameCaseInsensitive = true
};
private sealed record GitHubRelease(
string TagName,
string? Name,
string? Body,
bool Draft,
bool Prerelease,
IReadOnlyList<GitHubReleaseAsset> Assets);
private sealed record GitHubReleaseAsset(string Name, string BrowserDownloadUrl);
}
internal readonly record struct SemVersion(int Major, int Minor, int Patch, string? Prerelease) : IComparable<SemVersion>
{
private static readonly Regex VersionPattern = new(
@"^v?(?<major>0|[1-9]\d*)\.(?<minor>0|[1-9]\d*)\.(?<patch>0|[1-9]\d*)(?:-(?<pre>[0-9A-Za-z.-]+))?$",
RegexOptions.Compiled | RegexOptions.CultureInvariant);
public bool IsPrerelease => !string.IsNullOrWhiteSpace(Prerelease);
public static SemVersion Parse(string value)
{
Match match = VersionPattern.Match(value);
if (!match.Success) throw new FormatException("Invalid semantic version.");
return new SemVersion(
int.Parse(match.Groups["major"].Value, CultureInfo.InvariantCulture),
int.Parse(match.Groups["minor"].Value, CultureInfo.InvariantCulture),
int.Parse(match.Groups["patch"].Value, CultureInfo.InvariantCulture),
match.Groups["pre"].Success ? match.Groups["pre"].Value : null);
}
public static bool IsNewer(string candidate, string current)
{
try
{
return Parse(candidate).CompareTo(Parse(current)) > 0;
}
catch
{
return false;
}
}
public int CompareTo(SemVersion other)
{
int major = Major.CompareTo(other.Major);
if (major != 0) return major;
int minor = Minor.CompareTo(other.Minor);
if (minor != 0) return minor;
int patch = Patch.CompareTo(other.Patch);
if (patch != 0) return patch;
if (Prerelease == other.Prerelease) return 0;
if (Prerelease is null) return 1;
if (other.Prerelease is null) return -1;
return ComparePrerelease(Prerelease, other.Prerelease);
}
private static int ComparePrerelease(string left, string right)
{
string[] leftParts = left.Split('.');
string[] rightParts = right.Split('.');
int length = Math.Max(leftParts.Length, rightParts.Length);
for (int index = 0; index < length; index++)
{
if (index >= leftParts.Length) return -1;
if (index >= rightParts.Length) return 1;
bool leftNumeric = int.TryParse(leftParts[index], NumberStyles.None, CultureInfo.InvariantCulture, out int leftNumber);
bool rightNumeric = int.TryParse(rightParts[index], NumberStyles.None, CultureInfo.InvariantCulture, out int rightNumber);
if (leftNumeric && rightNumeric)
{
int numeric = leftNumber.CompareTo(rightNumber);
if (numeric != 0) return numeric;
continue;
}
if (leftNumeric != rightNumeric) return leftNumeric ? -1 : 1;
int text = string.CompareOrdinal(leftParts[index], rightParts[index]);
if (text != 0) return text;
}
return 0;
}
}