private static string GenerateModelsCode() { var appData = HostingEnvironment.MapPath("~/App_Data"); if (appData == null) throw new Exception("Panic: appData is null."); var modelsDirectory = Path.Combine(appData, "Models"); if (!Directory.Exists(modelsDirectory)) Directory.CreateDirectory(modelsDirectory); foreach (var file in Directory.GetFiles(modelsDirectory, "*.generated.cs")) System.IO.File.Delete(file); var ourFiles = Directory.Exists(modelsDirectory) ? Directory.GetFiles(modelsDirectory, "*.cs").ToDictionary(x => x, System.IO.File.ReadAllText) : new Dictionary<string, string>(); var umbraco = Application.GetApplication(); var typeModels = umbraco.GetAllTypes(); // using BuildManager references var referencedAssemblies = BuildManager.GetReferencedAssemblies().Cast<Assembly>().ToArray(); var parseResult = new CodeParser().Parse(ourFiles, referencedAssemblies); var builder = new TextBuilder(typeModels, parseResult, Config.ModelsNamespace); var codeBuilder = new StringBuilder(); builder.Generate(codeBuilder, builder.GetModelsToGenerate()); var code = codeBuilder.ToString(); // save code for debug purposes System.IO.File.WriteAllText(Path.Combine(modelsDirectory, "models.generated.cs"), code); return code; }
// invoked by the API // DISABLED - works but useless, because if we return type models that // reference some Clr types that exist only on the server and not in the // remove app, then what can we do with them? Better do everything on // the server. // //[System.Web.Http.HttpGet] // use the http one, not mvc, with api controllers! //[ModelsBuilderAuthFilter("developer")] // have to use our own, non-cookie-based, auth //public HttpResponseMessage GetTypeModels() //{ // var umbraco = Application.GetApplication(); // var modelTypes = umbraco.GetContentAndMediaTypes(); // return Request.CreateResponse(HttpStatusCode.OK, modelTypes, Configuration.Formatters.JsonFormatter); //} // //public const string GetTypeModelsUrl = ControllerUrl + "/GetTypeModels"; #endregion public static void GenerateModels(string appData, string bin) { var modelsDirectory = Path.Combine(appData, "Models"); if (!Directory.Exists(modelsDirectory)) Directory.CreateDirectory(modelsDirectory); foreach (var file in Directory.GetFiles(modelsDirectory, "*.generated.cs")) File.Delete(file); var umbraco = Application.GetApplication(); var typeModels = umbraco.GetAllTypes(); // using BuildManager references var referencedAssemblies = BuildManager.GetReferencedAssemblies().Cast<Assembly>().ToArray(); var ourFiles = Directory.GetFiles(modelsDirectory, "*.cs").ToDictionary(x => x, File.ReadAllText); var parseResult = new CodeParser().Parse(ourFiles, referencedAssemblies); var builder = new TextBuilder(typeModels, parseResult, Config.ModelsNamespace); foreach (var typeModel in builder.GetModelsToGenerate()) { var sb = new StringBuilder(); builder.Generate(sb, typeModel); var filename = Path.Combine(modelsDirectory, typeModel.ClrName + ".generated.cs"); File.WriteAllText(filename, sb.ToString()); } if (bin != null) { foreach (var file in Directory.GetFiles(modelsDirectory, "*.generated.cs")) ourFiles[file] = File.ReadAllText(file); var compiler = new Compiler(); foreach (var asm in referencedAssemblies) compiler.ReferencedAssemblies.Add(asm); compiler.Compile(bin, builder.GetModelsNamespace(), ourFiles); } OutOfDateModelsStatus.Clear(); }
[ModelsBuilderAuthFilter("developer")] // have to use our own, non-cookie-based, auth public HttpResponseMessage GetModels(GetModelsData data) { if (!Config.EnableApi) return Request.CreateResponse(HttpStatusCode.Forbidden, "API is not enabled."); var checkResult = CheckVersion(data.ClientVersion, data.MinServerVersionSupportingClient); if (!checkResult.Success) return checkResult.Result; var umbraco = Application.GetApplication(); var typeModels = umbraco.GetAllTypes(); // using BuildManager references var referencedAssemblies = BuildManager.GetReferencedAssemblies().Cast<Assembly>().ToArray(); var parseResult = new CodeParser().Parse(data.Files, referencedAssemblies); var builder = new TextBuilder(typeModels, parseResult, data.Namespace); var models = new Dictionary<string, string>(); foreach (var typeModel in builder.GetModelsToGenerate()) { var sb = new StringBuilder(); builder.Generate(sb, typeModel); models[typeModel.ClrName] = sb.ToString(); } return Request.CreateResponse(HttpStatusCode.OK, models, Configuration.Formatters.JsonFormatter); }
public void GenerateMixinType() { // Umbraco returns nice, pascal-cased names Configuration.Config.StaticMixinGetters = true; // try the new way var type1 = new TypeModel { Id = 1, Alias = "type1", ClrName = "Type1", ParentId = 0, BaseType = null, ItemType = TypeModel.ItemTypes.Content, IsMixin = true, }; type1.Properties.Add(new PropertyModel { Alias = "prop1", ClrName = "Prop1", ClrType = typeof(string), }); var type2 = new TypeModel { Id = 2, Alias = "type2", ClrName = "Type2", ParentId = 0, BaseType = null, ItemType = TypeModel.ItemTypes.Content, }; type2.MixinTypes.Add(type1); type2.Properties.Add(new PropertyModel { Alias = "prop2", ClrName = "Prop2", ClrType = typeof(int), }); var types = new[] { type1, type2 }; var code = new Dictionary<string, string> { }; var parseResult = new CodeParser().Parse(code); var builder = new TextBuilder(types, parseResult); var btypes = builder.TypeModels; var sb = new StringBuilder(); foreach (var model in builder.GetModelsToGenerate()) builder.Generate(sb, model); var gen = sb.ToString(); var version = typeof(Builder).Assembly.GetName().Version; var expected = @"//------------------------------------------------------------------------------ // <auto-generated> // This code was generated by a tool. // // Umbraco.ModelsBuilder v" + version + @" // // Changes to this file will be lost if the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------ "; Console.WriteLine(gen); return; //Assert.AreEqual(expected.ClearLf(), gen); }
public void PartialConstructorWithRename() { var type1 = new TypeModel { Id = 1, Alias = "type1", ClrName = "Type1", ParentId = 0, BaseType = null, ItemType = TypeModel.ItemTypes.Content, }; type1.Properties.Add(new PropertyModel { Alias = "prop1", ClrName = "Prop1", ClrType = typeof(string), }); var types = new[] { type1 }; var code = new Dictionary<string, string> { { "assembly", @" using Umbraco.ModelsBuilder; using Umbraco.Core.Models; [assembly:RenameContentType(""type1"", ""Type2"")] public partial class Type2 { public Type2(IPublishedContent content) : base(content) { // do our own stuff } } " } }; var parseResult = new CodeParser().Parse(code); Assert.IsFalse(parseResult.HasCtor("Type1")); Assert.IsTrue(parseResult.HasCtor("Type2")); var builder = new TextBuilder(types, parseResult); var btypes = builder.TypeModels; var sb = new StringBuilder(); builder.Generate(sb, builder.GetModelsToGenerate().First()); var gen = sb.ToString(); var version = typeof(Builder).Assembly.GetName().Version; var expected = @"//------------------------------------------------------------------------------ // <auto-generated> // This code was generated by a tool. // // Umbraco.ModelsBuilder v" + version + @" // // Changes to this file will be lost if the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------ using System; using System.Collections.Generic; using System.Linq.Expressions; using System.Web; using Umbraco.Core.Models; using Umbraco.Core.Models.PublishedContent; using Umbraco.Web; using Umbraco.ModelsBuilder; using Umbraco.ModelsBuilder.Umbraco; namespace Umbraco.Web.PublishedContentModels { // Content Type 1 with alias ""type1"" [PublishedContentModel(""type1"")] public partial class Type2 : PublishedContentModel { #pragma warning disable 0109 // new is redundant public new const string ModelTypeAlias = ""type1""; public new const PublishedItemType ModelItemType = PublishedItemType.Content; #pragma warning restore 0109 #pragma warning disable 0109 // new is redundant public new static PublishedContentType GetModelContentType() { return PublishedContentType.Get(ModelItemType, ModelTypeAlias); } #pragma warning restore 0109 public static PublishedPropertyType GetModelPropertyType<TValue>(Expression<Func<Type2, TValue>> selector) { return PublishedContentModelUtility.GetModelPropertyType(GetModelContentType(), selector); } [ImplementPropertyType(""prop1"")] public string Prop1 { get { return this.GetPropertyValue<string>(""prop1""); } } } } "; Console.WriteLine(gen); Assert.AreEqual(expected.ClearLf(), gen); }
public void MixinPropertyStatic() { Configuration.Config.StaticMixinGetters = true; // try the new way var type1 = new TypeModel { Id = 1, Alias = "type1", ClrName = "Type1", ParentId = 0, BaseType = null, ItemType = TypeModel.ItemTypes.Content, IsMixin = true, }; type1.Properties.Add(new PropertyModel { Alias = "prop1a", ClrName = "Prop1a", ClrType = typeof(string), }); type1.Properties.Add(new PropertyModel { Alias = "prop1b", ClrName = "Prop1b", ClrType = typeof(string), }); var type2 = new TypeModel { Id = 2, Alias = "type2", ClrName = "Type2", ParentId = 0, BaseType = null, ItemType = TypeModel.ItemTypes.Content, }; type2.MixinTypes.Add(type1); type2.Properties.Add(new PropertyModel { Alias = "prop2", ClrName = "Prop2", ClrType = typeof(int), }); var types = new[] { type1, type2 }; var code = new Dictionary<string, string> { {"assembly", @" using Umbraco.ModelsBuilder; using Test; namespace Test { public partial class Type1 { public static int GetProp1a(IType1 that) { return that.GetPropertyValue<int>(""prop1a""); } } } "} }; var parseResult = new CodeParser().Parse(code); var builder = new TextBuilder(types, parseResult); var btypes = builder.TypeModels; var sb = new StringBuilder(); foreach (var model in builder.GetModelsToGenerate()) builder.Generate(sb, model); var gen = sb.ToString(); var version = typeof(Builder).Assembly.GetName().Version; var expected = @"//------------------------------------------------------------------------------ // <auto-generated> // This code was generated by a tool. // // Umbraco.ModelsBuilder v" + version + @" // // Changes to this file will be lost if the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------ using System; using System.Collections.Generic; using System.Linq.Expressions; using System.Web; using Umbraco.Core.Models; using Umbraco.Core.Models.PublishedContent; using Umbraco.Web; using Umbraco.ModelsBuilder; using Umbraco.ModelsBuilder.Umbraco; namespace Umbraco.Web.PublishedContentModels { // Mixin content Type 1 with alias ""type1"" public partial interface IType1 : IPublishedContent { string Prop1a { get; } string Prop1b { get; } } [PublishedContentModel(""type1"")] public partial class Type1 : PublishedContentModel, IType1 { #pragma warning disable 0109 // new is redundant public new const string ModelTypeAlias = ""type1""; public new const PublishedItemType ModelItemType = PublishedItemType.Content; #pragma warning restore 0109 public Type1(IPublishedContent content) : base(content) { } #pragma warning disable 0109 // new is redundant public new static PublishedContentType GetModelContentType() { return PublishedContentType.Get(ModelItemType, ModelTypeAlias); } #pragma warning restore 0109 public static PublishedPropertyType GetModelPropertyType<TValue>(Expression<Func<Type1, TValue>> selector) { return PublishedContentModelUtility.GetModelPropertyType(GetModelContentType(), selector); } [ImplementPropertyType(""prop1a"")] public string Prop1a { get { return GetProp1a(this); } } [ImplementPropertyType(""prop1b"")] public string Prop1b { get { return GetProp1b(this); } } public static string GetProp1b(IType1 that) { return that.GetPropertyValue<string>(""prop1b""); } } } //------------------------------------------------------------------------------ // <auto-generated> // This code was generated by a tool. // // Umbraco.ModelsBuilder v" + version + @" // // Changes to this file will be lost if the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------ using System; using System.Collections.Generic; using System.Linq.Expressions; using System.Web; using Umbraco.Core.Models; using Umbraco.Core.Models.PublishedContent; using Umbraco.Web; using Umbraco.ModelsBuilder; using Umbraco.ModelsBuilder.Umbraco; namespace Umbraco.Web.PublishedContentModels { [PublishedContentModel(""type2"")] public partial class Type2 : PublishedContentModel, IType1 { #pragma warning disable 0109 // new is redundant public new const string ModelTypeAlias = ""type2""; public new const PublishedItemType ModelItemType = PublishedItemType.Content; #pragma warning restore 0109 public Type2(IPublishedContent content) : base(content) { } #pragma warning disable 0109 // new is redundant public new static PublishedContentType GetModelContentType() { return PublishedContentType.Get(ModelItemType, ModelTypeAlias); } #pragma warning restore 0109 public static PublishedPropertyType GetModelPropertyType<TValue>(Expression<Func<Type2, TValue>> selector) { return PublishedContentModelUtility.GetModelPropertyType(GetModelContentType(), selector); } [ImplementPropertyType(""prop2"")] public int Prop2 { get { return this.GetPropertyValue<int>(""prop2""); } } [ImplementPropertyType(""prop1a"")] public string Prop1a { get { return Type1.GetProp1a(this); } } [ImplementPropertyType(""prop1b"")] public string Prop1b { get { return Type1.GetProp1b(this); } } } } "; //Console.WriteLine(gen); Assert.AreEqual(expected.ClearLf(), gen); }