Changes namespaces, removes legacy PureLiveAssemblyAttribute

This commit is contained in:
Shannon
2019-10-28 21:53:32 +11:00
parent 51f89410ce
commit adbd2f0cca
22 changed files with 49 additions and 87 deletions

View File

@@ -2,7 +2,7 @@
using System.Reflection;
using Semver;
namespace Umbraco.ModelsBuilder.Api
namespace Umbraco.ModelsBuilder
{
/// <summary>
/// Manages API version handshake between client and server.

View File

@@ -8,12 +8,12 @@ using Umbraco.ModelsBuilder.Configuration;
using Umbraco.Web.Editors;
using Umbraco.Web.Models.ContentEditing;
namespace Umbraco.ModelsBuilder.Validation
namespace Umbraco.ModelsBuilder.BackOffice
{
/// <summary>
/// Used to validate the aliases for the content type when MB is enabled to ensure that
/// no illegal aliases are used
/// </summary>
/// Used to validate the aliases for the content type when MB is enabled to ensure that
/// no illegal aliases are used
/// </summary>
public class ContentTypeModelValidator : ContentTypeModelValidatorBase<DocumentTypeSave, PropertyTypeBasic>
{ }
@@ -32,8 +32,8 @@ namespace Umbraco.ModelsBuilder.Validation
{ }
public abstract class ContentTypeModelValidatorBase<TModel, TProperty> : EditorValidator<TModel>
where TModel: ContentTypeSave<TProperty>
where TProperty: PropertyTypeBasic
where TModel : ContentTypeSave<TProperty>
where TProperty : PropertyTypeBasic
{
protected override IEnumerable<ValidationResult> Validate(TModel model)
{
@@ -57,9 +57,7 @@ namespace Umbraco.ModelsBuilder.Validation
var validationResult = ValidateProperty(prop, groupIndex, propertyIndex);
if (validationResult != null)
{
yield return validationResult;
}
}
}
@@ -73,13 +71,11 @@ namespace Umbraco.ModelsBuilder.Validation
var alias = property.Alias;
if (reservedProperties.InvariantContains(alias) || reservedMethods.InvariantContains(alias))
{
return new ValidationResult(
string.Format("The alias {0} is a reserved term and cannot be used", alias), new[]
{
string.Format("Groups[{0}].Properties[{1}].Alias", groupIndex, propertyIndex)
});
}
return null;
}

View File

@@ -1,7 +1,6 @@
using System.Text;
using Umbraco.Core.Composing;
using Umbraco.ModelsBuilder.Configuration;
using Umbraco.ModelsBuilder.Umbraco;
namespace Umbraco.ModelsBuilder.BackOffice
{
@@ -29,7 +28,7 @@ namespace Umbraco.ModelsBuilder.BackOffice
var sb = new StringBuilder();
sb.Append("Version: ");
sb.Append(Api.ApiVersion.Current.Version);
sb.Append(ApiVersion.Current.Version);
sb.Append("<br />&nbsp;<br />");
sb.Append("ModelsBuilder is enabled, with the following configuration:");

View File

@@ -9,7 +9,6 @@ using System.Web.Hosting;
using Umbraco.Core.Exceptions;
using Umbraco.ModelsBuilder.Building;
using Umbraco.ModelsBuilder.Configuration;
using Umbraco.ModelsBuilder.Umbraco;
using Umbraco.Web.Editors;
using Umbraco.Web.WebApi.Filters;

View File

@@ -38,8 +38,7 @@ namespace Umbraco.ModelsBuilder.Building
"Umbraco.Core.Models",
"Umbraco.Core.Models.PublishedContent",
"Umbraco.Web",
"Umbraco.ModelsBuilder",
"Umbraco.ModelsBuilder.Umbraco",
"Umbraco.ModelsBuilder"
};
/// <summary>

View File

@@ -2,11 +2,9 @@
using System.IO;
using System.Text;
using System.Web;
using Umbraco.ModelsBuilder.Building;
using Umbraco.ModelsBuilder.Configuration;
using Umbraco.ModelsBuilder.Umbraco;
namespace Umbraco.ModelsBuilder.Umbraco
namespace Umbraco.ModelsBuilder.Building
{
public class ModelsGenerator
{

View File

@@ -5,7 +5,6 @@ using System.Text;
using System.Text.RegularExpressions;
using Umbraco.Core.Composing;
using Umbraco.Core.Configuration;
using Umbraco.ModelsBuilder.Api;
using Umbraco.ModelsBuilder.Configuration;
namespace Umbraco.ModelsBuilder.Building

View File

@@ -1,5 +1,4 @@
using System.Text;
using Umbraco.ModelsBuilder.Api;
namespace Umbraco.ModelsBuilder.Building
{

View File

@@ -1,10 +1,9 @@
using System.Collections.Generic;
using System.Linq;
using Umbraco.ModelsBuilder.Building;
namespace Umbraco.ModelsBuilder.Umbraco
namespace Umbraco.ModelsBuilder.Building
{
internal class ModelsBuilderHasher
internal class TypeModelHasher
{
public static string Hash(IEnumerable<TypeModel> typeModels)
{

View File

@@ -10,7 +10,6 @@ using Umbraco.Core.Services;
using Umbraco.Core.Services.Implement;
using Umbraco.ModelsBuilder.BackOffice;
using Umbraco.ModelsBuilder.Configuration;
using Umbraco.ModelsBuilder.Umbraco;
using Umbraco.Web;
using Umbraco.Web.JavaScript;
using Umbraco.Web.Mvc;

View File

@@ -2,8 +2,8 @@
using Umbraco.Core;
using Umbraco.Core.Composing;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.ModelsBuilder.Building;
using Umbraco.ModelsBuilder.Configuration;
using Umbraco.ModelsBuilder.Umbraco;
using Umbraco.Web.PublishedCache.NuCache;
namespace Umbraco.ModelsBuilder.Compose

View File

@@ -5,13 +5,13 @@ using System.Web.Hosting;
using Umbraco.Core.Exceptions;
using Umbraco.Core.IO;
using Umbraco.Core.Logging;
using Umbraco.ModelsBuilder.Building;
using Umbraco.ModelsBuilder.Configuration;
using Umbraco.ModelsBuilder.Umbraco;
using Umbraco.Web.Cache;
namespace Umbraco.ModelsBuilder.Umbraco
namespace Umbraco.ModelsBuilder
{
// supports LiveDll and LiveAppData - but not PureLive
// supports LiveAppData - but not PureLive
public sealed class LiveModelsProvider
{
private static Mutex _mutex;
@@ -80,7 +80,7 @@ namespace Umbraco.ModelsBuilder.Umbraco
try
{
_logger.Debug<LiveModelsProvider>("Generate models...");
const int timeout = 2*60*1000; // 2 mins
const int timeout = 2 * 60 * 1000; // 2 mins
_mutex.WaitOne(timeout); // wait until it is safe, and acquire
_logger.Info<LiveModelsProvider>("Generate models now.");
GenerateModels();
@@ -112,6 +112,6 @@ namespace Umbraco.ModelsBuilder.Umbraco
_modelGenerator.GenerateModels();
}
}
}

View File

@@ -2,12 +2,12 @@
using System.Web;
using Umbraco.Core;
using Umbraco.Core.Composing;
using Umbraco.ModelsBuilder.Umbraco;
using Umbraco.ModelsBuilder;
// will install only if configuration says it needs to be installed
[assembly: PreApplicationStartMethod(typeof(LiveModelsProviderModule), "Install")]
namespace Umbraco.ModelsBuilder.Umbraco
namespace Umbraco.ModelsBuilder
{
// have to do this because it's the only way to subscribe to EndRequest,
// module is installed by assembly attribute at the top of this file

View File

@@ -4,7 +4,7 @@ using System.Text;
using Umbraco.Core.Composing;
using Umbraco.ModelsBuilder.Configuration;
namespace Umbraco.ModelsBuilder.Umbraco
namespace Umbraco.ModelsBuilder
{
public sealed class ModelsGenerationError
{

View File

@@ -3,7 +3,7 @@ using Umbraco.Core.Composing;
using Umbraco.ModelsBuilder.Configuration;
using Umbraco.Web.Cache;
namespace Umbraco.ModelsBuilder.Umbraco
namespace Umbraco.ModelsBuilder
{
public sealed class OutOfDateModelsStatus
{

View File

@@ -5,7 +5,7 @@ using System.Linq.Expressions;
using Umbraco.Web.Composing;
using Umbraco.Core.Models.PublishedContent;
namespace Umbraco.ModelsBuilder.Umbraco
namespace Umbraco.ModelsBuilder
{
/// <summary>
/// This is called from within the generated model classes
@@ -48,7 +48,7 @@ namespace Umbraco.ModelsBuilder.Umbraco
}
public static IPublishedPropertyType GetModelPropertyType<TModel, TValue>(IPublishedContentType contentType, Expression<Func<TModel, TValue>> selector)
//where TModel : PublishedContentModel // fixme PublishedContentModel _or_ PublishedElementModel
//where TModel : PublishedContentModel // fixme PublishedContentModel _or_ PublishedElementModel
{
// fixme therefore, missing a check on TModel here
@@ -61,7 +61,7 @@ namespace Umbraco.ModelsBuilder.Umbraco
// see note above : accepted risk...
var attr = expr.Member
.GetCustomAttributes(typeof (ImplementPropertyTypeAttribute), false)
.GetCustomAttributes(typeof(ImplementPropertyTypeAttribute), false)
.OfType<ImplementPropertyTypeAttribute>()
.SingleOrDefault();

View File

@@ -1,15 +0,0 @@
using System;
namespace Umbraco.ModelsBuilder
{
/// <summary>
/// Indicates that an Assembly is a PureLive models assembly.
/// </summary>
/// <remarks>Though technically not required, ie models will work without it, the attribute
/// can be used by Umbraco view models binder to figure out whether the model type comes
/// from a PureLive Assembly.</remarks>
[Obsolete("Should use ModelsBuilderAssemblyAttribute but that requires a change in Umbraco Core.")]
[AttributeUsage(AttributeTargets.Assembly /*, AllowMultiple = false, Inherited = false*/)]
public sealed class PureLiveAssemblyAttribute : Attribute
{ }
}

View File

@@ -21,7 +21,7 @@ using Umbraco.ModelsBuilder.Building;
using Umbraco.ModelsBuilder.Configuration;
using File = System.IO.File;
namespace Umbraco.ModelsBuilder.Umbraco
namespace Umbraco.ModelsBuilder
{
internal class PureLiveModelFactory : ILivePublishedModelFactory, IRegisteredObject
{
@@ -101,7 +101,7 @@ namespace Umbraco.ModelsBuilder.Umbraco
var contentTypeAlias = element.ContentType.Alias;
// lookup model constructor (else null)
infos.TryGetValue(contentTypeAlias, out ModelInfo info);
infos.TryGetValue(contentTypeAlias, out var info);
// create model
return info == null ? element : info.Ctor(element);
@@ -234,10 +234,10 @@ namespace Umbraco.ModelsBuilder.Umbraco
get
{
if (_theBuildManager != null) return _theBuildManager;
var prop = typeof (BuildManager).GetProperty("TheBuildManager", BindingFlags.NonPublic | BindingFlags.Static);
var prop = typeof(BuildManager).GetProperty("TheBuildManager", BindingFlags.NonPublic | BindingFlags.Static);
if (prop == null)
throw new InvalidOperationException("Could not get BuildManager.TheBuildManager property.");
_theBuildManager = (BuildManager) prop.GetValue(null);
_theBuildManager = (BuildManager)prop.GetValue(null);
return _theBuildManager;
}
}
@@ -335,7 +335,7 @@ namespace Umbraco.ModelsBuilder.Umbraco
Directory.CreateDirectory(modelsDirectory);
var typeModels = UmbracoServices.GetAllTypes();
var currentHash = ModelsBuilderHasher.Hash(typeModels);
var currentHash = TypeModelHasher.Hash(typeModels);
var modelsHashFile = Path.Combine(modelsDirectory, "models.hash");
var modelsSrcFile = Path.Combine(modelsDirectory, "models.generated.cs");
var projFile = Path.Combine(modelsDirectory, "all.generated.cs");
@@ -456,8 +456,7 @@ namespace Umbraco.ModelsBuilder.Umbraco
// AssemblyVersion is so that we have a different version for each rebuild
var ver = _ver == _skipver ? ++_ver : _ver;
_ver++;
code = code.Replace("//ASSATTR", $@"[assembly: PureLiveAssembly]
[assembly:ModelsBuilderAssembly(PureLive = true, SourceHash = ""{currentHash}"")]
code = code.Replace("//ASSATTR", $@"[assembly:ModelsBuilderAssembly(PureLive = true, SourceHash = ""{currentHash}"")]
[assembly:System.Reflection.AssemblyVersion(""0.0.0.{ver}"")]");
File.WriteAllText(modelsSrcFile, code);
@@ -505,7 +504,7 @@ namespace Umbraco.ModelsBuilder.Umbraco
private static Infos RegisterModels(IEnumerable<Type> types)
{
var ctorArgTypes = new[] { typeof (IPublishedElement) };
var ctorArgTypes = new[] { typeof(IPublishedElement) };
var modelInfos = new Dictionary<string, ModelInfo>(StringComparer.InvariantCultureIgnoreCase);
var map = new Dictionary<string, Type>();
@@ -517,7 +516,7 @@ namespace Umbraco.ModelsBuilder.Umbraco
foreach (var ctor in type.GetConstructors())
{
var parms = ctor.GetParameters();
if (parms.Length == 1 && typeof (IPublishedElement).IsAssignableFrom(parms[0].ParameterType))
if (parms.Length == 1 && typeof(IPublishedElement).IsAssignableFrom(parms[0].ParameterType))
{
if (constructor != null)
throw new InvalidOperationException($"Type {type.FullName} has more than one public constructor with one argument of type, or implementing, IPropertySet.");
@@ -532,17 +531,17 @@ namespace Umbraco.ModelsBuilder.Umbraco
var attribute = type.GetCustomAttribute<PublishedModelAttribute>(false);
var typeName = attribute == null ? type.Name : attribute.ContentTypeAlias;
if (modelInfos.TryGetValue(typeName, out ModelInfo modelInfo))
if (modelInfos.TryGetValue(typeName, out var modelInfo))
throw new InvalidOperationException($"Both types {type.FullName} and {modelInfo.ModelType.FullName} want to be a model type for content type with alias \"{typeName}\".");
// fixme use Core's ReflectionUtilities.EmitCtor !!
// Yes .. DynamicMethod is uber slow
var meth = new DynamicMethod(string.Empty, typeof (IPublishedElement), ctorArgTypes, type.Module, true);
var meth = new DynamicMethod(string.Empty, typeof(IPublishedElement), ctorArgTypes, type.Module, true);
var gen = meth.GetILGenerator();
gen.Emit(OpCodes.Ldarg_0);
gen.Emit(OpCodes.Newobj, constructor);
gen.Emit(OpCodes.Ret);
var func = (Func<IPublishedElement, IPublishedElement>) meth.CreateDelegate(typeof (Func<IPublishedElement, IPublishedElement>));
var func = (Func<IPublishedElement, IPublishedElement>)meth.CreateDelegate(typeof(Func<IPublishedElement, IPublishedElement>));
modelInfos[typeName] = new ModelInfo { ParameterType = parameterType, Ctor = func, ModelType = type };
map[typeName] = type;
@@ -663,9 +662,7 @@ namespace Umbraco.ModelsBuilder.Umbraco
_logger.Info<PureLiveModelFactory>("Detected files changes.");
lock (SyncRoot) // don't reset while being locked
{
ResetModels();
}
}
public void Stop(bool immediate)

View File

@@ -49,7 +49,7 @@
<Compile Include="..\SolutionInfo.cs">
<Link>Properties\SolutionInfo.cs</Link>
</Compile>
<Compile Include="Api\ApiVersion.cs" />
<Compile Include="ApiVersion.cs" />
<Compile Include="Building\Builder.cs" />
<Compile Include="Building\PropertyModel.cs" />
<Compile Include="Building\TextBuilder.cs" />
@@ -65,24 +65,23 @@
<Compile Include="ModelsBuilderAssemblyAttribute.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="PublishedElementExtensions.cs" />
<Compile Include="PureLiveAssemblyAttribute.cs" />
<Compile Include="ReferencedAssemblies.cs" />
<Compile Include="TypeExtensions.cs" />
<Compile Include="HashCombiner.cs" />
<Compile Include="Umbraco\LiveModelsProvider.cs" />
<Compile Include="Umbraco\LiveModelsProviderModule.cs" />
<Compile Include="Umbraco\ModelsGenerator.cs" />
<Compile Include="LiveModelsProvider.cs" />
<Compile Include="LiveModelsProviderModule.cs" />
<Compile Include="Building\ModelsGenerator.cs" />
<Compile Include="BackOffice\ModelsBuilderBackOfficeController.cs" />
<Compile Include="Compose\ModelsBuilderComponent.cs" />
<Compile Include="Compose\ModelsBuilderComposer.cs" />
<Compile Include="Umbraco\ModelsBuilderHasher.cs" />
<Compile Include="Building\TypeModelHasher.cs" />
<Compile Include="Compose\ModelsBuilderInitializer.cs" />
<Compile Include="Umbraco\ModelsGenerationError.cs" />
<Compile Include="Umbraco\OutOfDateModelsStatus.cs" />
<Compile Include="Umbraco\PublishedModelUtility.cs" />
<Compile Include="Umbraco\PureLiveModelFactory.cs" />
<Compile Include="Umbraco\UmbracoServices.cs" />
<Compile Include="Validation\ContentTypeModelValidator.cs" />
<Compile Include="ModelsGenerationError.cs" />
<Compile Include="OutOfDateModelsStatus.cs" />
<Compile Include="PublishedModelUtility.cs" />
<Compile Include="PureLiveModelFactory.cs" />
<Compile Include="UmbracoServices.cs" />
<Compile Include="BackOffice\ContentTypeModelValidator.cs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp">

View File

@@ -11,9 +11,9 @@ using Umbraco.Core.Strings;
using Umbraco.ModelsBuilder.Building;
using Umbraco.ModelsBuilder.Configuration;
namespace Umbraco.ModelsBuilder.Umbraco
namespace Umbraco.ModelsBuilder
{
public class UmbracoServices
public sealed class UmbracoServices
{
private readonly IContentTypeService _contentTypeService;
private readonly IMediaTypeService _mediaTypeService;
@@ -184,11 +184,9 @@ namespace Umbraco.ModelsBuilder.Umbraco
{
var groups = typeModels.GroupBy(x => x.Alias.ToLowerInvariant());
foreach (var group in groups.Where(x => x.Count() > 1))
{
throw new NotSupportedException($"Alias \"{group.Key}\" is used by types"
+ $" {string.Join(", ", group.Select(x => x.ItemType + ":\"" + x.Alias + "\""))}. Aliases have to be unique."
+ " One of the aliases must be modified in order to use the ModelsBuilder.");
}
return typeModels;
}

View File

@@ -7,7 +7,6 @@ using Moq;
using NUnit.Framework;
using Umbraco.Core.Composing;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.ModelsBuilder.Api;
using Umbraco.ModelsBuilder.Building;
using Umbraco.ModelsBuilder.Configuration;
@@ -77,7 +76,6 @@ using Umbraco.Core.Models;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Web;
using Umbraco.ModelsBuilder;
using Umbraco.ModelsBuilder.Umbraco;
namespace Umbraco.Web.PublishedModels
{
@@ -193,7 +191,6 @@ using Umbraco.Core.Models;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Web;
using Umbraco.ModelsBuilder;
using Umbraco.ModelsBuilder.Umbraco;
namespace Umbraco.Web.PublishedModels
{

View File

@@ -5,7 +5,6 @@ using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using Umbraco.ModelsBuilder.Building;
using Umbraco.ModelsBuilder.Umbraco;
namespace Umbraco.ModelsBuilder.Tests
{