From 45c99bb3275718aad44ad1ea14a137cce48fcb75 Mon Sep 17 00:00:00 2001 From: Muhammad Rehan Saeed Date: Thu, 26 Nov 2020 09:33:42 +0000 Subject: [PATCH] Stop passing --urls to dotnet run --- Source/Boxed.DotnetNewTest/Project.cs | 30 ++++++++++++++++++- .../Boxed.DotnetNewTest/ProjectExtensions.cs | 27 +++++------------ .../TempDirectoryExtensions.cs | 12 ++++++-- 3 files changed, 46 insertions(+), 23 deletions(-) diff --git a/Source/Boxed.DotnetNewTest/Project.cs b/Source/Boxed.DotnetNewTest/Project.cs index 8b6e6ee7..80dbd331 100644 --- a/Source/Boxed.DotnetNewTest/Project.cs +++ b/Source/Boxed.DotnetNewTest/Project.cs @@ -1,5 +1,7 @@ namespace Boxed.DotnetNewTest { + using System; + /// /// A project created from a project template. /// @@ -11,14 +13,20 @@ public class Project /// The name of the project. /// The directory path to the project. /// The publish directory path. + /// The HTTPS port. + /// The HTTP port. public Project( string name, string directoryPath, - string publishDirectoryPath) + string publishDirectoryPath, + int httpsPort, + int httpPort) { this.Name = name; this.DirectoryPath = directoryPath; this.PublishDirectoryPath = publishDirectoryPath; + this.HttpsPort = httpsPort; + this.HttpPort = httpPort; } /// @@ -35,5 +43,25 @@ public Project( /// Gets the publish directory path to the project. /// public string PublishDirectoryPath { get; } + + /// + /// Gets the HTTP port. + /// + public int HttpPort { get; } + + /// + /// Gets the HTTPS port. + /// + public int HttpsPort { get; } + + /// + /// Gets the HTTP URL. + /// + public Uri HttpUrl => new Uri($"http://localhost:{this.HttpPort}"); + + /// + /// Gets the HTTPS URL. + /// + public Uri HttpsUrl => new Uri($"https://localhost:{this.HttpsPort}"); } } diff --git a/Source/Boxed.DotnetNewTest/ProjectExtensions.cs b/Source/Boxed.DotnetNewTest/ProjectExtensions.cs index 3d388bda..af3d78e1 100644 --- a/Source/Boxed.DotnetNewTest/ProjectExtensions.cs +++ b/Source/Boxed.DotnetNewTest/ProjectExtensions.cs @@ -249,15 +249,12 @@ public static async Task DotnetRunAsync( throw new ArgumentNullException(nameof(action)); } - var httpPort = PortHelper.GetFreeTcpPort(); - var httpUrl = new Uri($"http://localhost:{httpPort}"); - var httpClientHandler = new HttpClientHandler() { AllowAutoRedirect = false, ServerCertificateCustomValidationCallback = validateCertificate ?? DefaultValidateCertificate, }; - var httpClient = new HttpClient(httpClientHandler) { BaseAddress = httpUrl }; + var httpClient = new HttpClient(httpClientHandler) { BaseAddress = project.HttpUrl }; var projectFilePath = Path.Combine(project.DirectoryPath, projectRelativeDirectoryPath); var dotnetRun = await DotnetRunInternalAsync( @@ -267,8 +264,7 @@ public static async Task DotnetRunAsync( projectFilePath, noRestore, timeout, - showShellWindow, - httpUrl) + showShellWindow) .ConfigureAwait(false); try @@ -320,18 +316,13 @@ public static async Task DotnetRunAsync( throw new ArgumentNullException(nameof(action)); } - var httpPort = PortHelper.GetFreeTcpPort(); - var httpsPort = PortHelper.GetFreeTcpPort(); - var httpUrl = new Uri($"http://localhost:{httpPort}"); - var httpsUrl = new Uri($"https://localhost:{httpsPort}"); - var httpClientHandler = new HttpClientHandler() { AllowAutoRedirect = false, ServerCertificateCustomValidationCallback = validateCertificate ?? DefaultValidateCertificate, }; - var httpClient = new HttpClient(httpClientHandler) { BaseAddress = httpUrl }; - var httpsClient = new HttpClient(httpClientHandler) { BaseAddress = httpsUrl }; + var httpClient = new HttpClient(httpClientHandler) { BaseAddress = project.HttpUrl }; + var httpsClient = new HttpClient(httpClientHandler) { BaseAddress = project.HttpsUrl }; var projectFilePath = Path.Combine(project.DirectoryPath, projectRelativeDirectoryPath); var dotnetRun = await DotnetRunInternalAsync( @@ -341,9 +332,7 @@ public static async Task DotnetRunAsync( projectFilePath, noRestore, timeout, - showShellWindow, - httpUrl, - httpsUrl) + showShellWindow) .ConfigureAwait(false); try @@ -426,18 +415,16 @@ private static async Task DotnetRunInternalAsync( string directoryPath, bool? noRestore, TimeSpan? timeout, - bool showShellWindow, - params Uri[] urls) + bool showShellWindow) { #pragma warning disable CA2000 // Dispose objects before losing scope. Object disposed below. var cancellationTokenSource = new CancellationTokenSource(); #pragma warning restore CA2000 // Dispose objects before losing scope. Object disposed below. var noRestoreArgument = noRestore is null ? null : "--no-restore"; - var urlsParameter = string.Join(";", urls.Select(x => x.ToString())); var task = AssertStartAsync( directoryPath, "dotnet", - $"run {noRestoreArgument} --urls {urlsParameter}", + $"run {noRestoreArgument}", showShellWindow, cancellationTokenSource.Token); diff --git a/Source/Boxed.DotnetNewTest/TempDirectoryExtensions.cs b/Source/Boxed.DotnetNewTest/TempDirectoryExtensions.cs index 0f7693cd..85240482 100644 --- a/Source/Boxed.DotnetNewTest/TempDirectoryExtensions.cs +++ b/Source/Boxed.DotnetNewTest/TempDirectoryExtensions.cs @@ -42,11 +42,14 @@ public static async Task DotnetNewAsync( stringBuilder.Append($" --name \"{name}\""); } + var httpsPort = PortHelper.GetFreeTcpPort(); + var httpPort = PortHelper.GetFreeTcpPort(); + if (arguments is not null) { foreach (var argument in arguments) { - stringBuilder.Append($" --{argument.Key} \"{argument.Value}\""); + stringBuilder.Append($" --{argument.Key} \"{Replace(argument.Value, httpsPort, httpPort)}\""); } } @@ -64,7 +67,12 @@ await ProcessExtensions var projectDirectoryPath = name == null ? tempDirectory.DirectoryPath : Path.Combine(tempDirectory.DirectoryPath, name); var publishDirectoryPath = Path.Combine(projectDirectoryPath, "Publish"); - return new Project(name, projectDirectoryPath, publishDirectoryPath); + return new Project(name, projectDirectoryPath, publishDirectoryPath, httpsPort, httpPort); } + + private static string Replace(string value, int httpsPort, int httpPort) => + value + .Replace("{HTTPS_PORT}", httpsPort.ToString()) + .Replace("{HTTP_PORT}", httpPort.ToString()); } }