Making Project Tools use the CLI shared runtime if they have the prefercliruntime in the root of their package. This allows for project tools to not have to change when a new runtime comes up as long as they are compatible with the runtime that the CLI is using.

This commit is contained in:
Livar Cunha 2016-11-22 13:56:13 -08:00
parent da0e7b744b
commit dc87680337
11 changed files with 199 additions and 6 deletions

View file

@ -0,0 +1,12 @@
using System;
namespace ConsoleApplication
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello I prefer the cli runtime World!");
}
}
}

View file

@ -0,0 +1,42 @@
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" />
<PropertyGroup>
<TargetFramework>netcoreapp1.0</TargetFramework>
<OutputType>Exe</OutputType>
</PropertyGroup>
<ItemGroup>
<Compile Include="**\*.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="$(OutputPath)\$(AssemblyName).runtimeconfig.json">
<Pack>true</Pack>
<PackagePath>lib\$(TargetFramework)</PackagePath>
</Content>
<Content Include="prefercliruntime">
<Pack>true</Pack>
<PackagePath>\prefercliruntime</PackagePath>
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Sdk">
<Version>1.0.0-alpha-20161104-2</Version>
<PrivateAssets>All</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp1.0' ">
<PackageReference Include="Microsoft.NETCore.App">
<Version>1.1.0</Version>
</PackageReference>
</ItemGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<DefineConstants>$(DefineConstants);RELEASE</DefineConstants>
</PropertyGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View file

@ -25,6 +25,9 @@
<DotNetCliToolReference Include="dotnet-portable">
<Version>1.0.0</Version>
</DotNetCliToolReference>
<DotNetCliToolReference Include="dotnet-prefercliruntime">
<Version>1.0.0</Version>
</DotNetCliToolReference>
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Microsoft.CSharp.targets" />

View file

@ -155,6 +155,16 @@
<Clean>True</Clean>
<Frameworks>netcoreapp1.0</Frameworks>
</BaseTestPackageProject>
<BaseTestPackageProject Include="TestAssets/TestPackages/dotnet-prefercliruntime">
<Name>dotnet-prefercliruntime</Name>
<ProjectName>dotnet-prefercliruntime.csproj</ProjectName>
<IsTool>True</IsTool>
<IsApplicable>True</IsApplicable>
<VersionPrefix>1.0.0</VersionPrefix>
<VersionSuffix></VersionSuffix>
<Clean>True</Clean>
<Frameworks>netcoreapp1.0</Frameworks>
</BaseTestPackageProject>
<BaseTestPackageProject Include="TestAssets/TestPackages/ToolWithOutputName">
<Name>dotnet-tool-with-output-name</Name>
<ProjectName>ToolWithOutputName.csproj</ProjectName>

View file

@ -12,7 +12,7 @@ namespace Microsoft.DotNet.Cli.Utils
public static CompositeCommandResolver Create()
{
var environment = new EnvironmentProvider();
var packagedCommandSpecFactory = new PackagedCommandSpecFactory();
var packagedCommandSpecFactory = new PackagedCommandSpecFactoryWithCliRuntime();
var publishedPathCommandSpecFactory = new PublishPathCommandSpecFactory();
var platformCommandSpecFactory = default(IPlatformCommandSpecFactory);

View file

@ -10,6 +10,13 @@ namespace Microsoft.DotNet.Cli.Utils
{
public class PackagedCommandSpecFactory : IPackagedCommandSpecFactory
{
private Action<string, IList<string>> _addAdditionalArguments;
internal PackagedCommandSpecFactory(Action<string, IList<string>> addAdditionalArguments = null)
{
_addAdditionalArguments = addAdditionalArguments;
}
public CommandSpec CreateCommandSpecFromLibrary(
LockFileTargetLibrary toolLibrary,
string commandName,
@ -120,6 +127,11 @@ namespace Microsoft.DotNet.Cli.Utils
arguments.Add("--additionalprobingpath");
arguments.Add(nugetPackagesRoot);
if(_addAdditionalArguments != null)
{
_addAdditionalArguments(commandPath, arguments);
}
arguments.Add(commandPath);
arguments.AddRange(commandArguments);

View file

@ -0,0 +1,38 @@
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);
}
}
}

