Пример #1
0
 /// <summary>
 /// Extracts enum declarations from an assembly.
 /// </summary>
 private List <EnumDecl> ExtractEnums(Assembly assembly,
                                      CommentNavigator docNavigator, ProjectNavigator projNavigator)
 {
     return(TypesExtractor
            .GetEnums(assembly, new string[0])
            .Select(type => DeclarationConvertor.EnumToDecl(type, docNavigator, projNavigator))
            .ToList());
 }
Пример #2
0
        /// <summary>
        /// Converts to enum item declaration.
        /// </summary>
        private static EnumItem ToEnumItem(FieldInfo field, CommentNavigator navigator)
        {
            var item = new EnumItem();

            item.Name    = field.Name;
            item.Comment = navigator?.GetXmlComment(field);
            item.Label   = field.GetLabelFromAttribute();

            return(item);
        }
Пример #3
0
        /// <summary>
        /// Helper method which tries to create documentation navigator for given assembly.
        /// </summary>
        public static bool TryCreate(Assembly assembly, out CommentNavigator navigator)
        {
            string documentFile = Path.ChangeExtension(assembly.Location, ".xml");

            if (File.Exists(documentFile))
            {
                navigator = new CommentNavigator(documentFile);
                return(true);
            }
            navigator = null;
            return(false);
        }
Пример #4
0
        /// <summary>
        /// Converts given property into corresponding declaration element.
        /// </summary>
        private static ElementDecl ToElement(PropertyInfo property, CommentNavigator navigator)
        {
            var element = ToTypeMember <ElementDecl>(property.PropertyType);

            element.Vector   = property.PropertyType.IsVector();
            element.Name     = property.Name;
            element.Label    = property.GetLabelFromAttribute();
            element.Comment  = property.GetCommentFromAttribute() ?? navigator?.GetXmlComment(property);
            element.Optional = property.GetCustomAttribute <BsonRequiredAttribute>() == null ? YesNo.Y : (YesNo?)null;
            element.Hidden   = property.IsHidden();

            return(element);
        }
Пример #5
0
 /// <summary>
 /// Generates handler declare section which corresponds to given method.
 /// </summary>
 private static HandlerDeclareItem ToDeclare(MethodInfo method, CommentNavigator navigator)
 {
     return(new HandlerDeclareItem
     {
         Name = method.Name,
         Type = HandlerType.Job,
         Label = method.GetLabelFromAttribute(),
         Comment = method.GetCommentFromAttribute() ?? navigator?.GetXmlComment(method),
         Hidden = method.IsHidden(),
         Static = method.IsStatic ? YesNo.Y : (YesNo?)null,
         Params = method.GetParameters().Select(ToHandlerParam).ToList()
     });
 }
Пример #6
0
        /// <summary>
        /// Factory method which creates declaration corresponding to given type.
        /// </summary>
        public static IDecl ToDecl(System.Type type, CommentNavigator navigator, ProjectNavigator projNavigator)
        {
            if (type.IsSubclassOf(typeof(Enum)))
            {
                return(EnumToDecl(type, navigator, projNavigator));
            }

            if (type.IsSubclassOf(typeof(Data)))
            {
                return(TypeToDecl(type, navigator, projNavigator));
            }

            throw new ArgumentException($"{type.FullName} is not subclass of Enum or Data", nameof(type));
        }
Пример #7
0
        /// <summary>
        /// Writes data types schema into specified Context.
        /// </summary>
        public void Generate(Context context)
        {
            var assemblies = GetAssemblies();

            foreach (Assembly assembly in assemblies)
            {
                bool hasDocumentation = CommentNavigator.TryCreate(assembly, out CommentNavigator docNavigator);
                bool isProjectLocated = ProjectNavigator.TryCreate(null, assembly, out ProjectNavigator projNavigator);

                var declTypes = ExtractTypes(assembly, docNavigator, projNavigator);
                var declEnums = ExtractEnums(assembly, docNavigator, projNavigator);

                context.SaveMany(declTypes);
                context.SaveMany(declEnums);
            }
        }
