From 27acbdade69c6b98c7d1ad1f16987ab51fa88a47 Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Mon, 21 Nov 2022 14:25:55 +0800 Subject: [PATCH 1/3] Fix RemoveUnnecessaryPortsStep --- .../Templates/RemoveUnnecessaryPortsStep.cs | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Templates/RemoveUnnecessaryPortsStep.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Templates/RemoveUnnecessaryPortsStep.cs index be5948c8b6c..4996585e781 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Templates/RemoveUnnecessaryPortsStep.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Templates/RemoveUnnecessaryPortsStep.cs @@ -25,35 +25,44 @@ private static void RemoveUnnecessaryHttpApiHostPorts(ProjectBuildContext contex return; } - var portsToRemoveFromCors = new List(); + var portsToRemove = new List(); var appSettingsJson = JObject.Parse(httpApiHostAppSettings.Content); var appJson = (JObject)appSettingsJson["App"]; if (context.BuildArgs.UiFramework != UiFramework.Angular) { - var clientUrl = appJson.Property("ClientUrl")?.ToString(); - portsToRemoveFromCors.Add("http://localhost:4200"); + var angularUrl = appJson.Property("AngularUrl")?.ToString() ?? appJson.Property("ClientUrl")?.ToString(); + + portsToRemove.Add("http://localhost:4200"); - if (!clientUrl.IsNullOrWhiteSpace()) + if (!angularUrl.IsNullOrWhiteSpace()) { - httpApiHostAppSettings.SetContent(httpApiHostAppSettings.Content.Replace(clientUrl, string.Empty)); + httpApiHostAppSettings.SetContent(httpApiHostAppSettings.Content.Replace($"{angularUrl.EnsureEndsWith(',')}\n ", string.Empty)); } } if (context.BuildArgs.UiFramework != UiFramework.Blazor) { - portsToRemoveFromCors.Add("https://localhost:44307"); + portsToRemove.Add("https://localhost:44307"); } if (appJson["CorsOrigins"] != null) { var corsOrigins = appJson["CorsOrigins"].ToString(); - var newCorsOrigins = string.Join(",", corsOrigins.Split(',').Where(x => !portsToRemoveFromCors.Contains(x))); + var newCorsOrigins = string.Join(",", corsOrigins.Split(',').Where(x => !portsToRemove.Contains(x))); httpApiHostAppSettings.SetContent(httpApiHostAppSettings.Content.Replace(corsOrigins, newCorsOrigins)); } + + if (appJson["RedirectAllowedUrls"] != null) + { + var redirectAllowedUrls = appJson["RedirectAllowedUrls"].ToString(); + var newRedirectAllowedUrls = string.Join(",", redirectAllowedUrls.Split(',').Where(x => !portsToRemove.Contains(x))); + + httpApiHostAppSettings.SetContent(httpApiHostAppSettings.Content.Replace(redirectAllowedUrls, newRedirectAllowedUrls)); + } } private static void RemoveUnnecessaryDbMigratorClients(ProjectBuildContext context) From 07164249eea7d3f8956d97b1631a24870debd8c0 Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Mon, 21 Nov 2022 15:25:06 +0800 Subject: [PATCH 2/3] Update RemoveUnnecessaryPortsStep --- .../Templates/RemoveUnnecessaryPortsStep.cs | 42 ++++++++++--------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Templates/RemoveUnnecessaryPortsStep.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Templates/RemoveUnnecessaryPortsStep.cs index 4996585e781..63d8443a5cf 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Templates/RemoveUnnecessaryPortsStep.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Templates/RemoveUnnecessaryPortsStep.cs @@ -25,21 +25,18 @@ private static void RemoveUnnecessaryHttpApiHostPorts(ProjectBuildContext contex return; } + var lines = httpApiHostAppSettings.GetLines(); + var newlines = new List(); var portsToRemove = new List(); + var removeAngularUrl = false; var appSettingsJson = JObject.Parse(httpApiHostAppSettings.Content); var appJson = (JObject)appSettingsJson["App"]; if (context.BuildArgs.UiFramework != UiFramework.Angular) { - var angularUrl = appJson.Property("AngularUrl")?.ToString() ?? appJson.Property("ClientUrl")?.ToString(); - + removeAngularUrl = true; portsToRemove.Add("http://localhost:4200"); - - if (!angularUrl.IsNullOrWhiteSpace()) - { - httpApiHostAppSettings.SetContent(httpApiHostAppSettings.Content.Replace($"{angularUrl.EnsureEndsWith(',')}\n ", string.Empty)); - } } if (context.BuildArgs.UiFramework != UiFramework.Blazor) @@ -47,22 +44,27 @@ private static void RemoveUnnecessaryHttpApiHostPorts(ProjectBuildContext contex portsToRemove.Add("https://localhost:44307"); } - - if (appJson["CorsOrigins"] != null) + foreach (var line in lines) { - var corsOrigins = appJson["CorsOrigins"].ToString(); - var newCorsOrigins = string.Join(",", corsOrigins.Split(',').Where(x => !portsToRemove.Contains(x))); + var newLine = line; + if(removeAngularUrl && (line.Contains("AngularUrl")|| line.Contains("ClientUrl"))) + { + continue; + } - httpApiHostAppSettings.SetContent(httpApiHostAppSettings.Content.Replace(corsOrigins, newCorsOrigins)); - } - - if (appJson["RedirectAllowedUrls"] != null) - { - var redirectAllowedUrls = appJson["RedirectAllowedUrls"].ToString(); - var newRedirectAllowedUrls = string.Join(",", redirectAllowedUrls.Split(',').Where(x => !portsToRemove.Contains(x))); - - httpApiHostAppSettings.SetContent(httpApiHostAppSettings.Content.Replace(redirectAllowedUrls, newRedirectAllowedUrls)); + if(line.Contains("CorsOrigins") || line.Contains("RedirectAllowedUrls")) + { + var jsonValue = line.Contains("CorsOrigins") ? appJson["CorsOrigins"]?.ToString() : appJson["RedirectAllowedUrls"]?.ToString(); + if(!jsonValue.IsNullOrWhiteSpace()) + { + newLine = line.Replace(jsonValue, string.Join(",", jsonValue.Split(',').Where(x => !portsToRemove.Contains(x)))); + } + } + + newlines.Add(newLine); } + + httpApiHostAppSettings.SetContent(string.Join(Environment.NewLine, newlines)); } private static void RemoveUnnecessaryDbMigratorClients(ProjectBuildContext context) From f12fd7bd17ed4e620d4882d8f409cbaedb71f702 Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Mon, 21 Nov 2022 15:29:49 +0800 Subject: [PATCH 3/3] Update RemoveUnnecessaryPortsStep --- .../ProjectBuilding/Templates/RemoveUnnecessaryPortsStep.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Templates/RemoveUnnecessaryPortsStep.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Templates/RemoveUnnecessaryPortsStep.cs index 63d8443a5cf..2c232bcecfd 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Templates/RemoveUnnecessaryPortsStep.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Templates/RemoveUnnecessaryPortsStep.cs @@ -63,8 +63,8 @@ private static void RemoveUnnecessaryHttpApiHostPorts(ProjectBuildContext contex newlines.Add(newLine); } - - httpApiHostAppSettings.SetContent(string.Join(Environment.NewLine, newlines)); + + httpApiHostAppSettings.SetLines(newlines); } private static void RemoveUnnecessaryDbMigratorClients(ProjectBuildContext context)