View file

@ -11,6 +11,14 @@ namespace Microsoft.DotNet.Cli.Utils
private string _muxerPath;
internal string SharedFxVersion
{
get
{
return new DirectoryInfo(Path.GetDirectoryName(GetDataFromAppDomain("FX_DEPS_FILE"))).Name;
}
}
public string MuxerPath
{
get

View file

@ -69,6 +69,25 @@ namespace Microsoft.DotNet.Tests.EndToEnd
.HaveStdOutContaining("Hello Portable World!");;
}
[Fact]
public void ItCanRunToolsThatPrefersTheCliRuntime()
{
var testInstance = TestAssets.Get("MSBuildTestApp")
.CreateInstance()
.WithSourceFiles()
.WithRestoreFiles();
var testProjectDirectory = testInstance.Root;
new DotnetCommand()
.WithWorkingDirectory(testInstance.Root)
.ExecuteWithCapturedOutput("prefercliruntime")
.Should()
.Pass()
.And
.HaveStdOutContaining("Hello I prefer the cli runtime World!");;
}
[Fact]
public void ItCanRunAToolThatInvokesADependencyToolInACSProj()
{

View file

@ -17,7 +17,8 @@ namespace Microsoft.DotNet.Tests
{
public class GivenAProjectToolsCommandResolver : TestBase
{
private static readonly NuGetFramework s_toolPackageFramework = FrameworkConstants.CommonFrameworks.NetCoreApp10;
private static readonly NuGetFramework s_toolPackageFramework =
FrameworkConstants.CommonFrameworks.NetCoreApp10;
private const string TestProjectName = "AppWithToolDependency";
@ -260,16 +261,64 @@ namespace Microsoft.DotNet.Tests
File.Delete(depsJsonFile);
}
private ProjectToolsCommandResolver SetupProjectToolsCommandResolver(
IPackagedCommandSpecFactory packagedCommandSpecFactory = null)
[Fact]
public void It_adds_fx_version_as_a_param_when_the_tool_has_the_prefercliruntime_file()
{
var projectToolsCommandResolver = SetupProjectToolsCommandResolver();
var testInstance = TestAssetsManager
.CreateTestInstance("MSBuildTestApp")
.WithNuGetMSBuildFiles()
.WithLockFiles();
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "dotnet-prefercliruntime",
CommandArguments = null,
ProjectDirectory = testInstance.Path
};
var result = projectToolsCommandResolver.Resolve(commandResolverArguments);
result.Should().NotBeNull();
result.Args.Should().Contain("-fx-version 1.0.1");
}
[Fact]
public void It_does_not_add_fx_version_as_a_param_when_the_tool_does_not_have_the_prefercliruntime_file()
{
var projectToolsCommandResolver = SetupProjectToolsCommandResolver();
var testInstance = TestAssetsManager
.CreateTestInstance(TestProjectName)
.WithNuGetMSBuildFiles()
.WithLockFiles();
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "dotnet-portable",
CommandArguments = null,
ProjectDirectory = testInstance.Path
};
var result = projectToolsCommandResolver.Resolve(commandResolverArguments);
result.Should().NotBeNull();
result.Args.Should().NotContain("-fx-version");
}
private ProjectToolsCommandResolver SetupProjectToolsCommandResolver()
{
Environment.SetEnvironmentVariable(
Constants.MSBUILD_EXE_PATH,
Path.Combine(new RepoDirectoriesProvider().Stage2Sdk, "MSBuild.dll"));
packagedCommandSpecFactory = packagedCommandSpecFactory ?? new PackagedCommandSpecFactory();
var packagedCommandSpecFactory = new PackagedCommandSpecFactoryWithCliRuntime();
var projectToolsCommandResolver = new ProjectToolsCommandResolver(packagedCommandSpecFactory, new EnvironmentProvider());
var projectToolsCommandResolver =
new ProjectToolsCommandResolver(packagedCommandSpecFactory, new EnvironmentProvider());
return projectToolsCommandResolver;
}