Report a meaningful error when trying to run a multi-TFM project.

Add `--no-build` option to `dotnet run3` since even incrementally building a project takes a non-trivial amount of time.
This commit is contained in:
Eric Erhardt 2016-10-13 17:08:05 -05:00
parent 71666cc9b5
commit cbeb5b9912
2 changed files with 39 additions and 12 deletions

View file

@ -28,6 +28,9 @@ namespace Microsoft.DotNet.Tools.Run
CommandOption framework = app.Option( CommandOption framework = app.Option(
"-f|--framework <FRAMEWORK>", "Compile a specific framework", "-f|--framework <FRAMEWORK>", "Compile a specific framework",
CommandOptionType.SingleValue); CommandOptionType.SingleValue);
CommandOption noBuild = app.Option(
"--no-build", "Do not build the project before running.",
CommandOptionType.BoolValue);
CommandOption project = app.Option( CommandOption project = app.Option(
"-p|--project", "The path to the project file to run (defaults to the current directory if there is only one project).", "-p|--project", "The path to the project file to run (defaults to the current directory if there is only one project).",
CommandOptionType.SingleValue); CommandOptionType.SingleValue);
@ -38,6 +41,7 @@ namespace Microsoft.DotNet.Tools.Run
runCmd.Configuration = configuration.Value(); runCmd.Configuration = configuration.Value();
runCmd.Framework = framework.Value(); runCmd.Framework = framework.Value();
runCmd.NoBuild = noBuild.BoolValue ?? false;
runCmd.Project = project.Value(); runCmd.Project = project.Value();
runCmd.Args = app.RemainingArguments; runCmd.Args = app.RemainingArguments;

View file

@ -5,7 +5,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using Microsoft.Build.Execution; using Microsoft.Build.Evaluation;
using Microsoft.DotNet.Cli.Utils; using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Tools.MSBuild; using Microsoft.DotNet.Tools.MSBuild;
@ -15,6 +15,7 @@ namespace Microsoft.DotNet.Tools.Run
{ {
public string Configuration { get; set; } public string Configuration { get; set; }
public string Framework { get; set; } public string Framework { get; set; }
public bool NoBuild { get; set; }
public string Project { get; set; } public string Project { get; set; }
public IReadOnlyList<string> Args { get; set; } public IReadOnlyList<string> Args { get; set; }
@ -28,7 +29,10 @@ namespace Microsoft.DotNet.Tools.Run
{ {
Initialize(); Initialize();
EnsureProjectIsBuilt(); if (!NoBuild)
{
EnsureProjectIsBuilt();
}
ICommand runCommand = GetRunCommand(); ICommand runCommand = GetRunCommand();
@ -80,21 +84,16 @@ namespace Microsoft.DotNet.Tools.Run
globalProperties.Add("TargetFramework", Framework); 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)) if (string.IsNullOrEmpty(runProgram))
{ {
string outputType = projectInstance.GetPropertyValue("OutputType"); ThrowUnableToRunError(project);
throw new GracefulException(string.Join(Environment.NewLine,
"Unable to run your project.",
"Please ensure you have a runnable project type and ensure 'dotnet run' supports this project.",
$"The current OutputType is '{outputType}'."));
} }
string runArguments = projectInstance.GetPropertyValue("RunArguments"); string runArguments = project.GetPropertyValue("RunArguments");
string runWorkingDirectory = projectInstance.GetPropertyValue("RunWorkingDirectory"); string runWorkingDirectory = project.GetPropertyValue("RunWorkingDirectory");
string fullArguments = runArguments; string fullArguments = runArguments;
if (_args.Any()) if (_args.Any())
@ -108,6 +107,30 @@ namespace Microsoft.DotNet.Tools.Run
.WorkingDirectory(runWorkingDirectory); .WorkingDirectory(runWorkingDirectory);
} }
private void ThrowUnableToRunError(Project project)
{
string unableToRunYourProjectMessage = "Unable to run your project.";
string targetFrameworks = project.GetPropertyValue("TargetFrameworks");
if (!string.IsNullOrEmpty(targetFrameworks))
{
string targetFramework = project.GetPropertyValue("TargetFramework");
if (string.IsNullOrEmpty(targetFramework))
{
throw new GracefulException(string.Join(Environment.NewLine,
unableToRunYourProjectMessage,
"Your project targets multiple frameworks. Please specify which framework to run using '--framework'."));
}
}
string outputType = project.GetPropertyValue("OutputType");
throw new GracefulException(string.Join(Environment.NewLine,
unableToRunYourProjectMessage,
"Please ensure you have a runnable project type and ensure 'dotnet run' supports this project.",
$"The current OutputType is '{outputType}'."));
}
private void Initialize() private void Initialize()
{ {
if (string.IsNullOrWhiteSpace(Project)) if (string.IsNullOrWhiteSpace(Project))