From d61ebd3087dadc45edbc1c9fa224beacbf99f8ab Mon Sep 17 00:00:00 2001 From: Livar Cunha Date: Mon, 24 Oct 2016 12:41:27 -0700 Subject: [PATCH] Making OutputPathCommandResolver work with MSBuild. --- ...ldAppWithMultipleFrameworksAndTools.csproj | 37 +++++++++++++++ .../Program.cs | 12 +++++ .../CommandResolution/IProject.cs | 2 + .../CommandResolution/MSBuildProject.cs | 11 +++++ .../OutputPathCommandResolver.cs | 44 +++++------------- .../CommandResolution/ProjectJsonProject.cs | 9 ++++ ...GivenAProjectDependenciesCommandFactory.cs | 45 +++++++++++++++++++ 7 files changed, 126 insertions(+), 34 deletions(-) create mode 100644 TestAssets/TestProjects/MSBuildAppWithMultipleFrameworksAndTools/MSBuildAppWithMultipleFrameworksAndTools.csproj create mode 100644 TestAssets/TestProjects/MSBuildAppWithMultipleFrameworksAndTools/Program.cs diff --git a/TestAssets/TestProjects/MSBuildAppWithMultipleFrameworksAndTools/MSBuildAppWithMultipleFrameworksAndTools.csproj b/TestAssets/TestProjects/MSBuildAppWithMultipleFrameworksAndTools/MSBuildAppWithMultipleFrameworksAndTools.csproj new file mode 100644 index 000000000..a7fad9b20 --- /dev/null +++ b/TestAssets/TestProjects/MSBuildAppWithMultipleFrameworksAndTools/MSBuildAppWithMultipleFrameworksAndTools.csproj @@ -0,0 +1,37 @@ + + + + + + Exe + net451;netcoreapp1.0 + + + + + 1.0.0-alpha-20161019-1 + All + + + 1.0.0-* + + + + + 1.0.1 + + + + + + 1.0.0-* + + + + + + + + + + \ No newline at end of file diff --git a/TestAssets/TestProjects/MSBuildAppWithMultipleFrameworksAndTools/Program.cs b/TestAssets/TestProjects/MSBuildAppWithMultipleFrameworksAndTools/Program.cs new file mode 100644 index 000000000..f5f4b6d13 --- /dev/null +++ b/TestAssets/TestProjects/MSBuildAppWithMultipleFrameworksAndTools/Program.cs @@ -0,0 +1,12 @@ +using System; + +namespace ConsoleApplication +{ + public class Program + { + public static void Main() + { + Console.WriteLine("Hello World!"); + } + } +} diff --git a/src/Microsoft.DotNet.Cli.Utils/CommandResolution/IProject.cs b/src/Microsoft.DotNet.Cli.Utils/CommandResolution/IProject.cs index 640681c18..49d771037 100644 --- a/src/Microsoft.DotNet.Cli.Utils/CommandResolution/IProject.cs +++ b/src/Microsoft.DotNet.Cli.Utils/CommandResolution/IProject.cs @@ -16,6 +16,8 @@ namespace Microsoft.DotNet.Cli.Utils string RuntimeConfigJsonPath { get; } + string OutputPath { get; } + Dictionary EnvironmentVariables { get; } } } \ No newline at end of file diff --git a/src/Microsoft.DotNet.Cli.Utils/CommandResolution/MSBuildProject.cs b/src/Microsoft.DotNet.Cli.Utils/CommandResolution/MSBuildProject.cs index 465c4140d..5ede7d08d 100644 --- a/src/Microsoft.DotNet.Cli.Utils/CommandResolution/MSBuildProject.cs +++ b/src/Microsoft.DotNet.Cli.Utils/CommandResolution/MSBuildProject.cs @@ -39,6 +39,17 @@ namespace Microsoft.DotNet.Cli.Utils } } + public string OutputPath + { + get + { + return _project + .AllEvaluatedProperties + .FirstOrDefault(p => p.Name.Equals("TargetDir")) + .EvaluatedValue; + } + } + public Dictionary EnvironmentVariables { get diff --git a/src/Microsoft.DotNet.Cli.Utils/CommandResolution/OutputPathCommandResolver.cs b/src/Microsoft.DotNet.Cli.Utils/CommandResolution/OutputPathCommandResolver.cs index f3dcb968b..f6d40f667 100644 --- a/src/Microsoft.DotNet.Cli.Utils/CommandResolution/OutputPathCommandResolver.cs +++ b/src/Microsoft.DotNet.Cli.Utils/CommandResolution/OutputPathCommandResolver.cs @@ -42,19 +42,22 @@ namespace Microsoft.DotNet.Cli.Utils string outputPath, string buildBasePath) { - var projectContext = GetProjectContextFromDirectory( + var projectFactory = new ProjectFactory(_environment); + var project = projectFactory.GetProject( projectDirectory, - framework); + framework, + configuration, + buildBasePath, + outputPath); - if (projectContext == null) + if (project == null) { return null; } - var buildOutputPath = - projectContext.GetOutputPaths(configuration, buildBasePath, outputPath).RuntimeFiles.BasePath; + var buildOutputPath = project.OutputPath; - if (! Directory.Exists(buildOutputPath)) + if (!Directory.Exists(buildOutputPath)) { Reporter.Verbose.WriteLine($"outputpathresolver: {buildOutputPath} does not exist"); return null; @@ -62,34 +65,7 @@ namespace Microsoft.DotNet.Cli.Utils 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, - RuntimeEnvironmentRidExtensions.GetAllCandidateRuntimeIdentifiers()); - - if (projectContext.RuntimeIdentifier == null) - { - return null; - } - - return projectContext; - } - + internal override CommandResolutionStrategy GetCommandResolutionStrategy() { return CommandResolutionStrategy.OutputPath; diff --git a/src/Microsoft.DotNet.Cli.Utils/CommandResolution/ProjectJsonProject.cs b/src/Microsoft.DotNet.Cli.Utils/CommandResolution/ProjectJsonProject.cs index e6170da68..8a4b6a129 100644 --- a/src/Microsoft.DotNet.Cli.Utils/CommandResolution/ProjectJsonProject.cs +++ b/src/Microsoft.DotNet.Cli.Utils/CommandResolution/ProjectJsonProject.cs @@ -67,6 +67,15 @@ namespace Microsoft.DotNet.Cli.Utils } } + public string OutputPath + { + get + { + return + ProjectContext.GetOutputPaths(_configuration, _buildBasePath, _outputPath).RuntimeFiles.BasePath; + } + } + public Dictionary EnvironmentVariables { get diff --git a/test/Microsoft.DotNet.Cli.Utils.Tests/GivenAProjectDependenciesCommandFactory.cs b/test/Microsoft.DotNet.Cli.Utils.Tests/GivenAProjectDependenciesCommandFactory.cs index af6423a86..54ec9e943 100644 --- a/test/Microsoft.DotNet.Cli.Utils.Tests/GivenAProjectDependenciesCommandFactory.cs +++ b/test/Microsoft.DotNet.Cli.Utils.Tests/GivenAProjectDependenciesCommandFactory.cs @@ -1,6 +1,7 @@ // Copyright (c) .NET Foundation and contributors. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. +using System; using System.IO; using FluentAssertions; using Microsoft.DotNet.ProjectModel; @@ -15,6 +16,16 @@ namespace Microsoft.DotNet.Cli.Utils.Tests { private static readonly NuGetFramework s_desktopTestFramework = FrameworkConstants.CommonFrameworks.Net451; + private RepoDirectoriesProvider _repoDirectoriesProvider; + + public GivenAProjectDependenciesCommandFactory() + { + _repoDirectoriesProvider = new RepoDirectoriesProvider(); + Environment.SetEnvironmentVariable( + Constants.MSBUILD_EXE_PATH, + Path.Combine(_repoDirectoriesProvider.Stage2Sdk, "MSBuild.dll")); + } + [WindowsOnlyFact] public void It_resolves_desktop_apps_defaulting_to_Debug_Configuration() { @@ -46,6 +57,40 @@ namespace Microsoft.DotNet.Cli.Utils.Tests Path.GetFileName(command.CommandName).Should().Be("dotnet-desktop-and-portable.exe"); } + [WindowsOnlyFact] + public void It_resolves_desktop_apps_with_MSBuild_defaulting_to_Debug_Configuration() + { + var configuration = "Debug"; + + var testAssetManager = new TestAssetsManager(Path.Combine(RepoRoot, "TestAssets", "TestProjects")); + var testInstance = testAssetManager.CreateTestInstance("MSBuildAppWithMultipleFrameworksAndTools", "i") + .WithLockFiles(); + + var projectFile = Path.Combine(testInstance.TestRoot, "MSBuildAppWithMultipleFrameworksAndTools.csproj"); + + new Restore3Command() + .ExecuteWithCapturedOutput($"{projectFile} -s {_repoDirectoriesProvider.TestPackages}") + .Should() + .Pass(); + + new Build3Command() + .Execute($"{projectFile} --configuration {configuration}") + .Should() + .Pass(); + + var factory = new ProjectDependenciesCommandFactory( + s_desktopTestFramework, + null, + null, + null, + testInstance.TestRoot); + + var command = factory.Create("dotnet-desktop-and-portable", null); + + command.CommandName.Should().Contain(Path.Combine(testInstance.TestRoot, "bin", configuration)); + Path.GetFileName(command.CommandName).Should().Be("dotnet-desktop-and-portable.exe"); + } + [WindowsOnlyFact] public void It_resolves_desktop_apps_when_configuration_is_Debug() {