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:
parent
4e67459f0c
commit
a8c804cba7
1 changed files with 19 additions and 44 deletions
|
@ -6,48 +6,36 @@ using System.Collections.Generic;
|
|||
using System.IO;
|
||||
using System.Linq;
|
||||
using Microsoft.Build.Execution;
|
||||
using Microsoft.Build.Framework;
|
||||
using Microsoft.DotNet.Cli;
|
||||
using Microsoft.DotNet.Cli.Utils;
|
||||
using NuGet.Frameworks;
|
||||
|
||||
namespace Microsoft.DotNet.Tools.Run
|
||||
{
|
||||
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 Project { get; set; }
|
||||
public IReadOnlyList<string> Args { get; set; }
|
||||
|
||||
private readonly ICommandFactory _commandFactory;
|
||||
private List<string> _args;
|
||||
|
||||
public Run3Command()
|
||||
: this(new RunCommandFactory())
|
||||
{
|
||||
}
|
||||
|
||||
public Run3Command(ICommandFactory commandFactory)
|
||||
{
|
||||
_commandFactory = commandFactory;
|
||||
}
|
||||
|
||||
public int Start()
|
||||
{
|
||||
Initialize();
|
||||
|
||||
EnsureProjectIsBuilt();
|
||||
|
||||
ITaskItem runInfoItem = GetRunInformation();
|
||||
ICommand runCommand = GetRunCommand();
|
||||
|
||||
string commandName = runInfoItem.GetMetadata("CommandName");
|
||||
string[] args = runInfoItem.GetMetadata("Args").Split(';');
|
||||
|
||||
ICommand command = _commandFactory.Create(commandName, Enumerable.Concat(args, _args));
|
||||
|
||||
return command
|
||||
return runCommand
|
||||
.Execute()
|
||||
.ExitCode;
|
||||
}
|
||||
|
@ -73,7 +61,7 @@ namespace Microsoft.DotNet.Tools.Run
|
|||
}
|
||||
}
|
||||
|
||||
private ITaskItem GetRunInformation()
|
||||
private ICommand GetRunCommand()
|
||||
{
|
||||
Dictionary<string, string> globalProperties = new Dictionary<string, string>()
|
||||
{
|
||||
|
@ -87,30 +75,25 @@ namespace Microsoft.DotNet.Tools.Run
|
|||
|
||||
ProjectInstance projectInstance = new ProjectInstance(Project, globalProperties, null);
|
||||
|
||||
BuildRequestData buildRequestData = new BuildRequestData(projectInstance, new string[] { GetRunInformationTaskName });
|
||||
BuildParameters buildParameters = new BuildParameters();
|
||||
|
||||
BuildResult result = BuildManager.DefaultBuildManager.Build(buildParameters, buildRequestData);
|
||||
|
||||
TargetResult runInfoResult;
|
||||
if (!result.ResultsByTarget.TryGetValue(GetRunInformationTaskName, out runInfoResult))
|
||||
string runProgram = projectInstance.GetPropertyValue(RunCommandPropName);
|
||||
if (string.IsNullOrEmpty(runProgram))
|
||||
{
|
||||
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 +
|
||||
runInfoResult.Exception?.ToString());
|
||||
fullArguments += " " + ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(_args);
|
||||
}
|
||||
|
||||
ITaskItem runInfoItem = runInfoResult.Items.FirstOrDefault(i => i.ItemSpec == projectInstance.FullPath);
|
||||
if (runInfoItem == null)
|
||||
{
|
||||
throw new InvalidOperationException($"'{GetRunInformationTaskName}' did not return an ITaskItem with the project's FullPath as the ItemSpec.");
|
||||
}
|
||||
CommandSpec commandSpec = new CommandSpec(runProgram, fullArguments, CommandResolutionStrategy.None);
|
||||
|
||||
return runInfoItem;
|
||||
return Command.Create(commandSpec)
|
||||
.WorkingDirectory(runWorkingDirectory);
|
||||
}
|
||||
|
||||
private void Initialize()
|
||||
|
@ -144,13 +127,5 @@ namespace Microsoft.DotNet.Tools.Run
|
|||
_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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue