-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathToolPackageDownloader.cs
427 lines (365 loc) · 17.8 KB
/
ToolPackageDownloader.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.Reflection;
using Microsoft.DotNet.Cli.CommandFactory.CommandResolution;
using Microsoft.DotNet.Cli.Extensions;
using Microsoft.DotNet.Cli.NuGetPackageDownloader;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.Extensions.EnvironmentAbstractions;
using Microsoft.TemplateEngine.Utils;
using Newtonsoft.Json.Linq;
using NuGet.Client;
using NuGet.Common;
using NuGet.Configuration;
using NuGet.ContentModel;
using NuGet.Frameworks;
using NuGet.LibraryModel;
using NuGet.Packaging;
using NuGet.Packaging.Core;
using NuGet.ProjectModel;
using NuGet.Repositories;
using NuGet.RuntimeModel;
using NuGet.Versioning;
namespace Microsoft.DotNet.Cli.ToolPackage;
internal class ToolPackageDownloader : IToolPackageDownloader
{
private readonly IToolPackageStore _toolPackageStore;
// The directory that the tool package is returned
protected DirectoryPath _toolReturnPackageDirectory;
// The directory that the tool asset file is returned
protected DirectoryPath _toolReturnJsonParentDirectory;
// The directory that global tools first downloaded
// example: C:\Users\username\.dotnet\tools\.store\.stage\tempFolder
protected readonly DirectoryPath _globalToolStageDir;
// The directory that local tools first downloaded
// example: C:\Users\username\.nuget\package
protected readonly DirectoryPath _localToolDownloadDir;
// The directory that local tools' asset files located
// example: C:\Users\username\AppData\Local\Temp\tempFolder
protected readonly DirectoryPath _localToolAssetDir;
protected readonly string _runtimeJsonPath;
private readonly string _currentWorkingDirectory;
public ToolPackageDownloader(
IToolPackageStore store,
string runtimeJsonPathForTests = null,
string currentWorkingDirectory = null
)
{
_toolPackageStore = store ?? throw new ArgumentNullException(nameof(store));
_globalToolStageDir = _toolPackageStore.GetRandomStagingDirectory();
ISettings settings = Settings.LoadDefaultSettings(currentWorkingDirectory ?? Directory.GetCurrentDirectory());
_localToolDownloadDir = new DirectoryPath(SettingsUtility.GetGlobalPackagesFolder(settings));
_currentWorkingDirectory = currentWorkingDirectory;
_localToolAssetDir = new DirectoryPath(PathUtilities.CreateTempSubdirectory());
_runtimeJsonPath = runtimeJsonPathForTests ?? Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "RuntimeIdentifierGraph.json");
}
public IToolPackage InstallPackage(PackageLocation packageLocation, PackageId packageId,
VerbosityOptions verbosity = VerbosityOptions.normal,
VersionRange versionRange = null,
string targetFramework = null,
bool isGlobalTool = false,
bool isGlobalToolRollForward = false,
bool verifySignatures = true,
RestoreActionConfig restoreActionConfig = null
)
{
var packageRootDirectory = _toolPackageStore.GetRootPackageDirectory(packageId);
string rollbackDirectory = null;
return TransactionalAction.Run<IToolPackage>(
action: () =>
{
ILogger nugetLogger = new NullLogger();
if (verbosity.IsDetailedOrDiagnostic())
{
nugetLogger = new NuGetConsoleLogger();
}
if (versionRange == null)
{
var versionString = "*";
versionRange = VersionRange.Parse(versionString);
}
var toolDownloadDir = isGlobalTool ? _globalToolStageDir : _localToolDownloadDir;
var assetFileDirectory = isGlobalTool ? _globalToolStageDir : _localToolAssetDir;
var nugetPackageDownloader = new NuGetPackageDownloader.NuGetPackageDownloader(
toolDownloadDir,
verboseLogger: nugetLogger,
verifySignatures: verifySignatures,
shouldUsePackageSourceMapping: true,
restoreActionConfig: restoreActionConfig,
verbosityOptions: verbosity,
currentWorkingDirectory: _currentWorkingDirectory);
var packageSourceLocation = new PackageSourceLocation(packageLocation.NugetConfig, packageLocation.RootConfigDirectory, packageLocation.SourceFeedOverrides, packageLocation.AdditionalFeeds);
bool givenSpecificVersion = false;
if (versionRange.MinVersion != null && versionRange.MaxVersion != null && versionRange.MinVersion == versionRange.MaxVersion)
{
givenSpecificVersion = true;
}
NuGetVersion packageVersion = nugetPackageDownloader.GetBestPackageVersionAsync(packageId, versionRange, packageSourceLocation).GetAwaiter().GetResult();
rollbackDirectory = isGlobalTool ? toolDownloadDir.Value: new VersionFolderPathResolver(toolDownloadDir.Value).GetInstallPath(packageId.ToString(), packageVersion);
if (isGlobalTool)
{
NuGetv3LocalRepository nugetPackageRootDirectory = new(new VersionFolderPathResolver(_toolPackageStore.Root.Value).GetInstallPath(packageId.ToString(), packageVersion));
var globalPackage = nugetPackageRootDirectory.FindPackage(packageId.ToString(), packageVersion);
if (globalPackage != null)
{
throw new ToolPackageException(
string.Format(
CliStrings.ToolPackageConflictPackageId,
packageId,
packageVersion.ToNormalizedString()));
}
}
NuGetv3LocalRepository localRepository = new(toolDownloadDir.Value);
var package = localRepository.FindPackage(packageId.ToString(), packageVersion);
if (package == null)
{
DownloadAndExtractPackage(packageId, nugetPackageDownloader, toolDownloadDir.Value, packageVersion, packageSourceLocation, includeUnlisted: givenSpecificVersion).GetAwaiter().GetResult();
}
else if(isGlobalTool)
{
throw new ToolPackageException(
string.Format(
CliStrings.ToolPackageConflictPackageId,
packageId,
packageVersion.ToNormalizedString()));
}
CreateAssetFile(packageId, packageVersion, toolDownloadDir, assetFileDirectory, _runtimeJsonPath, targetFramework);
DirectoryPath toolReturnPackageDirectory;
DirectoryPath toolReturnJsonParentDirectory;
if (isGlobalTool)
{
toolReturnPackageDirectory = _toolPackageStore.GetPackageDirectory(packageId, packageVersion);
toolReturnJsonParentDirectory = _toolPackageStore.GetPackageDirectory(packageId, packageVersion);
var packageRootDirectory = _toolPackageStore.GetRootPackageDirectory(packageId);
Directory.CreateDirectory(packageRootDirectory.Value);
FileAccessRetrier.RetryOnMoveAccessFailure(() => Directory.Move(_globalToolStageDir.Value, toolReturnPackageDirectory.Value));
rollbackDirectory = toolReturnPackageDirectory.Value;
}
else
{
toolReturnPackageDirectory = toolDownloadDir;
toolReturnJsonParentDirectory = _localToolAssetDir;
}
var toolPackageInstance = new ToolPackageInstance(id: packageId,
version: packageVersion,
packageDirectory: toolReturnPackageDirectory,
assetsJsonParentDirectory: toolReturnJsonParentDirectory);
if (isGlobalToolRollForward)
{
UpdateRuntimeConfig(toolPackageInstance);
}
return toolPackageInstance;
},
rollback: () =>
{
if (rollbackDirectory != null && Directory.Exists(rollbackDirectory))
{
Directory.Delete(rollbackDirectory, true);
}
// Delete the root if it is empty
if (Directory.Exists(packageRootDirectory.Value) &&
!Directory.EnumerateFileSystemEntries(packageRootDirectory.Value).Any())
{
Directory.Delete(packageRootDirectory.Value, false);
}
});
}
// The following methods are copied from the LockFileUtils class in Nuget.Client
private static void AddToolsAssets(
ManagedCodeConventions managedCodeConventions,
LockFileTargetLibrary lockFileLib,
ContentItemCollection contentItems,
IReadOnlyList<SelectionCriteria> orderedCriteria)
{
var toolsGroup = GetLockFileItems(
orderedCriteria,
contentItems,
managedCodeConventions.Patterns.ToolsAssemblies);
lockFileLib.ToolsAssemblies.AddRange(toolsGroup);
}
private static void UpdateRuntimeConfig(
ToolPackageInstance toolPackageInstance
)
{
var runtimeConfigFilePath = Path.ChangeExtension(toolPackageInstance.Command.Executable.Value, ".runtimeconfig.json");
// Update the runtimeconfig.json file
if (File.Exists(runtimeConfigFilePath))
{
string existingJson = File.ReadAllText(runtimeConfigFilePath);
var jsonObject = JObject.Parse(existingJson);
if (jsonObject["runtimeOptions"] is JObject runtimeOptions)
{
runtimeOptions["rollForward"] = "Major";
string updateJson = jsonObject.ToString();
File.WriteAllText(runtimeConfigFilePath, updateJson);
}
}
}
private static IEnumerable<LockFileItem> GetLockFileItems(
IReadOnlyList<SelectionCriteria> criteria,
ContentItemCollection items,
params PatternSet[] patterns)
{
return GetLockFileItems(criteria, items, additionalAction: null, patterns);
}
private static IEnumerable<LockFileItem> GetLockFileItems(
IReadOnlyList<SelectionCriteria> criteria,
ContentItemCollection items,
Action<LockFileItem> additionalAction,
params PatternSet[] patterns)
{
// Loop through each criteria taking the first one that matches one or more items.
foreach (var managedCriteria in criteria)
{
var group = items.FindBestItemGroup(
managedCriteria,
patterns);
if (group != null)
{
foreach (var item in group.Items)
{
var newItem = new LockFileItem(item.Path);
if (item.Properties.TryGetValue("locale", out var locale))
{
newItem.Properties["locale"] = (string)locale;
}
if (item.Properties.TryGetValue("related", out var related))
{
newItem.Properties["related"] = (string)related;
}
additionalAction?.Invoke(newItem);
yield return newItem;
}
// Take only the first group that has items
break;
}
}
yield break;
}
private static async Task<NuGetVersion> DownloadAndExtractPackage(
PackageId packageId,
INuGetPackageDownloader nugetPackageDownloader,
string packagesRootPath,
NuGetVersion packageVersion,
PackageSourceLocation packageSourceLocation,
bool includeUnlisted = false
)
{
var packagePath = await nugetPackageDownloader.DownloadPackageAsync(packageId, packageVersion, packageSourceLocation, includeUnlisted: includeUnlisted).ConfigureAwait(false);
// look for package on disk and read the version
NuGetVersion version;
using (FileStream packageStream = File.OpenRead(packagePath))
{
PackageArchiveReader reader = new(packageStream);
NuspecReader nuspecReader = new NuspecReader(reader.GetNuspec());
version = nuspecReader.GetVersion();
bool requireLicenseAcceptance = nuspecReader.GetRequireLicenseAcceptance();
// If the package requires license acceptance, we need to ask the user to accept it
// TODO: Find a better way to handle this
if (requireLicenseAcceptance)
{
Console.WriteLine($"The package {packageId} requires license acceptance. Please accept the license to continue. [y]");
if (!Console.ReadKey().Key.Equals(ConsoleKey.Y))
{
throw new ToolPackageException($"User did not accept the license for package {packageId}.");
}
}
var packageHash = Convert.ToBase64String(new CryptoHashProvider("SHA512").CalculateHash(reader.GetNuspec()));
var hashPath = new VersionFolderPathResolver(packagesRootPath).GetHashPath(packageId.ToString(), version);
Directory.CreateDirectory(Path.GetDirectoryName(hashPath));
File.WriteAllText(hashPath, packageHash);
}
// Extract the package
var nupkgDir = new VersionFolderPathResolver(packagesRootPath).GetInstallPath(packageId.ToString(), version);
await nugetPackageDownloader.ExtractPackageAsync(packagePath, new DirectoryPath(nupkgDir));
return version;
}
private static void CreateAssetFile(
PackageId packageId,
NuGetVersion version,
DirectoryPath packagesRootPath,
DirectoryPath assetFileDirectory,
string runtimeJsonGraph,
string targetFramework = null
)
{
// To get runtimeGraph:
var runtimeGraph = JsonRuntimeFormat.ReadRuntimeGraph(runtimeJsonGraph);
// Create ManagedCodeConventions:
var conventions = new ManagedCodeConventions(runtimeGraph);
// Create LockFileTargetLibrary
var lockFileLib = new LockFileTargetLibrary()
{
Name = packageId.ToString(),
Version = version,
Type = LibraryType.Package,
PackageType = [PackageType.DotnetTool]
};
// Create NuGetv3LocalRepository
NuGetv3LocalRepository localRepository = new(packagesRootPath.Value);
var package = localRepository.FindPackage(packageId.ToString(), version);
var collection = new ContentItemCollection();
collection.Load(package.Files);
// Create criteria
var managedCriteria = new List<SelectionCriteria>(1);
// Use major.minor version of currently running version of .NET
NuGetFramework currentTargetFramework;
if(targetFramework != null)
{
currentTargetFramework = NuGetFramework.Parse(targetFramework);
}
else
{
currentTargetFramework = new NuGetFramework(FrameworkConstants.FrameworkIdentifiers.NetCoreApp, new Version(Environment.Version.Major, Environment.Version.Minor));
}
var standardCriteria = conventions.Criteria.ForFrameworkAndRuntime(
currentTargetFramework,
RuntimeInformation.RuntimeIdentifier);
managedCriteria.Add(standardCriteria);
// Create asset file
if (lockFileLib.PackageType.Contains(PackageType.DotnetTool))
{
AddToolsAssets(conventions, lockFileLib, collection, managedCriteria);
}
var lockFile = new LockFile();
var lockFileTarget = new LockFileTarget()
{
TargetFramework = currentTargetFramework,
RuntimeIdentifier = RuntimeInformation.RuntimeIdentifier
};
lockFileTarget.Libraries.Add(lockFileLib);
lockFile.Targets.Add(lockFileTarget);
new LockFileFormat().Write(Path.Combine(assetFileDirectory.Value, "project.assets.json"), lockFile);
}
public NuGetVersion GetNuGetVersion(
PackageLocation packageLocation,
PackageId packageId,
VerbosityOptions verbosity,
VersionRange versionRange = null,
bool isGlobalTool = false,
RestoreActionConfig restoreActionConfig = null)
{
ILogger nugetLogger = new NullLogger();
if (verbosity.IsDetailedOrDiagnostic())
{
nugetLogger = new NuGetConsoleLogger();
}
if (versionRange == null)
{
var versionString = "*";
versionRange = VersionRange.Parse(versionString);
}
var nugetPackageDownloader = new NuGetPackageDownloader.NuGetPackageDownloader(
packageInstallDir: isGlobalTool ? _globalToolStageDir : _localToolDownloadDir,
verboseLogger: nugetLogger,
shouldUsePackageSourceMapping: true,
verbosityOptions: verbosity,
restoreActionConfig: restoreActionConfig);
var packageSourceLocation = new PackageSourceLocation(
nugetConfig: packageLocation.NugetConfig,
rootConfigDirectory: packageLocation.RootConfigDirectory,
sourceFeedOverrides: packageLocation.SourceFeedOverrides,
additionalSourceFeeds: packageLocation.AdditionalFeeds);
return nugetPackageDownloader.GetBestPackageVersionAsync(packageId, versionRange, packageSourceLocation).GetAwaiter().GetResult();
}
}