From 9a04e0375661d88cd3b9db0a404741702fe332ce Mon Sep 17 00:00:00 2001 From: zidaneymar Date: Wed, 22 Jul 2020 16:50:53 +0800 Subject: [PATCH] dotnet runtime luis settings refactor, remove duplicate settings file --- Composer/plugins/runtimes/src/index.ts | 4 +- ...oft.BotFramework.Composer.Functions.csproj | 3 -- runtime/dotnet/azurefunctions/Startup.cs | 11 ++--- .../appsettings.Deployment.json | 4 -- .../appsettings.Development.json | 4 -- .../dotnet/azurefunctions/appsettings.json | 23 --------- runtime/dotnet/azurewebapp/Program.cs | 26 ++-------- .../azurewebapp/appsettings.Deployment.json | 4 -- .../azurewebapp/appsettings.Development.json | 4 -- runtime/dotnet/azurewebapp/appsettings.json | 23 --------- .../dotnet/core/ComposerBotPathExtensions.cs | 29 +++++++++++ runtime/dotnet/core/ComposerLuisExtensions.cs | 48 +++++++++++++++++++ runtime/dotnet/core/LuisConfigAdapter.cs | 21 -------- 13 files changed, 88 insertions(+), 116 deletions(-) delete mode 100644 runtime/dotnet/azurefunctions/appsettings.Deployment.json delete mode 100644 runtime/dotnet/azurefunctions/appsettings.Development.json delete mode 100644 runtime/dotnet/azurewebapp/appsettings.Deployment.json delete mode 100644 runtime/dotnet/azurewebapp/appsettings.Development.json create mode 100644 runtime/dotnet/core/ComposerBotPathExtensions.cs create mode 100644 runtime/dotnet/core/ComposerLuisExtensions.cs delete mode 100644 runtime/dotnet/core/LuisConfigAdapter.cs diff --git a/Composer/plugins/runtimes/src/index.ts b/Composer/plugins/runtimes/src/index.ts index 3f5ed59e6f..38c2d48a2c 100644 --- a/Composer/plugins/runtimes/src/index.ts +++ b/Composer/plugins/runtimes/src/index.ts @@ -46,9 +46,9 @@ export default async (composer: any): Promise => { let csproj = ''; // find publishing profile in list const profile = project.settings.publishTargets.find((p) => p.name === profileName); - if (profile.type === 'plugin-azure-publish') { + if (profile.type === 'azurePublish') { csproj = 'Microsoft.BotFramework.Composer.WebApp.csproj'; - } else if (profile.type === 'plugin-azure-functions-publish') { + } else if (profile.type === 'azureFunctionsPublish') { csproj = 'Microsoft.BotFramework.Composer.Functions.csproj'; } const publishFolder = path.join(runtimePath, 'bin', 'Release', 'netcoreapp3.1'); diff --git a/runtime/dotnet/azurefunctions/Microsoft.BotFramework.Composer.Functions.csproj b/runtime/dotnet/azurefunctions/Microsoft.BotFramework.Composer.Functions.csproj index 56562f912d..9e71db5d54 100644 --- a/runtime/dotnet/azurefunctions/Microsoft.BotFramework.Composer.Functions.csproj +++ b/runtime/dotnet/azurefunctions/Microsoft.BotFramework.Composer.Functions.csproj @@ -40,9 +40,6 @@ - - Always - Always diff --git a/runtime/dotnet/azurefunctions/Startup.cs b/runtime/dotnet/azurefunctions/Startup.cs index dae2d83eef..2aa26584d0 100644 --- a/runtime/dotnet/azurefunctions/Startup.cs +++ b/runtime/dotnet/azurefunctions/Startup.cs @@ -43,19 +43,16 @@ private IConfigurationRoot BuildConfiguration(string rootDirectory) var config = new ConfigurationBuilder(); // Config precedence 1: root app.settings - config - .SetBasePath(rootDirectory) - .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) - .UseLuisConfigAdapter() - .UseLuisSettings(); + config.SetBasePath(rootDirectory); + config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true); // Config precedence 2: ComposerDialogs/settings settings which are injected by the composer publish // Hard code the settings path to 'ComposerDialogs' for deployment + var configFile = Path.GetFullPath(Path.Combine(rootDirectory, @"ComposerDialogs/settings/appsettings.json")); config.AddJsonFile(configFile, optional: true, reloadOnChange: true); - // Config Precedence 3: Deployment specific config - config.AddJsonFile("appsettings.deployment.json", optional: true, reloadOnChange: true); + config.UseComposerLuisSettings(); if (!Debugger.IsAttached) { diff --git a/runtime/dotnet/azurefunctions/appsettings.Deployment.json b/runtime/dotnet/azurefunctions/appsettings.Deployment.json deleted file mode 100644 index 5b9a266f9e..0000000000 --- a/runtime/dotnet/azurefunctions/appsettings.Deployment.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "bot": "ComposerDialogs", - "root": "ComposerDialogs" -} \ No newline at end of file diff --git a/runtime/dotnet/azurefunctions/appsettings.Development.json b/runtime/dotnet/azurefunctions/appsettings.Development.json deleted file mode 100644 index 32b3ba9cf9..0000000000 --- a/runtime/dotnet/azurefunctions/appsettings.Development.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "bot": "../", - "root": "../" -} diff --git a/runtime/dotnet/azurefunctions/appsettings.json b/runtime/dotnet/azurefunctions/appsettings.json index 8b60e096e8..2c63c08510 100644 --- a/runtime/dotnet/azurefunctions/appsettings.json +++ b/runtime/dotnet/azurefunctions/appsettings.json @@ -1,25 +1,2 @@ { - "feature": { - "UseShowTypingMiddleware": false, - "UseInspectionMiddleware": false, - "RemoveRecipientMention": false - }, - "microsoftAppId": "", - "cosmosDb": { - "authKey": "", - "collectionId": "botstate-collection", - "cosmosDBEndpoint": "", - "databaseId": "botstate-db" - }, - "applicationInsights": { - "InstrumentationKey": "" - }, - "telemetry": { - "logPersonalInformation": false, - "logActivities": true - }, - "blobStorage": { - "connectionString": "", - "container": "transcripts" - } } diff --git a/runtime/dotnet/azurewebapp/Program.cs b/runtime/dotnet/azurewebapp/Program.cs index e080ee0004..1b6fb3dec2 100644 --- a/runtime/dotnet/azurewebapp/Program.cs +++ b/runtime/dotnet/azurewebapp/Program.cs @@ -1,13 +1,8 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. -using System; -using System.Collections.Generic; -using System.Diagnostics; using System.IO; -using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; -using Microsoft.Bot.Builder.AI.Luis; using Microsoft.BotFramework.Composer.Core; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; @@ -27,15 +22,11 @@ public static IHostBuilder CreateHostBuilder(string[] args) => { var env = hostingContext.HostingEnvironment; - builder.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true); - - if (env.IsDevelopment()) - { - // Local Debug - builder.AddJsonFile("appsettings.development.json", optional: true, reloadOnChange: true); - } + // Use Composer bot path adapter + builder.UseBotPathConverter(env.IsDevelopment()); var configuration = builder.Build(); + builder.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true); // Hard code the settings path to 'ComposerDialogs' while deployment var botRoot = configuration.GetValue("bot") ?? "ComposerDialogs"; @@ -43,15 +34,8 @@ public static IHostBuilder CreateHostBuilder(string[] args) => builder.AddJsonFile(configFile, optional: true, reloadOnChange: true); - // Need to put this part here to override the any customized settings - if (!env.IsDevelopment()) - { - //Azure Deploy - builder.AddJsonFile("appsettings.deployment.json", optional: true, reloadOnChange: true); - } - - builder.UseLuisConfigAdapter() - .UseLuisSettings(); + // Use Composer luis settings extensions + builder.UseComposerLuisSettings(); builder.AddEnvironmentVariables() .AddCommandLine(args); diff --git a/runtime/dotnet/azurewebapp/appsettings.Deployment.json b/runtime/dotnet/azurewebapp/appsettings.Deployment.json deleted file mode 100644 index 5b9a266f9e..0000000000 --- a/runtime/dotnet/azurewebapp/appsettings.Deployment.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "bot": "ComposerDialogs", - "root": "ComposerDialogs" -} \ No newline at end of file diff --git a/runtime/dotnet/azurewebapp/appsettings.Development.json b/runtime/dotnet/azurewebapp/appsettings.Development.json deleted file mode 100644 index edb1022967..0000000000 --- a/runtime/dotnet/azurewebapp/appsettings.Development.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "bot": "../../", - "root": "../../" -} diff --git a/runtime/dotnet/azurewebapp/appsettings.json b/runtime/dotnet/azurewebapp/appsettings.json index 8b60e096e8..2c63c08510 100644 --- a/runtime/dotnet/azurewebapp/appsettings.json +++ b/runtime/dotnet/azurewebapp/appsettings.json @@ -1,25 +1,2 @@ { - "feature": { - "UseShowTypingMiddleware": false, - "UseInspectionMiddleware": false, - "RemoveRecipientMention": false - }, - "microsoftAppId": "", - "cosmosDb": { - "authKey": "", - "collectionId": "botstate-collection", - "cosmosDBEndpoint": "", - "databaseId": "botstate-db" - }, - "applicationInsights": { - "InstrumentationKey": "" - }, - "telemetry": { - "logPersonalInformation": false, - "logActivities": true - }, - "blobStorage": { - "connectionString": "", - "container": "transcripts" - } } diff --git a/runtime/dotnet/core/ComposerBotPathExtensions.cs b/runtime/dotnet/core/ComposerBotPathExtensions.cs new file mode 100644 index 0000000000..f2d2e953fc --- /dev/null +++ b/runtime/dotnet/core/ComposerBotPathExtensions.cs @@ -0,0 +1,29 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using Microsoft.Extensions.Configuration; +using System.Collections.Generic; + +namespace Microsoft.BotFramework.Composer.Core +{ + /// + /// Bot path adapter, for development environment, use '../../' as the bot path, for deployment and production environment, use 'ComposerDialogs' as bot path + /// + public static class ComposerBotPathExtensions + { + public static IConfigurationBuilder UseBotPathConverter(this IConfigurationBuilder builder, bool isDevelopment = true) + { + var settings = new Dictionary(); + if (isDevelopment) + { + settings["bot"] = "../../"; + } + else + { + settings["bot"] = "ComposerDialogs"; + } + builder.AddInMemoryCollection(settings); + return builder; + } + } +} diff --git a/runtime/dotnet/core/ComposerLuisExtensions.cs b/runtime/dotnet/core/ComposerLuisExtensions.cs new file mode 100644 index 0000000000..1fc3943123 --- /dev/null +++ b/runtime/dotnet/core/ComposerLuisExtensions.cs @@ -0,0 +1,48 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using Microsoft.Extensions.Configuration; +using System; +using System.Collections.Generic; +using System.IO; + +namespace Microsoft.BotFramework.Composer.Core +{ + public static class ComposerLuisExtensions + { + /// + /// Setup configuration to utilize the settings file generated by bf luis:build. This is a luis settings extensions adapter aligning with Composer customized settings. + /// + /// + /// This will pick up LUIS_AUTHORING_REGION or --region settings as the setting to target. + /// This will pick up --environment as the environment to target. If environment is Development it will use the name of the logged in user. + /// This will pick up --root as the root folder to run in. + /// + /// Configuration builder to modify. + /// Modified configuration builder. + public static IConfigurationBuilder UseComposerLuisSettings(this IConfigurationBuilder builder) + { + var configuration = builder.Build(); + var botRoot = configuration.GetValue("bot") ?? "."; + var luisRegion = configuration.GetValue("LUIS_AUTHORING_REGION") ?? configuration.GetValue("luis:authoringRegion") ?? configuration.GetValue("luis:region") ?? "westus"; + var environment = configuration.GetValue("luis:environment") ?? Environment.UserName; + var settings = new Dictionary(); + settings["luis:endpoint"] = configuration.GetValue("luis:endpoint") ?? $"https://{luisRegion}.api.cognitive.microsoft.com"; + settings["BotRoot"] = botRoot; + builder.AddInMemoryCollection(settings); + if (environment == "Development") + { + environment = Environment.UserName; + } + + var settingsPath = Path.GetFullPath(Path.Combine(botRoot, "generated", $"luis.settings.{environment.ToLower()}.{luisRegion}.json")); + var settingsFile = new FileInfo(settingsPath); + if (settingsFile.Exists) + { + builder.AddJsonFile(settingsFile.FullName, optional: false, reloadOnChange: true); + } + + return builder; + } + } +} diff --git a/runtime/dotnet/core/LuisConfigAdapter.cs b/runtime/dotnet/core/LuisConfigAdapter.cs deleted file mode 100644 index c879fd1494..0000000000 --- a/runtime/dotnet/core/LuisConfigAdapter.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.Extensions.Configuration; - -namespace Microsoft.BotFramework.Composer.Core -{ - public static class LuisConfigAdapter - { - public static IConfigurationBuilder UseLuisConfigAdapter(this IConfigurationBuilder builder) - { - var configuration = builder.Build(); - var settings = new Dictionary(); - settings["environment"] = configuration.GetValue("luis:environment"); - settings["region"] = configuration.GetValue("luis:authoringRegion"); - builder.AddInMemoryCollection(settings); - return builder; - } - } -}