Add runtime mode (BackofficeDevelopment, Development and Production) (#12631)

* Add runtime mode setting

* Only enable Razor runtime compilation in development modes

* Only enable ModelsBuilder generation in development modes

* Fix disabling ModelsBuilder controllers

* Add IRuntimeModeValidationService and IRuntimeModeValidator

* Add JITOptimizerValidator

* Add UmbracoApplicationUrlValidator

* Add UseHttpsValidator

* Add RuntimeMinificationValidator

* Add ModelsBuilderModeValidator

* Remove .NET 6 preview 1 fix for Razor runtime compilation

* Only allow InMemoryAuto in backoffice development mode

* Make runtime mode validators public, so they can be easily removed if required

* Add comment to highlight removing RazorCompileOnBuild, RazorCompileOnPublish and CopyRazorGenerateFilesToPublishDirectory when using ModelsMode InMemoryAuto

* Add documentation

* Update src/Umbraco.Web.Common/ModelsBuilder/NoopModelsBuilderDashboardProvider.cs

Co-authored-by: Ronald Barendse <ronald@barend.se>

Co-authored-by: Nikolaj Geisle <70372949+Zeegaan@users.noreply.github.com>
This commit is contained in:
Ronald Barendse
2022-07-01 08:48:05 +02:00
committed by GitHub
parent 7d545a7dfc
commit dca4d0f167
25 changed files with 791 additions and 420 deletions

View File

@@ -23,39 +23,38 @@ using Umbraco.Cms.Web.BackOffice.Trees;
namespace Umbraco.Extensions;
/// <summary>
/// Extension methods for <see cref="IUmbracoBuilder" /> for the Umbraco back office
/// Extension methods for <see cref="IUmbracoBuilder"/> for the Umbraco back office
/// </summary>
public static partial class UmbracoBuilderExtensions
{
/// <summary>
/// Adds all required components to run the Umbraco back office
/// Adds all required components to run the Umbraco back office
/// </summary>
public static IUmbracoBuilder
AddBackOffice(this IUmbracoBuilder builder, Action<IMvcBuilder>? configureMvc = null) => builder
.AddConfiguration()
.AddUmbracoCore()
.AddWebComponents()
.AddRuntimeMinifier()
.AddBackOfficeCore()
.AddBackOfficeAuthentication()
.AddBackOfficeIdentity()
.AddMembersIdentity()
.AddBackOfficeAuthorizationPolicies()
.AddUmbracoProfiler()
.AddMvcAndRazor(configureMvc)
.AddWebServer()
.AddPreviewSupport()
.AddHostedServices()
.AddNuCache()
.AddDistributedCache()
.AddModelsBuilderDashboard()
.AddUnattendedInstallInstallCreateUser()
.AddCoreNotifications()
.AddLogViewer()
.AddExamine()
.AddExamineIndexes()
.AddControllersWithAmbiguousConstructors()
.AddSupplemenataryLocalizedTextFileSources();
public static IUmbracoBuilder AddBackOffice(this IUmbracoBuilder builder, Action<IMvcBuilder>? configureMvc = null) => builder
.AddConfiguration()
.AddUmbracoCore()
.AddWebComponents()
.AddRuntimeMinifier()
.AddBackOfficeCore()
.AddBackOfficeAuthentication()
.AddBackOfficeIdentity()
.AddMembersIdentity()
.AddBackOfficeAuthorizationPolicies()
.AddUmbracoProfiler()
.AddMvcAndRazor(configureMvc)
.AddWebServer()
.AddPreviewSupport()
.AddHostedServices()
.AddNuCache()
.AddDistributedCache()
.TryAddModelsBuilderDashboard()
.AddUnattendedInstallInstallCreateUser()
.AddCoreNotifications()
.AddLogViewer()
.AddExamine()
.AddExamineIndexes()
.AddControllersWithAmbiguousConstructors()
.AddSupplemenataryLocalizedTextFileSources();
public static IUmbracoBuilder AddUnattendedInstallInstallCreateUser(this IUmbracoBuilder builder)
{

View File

@@ -5,7 +5,7 @@ using Umbraco.Cms.Core.Notifications;
namespace Umbraco.Cms.Web.BackOffice.ModelsBuilder;
/// <summary>
/// Used in conjunction with <see cref="UmbracoBuilderExtensions.DisableModelsBuilderControllers" />
/// Used in conjunction with <see cref="UmbracoBuilderExtensions.DisableModelsBuilderControllers"/>
/// </summary>
internal class DisableModelsBuilderNotificationHandler : INotificationHandler<UmbracoApplicationStartingNotification>
{

View File

@@ -1,30 +1,59 @@
using Microsoft.Extensions.DependencyInjection;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.Dashboards;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Core.Notifications;
using Umbraco.Cms.Web.Common.Controllers;
using Umbraco.Cms.Web.Common.ModelsBuilder;
using Umbraco.Extensions;
namespace Umbraco.Cms.Web.BackOffice.ModelsBuilder;
/// <summary>
/// Extension methods for <see cref="IUmbracoBuilder" /> for the common Umbraco functionality
/// Extension methods for <see cref="IUmbracoBuilder"/> for the common Umbraco functionality
/// </summary>
public static class UmbracoBuilderExtensions
{
/// <summary>
/// Adds the ModelsBuilder dashboard.
/// Adds the ModelsBuilder dashboard, but only when not in production mode.
/// </summary>
public static IUmbracoBuilder AddModelsBuilderDashboard(this IUmbracoBuilder builder)
internal static IUmbracoBuilder TryAddModelsBuilderDashboard(this IUmbracoBuilder builder)
{
builder.Services.AddUnique<IModelsBuilderDashboardProvider, ModelsBuilderDashboardProvider>();
if (builder.Config.GetRuntimeMode() == RuntimeMode.Production)
{
builder.RemoveModelsBuilderDashboard();
}
else
{
builder.AddModelsBuilderDashboard();
}
return builder;
}
/// <summary>
/// Can be called if using an external models builder to remove the embedded models builder controller features
/// Adds the ModelsBuilder dashboard (dashboard and API controller are automatically added).
/// </summary>
public static IUmbracoBuilder DisableModelsBuilderControllers(this IUmbracoBuilder builder)
public static IUmbracoBuilder AddModelsBuilderDashboard(this IUmbracoBuilder builder)
{
builder.Services.AddSingleton<DisableModelsBuilderNotificationHandler>();
builder.Services.AddUnique<IModelsBuilderDashboardProvider, ModelsBuilderDashboardProvider>();
return builder;
}
/// <summary>
/// Removes the ModelsBuilder dashboard (and API controller).
/// </summary>
public static IUmbracoBuilder RemoveModelsBuilderDashboard(this IUmbracoBuilder builder)
{
builder.Dashboards().Remove<ModelsBuilderDashboard>();
builder.WithCollectionBuilder<UmbracoApiControllerTypeCollectionBuilder>().Remove<ModelsBuilderDashboardController>();
return builder;
}
/// <summary>
/// Can be called if using an external models builder to remove the embedded models builder controller features.
/// </summary>
public static IUmbracoBuilder DisableModelsBuilderControllers(this IUmbracoBuilder builder)
=> builder.AddNotificationHandler<UmbracoApplicationStartingNotification, DisableModelsBuilderNotificationHandler>();
}