Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/Assets/TestProjects/TestAppWithLaunchSettings/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;

namespace ConsoleApplication
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello world");
Console.WriteLine($"MyCoolEnvironmentVariableKey={Environment.GetEnvironmentVariable("MyCoolEnvironmentVariableKey")}");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"profiles": {
"TestAppWithLaunchSettings": {
"commandName": "Project",
"dotnetRunMessages": "true",
"environmentVariables": {
"MyCoolEnvironmentVariableKey": "MyCoolEnvironmentVariableValue"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), testAsset.props))\testAsset.props" />

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<OutputType>Exe</OutputType>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ namespace Microsoft.DotNet.Tools.Run.LaunchSettings
{
internal interface ILaunchSettingsProvider
{
string CommandName { get; }

LaunchSettingsApplyResult TryApplySettings(JsonElement model, ref ICommand command);
LaunchSettingsApplyResult TryGetLaunchSettings(JsonElement model);
}

}
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Microsoft.DotNet.Tools.Run.LaunchSettings
{
public class LaunchSettingsApplyResult
{
public LaunchSettingsApplyResult(bool success, string failureReason, string runAfterLaunch = null)
public LaunchSettingsApplyResult(bool success, string failureReason, ProjectLaunchSettingsModel launchSettings = null)
{
Success = success;
FailureReason = failureReason;
RunAfterLaunch = runAfterLaunch;
LaunchSettings = launchSettings;
}

public bool Success { get; }

public string FailureReason { get; }

public string RunAfterLaunch { get; }
public ProjectLaunchSettingsModel LaunchSettings { get; }
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System;
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
Expand All @@ -21,7 +24,7 @@ static LaunchSettingsManager()
};
}

public static LaunchSettingsApplyResult TryApplyLaunchSettings(string launchSettingsJsonContents, ref ICommand command, string profileName = null)
public static LaunchSettingsApplyResult TryApplyLaunchSettings(string launchSettingsJsonContents, string profileName = null)
{
try
{
Expand Down Expand Up @@ -90,7 +93,7 @@ public static LaunchSettingsApplyResult TryApplyLaunchSettings(string launchSett
return new LaunchSettingsApplyResult(false, string.Format(LocalizableStrings.LaunchProfileHandlerCannotBeLocated, commandName));
}

return provider.TryApplySettings(profileObject, ref command);
return provider.TryGetLaunchSettings(profileObject);
}
}
catch (JsonException ex)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;

