-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNugetClient.cs
303 lines (244 loc) · 10.9 KB
/
NugetClient.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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// (c) 2025 Sator Imaging, all rights reserved.
// Licensed under the Apache License version 2.0
// https://github.com/sator-imaging/Unity-Fundamentals
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using UnityEngine;
#nullable enable
namespace SatorImaging.UnityFundamentals.Editor
{
public class NugetClient
{
const string MIME_JSON = @"application/json";
const string MIME_ZIP = @"application/zip";
readonly static MediaTypeWithQualityHeaderValue HEADER_JSON = new(MIME_JSON);
readonly static MediaTypeWithQualityHeaderValue HEADER_ZIP = new(MIME_ZIP);
/// <summary>
/// <list type="table">
/// <item>0: package name</item>
/// </list>
/// </summary>
readonly static string NUGET_PACKAGE_EP = @"https://api.nuget.org/v3-flatcontainer/{0}/index.json";
/// <summary>
/// <list type="table">
/// <item>0: package name</item>
/// <item>1: semantic version</item>
/// </list>
/// </summary>
readonly static string NUGET_DOWNLOAD_URL = @"https://www.nuget.org/api/v2/package/{0}/{1}";
// TODO: grab files from here: https://github.com/dotnet/dotnet-api-docs/tree/main/xml
// or build from source: https://github.com/dotnet/standard/tree/v2.1.0
readonly static string FALLBACK_PACKAGE_NAME = "SatorImaging.CSharpApiDocumentation";
readonly static Dictionary<string, string> cache_urlByPackageName = new(capacity: 32);
[Serializable]
public sealed class Response
{
public string[]? versions;
}
public static NugetClient Default { [MethodImpl(MethodImplOptions.AggressiveInlining)] get; } = new();
/* logger ================================================================ */
public static class Logger
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Conditional("DEBUG")]
public static void Logging(LogType type, object message)
{
switch (type)
{
case LogType.Error:
UnityEngine.Debug.LogError(message);
break;
case LogType.Warning:
UnityEngine.Debug.LogWarning(message);
break;
case LogType.Log:
UnityEngine.Debug.Log(message);
break;
case LogType.Exception:
UnityEngine.Debug.LogException(message as Exception);
break;
case LogType.Assert:
UnityEngine.Debug.Assert(true, message);
break;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Conditional("__DISABLED__li11li11li11l1l1lii11i1li")]
public static void Verbose(LogType type, object message) => Logging(type, message);
}
/* instance members ================================================================ */
const string TEMP_DIR_NAME = nameof(SatorImaging) + "." + nameof(NugetClient);
private string b_tempFolderPath = Path.Combine(Path.GetTempPath(), TEMP_DIR_NAME).Replace('\\', '/');
public string TempFolderPath
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => b_tempFolderPath;
set
{
if (string.IsNullOrWhiteSpace(value))
{
throw new ArgumentException("null or whitespace");
}
if (value == b_tempFolderPath)
return;
value = value.Replace('\\', '/');
if (value[^1] == '/')
{
value = value[..^1];
}
if (!value.EndsWith('/' + TEMP_DIR_NAME, StringComparison.Ordinal))
{
value += '/' + TEMP_DIR_NAME;
}
b_tempFolderPath = value;
}
}
private HttpClient? b_httpClient;
public HttpClient HttpClient
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => b_httpClient ??= new();
set
{
if (value == null)
throw new ArgumentNullException(nameof(value));
if (value == b_httpClient)
return;
if (b_httpClient != null)
{
b_httpClient.CancelPendingRequests();
b_httpClient.Dispose();
}
b_httpClient = value;
}
}
public TimeSpan Timeout { [MethodImpl(MethodImplOptions.AggressiveInlining)] get; set; } = TimeSpan.FromSeconds(10);
/// <returns><see langword="false"/> if failed.</returns>
/// <inheritdoc cref="TryGetPackageUrlAsync(string, string?, CancellationToken)"/>
public bool TryGetPackageUrl(string packageName, string? targetVersion, [NotNullWhen(true)] out string? url)
{
using var cts = new CancellationTokenSource(Timeout);
url = Task.Run(async () => await TryGetPackageUrlAsync(packageName, targetVersion, cts.Token)).ConfigureAwait(false).GetAwaiter().GetResult();
return url != null;
}
/// <returns><see langword="false"/> if failed.</returns>
public bool TryDownloadPackageFile(string url, [NotNullWhen(true)] out string? tempZipFilePath)
{
using var cts = new CancellationTokenSource(Timeout);
tempZipFilePath = Task.Run(async () => await TryDownloadPackageFileAsync(url, cts.Token)).ConfigureAwait(false).GetAwaiter().GetResult();
return tempZipFilePath != null;
}
/* ===== async ===== */
/// <param name="targetVersion"><see langword="null"/> to get latest version.</param>
/// <returns><see langword="null"/> if package is not found.</returns>
public async ValueTask<string?> TryGetPackageUrlAsync(string packageName, string? targetVersion, CancellationToken cancellationToken = default)
{
if (targetVersion == null)
{
if (cache_urlByPackageName.TryGetValue(packageName, out var cachedUrl))
{
return cachedUrl;
}
}
var versions = await TryGetAvailableVersionAsync(packageName, cancellationToken);
string? foundVersion = null;
if (versions?.Length > 0)
{
foundVersion = (targetVersion == null)
? versions[^1]
: versions.FirstOrDefault(x => x == targetVersion)
;
}
if (foundVersion == null)
{
//// fallback!!
//return await TryGetPackageUrlAsync(FALLBACK_PACKAGE_NAME, cancellationToken);
return null;
}
var url = string.Format(NUGET_DOWNLOAD_URL, packageName, foundVersion);
cache_urlByPackageName[packageName] = url;
return url;
}
/// <returns><see langword="null"/> if failed to retrieve package versions.</returns>
public async ValueTask<string[]?> TryGetAvailableVersionAsync(string packageName, CancellationToken cancellationToken = default)
{
var client = HttpClient;
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(HEADER_JSON);
try
{
var endpoint = string.Format(NUGET_PACKAGE_EP, packageName);
Logger.Logging(LogType.Log, $"[NuGet] fetching nuget.org for package information: {packageName}\n{endpoint}\n");
var GET = await client.GetAsync(endpoint, cancellationToken);
if (!GET.IsSuccessStatusCode)
return null;
var json = await GET.Content.ReadAsStringAsync();
var response = JsonUtility.FromJson<Response>(json);
if (response.versions?.Length == 0)
{
response.versions = null;
}
return response.versions;
}
catch (Exception error)
{
Logger.Logging(LogType.Warning, error);
return null;
}
}
/// <returns><see langword="null"/> if failed to download otherwise file path to downloaded content.</returns>
public async ValueTask<string?> TryDownloadPackageFileAsync(string url, CancellationToken cancellationToken = default)
{
var cacheDirPath = TempFolderPath;
var cacheFileName = url.Replace("https://www.", string.Empty, StringComparison.OrdinalIgnoreCase)
.Replace("https://", string.Empty, StringComparison.OrdinalIgnoreCase)
.Replace("/", "--", StringComparison.Ordinal)
.Replace(':', '-')
+ ".zip";
var outputFilePath = Path.Combine(cacheDirPath, cacheFileName);
if (File.Exists(outputFilePath))
{
Logger.Verbose(LogType.Warning, $"Cache file found: {outputFilePath}");
return outputFilePath;
}
if (!Directory.Exists(cacheDirPath))
Directory.CreateDirectory(cacheDirPath);
var data = await TryDownloadPackageAsync(url, cancellationToken);
if (data == null)
return null;
await File.WriteAllBytesAsync(outputFilePath, data, cancellationToken);
return outputFilePath;
}
/// <returns><see langword="null"/> if failed or canceled.</returns>
public async ValueTask<byte[]?> TryDownloadPackageAsync(string url, CancellationToken cancellationToken = default)
{
var client = HttpClient;
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(HEADER_ZIP);
try
{
var GET = await client.GetAsync(url, cancellationToken);
if (!GET.IsSuccessStatusCode)
return null;
var result = await GET.Content.ReadAsByteArrayAsync();
if (result?.Length == 0)
result = null;
return result;
}
catch (Exception error)
{
Logger.Logging(LogType.Warning, error);
return null;
}
}
}
}