Update the CLI to the new Run static properties from the SDK.

See https://github.com/dotnet/sdk/issues/98
This commit is contained in:
Eric Erhardt 2016-09-12 14:47:29 -05:00
parent 4e67459f0c
commit a8c804cba7

View file

@ -6,48 +6,36 @@ using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using Microsoft.Build.Execution; using Microsoft.Build.Execution;
using Microsoft.Build.Framework;
using Microsoft.DotNet.Cli; using Microsoft.DotNet.Cli;
using Microsoft.DotNet.Cli.Utils; using Microsoft.DotNet.Cli.Utils;
using NuGet.Frameworks;
namespace Microsoft.DotNet.Tools.Run namespace Microsoft.DotNet.Tools.Run
{ {
public partial class Run3Command public partial class Run3Command
{ {
private const string GetRunInformationTaskName = "GetRunInformation"; private const string RunCommandPropName = "RunCommand";
private const string RunArgumentsPropName = "RunArguments";
private const string RunWorkingDirectoryPropName = "RunWorkingDirectory";
public string Configuration { get; set; } public string Configuration { get; set; }
public string Project { get; set; } public string Project { get; set; }
public IReadOnlyList<string> Args { get; set; } public IReadOnlyList<string> Args { get; set; }
private readonly ICommandFactory _commandFactory;
private List<string> _args; private List<string> _args;
public Run3Command() public Run3Command()
: this(new RunCommandFactory())
{ {
} }
public Run3Command(ICommandFactory commandFactory)
{
_commandFactory = commandFactory;
}
public int Start() public int Start()
{ {
Initialize(); Initialize();
EnsureProjectIsBuilt(); EnsureProjectIsBuilt();
ITaskItem runInfoItem = GetRunInformation(); ICommand runCommand = GetRunCommand();
string commandName = runInfoItem.GetMetadata("CommandName"); return runCommand
string[] args = runInfoItem.GetMetadata("Args").Split(';');
ICommand command = _commandFactory.Create(commandName, Enumerable.Concat(args, _args));
return command
.Execute() .Execute()
.ExitCode; .ExitCode;
} }
@ -73,7 +61,7 @@ namespace Microsoft.DotNet.Tools.Run
} }
} }
private ITaskItem GetRunInformation() private ICommand GetRunCommand()
{ {
Dictionary<string, string> globalProperties = new Dictionary<string, string>() Dictionary<string, string> globalProperties = new Dictionary<string, string>()
{ {
@ -87,30 +75,25 @@ namespace Microsoft.DotNet.Tools.Run
ProjectInstance projectInstance = new ProjectInstance(Project, globalProperties, null); ProjectInstance projectInstance = new ProjectInstance(Project, globalProperties, null);
BuildRequestData buildRequestData = new BuildRequestData(projectInstance, new string[] { GetRunInformationTaskName }); string runProgram = projectInstance.GetPropertyValue(RunCommandPropName);
BuildParameters buildParameters = new BuildParameters(); if (string.IsNullOrEmpty(runProgram))
BuildResult result = BuildManager.DefaultBuildManager.Build(buildParameters, buildRequestData);
TargetResult runInfoResult;
if (!result.ResultsByTarget.TryGetValue(GetRunInformationTaskName, out runInfoResult))
{ {
throw new InvalidOperationException($"Could not find a target named '{GetRunInformationTaskName}' in your project. Please ensure 'dotnet run' supports this project."); throw new InvalidOperationException($"The property named '{RunCommandPropName}' does not have a value in your project. Please ensure 'dotnet run' supports this project.");
} }
if (runInfoResult.ResultCode != TargetResultCode.Success) string runArguments = projectInstance.GetPropertyValue(RunArgumentsPropName);
string runWorkingDirectory = projectInstance.GetPropertyValue(RunWorkingDirectoryPropName);
string fullArguments = runArguments;
if (_args.Any())
{ {
throw new InvalidOperationException($"Could not get the run information for project {Project}. An internal MSBuild error has occured" + Environment.NewLine + fullArguments += " " + ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(_args);
runInfoResult.Exception?.ToString());
} }
ITaskItem runInfoItem = runInfoResult.Items.FirstOrDefault(i => i.ItemSpec == projectInstance.FullPath); CommandSpec commandSpec = new CommandSpec(runProgram, fullArguments, CommandResolutionStrategy.None);
if (runInfoItem == null)
{
throw new InvalidOperationException($"'{GetRunInformationTaskName}' did not return an ITaskItem with the project's FullPath as the ItemSpec.");
}
return runInfoItem; return Command.Create(commandSpec)
.WorkingDirectory(runWorkingDirectory);
} }
private void Initialize() private void Initialize()
@ -144,13 +127,5 @@ namespace Microsoft.DotNet.Tools.Run
_args = new List<string>(Args); _args = new List<string>(Args);
} }
} }
private class RunCommandFactory : ICommandFactory
{
public ICommand Create(string commandName, IEnumerable<string> args, NuGetFramework framework = null, string configuration = Constants.DefaultConfiguration)
{
return Command.Create(commandName, args, framework, configuration);
}
}
} }
} }