private void WriteProperty(StringBuilder sb, TypeModel type, PropertyModel property, string mixinClrName = null)
        {
            var mixinStatic = mixinClrName != null;

            sb.Append("\n");

            // Adds xml summary to each property containing
            // property name and property description
            if (!string.IsNullOrWhiteSpace(property.Name) || !string.IsNullOrWhiteSpace(property.Description))
            {
                sb.Append("\t\t///<summary>\n");

                if (!string.IsNullOrWhiteSpace(property.Description))
                {
                    sb.AppendFormat("\t\t/// {0}: {1}\n", XmlCommentString(property.Name), XmlCommentString(property.Description));
                }
                else
                {
                    sb.AppendFormat("\t\t/// {0}\n", XmlCommentString(property.Name));
                }

                sb.Append("\t\t///</summary>\n");
            }

            sb.AppendFormat("\t\t[ImplementPropertyType(\"{0}\")]\n", property.Alias);

            if (mixinStatic)
            {
                sb.Append("\t\tpublic ");
                WriteClrType(sb, property.ClrType);
                sb.AppendFormat(" {0}\n\t\t{{\n\t\t\tget {{ return {1}(this); }}\n\t\t}}\n",
                                property.ClrName, MixinStaticGetterName(property.ClrName));
            }
            else
            {
                sb.Append("\t\tpublic ");
                WriteClrType(sb, property.ClrType);
                sb.AppendFormat(" {0}\n\t\t{{\n\t\t\tget {{ return this.GetPropertyValue",
                                property.ClrName);
                if (property.ClrType != typeof(object))
                {
                    sb.Append("<");
                    WriteClrType(sb, property.ClrType);
                    sb.Append(">");
                }
                sb.AppendFormat("(\"{0}\"); }}\n\t\t}}\n",
                                property.Alias);
            }

            if (!mixinStatic)
            {
                return;
            }

            var mixinStaticGetterName = MixinStaticGetterName(property.ClrName);

            if (type.StaticMixinMethods.Contains(mixinStaticGetterName))
            {
                return;
            }

            sb.Append("\n");

            if (!string.IsNullOrWhiteSpace(property.Name))
            {
                sb.AppendFormat("\t\t/// <summary>Static getter for {0}</summary>\n", XmlCommentString(property.Name));
            }

            sb.Append("\t\tpublic static ");
            WriteClrType(sb, property.ClrType);
            sb.AppendFormat(" {0}(I{1} that) {{ return that.GetPropertyValue",
                            mixinStaticGetterName, mixinClrName);
            if (property.ClrType != typeof(object))
            {
                sb.Append("<");
                WriteClrType(sb, property.ClrType);
                sb.Append(">");
            }
            sb.AppendFormat("(\"{0}\"); }}\n",
                            property.Alias);
        }
