From 452e642ac7540b7188ba4210682098c2c6a48705 Mon Sep 17 00:00:00 2001 From: mlorbetske Date: Mon, 29 May 2017 12:54:19 -0700 Subject: [PATCH] Make errors more specific, add support for setting the launch URL for ASP.NET apps --- .../LaunchSettings/ILaunchSettingsProvider.cs | 2 +- .../LaunchSettingsApplyResult.cs | 18 +++++++++++++ .../LaunchSettings/LaunchSettingsManager.cs | 25 ++++++++++--------- .../ProjectLaunchSettingsProvider.cs | 15 ++++++----- .../commands/dotnet-run/LocalizableStrings.cs | 14 ++++++++++- src/dotnet/commands/dotnet-run/RunCommand.cs | 24 +++++++++++++----- .../GivenDotnetRunRunsCsProj.cs | 2 +- 7 files changed, 73 insertions(+), 27 deletions(-) create mode 100644 src/dotnet/commands/dotnet-run/LaunchSettings/LaunchSettingsApplyResult.cs diff --git a/src/dotnet/commands/dotnet-run/LaunchSettings/ILaunchSettingsProvider.cs b/src/dotnet/commands/dotnet-run/LaunchSettings/ILaunchSettingsProvider.cs index b860aa545..64840872e 100644 --- a/src/dotnet/commands/dotnet-run/LaunchSettings/ILaunchSettingsProvider.cs +++ b/src/dotnet/commands/dotnet-run/LaunchSettings/ILaunchSettingsProvider.cs @@ -7,7 +7,7 @@ namespace Microsoft.DotNet.Tools.Run.LaunchSettings { string CommandName { get; } - bool TryApplySettings(JObject document, JObject model, ref ICommand command, out string runAfterLaunch); + LaunchSettingsApplyResult TryApplySettings(JObject document, JObject model, ref ICommand command); } } diff --git a/src/dotnet/commands/dotnet-run/LaunchSettings/LaunchSettingsApplyResult.cs b/src/dotnet/commands/dotnet-run/LaunchSettings/LaunchSettingsApplyResult.cs new file mode 100644 index 000000000..04b9dca3b --- /dev/null +++ b/src/dotnet/commands/dotnet-run/LaunchSettings/LaunchSettingsApplyResult.cs @@ -0,0 +1,18 @@ +namespace Microsoft.DotNet.Tools.Run.LaunchSettings +{ + public class LaunchSettingsApplyResult + { + public LaunchSettingsApplyResult(bool success, string failureReason, string runAfterLaunch = null) + { + Success = success; + FailureReason = failureReason; + RunAfterLaunch = runAfterLaunch; + } + + public bool Success { get; } + + public string FailureReason { get; } + + public string RunAfterLaunch { get; } + } +} diff --git a/src/dotnet/commands/dotnet-run/LaunchSettings/LaunchSettingsManager.cs b/src/dotnet/commands/dotnet-run/LaunchSettings/LaunchSettingsManager.cs index 6f76fa5b1..8246e820c 100644 --- a/src/dotnet/commands/dotnet-run/LaunchSettings/LaunchSettingsManager.cs +++ b/src/dotnet/commands/dotnet-run/LaunchSettings/LaunchSettingsManager.cs @@ -21,7 +21,7 @@ namespace Microsoft.DotNet.Tools.Run.LaunchSettings }; } - public static bool TryApplyLaunchSettings(string launchSettingsJsonContents, ref ICommand command, out string runAfterLaunch, string profileName = null) + public static LaunchSettingsApplyResult TryApplyLaunchSettings(string launchSettingsJsonContents, ref ICommand command, string profileName = null) { try { @@ -30,8 +30,7 @@ namespace Microsoft.DotNet.Tools.Run.LaunchSettings if (profilesObject == null) { - runAfterLaunch = null; - return false; + return new LaunchSettingsApplyResult(false, LocalizableStrings.LaunchProfilesCollectionIsNotAJsonObject); } JObject profileObject; @@ -47,8 +46,7 @@ namespace Microsoft.DotNet.Tools.Run.LaunchSettings if (profileObject == null) { - runAfterLaunch = null; - return false; + return new LaunchSettingsApplyResult(false, LocalizableStrings.LaunchProfileIsNotAJsonObject); } } @@ -72,18 +70,21 @@ namespace Microsoft.DotNet.Tools.Run.LaunchSettings var commandName = profileObject?[CommandNameKey]?.Value(); - if (profileObject == null || !TryLocateHandler(commandName, out ILaunchSettingsProvider provider)) + if (profileObject == null) { - runAfterLaunch = null; - return false; + return new LaunchSettingsApplyResult(false, LocalizableStrings.UsableLaunchProfileCannotBeLocated); } - return provider.TryApplySettings(model, profileObject, ref command, out runAfterLaunch); + if (!TryLocateHandler(commandName, out ILaunchSettingsProvider provider)) + { + return new LaunchSettingsApplyResult(false, string.Format(LocalizableStrings.LaunchProfileHandlerCannotBeLocated, commandName)); + } + + return provider.TryApplySettings(model, profileObject, ref command); } - catch + catch (Exception ex) { - runAfterLaunch = null; - return false; + return new LaunchSettingsApplyResult(false, string.Format(LocalizableStrings.UnexpectedExceptionProcessingLaunchSettings, ex.Message)); } } diff --git a/src/dotnet/commands/dotnet-run/LaunchSettings/ProjectLaunchSettingsProvider.cs b/src/dotnet/commands/dotnet-run/LaunchSettings/ProjectLaunchSettingsProvider.cs index 905c4925a..033f04381 100644 --- a/src/dotnet/commands/dotnet-run/LaunchSettings/ProjectLaunchSettingsProvider.cs +++ b/src/dotnet/commands/dotnet-run/LaunchSettings/ProjectLaunchSettingsProvider.cs @@ -11,7 +11,7 @@ namespace Microsoft.DotNet.Tools.Run.LaunchSettings public string CommandName => CommandNameValue; - public bool TryApplySettings(JObject document, JObject model, ref ICommand command, out string runAfterLaunch) + public LaunchSettingsApplyResult TryApplySettings(JObject document, JObject model, ref ICommand command) { try { @@ -26,13 +26,16 @@ namespace Microsoft.DotNet.Tools.Run.LaunchSettings command.EnvironmentVariable(entry.Key, value); } - runAfterLaunch = null; - return true; + if (!string.IsNullOrEmpty(config.ApplicationUrl)) + { + command.EnvironmentVariable("ASPNETCORE_URLS", config.ApplicationUrl); + } + + return new LaunchSettingsApplyResult(true, null, config.LaunchUrl); } - catch + catch (Exception ex) { - runAfterLaunch = null; - return false; + return new LaunchSettingsApplyResult(false, ex.Message); } } diff --git a/src/dotnet/commands/dotnet-run/LocalizableStrings.cs b/src/dotnet/commands/dotnet-run/LocalizableStrings.cs index 5664f8b11..7d0374957 100644 --- a/src/dotnet/commands/dotnet-run/LocalizableStrings.cs +++ b/src/dotnet/commands/dotnet-run/LocalizableStrings.cs @@ -35,8 +35,20 @@ namespace Microsoft.DotNet.Tools.Run public const string RunCommandExceptionCouldNotLocateALaunchSettingsFile = "The specified launch profile could not be located."; - public const string RunCommandExceptionCouldNotApplyLaunchSettings = "The launch profile \"{0}\" could not be applied."; + public const string RunCommandExceptionCouldNotApplyLaunchSettings = "The launch profile \"{0}\" could not be applied.\n{1}"; public const string DefaultLaunchProfileDisplayName = "(Default)"; + + public const string UsingLaunchSettingsFromMessage = "Using launch settings from {0}..."; + + public const string LaunchProfileIsNotAJsonObject = "Launch profile is not a JSON object."; + + public const string LaunchProfileHandlerCannotBeLocated = "The launch profile type '{0}' is not supported."; + + public const string UsableLaunchProfileCannotBeLocated = "A usable launch profile could not be located."; + + public const string UnexpectedExceptionProcessingLaunchSettings = "An unexpected exception occurred while processing launch settings:\n{0}"; + + public const string LaunchProfilesCollectionIsNotAJsonObject = "The 'profiles' property of the launch settings document is not a JSON object."; } } diff --git a/src/dotnet/commands/dotnet-run/RunCommand.cs b/src/dotnet/commands/dotnet-run/RunCommand.cs index 7b7d198e5..87390c14f 100644 --- a/src/dotnet/commands/dotnet-run/RunCommand.cs +++ b/src/dotnet/commands/dotnet-run/RunCommand.cs @@ -44,18 +44,30 @@ namespace Microsoft.DotNet.Tools.Run var launchSettingsPath = Path.Combine(buildPathContainer, "Properties", "launchSettings.json"); if (File.Exists(launchSettingsPath)) { - var launchSettingsFileContents = File.ReadAllText(launchSettingsPath); - if (!LaunchSettingsManager.TryApplyLaunchSettings(launchSettingsFileContents, ref runCommand, out string runAfterLaunch, LaunchProfile)) + Reporter.Output.WriteLine(string.Format(LocalizableStrings.UsingLaunchSettingsFromMessage, launchSettingsPath)); + string profileName = string.IsNullOrEmpty(LaunchProfile) ? LocalizableStrings.DefaultLaunchProfileDisplayName : LaunchProfile; + + try { - string profileName = string.IsNullOrEmpty(LaunchProfile) ? LocalizableStrings.DefaultLaunchProfileDisplayName : LaunchProfile; - //Error that the launch profile couldn't be applied - Reporter.Error.WriteLine(string.Format(LocalizableStrings.RunCommandExceptionCouldNotApplyLaunchSettings, profileName)); + var launchSettingsFileContents = File.ReadAllText(launchSettingsPath); + var applyResult = LaunchSettingsManager.TryApplyLaunchSettings(launchSettingsFileContents, ref runCommand, LaunchProfile); + if (!applyResult.Success) + { + //Error that the launch profile couldn't be applied + Reporter.Error.WriteLine(string.Format(LocalizableStrings.RunCommandExceptionCouldNotApplyLaunchSettings, profileName, applyResult.FailureReason).Bold().Red()); + } + } + catch (IOException ex) + { + Reporter.Error.WriteLine(string.Format(LocalizableStrings.RunCommandExceptionCouldNotApplyLaunchSettings, profileName).Bold().Red()); + Reporter.Error.WriteLine(ex.Message.Bold().Red()); + return -1; } } else if (!string.IsNullOrEmpty(LaunchProfile)) { //Error that the launch profile couldn't be found - Reporter.Error.WriteLine(LocalizableStrings.RunCommandExceptionCouldNotLocateALaunchSettingsFile); + Reporter.Error.WriteLine(LocalizableStrings.RunCommandExceptionCouldNotLocateALaunchSettingsFile.Bold().Red()); } } diff --git a/test/dotnet-run.Tests/GivenDotnetRunRunsCsProj.cs b/test/dotnet-run.Tests/GivenDotnetRunRunsCsProj.cs index 49cf9c3fe..221267853 100644 --- a/test/dotnet-run.Tests/GivenDotnetRunRunsCsProj.cs +++ b/test/dotnet-run.Tests/GivenDotnetRunRunsCsProj.cs @@ -436,7 +436,7 @@ namespace Microsoft.DotNet.Cli.Run.Tests } [Fact] - public void ItPrintsAnErrorWhenLaunchSettingsArCorrupted() + public void ItPrintsAnErrorWhenLaunchSettingsAreCorrupted() { var testAppName = "MSBuildTestAppWithCorruptedLaunchSettings"; var testInstance = TestAssets.Get(testAppName)