Пример #8
0
        /// <summary>
        /// Converts enum to EnumDecl
        /// </summary>
        public static EnumDecl EnumToDecl(System.Type type, CommentNavigator navigator, ProjectNavigator projNavigator)
        {
            if (!type.IsSubclassOf(typeof(Enum)))
            {
                throw new ArgumentException($"Cannot create enum declaration from type: {type.FullName}.");
            }

            EnumDecl decl = new EnumDecl();

            decl.Name     = type.Name;
            decl.Comment  = GetCommentFromAttribute(type) ?? navigator?.GetXmlComment(type);
            decl.Category = projNavigator?.GetTypeLocation(type);
            decl.Module   = new ModuleKey {
                ModuleName = type.Namespace
            };
            decl.Label = GetLabelFromAttribute(type) ?? type.Name;

            List <FieldInfo> items = type.GetFields(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Static).ToList();

            decl.Items = items.Select(i => ToEnumItem(i, navigator)).ToList();
            return(decl);
        }
Пример #9
0
        /// <summary>
        /// Converts type inherited from Data to TypeDecl
        /// </summary>
        public static TypeDecl TypeToDecl(System.Type type, CommentNavigator navigator, ProjectNavigator projNavigator)
        {
            if (!type.IsSubclassOf(typeof(Data)))
            {
                throw new ArgumentException($"Cannot create type declaration from type: {type.FullName}.");
            }

            TypeDecl decl = new TypeDecl();

            decl.Module = new ModuleKey {
                ModuleName = type.Namespace
            };
            decl.Category = projNavigator?.GetTypeLocation(type);
            decl.Name     = type.Name;
            decl.Label    = GetLabelFromAttribute(type) ?? type.Name;
            decl.Comment  = GetCommentFromAttribute(type) ?? navigator?.GetXmlComment(type);
            decl.Kind     = GetKind(type);
            decl.IsRecord = type.IsSubclassOf(typeof(Record));
            decl.Inherit  = IsRoot(type.BaseType)
                               ? null
                               : CreateTypeDeclKey(type.BaseType.Namespace, type.BaseType.Name);
            decl.Index = GetIndexesFromAttributes(type);

            // Skip special (property getters, setters, etc) and inherited methods
            List <MethodInfo> handlers = type.GetMethods(PublicInstanceDeclaredFlags)
                                         .Where(IsProperHandler)
                                         .ToList();

            var declares   = new List <HandlerDeclareItem>();
            var implements = new List <HandlerImplementItem>();

            foreach (MethodInfo method in handlers)
            {
                // Abstract methods have only declaration
                if (method.IsAbstract)
                {
                    declares.Add(ToDeclare(method, navigator));
                }
                // Overriden methods are marked with override
                else if (method.GetBaseDefinition() != method)
                {
                    // TODO: Temp adding declare to avoid signature search in bases.
                    declares.Add(ToDeclare(method, navigator));
                    implements.Add(ToImplement(method));
                }
                // Case for methods without modifiers
                else
                {
                    declares.Add(ToDeclare(method, navigator));
                    implements.Add(ToImplement(method));
                }
            }

            // Add method information to declaration
            if (declares.Any())
            {
                decl.Declare = new HandlerDeclareBlock {
                    Handlers = declares
                }
            }
            ;
            if (implements.Any())
            {
                decl.Implement = new HandlerImplementBlock {
                    Handlers = implements
                }
            }
            ;

            List <PropertyInfo> dataProperties = type.GetProperties(PublicInstanceDeclaredFlags)
                                                 .Where(p => IsAllowedType(p.PropertyType))
                                                 .Where(IsPublicGetSet).ToList();

            decl.Elements = dataProperties.Select(p => ToElement(p, navigator)).ToList();
            decl.Keys     = GetKeyProperties(type)
                            .Where(p => IsAllowedType(p.PropertyType))
                            .Where(IsPublicGetSet)
                            .Select(t => t.Name).ToList();

            return(decl);
        }