namespace Microsoft.DotNet.Tools.Run.LaunchSettings
{
public class ProjectLaunchSettingsModel
{
public string CommandLineArgs { get; set; }

public bool LaunchBrowser { get; set; }

public string LaunchUrl { get; set; }

public string ApplicationUrl { get; set; }

public string DotNetRunMessages { get; set; }

public Dictionary<string, string> EnvironmentVariables { get; } = new Dictionary<string, string>(StringComparer.Ordinal);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using System;
using System.Collections.Generic;
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Text.Json;
using Microsoft.DotNet.Cli.Utils;

namespace Microsoft.DotNet.Tools.Run.LaunchSettings
{
Expand All @@ -11,7 +12,7 @@ internal class ProjectLaunchSettingsProvider : ILaunchSettingsProvider

public string CommandName => CommandNameValue;

public LaunchSettingsApplyResult TryApplySettings(JsonElement model, ref ICommand command)
public LaunchSettingsApplyResult TryGetLaunchSettings(JsonElement model)
{
var config = new ProjectLaunchSettingsModel();
foreach (var property in model.EnumerateObject())
Expand Down Expand Up @@ -52,14 +53,23 @@ public LaunchSettingsApplyResult TryApplySettings(JsonElement model, ref IComman

config.ApplicationUrl = applicationUrlValue;
}
else if (string.Equals(property.Name, nameof(ProjectLaunchSettingsModel.DotNetRunMessages), StringComparison.OrdinalIgnoreCase))
{
if (!TryGetStringValue(property.Value, out var dotNetRunMessages))
{
return new LaunchSettingsApplyResult(false, string.Format(LocalizableStrings.CouldNotConvertToString, property.Name));
}

config.DotNetRunMessages = dotNetRunMessages;
}
else if (string.Equals(property.Name, nameof(ProjectLaunchSettingsModel.EnvironmentVariables), StringComparison.OrdinalIgnoreCase))
{
if (property.Value.ValueKind != JsonValueKind.Object)
{
return new LaunchSettingsApplyResult(false, string.Format(LocalizableStrings.ValueMustBeAnObject, property.Name));
}

foreach(var environmentVariable in property.Value.EnumerateObject())
foreach (var environmentVariable in property.Value.EnumerateObject())
{
if (TryGetStringValue(environmentVariable.Value, out var environmentVariableValue))
{
Expand All @@ -69,21 +79,7 @@ public LaunchSettingsApplyResult TryApplySettings(JsonElement model, ref IComman
}
}

if (!string.IsNullOrEmpty(config.ApplicationUrl))
{
command.EnvironmentVariable("ASPNETCORE_URLS", config.ApplicationUrl);
}

//For now, ignore everything but the environment variables section

foreach (var entry in config.EnvironmentVariables)
{
string value = Environment.ExpandEnvironmentVariables(entry.Value);
//NOTE: MSBuild variables are not expanded like they are in VS
command.EnvironmentVariable(entry.Key, value);
}

return new LaunchSettingsApplyResult(true, null, config.LaunchUrl);
return new LaunchSettingsApplyResult(true, null, config);
}

private static bool TryGetBooleanValue(JsonElement element, out bool value)
Expand Down Expand Up @@ -136,23 +132,5 @@ private static bool TryGetStringValue(JsonElement element, out string value)
return false;
}
}

private class ProjectLaunchSettingsModel
{
public ProjectLaunchSettingsModel()
{
EnvironmentVariables = new Dictionary<string, string>(StringComparer.Ordinal);
}

public string CommandLineArgs { get; set; }

public bool LaunchBrowser { get; set; }

public string LaunchUrl { get; set; }

public string ApplicationUrl { get; set; }

public Dictionary<string, string> EnvironmentVariables { get; }
}
}
}
5 changes: 4 additions & 1 deletion src/Cli/dotnet/commands/dotnet-run/LocalizableStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@
<data name="RuntimeOptionDescription" xml:space="preserve">
<value>The target runtime to run for.</value>
</data>
<data name="RunCommandBuilding" xml:space="preserve">
<value>Building...</value>
</data>
<data name="RunCommandException" xml:space="preserve">
<value>The build failed. Fix the build errors and run again.</value>
</data>
Expand Down Expand Up @@ -214,4 +217,4 @@ The current {1} is '{2}'.</value>
<data name="ValueMustBeAnObject" xml:space="preserve">
<value>The property '{0}' must be an object if it is specified.</value>
</data>
</root>
</root>
40 changes: 32 additions & 8 deletions src/Cli/dotnet/commands/dotnet-run/RunCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
using Microsoft.Build.Execution;
using Microsoft.Build.Exceptions;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Tools;
using Microsoft.DotNet.Tools.MSBuild;
using Microsoft.DotNet.Tools.Run.LaunchSettings;
using Microsoft.DotNet.CommandFactory;

Expand Down Expand Up @@ -41,17 +39,37 @@ public int Execute()
{
Initialize();

if (!TryGetLaunchProfileSettingsIfNeeded(out var launchSettings))
{
return 1;
}

if (ShouldBuild)
{
if (string.Equals("true", launchSettings?.DotNetRunMessages, StringComparison.OrdinalIgnoreCase))
{
Reporter.Output.WriteLine(LocalizableStrings.RunCommandBuilding);
}

EnsureProjectIsBuilt();
}

try
{
ICommand targetCommand = GetTargetCommand();
if (!ApplyLaunchProfileSettingsIfNeeded(ref targetCommand))
if (launchSettings != null)
{
return 1;
if (!string.IsNullOrEmpty(launchSettings.ApplicationUrl))
{
targetCommand.EnvironmentVariable("ASPNETCORE_URLS", launchSettings.ApplicationUrl);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no test coverage for this code

}

foreach (var entry in launchSettings.EnvironmentVariables)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no test coverage for this code

{
string value = Environment.ExpandEnvironmentVariables(entry.Value);
//NOTE: MSBuild variables are not expanded like they are in VS
targetCommand.EnvironmentVariable(entry.Key, value);
}
}

// Ignore Ctrl-C for the remainder of the command's execution
Expand Down Expand Up @@ -92,8 +110,9 @@ public RunCommand(string configuration,
Interactive = interactive;
}

private bool ApplyLaunchProfileSettingsIfNeeded(ref ICommand targetCommand)
private bool TryGetLaunchProfileSettingsIfNeeded(out ProjectLaunchSettingsModel launchSettingsModel)
{
launchSettingsModel = default;
if (!UseLaunchProfile)
{
return true;
Expand All @@ -117,7 +136,8 @@ private bool ApplyLaunchProfileSettingsIfNeeded(ref ICommand targetCommand)

if (File.Exists(launchSettingsPath))
{
if (!HasQuietVerbosity) {
if (!HasQuietVerbosity)
{
Reporter.Output.WriteLine(string.Format(LocalizableStrings.UsingLaunchSettingsFromMessage, launchSettingsPath));
}

Expand All @@ -126,11 +146,15 @@ private bool ApplyLaunchProfileSettingsIfNeeded(ref ICommand targetCommand)
try
{
var launchSettingsFileContents = File.ReadAllText(launchSettingsPath);
var applyResult = LaunchSettingsManager.TryApplyLaunchSettings(launchSettingsFileContents, ref targetCommand, LaunchProfile);
var applyResult = LaunchSettingsManager.TryApplyLaunchSettings(launchSettingsFileContents, LaunchProfile);
if (!applyResult.Success)
{
Reporter.Error.WriteLine(string.Format(LocalizableStrings.RunCommandExceptionCouldNotApplyLaunchSettings, profileName, applyResult.FailureReason).Bold().Red());
}
else
{
launchSettingsModel = applyResult.LaunchSettings;
}
}
catch (IOException ex)
{
Expand All @@ -155,7 +179,7 @@ private void EnsureProjectIsBuilt()
new RestoringCommand(
restoreArgs.Prepend(Project),
restoreArgs,
new [] { Project },
new[] { Project },
NoRestore
).Execute();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
<target state="translated">Hodnotu vlastnosti {0} nejde převést na řetězec.</target>
<note />
</trans-unit>
<trans-unit id="RunCommandBuilding">
<source>Building...</source>
<target state="new">Building...</target>
<note />
</trans-unit>
<trans-unit id="RunCommandException">
<source>The build failed. Fix the build errors and run again.</source>
<target state="translated">Sestavení se nepovedlo. Opravte v sestavení chyby a spusťte ho znovu.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
<target state="translated">Der Wert der Eigenschaft "{0}" konnte nicht in eine Zeichenfolge konvertiert werden.</target>
<note />
</trans-unit>
<trans-unit id="RunCommandBuilding">
<source>Building...</source>
<target state="new">Building...</target>
<note />
</trans-unit>
<trans-unit id="RunCommandException">
<source>The build failed. Fix the build errors and run again.</source>
<target state="translated">Fehler beim Buildvorgang. Beheben Sie die Buildfehler, und versuchen Sie es anschließend noch mal.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
<target state="translated">No se pudo convertir el valor de la propiedad "{0}" en una cadena.</target>
<note />
</trans-unit>
<trans-unit id="RunCommandBuilding">
<source>Building...</source>
<target state="new">Building...</target>
<note />
</trans-unit>
<trans-unit id="RunCommandException">
<source>The build failed. Fix the build errors and run again.</source>
<target state="translated">No se pudo llevar a cabo la compilación. Corrija los errores de compilación y vuelva a ejecutar el proyecto.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
<target state="translated">Impossible de convertir la valeur de la propriété '{0}' en chaîne.</target>
<note />
</trans-unit>
<trans-unit id="RunCommandBuilding">
<source>Building...</source>
<target state="new">Building...</target>
<note />
</trans-unit>
<trans-unit id="RunCommandException">
<source>The build failed. Fix the build errors and run again.</source>
<target state="translated">La build a échoué. Corrigez les erreurs de la build et réexécutez-la.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
<target state="translated">Non è stato possibile convertire il valore della proprietà '{0}' in una stringa.</target>
<note />
</trans-unit>
<trans-unit id="RunCommandBuilding">
<source>Building...</source>
<target state="new">Building...</target>
<note />
</trans-unit>
<trans-unit id="RunCommandException">
<source>The build failed. Fix the build errors and run again.</source>
<target state="translated">La compilazione non è riuscita. Correggere gli errori di compilazione e ripetere l'esecuzione.</target>
Expand Down
Loading