-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVersionHelperGit.cs
More file actions
216 lines (189 loc) · 6.7 KB
/
Copy pathVersionHelperGit.cs
File metadata and controls
216 lines (189 loc) · 6.7 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
using System;
using System.Net.Http;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
namespace EasyVersionBackup
{
public static class VersionHelperGit
{
private const string LatestReleaseApiUrl =
"https://api.github.com/repos/UncleRiot/EasyVersionBackup/releases/latest";
private static readonly HttpClient HttpClient =
CreateHttpClient();
public static async Task<VersionHelperGitResult> CheckForUpdateAsync(
string currentVersion,
CancellationToken cancellationToken = default)
{
try
{
using HttpResponseMessage response =
await HttpClient.GetAsync(
LatestReleaseApiUrl,
cancellationToken);
if (!response.IsSuccessStatusCode)
{
return VersionHelperGitResult.CreateConnectionFailed(
$"GitHub returned HTTP {(int)response.StatusCode}.");
}
string json =
await response.Content.ReadAsStringAsync(
cancellationToken);
using JsonDocument document =
JsonDocument.Parse(json);
JsonElement root = document.RootElement;
string latestVersionText =
root.TryGetProperty(
"tag_name",
out JsonElement tagNameElement)
? tagNameElement.GetString() ??
string.Empty
: string.Empty;
string downloadUrl =
root.TryGetProperty(
"html_url",
out JsonElement htmlUrlElement)
? htmlUrlElement.GetString() ??
string.Empty
: string.Empty;
if (!TryParseVersion(
currentVersion,
out Version currentParsedVersion))
{
return VersionHelperGitResult.CreateConnectionFailed(
"The installed version could not be parsed.");
}
if (!TryParseVersion(
latestVersionText,
out Version latestParsedVersion))
{
return VersionHelperGitResult.CreateConnectionFailed(
"The GitHub release version could not be parsed.");
}
if (latestParsedVersion <=
currentParsedVersion)
{
return VersionHelperGitResult.CreateNoUpdate();
}
return VersionHelperGitResult.CreateUpdateAvailable(
latestVersionText,
downloadUrl);
}
catch (OperationCanceledException)
when (cancellationToken.IsCancellationRequested)
{
throw;
}
catch (OperationCanceledException)
{
return VersionHelperGitResult.CreateConnectionFailed(
"The GitHub update request timed out.");
}
catch (HttpRequestException exception)
{
return VersionHelperGitResult.CreateConnectionFailed(
exception.Message);
}
catch (JsonException exception)
{
return VersionHelperGitResult.CreateConnectionFailed(
"GitHub returned invalid release data: " +
exception.Message);
}
catch (Exception exception)
{
return VersionHelperGitResult.CreateConnectionFailed(
exception.Message);
}
}
private static HttpClient CreateHttpClient()
{
HttpClient httpClient = new HttpClient
{
Timeout = TimeSpan.FromSeconds(15)
};
httpClient.DefaultRequestHeaders.UserAgent.ParseAdd(
"EasyVersionBackup");
return httpClient;
}
private static bool TryParseVersion(
string versionText,
out Version version)
{
version = new Version(0, 0, 0);
if (string.IsNullOrWhiteSpace(versionText))
{
return false;
}
string normalizedVersionText =
versionText.Trim();
if (normalizedVersionText.StartsWith(
"v",
StringComparison.OrdinalIgnoreCase))
{
normalizedVersionText =
normalizedVersionText.Substring(1);
}
if (!Version.TryParse(
normalizedVersionText,
out Version? parsedVersion) ||
parsedVersion == null)
{
return false;
}
version = parsedVersion;
return true;
}
}
public sealed class VersionHelperGitResult
{
private VersionHelperGitResult(
bool canConnectToGitHub,
bool updateAvailable,
string latestVersion,
string downloadUrl,
string errorMessage)
{
CanConnectToGitHub = canConnectToGitHub;
UpdateAvailable = updateAvailable;
LatestVersion = latestVersion;
DownloadUrl = downloadUrl;
ErrorMessage = errorMessage;
}
public bool CanConnectToGitHub { get; }
public bool UpdateAvailable { get; }
public string LatestVersion { get; }
public string DownloadUrl { get; }
public string ErrorMessage { get; }
public static VersionHelperGitResult CreateConnectionFailed(
string errorMessage)
{
return new VersionHelperGitResult(
false,
false,
string.Empty,
string.Empty,
errorMessage);
}
public static VersionHelperGitResult CreateNoUpdate()
{
return new VersionHelperGitResult(
true,
false,
string.Empty,
string.Empty,
string.Empty);
}
public static VersionHelperGitResult CreateUpdateAvailable(
string latestVersion,
string downloadUrl)
{
return new VersionHelperGitResult(
true,
true,
latestVersion,
downloadUrl,
string.Empty);
}
}
}