Skip to content

Commit fac59a8

Browse files
committed
Updates to interface to support tool exec
GetNuGetVersion now returns the package source, and add method to check if tool is downloaded
1 parent a4ee7d1 commit fac59a8

File tree

6 files changed

+63
-24
lines changed

6 files changed

+63
-24
lines changed

src/Cli/dotnet/Commands/Tool/Install/ToolInstallGlobalOrToolPathCommand.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,8 @@ private NuGetVersion GetBestMatchNugetVersion(PackageId packageId, VersionRange
241241
packageId: packageId,
242242
versionRange: versionRange,
243243
verbosity: _verbosity,
244-
isGlobalTool: true,
245244
restoreActionConfig: restoreActionConfig
246-
);
245+
).version;
247246
}
248247

249248
private static bool ToolVersionAlreadyInstalled(IToolPackage oldPackageNullable, NuGetVersion nuGetVersion)

src/Cli/dotnet/ToolPackage/IToolPackageDownloader.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#nullable disable
55

66
using Microsoft.DotNet.Cli.NuGetPackageDownloader;
7+
using NuGet.Configuration;
78
using NuGet.Versioning;
89

910
namespace Microsoft.DotNet.Cli.ToolPackage;
@@ -21,12 +22,16 @@ IToolPackage InstallPackage(PackageLocation packageLocation,
2122
RestoreActionConfig restoreActionConfig = null
2223
);
2324

24-
NuGetVersion GetNuGetVersion(
25+
(NuGetVersion version, PackageSource source) GetNuGetVersion(
2526
PackageLocation packageLocation,
2627
PackageId packageId,
2728
VerbosityOptions verbosity,
2829
VersionRange versionRange = null,
29-
bool isGlobalTool = false,
3030
RestoreActionConfig restoreActionConfig = null
3131
);
32+
33+
bool IsLocalToolDownloaded(
34+
PackageId packageId,
35+
NuGetVersion packageVersion,
36+
string targetFramework = null);
3237
}

src/Cli/dotnet/ToolPackage/ToolPackageDownloaderBase.cs

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Newtonsoft.Json.Linq;
1111
using NuGet.Client;
1212
using NuGet.Commands;
13+
using NuGet.Commands.Restore;
1314
using NuGet.Common;
1415
using NuGet.Configuration;
1516
using NuGet.ContentModel;
@@ -272,10 +273,50 @@ protected virtual void DownloadTool(
272273
DownloadAndExtractPackage(packageId, nugetPackageDownloader, packageDownloadDir.Value, packageVersion, packageSourceLocation, includeUnlisted: givenSpecificVersion);
273274
}
274275

275-
CreateAssetFile(packageId, packageVersion, packageDownloadDir, Path.Combine(assetFileDirectory.Value, "project.assets.json"), _runtimeJsonPath, targetFramework);
276+
CreateAssetFile(packageId, packageVersion, packageDownloadDir, Path.Combine(assetFileDirectory.Value, ToolPackageInstance.AssetsFileName), _runtimeJsonPath, targetFramework);
276277

277278
// Also download RID-specific package if needed
279+
var ridSpecificPackage = ResolveRidSpecificPackage(packageId, packageVersion, packageDownloadDir, assetFileDirectory);
280+
if (ridSpecificPackage != null)
281+
{
282+
if (!IsPackageInstalled(new PackageId(ridSpecificPackage.Id), ridSpecificPackage.Version, packageDownloadDir.Value))
283+
{
284+
DownloadAndExtractPackage(new PackageId(ridSpecificPackage.Id), nugetPackageDownloader, packageDownloadDir.Value, ridSpecificPackage.Version, packageSourceLocation, includeUnlisted: true);
285+
}
286+
287+
CreateAssetFile(new PackageId(ridSpecificPackage.Id), ridSpecificPackage.Version, packageDownloadDir, Path.Combine(assetFileDirectory.Value, ToolPackageInstance.RidSpecificPackageAssetsFileName), _runtimeJsonPath, targetFramework);
288+
}
289+
}
290+
291+
public bool IsLocalToolDownloaded(
292+
PackageId packageId,
293+
NuGetVersion packageVersion,
294+
string? targetFramework = null)
295+
{
296+
if (!IsPackageInstalled(packageId, packageVersion, _localToolDownloadDir.Value))
297+
{
298+
return false;
299+
}
300+
CreateAssetFile(packageId, packageVersion, _localToolDownloadDir, Path.Combine(_localToolAssetDir.Value, ToolPackageInstance.AssetsFileName), _runtimeJsonPath, targetFramework);
301+
302+
var ridSpecificPackage = ResolveRidSpecificPackage(packageId, packageVersion, _localToolDownloadDir, _localToolAssetDir);
303+
if (ridSpecificPackage != null)
304+
{
305+
return IsPackageInstalled(new PackageId(ridSpecificPackage.Id), ridSpecificPackage.Version, _localToolDownloadDir.Value);
306+
}
307+
else
308+
{
309+
return true;
310+
}
311+
}
312+
313+
private PackageIdentity? ResolveRidSpecificPackage(PackageId packageId,
314+
NuGetVersion packageVersion,
315+
DirectoryPath packageDownloadDir,
316+
DirectoryPath assetFileDirectory)
317+
{
278318
var toolConfiguration = GetToolConfiguration(packageId, packageDownloadDir, assetFileDirectory);
319+
279320
if (toolConfiguration.RidSpecificPackages?.Any() == true)
280321
{
281322
var runtimeGraph = JsonRuntimeFormat.ReadRuntimeGraph(_runtimeJsonPath);
@@ -288,16 +329,11 @@ protected virtual void DownloadTool(
288329

289330
var resolvedPackage = toolConfiguration.RidSpecificPackages[bestRuntimeIdentifier];
290331

291-
if (!IsPackageInstalled(new PackageId(resolvedPackage.Id), resolvedPackage.Version, packageDownloadDir.Value))
292-
{
293-
DownloadAndExtractPackage(new PackageId(resolvedPackage.Id), nugetPackageDownloader, packageDownloadDir.Value, resolvedPackage.Version, packageSourceLocation, includeUnlisted: true);
294-
}
295-
296-
CreateAssetFile(new PackageId(resolvedPackage.Id), resolvedPackage.Version, packageDownloadDir, Path.Combine(assetFileDirectory.Value, ToolPackageInstance.RidSpecificPackageAssetsFileName), _runtimeJsonPath, targetFramework);
332+
return resolvedPackage;
297333
}
298-
}
299334

300-
335+
return null;
336+
}
301337

