dotnet-installer/src/Microsoft.DotNet.Cli.Utils/CommandResolution/PackagedCommandSpecFactoryWithCliRuntime.cs

38 lines
1.2 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.DotNet.Tools.Common;
using NuGet.Packaging;
using NuGet.ProjectModel;
namespace Microsoft.DotNet.Cli.Utils
{
public class PackagedCommandSpecFactoryWithCliRuntime : PackagedCommandSpecFactory
{
public PackagedCommandSpecFactoryWithCliRuntime() : base(AddAditionalParameters)
{
}
private static void AddAditionalParameters(string commandPath, IList<string> arguments)
{
if(PrefersCliRuntime(commandPath))
{
arguments.Add("--fx-version");
arguments.Add(new Muxer().SharedFxVersion);
}
}
private static bool PrefersCliRuntime(string commandPath)
{
var libTFMPackageDirectory = Path.GetDirectoryName(commandPath);
var packageDirectory = Path.Combine(libTFMPackageDirectory, "..", "..");
var preferCliRuntimePath = Path.Combine(packageDirectory, "prefercliruntime");
Reporter.Verbose.WriteLine(
$"packagedcommandspecfactory: Looking for prefercliruntime file at `{preferCliRuntimePath}`");
return File.Exists(preferCliRuntimePath);
}
}
}