Merge pull request #2297 from Sridhar-MS/fix-2126

Execute project dependency command from output directory for Desktop.
This commit is contained in:
Sridhar Periyasamy 2016-04-07 13:46:30 -07:00
commit 5768f4c973
5 changed files with 143 additions and 7 deletions

View file

@ -1,4 +1,5 @@
using System;
using System.Reflection;
namespace ConsoleApplication
{
@ -11,6 +12,8 @@ namespace ConsoleApplication
#elif NETSTANDARD1_5
Console.WriteLine($"Hello {string.Join(" ", args)} From .NETStandardApp,Version=v1.5");
#endif
var currentAssemblyPath = typeof(ConsoleApplication.Program).GetTypeInfo().Assembly.Location;
Console.WriteLine($"Current Assembly Directory - {currentAssemblyPath}");
}
}
}

View file

@ -23,6 +23,9 @@
// command loaded from rooted path
RootedPath,
// command loaded from project build output path
OutputPath,
// command not found
None
}

View file

@ -0,0 +1,96 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using Microsoft.DotNet.ProjectModel;
using Microsoft.DotNet.ProjectModel.Graph;
using Microsoft.Extensions.PlatformAbstractions;
using NuGet.Frameworks;
using NuGet.Packaging;
namespace Microsoft.DotNet.Cli.Utils
{
public class OutputPathCommandResolver : AbstractPathBasedCommandResolver
{
public OutputPathCommandResolver(IEnvironmentProvider environment,
IPlatformCommandSpecFactory commandSpecFactory) : base(environment, commandSpecFactory)
{ }
internal override string ResolveCommandPath(CommandResolverArguments commandResolverArguments)
{
if (commandResolverArguments.Framework == null
|| commandResolverArguments.ProjectDirectory == null
|| commandResolverArguments.Configuration == null
|| commandResolverArguments.CommandName == null)
{
return null;
}
return ResolveFromProjectOutput(
commandResolverArguments.ProjectDirectory,
commandResolverArguments.Framework,
commandResolverArguments.Configuration,
commandResolverArguments.CommandName,
commandResolverArguments.CommandArguments.OrEmptyIfNull(),
commandResolverArguments.OutputPath,
commandResolverArguments.BuildBasePath);
}
private string ResolveFromProjectOutput(
string projectDirectory,
NuGetFramework framework,
string configuration,
string commandName,
IEnumerable<string> commandArguments,
string outputPath,
string buildBasePath)
{
var projectContext = GetProjectContextFromDirectory(
projectDirectory,
framework);
if (projectContext == null)
{
return null;
}
var buildOutputPath = projectContext.GetOutputPaths(configuration, buildBasePath, outputPath).RuntimeFiles.BasePath;
return _environment.GetCommandPathFromRootPath(buildOutputPath, commandName);
}
private ProjectContext GetProjectContextFromDirectory(string directory, NuGetFramework framework)
{
if (directory == null || framework == null)
{
return null;
}
var projectRootPath = directory;
if (!File.Exists(Path.Combine(projectRootPath, Project.FileName)))
{
return null;
}
var projectContext = ProjectContext.Create(
projectRootPath,
framework,
PlatformServices.Default.Runtime.GetAllCandidateRuntimeIdentifiers());
if (projectContext.RuntimeIdentifier == null)
{
return null;
}
return projectContext;
}
internal override CommandResolutionStrategy GetCommandResolutionStrategy()
{
return CommandResolutionStrategy.OutputPath;
}
}
}

View file

@ -2,6 +2,7 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.Collections.Generic;
using Microsoft.Extensions.PlatformAbstractions;
using NuGet.Frameworks;
namespace Microsoft.DotNet.Cli.Utils
@ -76,7 +77,7 @@ namespace Microsoft.DotNet.Cli.Utils
ProjectDirectory = projectDirectory
};
var commandResolver = GetProjectDependenciesCommandResolver();
var commandResolver = GetProjectDependenciesCommandResolver(framework);
var commandSpec = commandResolver.Resolve(commandResolverArguments);
if (commandSpec == null)
@ -87,12 +88,29 @@ namespace Microsoft.DotNet.Cli.Utils
return commandSpec;
}
private ICommandResolver GetProjectDependenciesCommandResolver()
private ICommandResolver GetProjectDependenciesCommandResolver(NuGetFramework framework)
{
var environment = new EnvironmentProvider();
var packagedCommandSpecFactory = new PackagedCommandSpecFactory();
return new ProjectDependenciesCommandResolver(environment, packagedCommandSpecFactory);
if (framework.IsDesktop())
{
IPlatformCommandSpecFactory platformCommandSpecFactory = null;
if (PlatformServices.Default.Runtime.OperatingSystemPlatform == Platform.Windows)
{
platformCommandSpecFactory = new WindowsExePreferredCommandSpecFactory();
}
else
{
platformCommandSpecFactory = new GenericPlatformCommandSpecFactory();
}
return new OutputPathCommandResolver(environment, platformCommandSpecFactory);
}
else
{
var packagedCommandSpecFactory = new PackagedCommandSpecFactory();
return new ProjectDependenciesCommandResolver(environment, packagedCommandSpecFactory);
}
}
}
}

View file

@ -2,9 +2,11 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Tools.Test.Utilities;
using Microsoft.Extensions.PlatformAbstractions;
using System.Runtime.InteropServices;
using Xunit;
using FluentAssertions;
@ -44,9 +46,8 @@ namespace Microsoft.DotNet.Tests
// need conditional theories so we can skip on non-Windows
[Theory]
[InlineData(".NETStandardApp,Version=v1.5", "CoreFX")]
[InlineData(".NETFramework,Version=v4.5.1", "NetFX")]
public void TestFrameworkSpecificDependencyToolsCanBeInvoked(string framework, string args)
[MemberData("DependencyToolArguments")]
public void TestFrameworkSpecificDependencyToolsCanBeInvoked(string framework, string args, string expectedDependencyToolPath)
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
@ -65,10 +66,25 @@ namespace Microsoft.DotNet.Tests
result.Should().HaveStdOutContaining(framework);
result.Should().HaveStdOutContaining(args);
result.Should().HaveStdOutContaining(expectedDependencyToolPath);
result.Should().NotHaveStdErr();
result.Should().Pass();
}
public static IEnumerable<object[]> DependencyToolArguments
{
get
{
var rid = PlatformServices.Default.Runtime.GetLegacyRestoreRuntimeIdentifier();
var projectOutputPath = $"AppWithDirectDependencyDesktopAndPortable\\bin\\Debug\\net451\\{rid}\\dotnet-desktop-and-portable.exe";
return new[]
{
new object[] { ".NETStandardApp,Version=v1.5", "CoreFX", "lib\\netstandard1.5\\dotnet-desktop-and-portable.dll" },
new object[] { ".NETFramework,Version=v4.5.1", "NetFX", projectOutputPath }
};
}
}
[Fact]
public void TestProjectDependencyIsNotAvailableThroughDriver()
{