Make errors more specific, add support for setting the launch URL for ASP.NET apps
This commit is contained in:
parent
7ebbef4293
commit
fc8428681e
7 changed files with 73 additions and 27 deletions
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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; }
|
||||
}
|
||||
}
|
|
@ -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<string>();
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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.";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue