Skip to content
This repository was archived by the owner on Apr 20, 2023. It is now read-only.

Commit 452e642

Browse files
committed
Make errors more specific, add support for setting the launch URL for ASP.NET apps
1 parent 59218f4 commit 452e642

File tree

7 files changed

+73
-27
lines changed

7 files changed

+73
-27
lines changed

src/dotnet/commands/dotnet-run/LaunchSettings/ILaunchSettingsProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public interface ILaunchSettingsProvider
77
{
88
string CommandName { get; }
99

10-
bool TryApplySettings(JObject document, JObject model, ref ICommand command, out string runAfterLaunch);
10+
LaunchSettingsApplyResult TryApplySettings(JObject document, JObject model, ref ICommand command);
1111
}
1212

1313
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace Microsoft.DotNet.Tools.Run.LaunchSettings
2+
{
3+
public class LaunchSettingsApplyResult
4+
{
5+
public LaunchSettingsApplyResult(bool success, string failureReason, string runAfterLaunch = null)
6+
{
7+
Success = success;
8+
FailureReason = failureReason;
9+
RunAfterLaunch = runAfterLaunch;
10+
}
11+
12+
public bool Success { get; }
13+
14+
public string FailureReason { get; }
15+
16+
public string RunAfterLaunch { get; }
17+
}
18+
}

src/dotnet/commands/dotnet-run/LaunchSettings/LaunchSettingsManager.cs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ static LaunchSettingsManager()
2121
};
2222
}
2323

24-
public static bool TryApplyLaunchSettings(string launchSettingsJsonContents, ref ICommand command, out string runAfterLaunch, string profileName = null)
24+
public static LaunchSettingsApplyResult TryApplyLaunchSettings(string launchSettingsJsonContents, ref ICommand command, string profileName = null)
2525
{
2626
try
2727
{
@@ -30,8 +30,7 @@ public static bool TryApplyLaunchSettings(string launchSettingsJsonContents, ref
3030

3131
if (profilesObject == null)
3232
{
33-
runAfterLaunch = null;
34-
return false;
33+
return new LaunchSettingsApplyResult(false, LocalizableStrings.LaunchProfilesCollectionIsNotAJsonObject);
3534
}
3635

3736
JObject profileObject;
@@ -47,8 +46,7 @@ public static bool TryApplyLaunchSettings(string launchSettingsJsonContents, ref
4746

4847
if (profileObject == null)
4948
{
50-
runAfterLaunch = null;
51-
return false;
49+
return new LaunchSettingsApplyResult(false, LocalizableStrings.LaunchProfileIsNotAJsonObject);
5250
}
5351
}
5452

@@ -72,18 +70,21 @@ public static bool TryApplyLaunchSettings(string launchSettingsJsonContents, ref
7270

7371
var commandName = profileObject?[CommandNameKey]?.Value<string>();
7472

75-
if (profileObject == null || !TryLocateHandler(commandName, out ILaunchSettingsProvider provider))
73+
if (profileObject == null)
74+
{
75+
return new LaunchSettingsApplyResult(false, LocalizableStrings.UsableLaunchProfileCannotBeLocated);
76+
}
77+
78+
if (!TryLocateHandler(commandName, out ILaunchSettingsProvider provider))
7679
{
77-
runAfterLaunch = null;
78-
return false;
80+
return new LaunchSettingsApplyResult(false, string.Format(LocalizableStrings.LaunchProfileHandlerCannotBeLocated, commandName));
7981
}
8082

81-
return provider.TryApplySettings(model, profileObject, ref command, out runAfterLaunch);
83+
return provider.TryApplySettings(model, profileObject, ref command);
8284
}
83-
catch
85+
catch (Exception ex)
8486
{
85-
runAfterLaunch = null;
86-
return false;
87+
return new LaunchSettingsApplyResult(false, string.Format(LocalizableStrings.UnexpectedExceptionProcessingLaunchSettings, ex.Message));
8788
}
8889
}
8990

src/dotnet/commands/dotnet-run/LaunchSettings/ProjectLaunchSettingsProvider.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class ProjectLaunchSettingsProvider : ILaunchSettingsProvider
1111

1212
public string CommandName => CommandNameValue;
1313

14-
public bool TryApplySettings(JObject document, JObject model, ref ICommand command, out string runAfterLaunch)
14+
public LaunchSettingsApplyResult TryApplySettings(JObject document, JObject model, ref ICommand command)
1515
{
1616
try
1717
{
@@ -26,13 +26,16 @@ public bool TryApplySettings(JObject document, JObject model, ref ICommand comma
2626
command.EnvironmentVariable(entry.Key, value);
2727
}
2828

29-
runAfterLaunch = null;
30-
return true;
29+
if (!string.IsNullOrEmpty(config.ApplicationUrl))
30+
{
31+
command.EnvironmentVariable("ASPNETCORE_URLS", config.ApplicationUrl);
32+
}
33+
34+
return new LaunchSettingsApplyResult(true, null, config.LaunchUrl);
3135
}
32-
catch
36+
catch (Exception ex)
3337
{
34-
runAfterLaunch = null;
35-
return false;
38+
return new LaunchSettingsApplyResult(false, ex.Message);
3639
}
3740
}
3841

src/dotnet/commands/dotnet-run/LocalizableStrings.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,20 @@ internal class LocalizableStrings
3535

3636
public const string RunCommandExceptionCouldNotLocateALaunchSettingsFile = "The specified launch profile could not be located.";
3737

38-
public const string RunCommandExceptionCouldNotApplyLaunchSettings = "The launch profile \"{0}\" could not be applied.";
38+
public const string RunCommandExceptionCouldNotApplyLaunchSettings = "The launch profile \"{0}\" could not be applied.\n{1}";
3939

4040
public const string DefaultLaunchProfileDisplayName = "(Default)";
41+
42+
public const string UsingLaunchSettingsFromMessage = "Using launch settings from {0}...";
43+
44+
public const string LaunchProfileIsNotAJsonObject = "Launch profile is not a JSON object.";
45+
46+
public const string LaunchProfileHandlerCannotBeLocated = "The launch profile type '{0}' is not supported.";
47+
48+
public const string UsableLaunchProfileCannotBeLocated = "A usable launch profile could not be located.";
49+
50+
public const string UnexpectedExceptionProcessingLaunchSettings = "An unexpected exception occurred while processing launch settings:\n{0}";
51+
52+
public const string LaunchProfilesCollectionIsNotAJsonObject = "The 'profiles' property of the launch settings document is not a JSON object.";
4153
}
4254
}

src/dotnet/commands/dotnet-run/RunCommand.cs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,30 @@ public int Start()
4444
var launchSettingsPath = Path.Combine(buildPathContainer, "Properties", "launchSettings.json");
4545
if (File.Exists(launchSettingsPath))
4646
{
47-
var launchSettingsFileContents = File.ReadAllText(launchSettingsPath);
48-
if (!LaunchSettingsManager.TryApplyLaunchSettings(launchSettingsFileContents, ref runCommand, out string runAfterLaunch, LaunchProfile))
47+
Reporter.Output.WriteLine(string.Format(LocalizableStrings.UsingLaunchSettingsFromMessage, launchSettingsPath));
48+
string profileName = string.IsNullOrEmpty(LaunchProfile) ? LocalizableStrings.DefaultLaunchProfileDisplayName : LaunchProfile;
49+
50+
try
51+
{
52+
var launchSettingsFileContents = File.ReadAllText(launchSettingsPath);
53+
var applyResult = LaunchSettingsManager.TryApplyLaunchSettings(launchSettingsFileContents, ref runCommand, LaunchProfile);
54+
if (!applyResult.Success)
55+
{
56+
//Error that the launch profile couldn't be applied
57+
Reporter.Error.WriteLine(string.Format(LocalizableStrings.RunCommandExceptionCouldNotApplyLaunchSettings, profileName, applyResult.FailureReason).Bold().Red());
58+
}
59+
}
60+
catch (IOException ex)
4961
{
50-
string profileName = string.IsNullOrEmpty(LaunchProfile) ? LocalizableStrings.DefaultLaunchProfileDisplayName : LaunchProfile;
51-
//Error that the launch profile couldn't be applied
52-
Reporter.Error.WriteLine(string.Format(LocalizableStrings.RunCommandExceptionCouldNotApplyLaunchSettings, profileName));
62+
Reporter.Error.WriteLine(string.Format(LocalizableStrings.RunCommandExceptionCouldNotApplyLaunchSettings, profileName).Bold().Red());
63+
Reporter.Error.WriteLine(ex.Message.Bold().Red());
64+
return -1;
5365
}
5466
}
5567
else if (!string.IsNullOrEmpty(LaunchProfile))
5668
{
5769
//Error that the launch profile couldn't be found
58-
Reporter.Error.WriteLine(LocalizableStrings.RunCommandExceptionCouldNotLocateALaunchSettingsFile);
70+
Reporter.Error.WriteLine(LocalizableStrings.RunCommandExceptionCouldNotLocateALaunchSettingsFile.Bold().Red());
5971
}
6072
}
6173

test/dotnet-run.Tests/GivenDotnetRunRunsCsProj.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ public void ItSkipsLaunchProfilesWhenThereIsNoUsableDefault()
436436
}
437437

438438
[Fact]
439-
public void ItPrintsAnErrorWhenLaunchSettingsArCorrupted()
439+
public void ItPrintsAnErrorWhenLaunchSettingsAreCorrupted()
440440
{
441441
var testAppName = "MSBuildTestAppWithCorruptedLaunchSettings";
442442
var testInstance = TestAssets.Get(testAppName)

0 commit comments

Comments
 (0)