Make errors more specific, add support for setting the launch URL for ASP.NET apps

This commit is contained in:
mlorbetske 2017-05-29 12:54:19 -07:00
parent 59218f4922
commit 452e642ac7
7 changed files with 73 additions and 27 deletions

View file

@ -7,7 +7,7 @@ namespace Microsoft.DotNet.Tools.Run.LaunchSettings
{ {
string CommandName { get; } string CommandName { get; }
bool TryApplySettings(JObject document, JObject model, ref ICommand command, out string runAfterLaunch); LaunchSettingsApplyResult TryApplySettings(JObject document, JObject model, ref ICommand command);
} }
} }

View file

@ -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; }
}
}

View file

@ -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 try
{ {
@ -30,8 +30,7 @@ namespace Microsoft.DotNet.Tools.Run.LaunchSettings
if (profilesObject == null) if (profilesObject == null)
{ {
runAfterLaunch = null; return new LaunchSettingsApplyResult(false, LocalizableStrings.LaunchProfilesCollectionIsNotAJsonObject);
return false;
} }
JObject profileObject; JObject profileObject;
@ -47,8 +46,7 @@ namespace Microsoft.DotNet.Tools.Run.LaunchSettings
if (profileObject == null) if (profileObject == null)
{ {
runAfterLaunch = null; return new LaunchSettingsApplyResult(false, LocalizableStrings.LaunchProfileIsNotAJsonObject);
return false;
} }
} }
@ -72,18 +70,21 @@ namespace Microsoft.DotNet.Tools.Run.LaunchSettings
var commandName = profileObject?[CommandNameKey]?.Value<string>(); var commandName = profileObject?[CommandNameKey]?.Value<string>();
if (profileObject == null || !TryLocateHandler(commandName, out ILaunchSettingsProvider provider)) if (profileObject == null)
{ {
runAfterLaunch = null; return new LaunchSettingsApplyResult(false, LocalizableStrings.UsableLaunchProfileCannotBeLocated);
return false;
} }
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 new LaunchSettingsApplyResult(false, string.Format(LocalizableStrings.UnexpectedExceptionProcessingLaunchSettings, ex.Message));
return false;
} }
} }

View file

@ -11,7 +11,7 @@ namespace Microsoft.DotNet.Tools.Run.LaunchSettings
public string CommandName => CommandNameValue; 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 try
{ {
@ -26,13 +26,16 @@ namespace Microsoft.DotNet.Tools.Run.LaunchSettings
command.EnvironmentVariable(entry.Key, value); command.EnvironmentVariable(entry.Key, value);
} }
runAfterLaunch = null; if (!string.IsNullOrEmpty(config.ApplicationUrl))
return true; {
command.EnvironmentVariable("ASPNETCORE_URLS", config.ApplicationUrl);
}
return new LaunchSettingsApplyResult(true, null, config.LaunchUrl);
} }
catch catch (Exception ex)
{ {
runAfterLaunch = null; return new LaunchSettingsApplyResult(false, ex.Message);
return false;
} }
} }

View file

@ -35,8 +35,20 @@ namespace Microsoft.DotNet.Tools.Run
public const string RunCommandExceptionCouldNotLocateALaunchSettingsFile = "The specified launch profile could not be located."; 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 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.";
} }
} }

View file

@ -44,18 +44,30 @@ namespace Microsoft.DotNet.Tools.Run
var launchSettingsPath = Path.Combine(buildPathContainer, "Properties", "launchSettings.json"); var launchSettingsPath = Path.Combine(buildPathContainer, "Properties", "launchSettings.json");
if (File.Exists(launchSettingsPath)) if (File.Exists(launchSettingsPath))
{ {
var launchSettingsFileContents = File.ReadAllText(launchSettingsPath); Reporter.Output.WriteLine(string.Format(LocalizableStrings.UsingLaunchSettingsFromMessage, launchSettingsPath));
if (!LaunchSettingsManager.TryApplyLaunchSettings(launchSettingsFileContents, ref runCommand, out string runAfterLaunch, LaunchProfile)) string profileName = string.IsNullOrEmpty(LaunchProfile) ? LocalizableStrings.DefaultLaunchProfileDisplayName : LaunchProfile;
try
{ {
string profileName = string.IsNullOrEmpty(LaunchProfile) ? LocalizableStrings.DefaultLaunchProfileDisplayName : LaunchProfile; var launchSettingsFileContents = File.ReadAllText(launchSettingsPath);
//Error that the launch profile couldn't be applied var applyResult = LaunchSettingsManager.TryApplyLaunchSettings(launchSettingsFileContents, ref runCommand, LaunchProfile);
Reporter.Error.WriteLine(string.Format(LocalizableStrings.RunCommandExceptionCouldNotApplyLaunchSettings, profileName)); 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)) else if (!string.IsNullOrEmpty(LaunchProfile))
{ {
//Error that the launch profile couldn't be found //Error that the launch profile couldn't be found
Reporter.Error.WriteLine(LocalizableStrings.RunCommandExceptionCouldNotLocateALaunchSettingsFile); Reporter.Error.WriteLine(LocalizableStrings.RunCommandExceptionCouldNotLocateALaunchSettingsFile.Bold().Red());
} }
} }

View file

@ -436,7 +436,7 @@ namespace Microsoft.DotNet.Cli.Run.Tests
} }
[Fact] [Fact]
public void ItPrintsAnErrorWhenLaunchSettingsArCorrupted() public void ItPrintsAnErrorWhenLaunchSettingsAreCorrupted()
{ {
var testAppName = "MSBuildTestAppWithCorruptedLaunchSettings"; var testAppName = "MSBuildTestAppWithCorruptedLaunchSettings";
var testInstance = TestAssets.Get(testAppName) var testInstance = TestAssets.Get(testAppName)