302338
protected void UpdateRuntimeConfig(
303339
ToolPackageInstance toolPackageInstance
@@ -320,12 +356,11 @@ ToolPackageInstance toolPackageInstance
320356
}
321357
}
322358

323-
public virtual NuGetVersion GetNuGetVersion(
359+
public virtual (NuGetVersion version, PackageSource source) GetNuGetVersion(
324360
PackageLocation packageLocation,
325361
PackageId packageId,
326362
VerbosityOptions verbosity,
327363
VersionRange? versionRange = null,
328-
bool isGlobalTool = false,
329364
RestoreActionConfig? restoreActionConfig = null)
330365
{
331366
if (versionRange == null)
@@ -346,6 +381,6 @@ public virtual NuGetVersion GetNuGetVersion(
346381
additionalSourceFeeds: packageLocation.AdditionalFeeds,
347382
basePath: _currentWorkingDirectory);
348383

349-
return nugetPackageDownloader.GetBestPackageVersionAsync(packageId, versionRange, packageSourceLocation).GetAwaiter().GetResult();
384+
return nugetPackageDownloader.GetBestPackageVersionAndSourceAsync(packageId, versionRange, packageSourceLocation).GetAwaiter().GetResult();
350385
}
351386
}

src/Cli/dotnet/ToolPackage/ToolPackageInstance.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ internal class ToolPackageInstance : IToolPackage
3737

3838
private IFileSystem _fileSystem;
3939

40-
private const string AssetsFileName = "project.assets.json";
40+
public const string AssetsFileName = "project.assets.json";
4141
public const string RidSpecificPackageAssetsFileName = "project.assets.ridpackage.json";
4242
private const string ToolSettingsFileName = "DotnetToolSettings.xml";
4343

test/Microsoft.DotNet.PackageInstall.Tests/ToolPackageDownloaderTests.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,7 @@ public void GivenAllButNoPackageVersionItReturnLatestStableVersion(bool testMock
167167
var package = downloader.GetNuGetVersion(
168168
new PackageLocation(nugetConfig: testDir.WithFile("NuGet.config")),
169169
packageId: TestPackageId,
170-
verbosity: TestVerbosity,
171-
isGlobalTool: true);
170+
verbosity: TestVerbosity).version;
172171

173172
package.OriginalVersion.Should().Be(TestPackageVersion);
174173
}
@@ -194,8 +193,7 @@ public void GivenASpecificVersionGetCorrectVersion(bool testMockBehaviorIsInSync
194193
additionalFeeds: new[] { emptySource }),
195194
packageId: TestPackageId,
196195
verbosity: TestVerbosity,
197-
versionRange: VersionRange.Parse(requestedVersion),
198-
isGlobalTool: true);
196+
versionRange: VersionRange.Parse(requestedVersion)).version;
199197

200198
package.OriginalVersion.Should().Be(expectedVersion);
201199
}

test/Microsoft.DotNet.Tools.Tests.ComponentMocks/ToolPackageDownloaderMock.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Microsoft.DotNet.Cli.ToolPackage;
1111
using Microsoft.DotNet.Cli.Utils;
1212
using Microsoft.Extensions.EnvironmentAbstractions;
13+
using NuGet.Configuration;
1314
using NuGet.Frameworks;
1415
using NuGet.Versioning;
1516

@@ -309,12 +310,11 @@ private static bool ExcludeOtherFeeds(FilePath nugetConfig, MockFeed f)
309310
|| (f.Type == MockFeedType.ExplicitNugetConfig && f.Uri == nugetConfig.Value);
310311
}
311312

312-
public NuGetVersion GetNuGetVersion(
313+
public (NuGetVersion version, PackageSource source) GetNuGetVersion(
313314
PackageLocation packageLocation,
314315
PackageId packageId,
315316
VerbosityOptions verbosity,
316317
VersionRange versionRange = null,
317-
bool isGlobalTool = false,
318318
RestoreActionConfig restoreActionConfig = null)
319319
{
320320
versionRange = VersionRange.Parse(versionRange?.OriginalString ?? "*");
@@ -331,9 +331,11 @@ public NuGetVersion GetNuGetVersion(
331331
packageLocation.RootConfigDirectory,
332332
packageLocation.SourceFeedOverrides);
333333

334-
return NuGetVersion.Parse(feedPackage.Version);
334+
return (NuGetVersion.Parse(feedPackage.Version), new PackageSource("http://mock-feed", "MockFeed"));
335335
}
336336

337+
public bool IsLocalToolDownloaded(PackageId packageId, NuGetVersion packageVersion, string targetFramework = null) => throw new NotImplementedException();
338+
337339
private class TestToolPackage : IToolPackage
338340
{
339341
public PackageId Id { get; set; }

0 commit comments

Comments
 (0)