Port #4403 back into the main source code.
This change was lost when we converted the MSBuild "run3" verb to just "run". Fix #6076
This commit is contained in:
parent
1574349a59
commit
55eb812664
5 changed files with 71 additions and 38 deletions
|
@ -5,7 +5,7 @@ using System;
|
|||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Microsoft.Build.Execution;
|
||||
using Microsoft.Build.Evaluation;
|
||||
using Microsoft.DotNet.Cli.Utils;
|
||||
using Microsoft.DotNet.Tools.MSBuild;
|
||||
|
||||
|
@ -15,10 +15,12 @@ namespace Microsoft.DotNet.Tools.Run
|
|||
{
|
||||
public string Configuration { get; set; }
|
||||
public string Framework { get; set; }
|
||||
public bool NoBuild { get; set; }
|
||||
public string Project { get; set; }
|
||||
public IReadOnlyList<string> Args { get; set; }
|
||||
|
||||
private List<string> _args;
|
||||
private bool ShouldBuild => !NoBuild;
|
||||
|
||||
public RunCommand()
|
||||
{
|
||||
|
@ -28,7 +30,10 @@ namespace Microsoft.DotNet.Tools.Run
|
|||
{
|
||||
Initialize();
|
||||
|
||||
EnsureProjectIsBuilt();
|
||||
if (ShouldBuild)
|
||||
{
|
||||
EnsureProjectIsBuilt();
|
||||
}
|
||||
|
||||
ICommand runCommand = GetRunCommand();
|
||||
|
||||
|
@ -40,9 +45,9 @@ namespace Microsoft.DotNet.Tools.Run
|
|||
private void EnsureProjectIsBuilt()
|
||||
{
|
||||
List<string> buildArgs = new List<string>();
|
||||
|
||||
buildArgs.Add(Project);
|
||||
|
||||
|
||||
buildArgs.Add(Project);
|
||||
|
||||
buildArgs.Add("/nologo");
|
||||
buildArgs.Add("/verbosity:quiet");
|
||||
|
||||
|
@ -82,23 +87,16 @@ namespace Microsoft.DotNet.Tools.Run
|
|||
globalProperties.Add("TargetFramework", Framework);
|
||||
}
|
||||
|
||||
ProjectInstance projectInstance = new ProjectInstance(Project, globalProperties, null);
|
||||
Project project = new Project(Project, globalProperties, null);
|
||||
|
||||
string runProgram = projectInstance.GetPropertyValue("RunCommand");
|
||||
string runProgram = project.GetPropertyValue("RunCommand");
|
||||
if (string.IsNullOrEmpty(runProgram))
|
||||
{
|
||||
string outputType = projectInstance.GetPropertyValue("OutputType");
|
||||
|
||||
throw new GracefulException(
|
||||
string.Format(
|
||||
LocalizableStrings.RunCommandExceptionUnableToRun,
|
||||
"dotnet run",
|
||||
"OutputType",
|
||||
outputType));
|
||||
ThrowUnableToRunError(project);
|
||||
}
|
||||
|
||||
string runArguments = projectInstance.GetPropertyValue("RunArguments");
|
||||
string runWorkingDirectory = projectInstance.GetPropertyValue("RunWorkingDirectory");
|
||||
string runArguments = project.GetPropertyValue("RunArguments");
|
||||
string runWorkingDirectory = project.GetPropertyValue("RunWorkingDirectory");
|
||||
|
||||
string fullArguments = runArguments;
|
||||
if (_args.Any())
|
||||
|
@ -112,6 +110,30 @@ namespace Microsoft.DotNet.Tools.Run
|
|||
.WorkingDirectory(runWorkingDirectory);
|
||||
}
|
||||
|
||||
private void ThrowUnableToRunError(Project project)
|
||||
{
|
||||
string targetFrameworks = project.GetPropertyValue("TargetFrameworks");
|
||||
if (!string.IsNullOrEmpty(targetFrameworks))
|
||||
{
|
||||
string targetFramework = project.GetPropertyValue("TargetFramework");
|
||||
if (string.IsNullOrEmpty(targetFramework))
|
||||
{
|
||||
var framework = "--framework";
|
||||
|
||||
throw new GracefulException(LocalizableStrings.RunCommandExceptionUnableToRunSpecifyFramework, framework);
|
||||
}
|
||||
}
|
||||
|
||||
string outputType = project.GetPropertyValue("OutputType");
|
||||
|
||||
throw new GracefulException(
|
||||
string.Format(
|
||||
LocalizableStrings.RunCommandExceptionUnableToRun,
|
||||
"dotnet run",
|
||||
"OutputType",
|
||||
outputType));
|
||||
}
|
||||
|
||||
private void Initialize()
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(Project))
|
||||
|
@ -123,13 +145,11 @@ namespace Microsoft.DotNet.Tools.Run
|
|||
{
|
||||
var project = "--project";
|
||||
|
||||
throw new InvalidOperationException(
|
||||
$"Couldn't find a project to run. Ensure a project exists in {directory}, or pass the path to the project using {project}");
|
||||
throw new GracefulException(LocalizableStrings.RunCommandExceptionNoProjects, directory, project);
|
||||
}
|
||||
else if (projectFiles.Length > 1)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"Specify which project file to use because {directory} contains more than one project file.");
|
||||
throw new GracefulException(LocalizableStrings.RunCommandExceptionMultipleProjects, directory);
|
||||
}
|
||||
|
||||
Project = projectFiles[0];
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue