Beispiel #1
0
        protected override void CollectInput()
        {
            IEnumerable <SchemaData>       schemas       = new List <SchemaData>();
            IEnumerable <PageTemplateData> pageTemplates = new List <PageTemplateData>();

            if (!string.IsNullOrEmpty(Config.SchemaId))
            {
                schemas = SchemaCollector.FindSchema(Config.SchemaId);
            }
            else if (!string.IsNullOrEmpty(Config.TemplateId))
            {
                pageTemplates = TemplateCollector.FindTemplate(Config.TemplateId, true).Cast <PageTemplateData>();
            }
            else if (!string.IsNullOrEmpty(Config.PublicationId))
            {
                schemas       = SchemaCollector.FindSchemasForPublication(Config.PublicationId);
                pageTemplates = TemplateCollector.FindTemplatesForPublication(Config.PublicationId, true).Cast <PageTemplateData>();
            }
            else if (!string.IsNullOrEmpty(Config.FolderId))
            {
                schemas       = SchemaCollector.FindSchemasForFolder(Config.FolderId);
                pageTemplates = TemplateCollector.FindTemplatesForFolder(Config.FolderId, true).Cast <PageTemplateData>();
            }
            else
            {
                throw new Exception("You need to specify one of the following: schema (-s), publication (-p) or folder (-f)");
            }
            log.Info($"Found {schemas.Count()} schemas");
            ModelRegistry.GetInstance(Config).AddSchemas(schemas);
            log.Info($"Found {pageTemplates.Count()} page templates");
            ModelRegistry.GetInstance(Config).AddPageTemplates(pageTemplates);
        }
Beispiel #2
0
 protected override void Analyze()
 {
     Console.WriteLine("TcmUri;Title;RootElementName;TypeName;Purpose;PropertyDefinitions.Count");
     foreach (var model in ModelRegistry.GetInstance(Config).ViewModels)
     {
         Console.WriteLine($"{model.TcmUri};{model.Title};{model.RootElementName};{model.TypeName};{model.Purpose};{model.PropertyDefinitions.Count()}");
     }
 }
Beispiel #3
0
        private void RazorForModel(ModelDefinition modelDefinition, string viewName, IndentedStringBuilder sb, string modelVariable = "Model")
        {
            sb.AppendLine("<table class=\"table table-dark\">");
            sb.AppendLine("<tr>");
            sb.AppendLine("<td>Model Type</td>");
            sb.AppendLine("<td>");
            sb.AppendLine($"@{modelVariable}.GetType()");
            sb.AppendLine("</td>");
            sb.AppendLine("</tr>");
            if (viewName != null)
            {
                sb.AppendLine("<tr>");
                sb.AppendLine("<td>View Name</td>");
                sb.AppendLine("<td>");
                sb.AppendLine($"{viewName}");
                sb.AppendLine("</td>");
                sb.AppendLine("</tr>");
            }
            if (modelDefinition.Purpose == Tridion.ContentManager.CoreService.Client.SchemaPurpose.Multimedia)
            {
                sb.AppendLine("<tr>");
                sb.AppendLine("<td>Multimedia</td>");
                sb.AppendLine("<td>");
                sb.AppendLine($"@if ({modelVariable}.Multimedia.MimeType == \"image/png\" || {modelVariable}.Multimedia.MimeType == \"image/jpeg\" || {modelVariable}.Multimedia.MimeType == \"image/gif\")");
                sb.Indent();
                sb.AppendLine($"<img src=\"@{modelVariable}.Multimedia.Url\" />");
                sb.Outdent();
                sb.AppendLine("else");
                sb.Indent();
                sb.AppendLine($"<a href=\"@{modelVariable}.Multimedia.Url\">@{modelVariable}.Multimedia.Url</a>");
                sb.Outdent();
                sb.AppendLine("</td>");
                sb.AppendLine("</tr>");
            }

            foreach (var propertyDefinition in modelDefinition.PropertyDefinitions)
            {
                sb.Indent(false);
                sb.AppendLine("<tr>");
                sb.Indent(false);
                sb.AppendLine($"<td>{propertyDefinition.Name}</td>");
                sb.AppendLine("<td>");

                var varName = propertyDefinition.IsMultipleValue ? propertyDefinition.Name?.GetSingular()?.LCFirst() : modelVariable + "." + propertyDefinition.Name;
                sb.AppendLine($"@if ({modelVariable}.{propertyDefinition.Name} != null)");
                sb.Indent();
                if (propertyDefinition.IsMultipleValue)
                {
                    if ((!propertyDefinition.IsComplexType) || propertyDefinition.FieldType == FieldType.Regions)
                    {
                        sb.AppendLine("<ul>");
                        sb.AppendLine($"@foreach (var {varName} in {modelVariable}.{propertyDefinition.Name})");
                    }
                    else
                    {
                        sb.AppendLine($"foreach (var {varName} in {modelVariable}.{propertyDefinition.Name})");
                    }
                    sb.Indent();
                }
                if (propertyDefinition.FieldType == FieldType.Entities)
                {
                    sb.AppendLine($"@Html.RenderEntity({varName})");
                }
                else if (propertyDefinition.FieldType == FieldType.Regions)
                {
                    sb.AppendLine($"<li>Region with name: @{varName}.Name and view @{varName}.ViewName, containing @{varName}.Entities.Count entities</li>");
                }
                else if (propertyDefinition.IsComplexType)
                {
                    if ((propertyDefinition.TargetSchemas == null || !propertyDefinition.TargetSchemas.Any()) && (propertyDefinition.FieldType == FieldType.ComponentLink || propertyDefinition.FieldType == FieldType.MultiMediaLink))
                    {
                        sb.AppendLine($"link without a specific target model<br/>Resolved URL: {varName}.Url");
                    }
                    else
                    {
                        // get the model definition for the embedded schema
                        var embeddedModel = ModelRegistry.GetInstance(Config).GetViewModelDefinition(propertyDefinition.TargetSchemas.FirstOrDefault()?.IdRef);
                        RazorForModel(embeddedModel, null, sb, varName);
                    }
                }
                else
                {
                    if (propertyDefinition.IsMultipleValue)
                    {
                        sb.AppendLine($"<li>@{varName}</li>");
                    }
                    else
                    {
                        sb.AppendLine($"@{varName}");
                    }
                }
                if (propertyDefinition.IsMultipleValue)
                {
                    sb.Outdent();
                    if ((!propertyDefinition.IsComplexType) || propertyDefinition.FieldType == FieldType.Regions)
                    {
                        sb.AppendLine("</ul>");
                    }
                }
                sb.Outdent();
                sb.AppendLine("</td>");
                sb.Outdent(false);
                sb.AppendLine("</tr>");
                sb.Outdent(false);
            }
            sb.AppendLine("</table>");
        }
