Skip to content

Commit 284c974

Browse files
committed
Load dotnet.dll
1 parent 9cc867e commit 284c974

3 files changed

Lines changed: 18 additions & 5 deletions

File tree

src/NuGet.Core/NuGet.CommandLine.XPlat/NuGetCommands.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55

66
using System;
77
using System.CommandLine;
8+
using System.IO;
89
using System.Linq;
10+
using System.Reflection;
911
using Microsoft.Build.Construction;
1012
using Microsoft.Build.Evaluation;
1113
using NuGet.CommandLine.XPlat.Commands.Package.PackageDownload;
@@ -18,8 +20,6 @@ namespace NuGet.CommandLine.XPlat;
1820
/// </summary>
1921
public static class NuGetCommands
2022
{
21-
public static IVirtualProjectBuilder? VirtualProjectBuilder { get; set; }
22-
2323
/// <summary>
2424
/// <para>Adds NuGet's dotnet CLI commands to the dotnet CLI RootCommand object</para>
2525
/// </summary>
@@ -57,4 +57,19 @@ public interface IVirtualProjectBuilder
5757
bool IsValidEntryPointPath(string entryPointFilePath);
5858

5959
ProjectRootElement CreateProjectRootElement(string entryPointFilePath, ProjectCollection projectCollection);
60+
61+
internal static IVirtualProjectBuilder? TryLoad()
62+
{
63+
var assemblyPath = Path.Join(AppContext.BaseDirectory, "dotnet.dll");
64+
65+
if (!File.Exists(assemblyPath))
66+
{
67+
return null;
68+
}
69+
70+
var type = Assembly.LoadFile(assemblyPath)
71+
.GetExportedTypes()
72+
.FirstOrDefault(static t => t.IsAssignableTo(typeof(IVirtualProjectBuilder)));
73+
return type != null ? (IVirtualProjectBuilder?)Activator.CreateInstance(type) : null;
74+
}
6075
}

src/NuGet.Core/NuGet.CommandLine.XPlat/PublicAPI.Unshipped.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,3 @@
22
NuGet.CommandLine.XPlat.IVirtualProjectBuilder
33
NuGet.CommandLine.XPlat.IVirtualProjectBuilder.CreateProjectRootElement(string! entryPointFilePath, Microsoft.Build.Evaluation.ProjectCollection! projectCollection) -> Microsoft.Build.Construction.ProjectRootElement!
44
NuGet.CommandLine.XPlat.IVirtualProjectBuilder.IsValidEntryPointPath(string! entryPointFilePath) -> bool
5-
static NuGet.CommandLine.XPlat.NuGetCommands.VirtualProjectBuilder.get -> NuGet.CommandLine.XPlat.IVirtualProjectBuilder?
6-
static NuGet.CommandLine.XPlat.NuGetCommands.VirtualProjectBuilder.set -> void

src/NuGet.Core/NuGet.CommandLine.XPlat/Utility/MSBuildAPIUtility.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1039,7 +1039,7 @@ private static IEnumerable<string> GetProjectFrameworks(Project project)
10391039

10401040
private static ProjectRootElement TryOpenProjectRootElement(string filename)
10411041
{
1042-
var virtualProjectBuilder = NuGetCommands.VirtualProjectBuilder;
1042+
var virtualProjectBuilder = IVirtualProjectBuilder.TryLoad();
10431043
if (virtualProjectBuilder != null && virtualProjectBuilder.IsValidEntryPointPath(filename))
10441044
{
10451045
return virtualProjectBuilder.CreateProjectRootElement(filename, ProjectCollection.GlobalProjectCollection);

0 commit comments

Comments
 (0)