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
|
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.
namespace WixToolset.Core.ExtensionCache
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using NuGet.Common;
using NuGet.Configuration;
using NuGet.Credentials;
using NuGet.Packaging;
using NuGet.Protocol;
using NuGet.Protocol.Core.Types;
using NuGet.Versioning;
using WixToolset.Extensibility.Data;
using WixToolset.Extensibility.Services;
/// <summary>
/// Extension cache manager.
/// </summary>
internal class ExtensionCacheManager
{
private IReadOnlyCollection<IExtensionCacheLocation> cacheLocations;
public ExtensionCacheManager(IMessaging messaging, IExtensionManager extensionManager)
{
this.Messaging = messaging;
this.ExtensionManager = extensionManager;
}
private IMessaging Messaging { get; }
private IExtensionManager ExtensionManager { get; }
public async Task<bool> AddAsync(bool global, string extension, CancellationToken cancellationToken)
{
if (String.IsNullOrEmpty(extension))
{
throw new ArgumentNullException(nameof(extension));
}
(var extensionId, var extensionVersion) = ParseExtensionReference(extension);
var result = await this.DownloadAndExtractAsync(global, extensionId, extensionVersion, cancellationToken);
return result;
}
public Task<bool> RemoveAsync(bool global, string extension, CancellationToken cancellationToken)
{
if (String.IsNullOrEmpty(extension))
{
throw new ArgumentNullException(nameof(extension));
}
(var extensionId, var extensionVersion) = ParseExtensionReference(extension);
var cacheFolder = this.GetCacheFolder(global);
var extensionFolder = Path.Combine(cacheFolder, extensionId, extensionVersion);
if (Directory.Exists(extensionFolder))
{
cancellationToken.ThrowIfCancellationRequested();
Directory.Delete(cacheFolder, true);
return Task.FromResult(true);
}
return Task.FromResult(false);
}
public Task<IEnumerable<CachedExtension>> ListAsync(bool global, string extension, CancellationToken cancellationToken)
{
var found = new List<CachedExtension>();
(var extensionId, var extensionVersion) = ParseExtensionReference(extension);
var cacheFolder = this.GetCacheFolder(global);
var searchFolder = Path.Combine(cacheFolder, extensionId, extensionVersion);
if (!Directory.Exists(searchFolder))
{
}
else if (!String.IsNullOrEmpty(extensionVersion)) // looking for an explicit version of an extension.
{
var present = ExtensionFileExists(cacheFolder, extensionId, extensionVersion);
found.Add(new CachedExtension(extensionId, extensionVersion, !present));
}
else // looking for all versions of an extension or all versions of all extensions.
{
IEnumerable<string> foundExtensionIds;
if (String.IsNullOrEmpty(extensionId))
{
// Looking for all versions of all extensions.
foundExtensionIds = Directory.GetDirectories(cacheFolder).Select(folder => Path.GetFileName(folder)).ToList();
}
else
{
// Looking for all versions of a single extension.
var extensionFolder = Path.Combine(cacheFolder, extensionId);
foundExtensionIds = Directory.Exists(extensionFolder) ? new[] { extensionId } : Array.Empty<string>();
}
foreach (var foundExtensionId in foundExtensionIds)
{
var extensionFolder = Path.Combine(cacheFolder, foundExtensionId);
foreach (var folder in Directory.GetDirectories(extensionFolder))
{
cancellationToken.ThrowIfCancellationRequested();
var foundExtensionVersion = Path.GetFileName(folder);
if (!NuGetVersion.TryParse(foundExtensionVersion, out _))
{
continue;
}
var present = ExtensionFileExists(cacheFolder, foundExtensionId, foundExtensionVersion);
found.Add(new CachedExtension(foundExtensionId, foundExtensionVersion, !present));
}
}
}
return Task.FromResult((IEnumerable<CachedExtension>)found);
}
private string GetCacheFolder(bool global)
{
if (this.cacheLocations == null)
{
this.cacheLocations = this.ExtensionManager.GetCacheLocations();
}
var requestedScope = global ? ExtensionCacheLocationScope.User : ExtensionCacheLocationScope.Project;
var cacheLocation = this.cacheLocations.First(l => l.Scope == requestedScope);
return cacheLocation.Path;
}
private async Task<bool> DownloadAndExtractAsync(bool global, string id, string version, CancellationToken cancellationToken)
{
var logger = NullLogger.Instance;
DefaultCredentialServiceUtility.SetupDefaultCredentialService(logger, nonInteractive: false);
var settings = Settings.LoadDefaultSettings(root: Environment.CurrentDirectory);
var sources = PackageSourceProvider.LoadPackageSources(settings).Where(s => s.IsEnabled);
using (var cache = new SourceCacheContext())
{
PackageSource versionSource = null;
var nugetVersion = String.IsNullOrEmpty(version) ? null : new NuGetVersion(version);
if (nugetVersion is null)
{
foreach (var source in sources)
{
var repository = Repository.Factory.GetCoreV3(source.Source);
var resource = await repository.GetResourceAsync<FindPackageByIdResource>();
try
{
var availableVersions = await resource.GetAllVersionsAsync(id, cache, logger, cancellationToken);
foreach (var availableVersion in availableVersions)
{
if (nugetVersion is null || nugetVersion < availableVersion)
{
nugetVersion = availableVersion;
versionSource = source;
}
}
}
catch (FatalProtocolException e)
{
this.Messaging.Write(ExtensionCacheWarnings.NugetException(id, e.Message));
}
}
if (nugetVersion is null)
{
return false;
}
}
var searchSources = versionSource is null ? sources : new[] { versionSource };
var cacheFolder = this.GetCacheFolder(global);
var extensionFolder = Path.Combine(cacheFolder, id, nugetVersion.ToString());
foreach (var source in searchSources)
{
var repository = Repository.Factory.GetCoreV3(source.Source);
var resource = await repository.GetResourceAsync<FindPackageByIdResource>();
using (var stream = new MemoryStream())
{
var downloaded = await resource.CopyNupkgToStreamAsync(id, nugetVersion, stream, cache, logger, cancellationToken);
if (downloaded)
{
stream.Position = 0;
Directory.CreateDirectory(extensionFolder);
using (var archive = new PackageArchiveReader(stream))
{
var files = PackagingConstants.Folders.Known.SelectMany(folder => archive.GetFiles(folder)).Distinct(StringComparer.OrdinalIgnoreCase);
await archive.CopyFilesAsync(extensionFolder, files, this.ExtractProgress, logger, cancellationToken);
}
return true;
}
}
}
}
return false;
}
private string ExtractProgress(string sourceFile, string targetPath, Stream fileStream)
{
return fileStream.CopyToFile(targetPath);
}
private static (string extensionId, string extensionVersion) ParseExtensionReference(string extensionReference)
{
var extensionId = extensionReference ?? String.Empty;
var extensionVersion = String.Empty;
var index = extensionId.LastIndexOf('/');
if (index > 0)
{
extensionVersion = extensionReference.Substring(index + 1);
extensionId = extensionReference.Substring(0, index);
if (!NuGetVersion.TryParse(extensionVersion, out _))
{
throw new ArgumentException($"Invalid extension version in {extensionReference}");
}
if (String.IsNullOrEmpty(extensionId))
{
throw new ArgumentException($"Invalid extension id in {extensionReference}");
}
}
return (extensionId, extensionVersion);
}
private static bool ExtensionFileExists(string baseFolder, string extensionId, string extensionVersion)
{
var toolsFolder = Path.Combine(baseFolder, extensionId, extensionVersion, "tools");
if (!Directory.Exists(toolsFolder))
{
return false;
}
var extensionAssembly = Path.Combine(toolsFolder, extensionId + ".dll");
var present = File.Exists(extensionAssembly);
if (!present)
{
extensionAssembly = Path.Combine(toolsFolder, extensionId + ".exe");
present = File.Exists(extensionAssembly);
}
return present;
}
}
}
|