Beispiel #4
0
        public override IDictionary <string, string> WriteCode()
        {
            IDictionary <string, string> class2code = new Dictionary <string, string>();
            IndentedStringBuilder        sb         = new IndentedStringBuilder(Config.IndentNrOfSpaces);
            ModelRegistry modelRegistry             = ModelRegistry.GetInstance(Config);

            log.DebugFormat("Started GenerateCode with {0} models", modelRegistry.ViewModels.Count);
            sb.Indent(false);

            foreach (var modelDefinition in modelRegistry.UniqueViewModels)
            {
                string attributeName   = "";
                string attributeParams = "";
                if (modelDefinition.Purpose == Tridion.ContentManager.CoreService.Client.SchemaPurpose.Region)
                {
                    attributeName   = Config.PageModelAttributeName;
                    attributeParams = string.Format("TemplateTitle = \"{0}\"", string.IsNullOrEmpty(modelDefinition.RootElementName) ? modelDefinition.Title : modelDefinition.RootElementName);
                }
                else
                {
                    attributeName   = Config.ModelAttributeName;
                    attributeParams = string.Format("\"{0}\", {1}", string.IsNullOrEmpty(modelDefinition.RootElementName) ? modelDefinition.Title : modelDefinition.RootElementName,
                                                    modelDefinition.Purpose == Tridion.ContentManager.CoreService.Client.SchemaPurpose.Embedded ? "false" : "true");
                }
                //var implRenderableViewModel = viewModelDefinition.Purpose == Tridion.ContentManager.CoreService.Client.SchemaPurpose.Component ? ", IRenderableViewModel" : "";
                sb.AppendLine("///<summary>");
                sb.AppendLine("/// Class is auto-generated from Tridion {2} {0} ({1})", modelDefinition.Title, modelDefinition.TcmUri, modelDefinition.TcmUri.EndsWith("-128") ? "page template" : "schema");
                sb.AppendLine("/// Date: {0}", DateTime.Now);
                sb.AppendLine("/// </summary>");
                sb.AppendLine("[{0}({1})]",
                              attributeName, attributeParams);
                if (modelDefinition.Purpose == Tridion.ContentManager.CoreService.Client.SchemaPurpose.Region) // note: for page models, the schemapurpose is always set to Region, even if the model is based on a page template!
                {
                    sb.AppendLine("public partial class {0} : {1}", modelDefinition.TypeName, string.Join(",", Config.BaseClassesForPages));
                }
                else if (modelDefinition.Purpose == Tridion.ContentManager.CoreService.Client.SchemaPurpose.Multimedia)
                {
                    sb.AppendLine("public partial class {0} : {1}", modelDefinition.TypeName, string.Join(",", Config.BaseClassesForMultimedia));
                }
                else if (modelDefinition.Purpose == Tridion.ContentManager.CoreService.Client.SchemaPurpose.Embedded)
                {
                    sb.AppendLine("public partial class {0} : {1}", modelDefinition.TypeName, string.Join(",", Config.BaseClassesForEmbedded));
                }
                else
                {
                    sb.AppendLine("public partial class {0} : {1}", modelDefinition.TypeName, string.Join(",", Config.BaseClasses));
                }
                log.Debug("added partial class");
                sb.Indent();

                foreach (var propertyDefinition in modelDefinition.PropertyDefinitions)
                {
                    sb.AppendLine("[{0}]", propertyDefinition.GetPropertyAttribute(modelDefinition.TypeName));
                    sb.Append("public virtual {0} {1} ",
                              propertyDefinition.IsMultipleValue ? "List<" + propertyDefinition.PropertyType + ">" : propertyDefinition.PropertyType,
                              modelDefinition.TypeName == propertyDefinition.Name ? propertyDefinition.Name + "Field" : propertyDefinition.Name);
                    sb.AppendLine("{ get; set; }");
                }

                if (!Config.NoRenderData)
                {
                    sb.AppendLine("[RenderData]");
                    sb.AppendLine("public IRenderData RenderData { get; set; }");
                }
                sb.Outdent();
                sb.AppendLine("");

                var key = modelDefinition.Purpose == Tridion.ContentManager.CoreService.Client.SchemaPurpose.Region ? "Pages/" + modelDefinition.TypeName : "Entities/" + modelDefinition.TypeName;
                class2code.Add(key, sb.ToString());
                sb.Reset();
            }
            return(class2code);
        }