Пример #2
0
        private void WriteProperty(StringBuilder sb, TypeModel type, PropertyModel property, string mixinClrName = null)
        {
            var mixinStatic = mixinClrName != null;

            sb.Append("\n");

            if (property.Errors != null)
            {
                sb.Append("\t\t/*\n");
                sb.Append("\t\t * THIS PROPERTY CANNOT BE IMPLEMENTED, BECAUSE:\n");
                sb.Append("\t\t *\n");
                var first = true;
                foreach (var error in property.Errors)
                {
                    if (first)
                    {
                        first = false;
                    }
                    else
                    {
                        sb.Append("\t\t *\n");
                    }
                    foreach (var s in SplitError(error))
                    {
                        sb.Append("\t\t * ");
                        sb.Append(s);
                        sb.Append("\n");
                    }
                }
                sb.Append("\t\t *\n");
                sb.Append("\n");
            }

            // Adds xml summary to each property containing
            // property name and property description
            if (!string.IsNullOrWhiteSpace(property.Name) || !string.IsNullOrWhiteSpace(property.Description))
            {
                sb.Append("\t\t///<summary>\n");

                if (!string.IsNullOrWhiteSpace(property.Description))
                {
                    sb.AppendFormat("\t\t/// {0}: {1}\n", XmlCommentString(property.Name), XmlCommentString(property.Description));
                }
                else
                {
                    sb.AppendFormat("\t\t/// {0}\n", XmlCommentString(property.Name));
                }

                sb.Append("\t\t///</summary>\n");
            }

            WriteGeneratedCodeAttribute(sb, "\t\t");
            sb.AppendFormat("\t\t[ImplementPropertyType(\"{0}\")]\n", property.Alias);

            if (mixinStatic)
            {
                sb.Append("\t\tpublic ");
                WriteClrType(sb, property.ClrType);
                sb.AppendFormat(" {0}\n\t\t{{\n\t\t\tget {{ return {1}(this); }}\n\t\t}}\n",
                                property.ClrName, MixinStaticGetterName(property.ClrName));
            }
            else
            {
                sb.Append("\t\tpublic ");
                WriteClrType(sb, property.ClrType);
                sb.AppendFormat(" {0}\n\t\t{{\n\t\t\tget {{ return this.GetPropertyValue",
                                property.ClrName);
                if (property.ClrType != typeof(object))
                {
                    sb.Append("<");
                    WriteClrType(sb, property.ClrType);
                    sb.Append(">");
                }
                sb.AppendFormat("(\"{0}\"); }}\n\t\t}}\n",
                                property.Alias);
            }

            if (property.Errors != null)
            {
                sb.Append("\n");
                sb.Append("\t\t *\n");
                sb.Append("\t\t */\n");
            }

            if (!mixinStatic)
            {
                return;
            }

            var mixinStaticGetterName = MixinStaticGetterName(property.ClrName);

            if (type.StaticMixinMethods.Contains(mixinStaticGetterName))
            {
                return;
            }

            sb.Append("\n");

            if (!string.IsNullOrWhiteSpace(property.Name))
            {
                sb.AppendFormat("\t\t/// <summary>Static getter for {0}</summary>\n", XmlCommentString(property.Name));
            }

            WriteGeneratedCodeAttribute(sb, "\t\t");
            sb.Append("\t\tpublic static ");
            WriteClrType(sb, property.ClrType);
            sb.AppendFormat(" {0}(I{1} that) {{ return that.GetPropertyValue",
                            mixinStaticGetterName, mixinClrName);
            if (property.ClrType != typeof(object))
            {
                sb.Append("<");
                WriteClrType(sb, property.ClrType);
                sb.Append(">");
            }
            sb.AppendFormat("(\"{0}\"); }}\n",
                            property.Alias);
        }
        private static IList<TypeModel> GetTypes(PublishedItemType itemType, IContentTypeBase[] contentTypes)
        {
            var typeModels = new List<TypeModel>();

            // get the types and the properties
            foreach (var contentType in contentTypes)
            {
                var typeModel = new TypeModel
                {
                    Id = contentType.Id,
                    Alias = contentType.Alias,
                    ClrName = contentType.Alias.ToCleanString(CleanStringType.ConvertCase | CleanStringType.PascalCase),
                    ParentId = contentType.ParentId,

                    Name = contentType.Name,
                    Description = contentType.Description
                };

                switch (itemType)
                {
                    case PublishedItemType.Content:
                        typeModel.ItemType = TypeModel.ItemTypes.Content;
                        break;
                    case PublishedItemType.Media:
                        typeModel.ItemType = TypeModel.ItemTypes.Media;
                        break;
                    case PublishedItemType.Member:
                        typeModel.ItemType = TypeModel.ItemTypes.Member;
                        break;
                    default:
                        throw new InvalidOperationException(string.Format("Unsupported PublishedItemType \"{0}\".", itemType));
                }

                typeModels.Add(typeModel);

                var publishedContentType = PublishedContentType.Get(itemType, contentType.Alias);

                foreach (var propertyType in contentType.PropertyTypes)
                {
                    var propertyModel = new PropertyModel
                    {
                        Alias = propertyType.Alias,
                        ClrName = propertyType.Alias.ToCleanString(CleanStringType.ConvertCase | CleanStringType.PascalCase),

                        Name = propertyType.Name,
                        Description = propertyType.Description
                    };

                    var publishedPropertyType = publishedContentType.GetPropertyType(propertyType.Alias);
                    propertyModel.ClrType = publishedPropertyType.ClrType;

                    typeModel.Properties.Add(propertyModel);
                }
            }

            // wire the base types
            foreach (var typeModel in typeModels.Where(x => x.ParentId > 0))
            {
                typeModel.BaseType = typeModels.SingleOrDefault(x => x.Id == typeModel.ParentId);
                // Umbraco 7.4 introduces content types containers, so even though ParentId > 0, the parent might
                // not be a content type - here we assume that BaseType being null while ParentId > 0 means that 
                // the parent is a container (and we don't check).
                typeModel.IsParent = typeModel.BaseType != null;
            }

            // discover mixins
            foreach (var contentType in contentTypes)
            {
                var typeModel = typeModels.SingleOrDefault(x => x.Id == contentType.Id);
                if (typeModel == null) throw new Exception("Panic: no type model matching content type.");

                IEnumerable<IContentTypeComposition> compositionTypes;
                var contentTypeAsMedia = contentType as IMediaType;
                var contentTypeAsContent = contentType as IContentType;
                var contentTypeAsMember = contentType as IMemberType;
                if (contentTypeAsMedia != null) compositionTypes = contentTypeAsMedia.ContentTypeComposition;
                else if (contentTypeAsContent != null) compositionTypes = contentTypeAsContent.ContentTypeComposition;
                else if (contentTypeAsMember != null) compositionTypes = contentTypeAsMember.ContentTypeComposition;
                else throw new Exception(string.Format("Panic: unsupported type \"{0}\".", contentType.GetType().FullName));

                foreach (var compositionType in compositionTypes)
                {
                    var compositionModel = typeModels.SingleOrDefault(x => x.Id == compositionType.Id);
                    if (compositionModel == null) throw new Exception("Panic: composition type does not exist.");

                    if (compositionType.Id == contentType.ParentId) continue;

                    // add to mixins
                    typeModel.MixinTypes.Add(compositionModel);

                    // mark as mixin - as well as parents
                    compositionModel.IsMixin = true;
                    while ((compositionModel = compositionModel.BaseType) != null)
                        compositionModel.IsMixin = true;
                }
            }

            return typeModels;
        }
 private void WriteInterfaceProperty(StringBuilder sb, PropertyModel property)
 {
     if (!string.IsNullOrWhiteSpace(property.Name))
         sb.AppendFormat("\t\t/// <summary>{0}</summary>\n", XmlCommentString(property.Name));
     sb.Append("\t\t");
     WriteClrType(sb, property.ClrType);
     sb.AppendFormat(" {0} {{ get; }}\n",
         property.ClrName);
 }
        private void WriteProperty(StringBuilder sb, TypeModel type, PropertyModel property, string mixinClrName = null)
        {
            var mixinStatic = mixinClrName != null;

            sb.Append("\n");
            
            // Adds xml summary to each property containing
            // property name and property description
            if (!string.IsNullOrWhiteSpace(property.Name) || !string.IsNullOrWhiteSpace(property.Description))
            {
                sb.Append("\t\t///<summary>\n");
                
                if (!string.IsNullOrWhiteSpace(property.Description))
                    sb.AppendFormat("\t\t/// {0}: {1}\n", XmlCommentString(property.Name), XmlCommentString(property.Description));
                else
                    sb.AppendFormat("\t\t/// {0}\n", XmlCommentString(property.Name));

                sb.Append("\t\t///</summary>\n");
            }

            sb.AppendFormat("\t\t[ImplementPropertyType(\"{0}\")]\n", property.Alias);

            if (mixinStatic)
            {
                sb.Append("\t\tpublic ");
                WriteClrType(sb, property.ClrType);
                sb.AppendFormat(" {0}\n\t\t{{\n\t\t\tget {{ return {1}(this); }}\n\t\t}}\n",
                    property.ClrName, MixinStaticGetterName(property.ClrName));
            }
            else
            {
                sb.Append("\t\tpublic ");
                WriteClrType(sb, property.ClrType);
                sb.AppendFormat(" {0}\n\t\t{{\n\t\t\tget {{ return this.GetPropertyValue",
                    property.ClrName);
                if (property.ClrType != typeof(object))
                {
                    sb.Append("<");
                    WriteClrType(sb, property.ClrType);
                    sb.Append(">");
                }
                sb.AppendFormat("(\"{0}\"); }}\n\t\t}}\n",
                    property.Alias);
            }

            if (!mixinStatic) return;

            var mixinStaticGetterName = MixinStaticGetterName(property.ClrName);

            if (type.StaticMixinMethods.Contains(mixinStaticGetterName)) return;

            sb.Append("\n");

            if (!string.IsNullOrWhiteSpace(property.Name))
                sb.AppendFormat("\t\t/// <summary>Static getter for {0}</summary>\n", XmlCommentString(property.Name));

            sb.Append("\t\tpublic static ");
            WriteClrType(sb, property.ClrType);
            sb.AppendFormat(" {0}(I{1} that) {{ return that.GetPropertyValue",
                mixinStaticGetterName, mixinClrName);
            if (property.ClrType != typeof(object))
            {
                sb.Append("<");
                WriteClrType(sb, property.ClrType);
                sb.Append(">");
            }
            sb.AppendFormat("(\"{0}\"); }}\n",
                property.Alias);
        }
        private void WriteMixinProperty(StringBuilder sb, PropertyModel property, string mixinClrName)
        {
            sb.Append("\n");
            
            // Adds xml summary to each property containing
            // property name and property description
            if (!string.IsNullOrWhiteSpace(property.Name) || !string.IsNullOrWhiteSpace(property.Description))
            {
                sb.Append("\t\t///<summary>\n");

                if (!string.IsNullOrWhiteSpace(property.Description))
                    sb.AppendFormat("\t\t/// {0}: {1}\n", XmlCommentString(property.Name), XmlCommentString(property.Description));
                else
                    sb.AppendFormat("\t\t/// {0}\n", XmlCommentString(property.Name));

                sb.Append("\t\t///</summary>\n");
            }

            sb.AppendFormat("\t\t[ImplementPropertyType(\"{0}\")]\n", property.Alias);

            sb.Append("\t\tpublic ");
            WriteClrType(sb, property.ClrType);
            sb.AppendFormat(" {0}\n\t\t{{\n\t\t\tget {{ return {1}.{2}(this); }}\n\t\t}}\n",
                property.ClrName, mixinClrName, MixinStaticGetterName(property.ClrName));
        }