Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ internal partial class ProjectRetargetHandler
{
internal class RetargetSDKDescription : TargetDescriptionBase
{
internal static RetargetSDKDescription Create(string sdkVersion)
internal static RetargetSDKDescription Create(string sdkVersion, string arch)
{
return new RetargetSDKDescription(sdkVersion);
return new RetargetSDKDescription(sdkVersion, arch);
}

private RetargetSDKDescription(string sdkVersion) : base(
private RetargetSDKDescription(string sdkVersion, string arch) : base(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider taking the Architecture enum here so that it's clear what domain of values are valid. Then do the conversion to lower case whatever internally when formatting a string.

targetId: Guid.NewGuid(),
displayName: $".NET SDK {sdkVersion}",
order: 1,
supported: true,
description: string.Format(VSResources.RetargetingSDKDescription, sdkVersion),
canRetarget: true, // this means we want to show this option in the retarget dialog
guidanceLink: $"https://dotnet.microsoft.com/en-us/download/dotnet/thank-you/sdk-{sdkVersion}-windows-x64-installer")
guidanceLink: $"https://dotnet.microsoft.com/en-us/download/dotnet/thank-you/sdk-{sdkVersion}-windows-{arch}-installer")
{
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ internal sealed partial class ProjectRetargetHandler : IProjectRetargetHandler,
private readonly IProjectThreadingService _projectThreadingService;
private readonly IVsService<SVsTrackProjectRetargeting, IVsTrackProjectRetargeting2> _projectRetargetingService;
private readonly IVsService<SVsSolution, IVsSolution> _solutionService;
private readonly IEnvironment _environment;
private readonly IDotNetEnvironment _dotnetEnvironment;

private Guid _currentSdkDescriptionId = Guid.Empty;
Expand All @@ -31,13 +32,15 @@ public ProjectRetargetHandler(
IProjectThreadingService projectThreadingService,
IVsService<SVsTrackProjectRetargeting, IVsTrackProjectRetargeting2> projectRetargetingService,
IVsService<SVsSolution, IVsSolution> solutionService,
IEnvironment environment,
IDotNetEnvironment dotnetEnvironment)
{
_releasesProvider = releasesProvider;
_fileSystem = fileSystem;
_projectThreadingService = projectThreadingService;
_projectRetargetingService = projectRetargetingService;
_solutionService = solutionService;
_environment = environment;
_dotnetEnvironment = dotnetEnvironment;
}

Expand Down Expand Up @@ -99,21 +102,23 @@ public Task RetargetAsync(TextWriter outputLogger, RetargetOptions options, IPro
return null;
}

string architecture = _environment.ProcessArchitecture.ToString().ToLowerInvariant();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider reusing:

private static string GetArchitectureSubKey(Architecture architecture)
{
return architecture switch
{
Architecture.X86 => "x86",
Architecture.X64 => "x64",
Architecture.Arm => "arm",
Architecture.Arm64 => "arm64",
_ => architecture.ToString().ToLower()
};

Avoids an allocation.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

refactored out to an extensions method and renamed


if (_currentSdkDescriptionId == Guid.Empty)
{
// register the current and retarget versions, note there is a bug in the current implementation
// ultimately we will need to just set retarget. Right now, we need to register two
// targets, we want to create two distinct ones, as the bug workaround requires different guids

IVsProjectTargetDescription currentSdkDescription = RetargetSDKDescription.Create(retargetVersion.ToString()); // this won't be needed
IVsProjectTargetDescription currentSdkDescription = RetargetSDKDescription.Create(retargetVersion.ToString(), architecture); // this won't be needed
retargetingService.RegisterProjectTarget(currentSdkDescription); // this wont be needed.
_currentSdkDescriptionId = currentSdkDescription.TargetId;
}

if (_sdkRetargetId == Guid.Empty)
{
IVsProjectTargetDescription retargetSdkDescription = RetargetSDKDescription.Create(retargetVersion.ToString()); // this won't be needed
retargetingService.RegisterProjectTarget(retargetSdkDescription); // this wont be needed.
IVsProjectTargetDescription retargetSdkDescription = RetargetSDKDescription.Create(retargetVersion.ToString(), architecture);
retargetingService.RegisterProjectTarget(retargetSdkDescription);
_sdkRetargetId = retargetSdkDescription.TargetId;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE.md file in the project root for more information.

using System.Runtime.InteropServices;
using Microsoft.VisualStudio.IO;
using Microsoft.VisualStudio.ProjectSystem.VS.Setup;
using Microsoft.VisualStudio.Shell.Interop;
Expand Down Expand Up @@ -240,8 +241,11 @@ public async Task CheckForRetargetAsync_FindsGlobalJsonInParentDirectory()
Assert.NotNull(result);
}

[Fact]
public async Task CheckForRetargetAsync_RegistersTargetDescriptions()
[Theory]
[InlineData("arm64")]
[InlineData("x86")]
[InlineData("x64")]
public async Task CheckForRetargetAsync_RegistersTargetDescriptions(string arch)
{
var fileSystem = new IFileSystemMock();
var solution = CreateSolutionWithDirectory(@"C:\Solution");
Expand All @@ -255,22 +259,35 @@ public async Task CheckForRetargetAsync_RegistersTargetDescriptions()
var dotnetEnvironment = Mock.Of<IDotNetEnvironment>(
s => s.IsSdkInstalled("8.0.200") == false);

var environment = new IEnvironmentMock();
environment.ProcessArchitecture = (Architecture)Enum.Parse(typeof(Architecture), arch, ignoreCase: true);

IVsProjectTargetDescription? capturedDescription = null;

var retargetingService = new Mock<IVsTrackProjectRetargeting2>();
retargetingService.Setup(r => r.RegisterProjectTarget(It.IsAny<IVsProjectTargetDescription>()))
.Callback<IVsProjectTargetDescription>(desc => capturedDescription ??= desc) // capture the first call, as thats where the link is
.Returns(HResult.OK);

var handler = CreateInstance(
fileSystem: fileSystem,
solution: solution,
releasesProvider: releasesProvider,
dotnetEnvironment: dotnetEnvironment,
environment: environment.Object,
trackProjectRetargeting: retargetingService.Object);

var result = await handler.CheckForRetargetAsync(RetargetCheckOptions.ProjectLoad);

Assert.NotNull(result);
Assert.NotNull(capturedDescription);
var guidanceLink = capturedDescription.GetProperty((uint)__VSPTDPROPID.VSPTDPROPID_ProjectRetargetingGuidanceLink) as string;
Assert.NotNull(guidanceLink);

// Verify RegisterProjectTarget was called twice (workaround for bug)
retargetingService.Verify(r => r.RegisterProjectTarget(It.IsAny<IVsProjectTargetDescription>()), Times.Exactly(2));
// verify that we have the correct architecture in the link
Assert.Contains(arch, guidanceLink!, StringComparison.OrdinalIgnoreCase);
}

[Fact]
Expand Down Expand Up @@ -398,11 +415,13 @@ private static ProjectRetargetHandler CreateInstance(
IProjectThreadingService? threadingService = null,
IVsTrackProjectRetargeting2? trackProjectRetargeting = null,
IVsSolution? solution = null,
IEnvironment? environment = null,
IDotNetEnvironment? dotnetEnvironment = null)
{
releasesProvider ??= Mock.Of<IDotNetReleasesProvider>();
fileSystem ??= new IFileSystemMock();
threadingService ??= IProjectThreadingServiceFactory.Create();
environment ??= Mock.Of<IEnvironment>();

var retargetingService = IVsServiceFactory.Create<SVsTrackProjectRetargeting, IVsTrackProjectRetargeting2>(trackProjectRetargeting);
var solutionService = IVsServiceFactory.Create<SVsSolution, IVsSolution>(solution);
Expand All @@ -415,6 +434,7 @@ private static ProjectRetargetHandler CreateInstance(
threadingService,
retargetingService,
solutionService,
environment,
dotnetEnvironment);
}

Expand Down