2019-06-24 11:58:36 +02:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
|
using System.Web;
|
|
|
|
|
|
using System.Web.Mvc;
|
|
|
|
|
|
using System.Web.Routing;
|
2020-03-03 11:18:54 +00:00
|
|
|
|
using Umbraco.Configuration;
|
2020-03-03 14:15:30 +01:00
|
|
|
|
using Umbraco.Core.Configuration;
|
2019-06-24 11:58:36 +02:00
|
|
|
|
using Umbraco.Core.Composing;
|
|
|
|
|
|
using Umbraco.Core.IO;
|
|
|
|
|
|
using Umbraco.Core.Services;
|
|
|
|
|
|
using Umbraco.Core.Services.Implement;
|
2019-12-20 17:36:44 +01:00
|
|
|
|
using Umbraco.Core.Strings;
|
2019-10-29 00:25:03 +11:00
|
|
|
|
using Umbraco.ModelsBuilder.Embedded.BackOffice;
|
2019-06-24 11:58:36 +02:00
|
|
|
|
using Umbraco.Web;
|
|
|
|
|
|
using Umbraco.Web.Mvc;
|
2020-04-02 21:19:42 +11:00
|
|
|
|
using Umbraco.Web.WebAssets;
|
2019-06-24 11:58:36 +02:00
|
|
|
|
|
2019-10-29 00:25:03 +11:00
|
|
|
|
namespace Umbraco.ModelsBuilder.Embedded.Compose
|
2019-06-24 11:58:36 +02:00
|
|
|
|
{
|
2019-10-29 01:14:10 +11:00
|
|
|
|
internal class ModelsBuilderComponent : IComponent
|
2019-06-24 11:58:36 +02:00
|
|
|
|
{
|
2019-10-28 18:02:52 +11:00
|
|
|
|
private readonly IModelsBuilderConfig _config;
|
2019-12-18 18:55:00 +01:00
|
|
|
|
private readonly IShortStringHelper _shortStringHelper;
|
2019-10-28 18:02:52 +11:00
|
|
|
|
private readonly LiveModelsProvider _liveModelsProvider;
|
2019-10-28 19:08:42 +11:00
|
|
|
|
private readonly OutOfDateModelsStatus _outOfDateModels;
|
2019-06-24 11:58:36 +02:00
|
|
|
|
|
2019-12-24 09:08:47 +01:00
|
|
|
|
public ModelsBuilderComponent(IModelsBuilderConfig config, IShortStringHelper shortStringHelper, LiveModelsProvider liveModelsProvider, OutOfDateModelsStatus outOfDateModels)
|
2019-06-24 11:58:36 +02:00
|
|
|
|
{
|
|
|
|
|
|
_config = config;
|
2019-12-18 18:55:00 +01:00
|
|
|
|
_shortStringHelper = shortStringHelper;
|
2019-10-28 18:02:52 +11:00
|
|
|
|
_liveModelsProvider = liveModelsProvider;
|
2019-10-28 19:08:42 +11:00
|
|
|
|
_outOfDateModels = outOfDateModels;
|
2019-12-20 17:36:44 +01:00
|
|
|
|
_shortStringHelper = shortStringHelper;
|
2019-06-24 11:58:36 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Initialize()
|
|
|
|
|
|
{
|
|
|
|
|
|
// always setup the dashboard
|
|
|
|
|
|
// note: UmbracoApiController instances are automatically registered
|
|
|
|
|
|
InstallServerVars();
|
|
|
|
|
|
|
|
|
|
|
|
ContentModelBinder.ModelBindingException += ContentModelBinder_ModelBindingException;
|
|
|
|
|
|
|
2019-10-29 11:02:18 +11:00
|
|
|
|
if (_config.Enable)
|
|
|
|
|
|
FileService.SavingTemplate += FileService_SavingTemplate;
|
2019-06-24 11:58:36 +02:00
|
|
|
|
|
|
|
|
|
|
if (_config.ModelsMode.IsLiveNotPure())
|
2019-10-28 18:02:52 +11:00
|
|
|
|
_liveModelsProvider.Install();
|
2019-06-24 11:58:36 +02:00
|
|
|
|
|
|
|
|
|
|
if (_config.FlagOutOfDateModels)
|
2019-10-28 19:08:42 +11:00
|
|
|
|
_outOfDateModels.Install();
|
2019-06-24 11:58:36 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Terminate()
|
|
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
|
|
private void InstallServerVars()
|
|
|
|
|
|
{
|
|
|
|
|
|
// register our url - for the backoffice api
|
|
|
|
|
|
ServerVariablesParser.Parsing += (sender, serverVars) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!serverVars.ContainsKey("umbracoUrls"))
|
2019-10-23 14:11:24 +02:00
|
|
|
|
throw new ArgumentException("Missing umbracoUrls.");
|
2019-06-24 11:58:36 +02:00
|
|
|
|
var umbracoUrlsObject = serverVars["umbracoUrls"];
|
|
|
|
|
|
if (umbracoUrlsObject == null)
|
2019-10-23 14:11:24 +02:00
|
|
|
|
throw new ArgumentException("Null umbracoUrls");
|
2019-06-24 11:58:36 +02:00
|
|
|
|
if (!(umbracoUrlsObject is Dictionary<string, object> umbracoUrls))
|
2019-10-23 14:11:24 +02:00
|
|
|
|
throw new ArgumentException("Invalid umbracoUrls");
|
2019-06-24 11:58:36 +02:00
|
|
|
|
|
|
|
|
|
|
if (!serverVars.ContainsKey("umbracoPlugins"))
|
2019-10-23 14:11:24 +02:00
|
|
|
|
throw new ArgumentException("Missing umbracoPlugins.");
|
2019-06-24 11:58:36 +02:00
|
|
|
|
if (!(serverVars["umbracoPlugins"] is Dictionary<string, object> umbracoPlugins))
|
2019-10-23 14:11:24 +02:00
|
|
|
|
throw new ArgumentException("Invalid umbracoPlugins");
|
2019-06-24 11:58:36 +02:00
|
|
|
|
|
|
|
|
|
|
if (HttpContext.Current == null) throw new InvalidOperationException("HttpContext is null");
|
|
|
|
|
|
var urlHelper = new UrlHelper(new RequestContext(new HttpContextWrapper(HttpContext.Current), new RouteData()));
|
|
|
|
|
|
|
2019-10-29 01:14:10 +11:00
|
|
|
|
umbracoUrls["modelsBuilderBaseUrl"] = urlHelper.GetUmbracoApiServiceBaseUrl<ModelsBuilderDashboardController>(controller => controller.BuildModels());
|
2019-06-24 11:58:36 +02:00
|
|
|
|
umbracoPlugins["modelsBuilder"] = GetModelsBuilderSettings();
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private Dictionary<string, object> GetModelsBuilderSettings()
|
|
|
|
|
|
{
|
|
|
|
|
|
var settings = new Dictionary<string, object>
|
|
|
|
|
|
{
|
2019-10-29 11:02:18 +11:00
|
|
|
|
{"enabled", _config.Enable}
|
2019-06-24 11:58:36 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return settings;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Used to check if a template is being created based on a document type, in this case we need to
|
|
|
|
|
|
/// ensure the template markup is correct based on the model name of the document type
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
|
private void FileService_SavingTemplate(IFileService sender, Core.Events.SaveEventArgs<Core.Models.ITemplate> e)
|
|
|
|
|
|
{
|
|
|
|
|
|
// don't do anything if the factory is not enabled
|
|
|
|
|
|
// because, no factory = no models (even if generation is enabled)
|
|
|
|
|
|
if (!_config.EnableFactory) return;
|
|
|
|
|
|
|
|
|
|
|
|
// don't do anything if this special key is not found
|
|
|
|
|
|
if (!e.AdditionalData.ContainsKey("CreateTemplateForContentType")) return;
|
|
|
|
|
|
|
|
|
|
|
|
// ensure we have the content type alias
|
|
|
|
|
|
if (!e.AdditionalData.ContainsKey("ContentTypeAlias"))
|
|
|
|
|
|
throw new InvalidOperationException("The additionalData key: ContentTypeAlias was not found");
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var template in e.SavedEntities)
|
|
|
|
|
|
// if it is in fact a new entity (not been saved yet) and the "CreateTemplateForContentType" key
|
|
|
|
|
|
// is found, then it means a new template is being created based on the creation of a document type
|
|
|
|
|
|
if (!template.HasIdentity && string.IsNullOrWhiteSpace(template.Content))
|
|
|
|
|
|
{
|
|
|
|
|
|
// ensure is safe and always pascal cased, per razor standard
|
|
|
|
|
|
// + this is how we get the default model name in Umbraco.ModelsBuilder.Umbraco.Application
|
|
|
|
|
|
var alias = e.AdditionalData["ContentTypeAlias"].ToString();
|
|
|
|
|
|
var name = template.Name; // will be the name of the content type since we are creating
|
2019-12-20 17:36:44 +01:00
|
|
|
|
var className = UmbracoServices.GetClrName(_shortStringHelper, name, alias);
|
2019-06-24 11:58:36 +02:00
|
|
|
|
|
|
|
|
|
|
var modelNamespace = _config.ModelsNamespace;
|
|
|
|
|
|
|
|
|
|
|
|
// we do not support configuring this at the moment, so just let Umbraco use its default value
|
|
|
|
|
|
//var modelNamespaceAlias = ...;
|
|
|
|
|
|
|
|
|
|
|
|
var markup = ViewHelper.GetDefaultFileContent(
|
|
|
|
|
|
modelClassName: className,
|
|
|
|
|
|
modelNamespace: modelNamespace/*,
|
|
|
|
|
|
modelNamespaceAlias: modelNamespaceAlias*/);
|
|
|
|
|
|
|
|
|
|
|
|
//set the template content to the new markup
|
|
|
|
|
|
template.Content = markup;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void ContentModelBinder_ModelBindingException(object sender, ContentModelBinder.ModelBindingArgs args)
|
|
|
|
|
|
{
|
|
|
|
|
|
var sourceAttr = args.SourceType.Assembly.GetCustomAttribute<ModelsBuilderAssemblyAttribute>();
|
|
|
|
|
|
var modelAttr = args.ModelType.Assembly.GetCustomAttribute<ModelsBuilderAssemblyAttribute>();
|
|
|
|
|
|
|
|
|
|
|
|
// if source or model is not a ModelsBuider type...
|
|
|
|
|
|
if (sourceAttr == null || modelAttr == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
// if neither are ModelsBuilder types, give up entirely
|
|
|
|
|
|
if (sourceAttr == null && modelAttr == null)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
// else report, but better not restart (loops?)
|
|
|
|
|
|
args.Message.Append(" The ");
|
|
|
|
|
|
args.Message.Append(sourceAttr == null ? "view model" : "source");
|
|
|
|
|
|
args.Message.Append(" is a ModelsBuilder type, but the ");
|
|
|
|
|
|
args.Message.Append(sourceAttr != null ? "view model" : "source");
|
|
|
|
|
|
args.Message.Append(" is not. The application is in an unstable state and should be restarted.");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// both are ModelsBuilder types
|
2019-10-28 18:02:52 +11:00
|
|
|
|
var pureSource = sourceAttr.PureLive;
|
|
|
|
|
|
var pureModel = modelAttr.PureLive;
|
2019-06-24 11:58:36 +02:00
|
|
|
|
|
2019-10-28 18:02:52 +11:00
|
|
|
|
if (sourceAttr.PureLive || modelAttr.PureLive)
|
|
|
|
|
|
if (pureSource == false || pureModel == false)
|
|
|
|
|
|
{
|
2019-06-24 11:58:36 +02:00
|
|
|
|
// only one is pure - report, but better not restart (loops?)
|
2019-10-28 18:02:52 +11:00
|
|
|
|
args.Message.Append(pureSource
|
|
|
|
|
|
? " The content model is PureLive, but the view model is not."
|
|
|
|
|
|
: " The view model is PureLive, but the content model is not.");
|
|
|
|
|
|
args.Message.Append(" The application is in an unstable state and should be restarted.");
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2019-06-24 11:58:36 +02:00
|
|
|
|
// both are pure - report, and if different versions, restart
|
|
|
|
|
|
// if same version... makes no sense... and better not restart (loops?)
|
2019-10-28 18:02:52 +11:00
|
|
|
|
var sourceVersion = args.SourceType.Assembly.GetName().Version;
|
2019-06-24 11:58:36 +02:00
|
|
|
|
var modelVersion = args.ModelType.Assembly.GetName().Version;
|
2019-10-28 18:02:52 +11:00
|
|
|
|
args.Message.Append(" Both view and content models are PureLive, with ");
|
|
|
|
|
|
args.Message.Append(sourceVersion == modelVersion
|
|
|
|
|
|
? "same version. The application is in an unstable state and should be restarted."
|
|
|
|
|
|
: "different versions. The application is in an unstable state and is going to be restarted.");
|
|
|
|
|
|
args.Restart = sourceVersion != modelVersion;
|
|
|
|
|
|
}
|
2019-06-24 11:58:36 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|