Пример #1
0
        /// <inheritdoc />
        public (IMarkDocLogger logger, IReadOnlyCollection <IProcess> processes, Func <CancellationToken, ValueTask> executor) GenerateExecutor(IReadOnlyDictionary <string, IReadOnlyDictionary <string, string> > configuration)
        {
            var globalSettings = GetSettings <IGlobalSettings>(configuration);
            var linkerSettings = (LinkerSettings)GetSettings <ILinkerSettings>(configuration);
            var memberSettings = GetSettings <IMemberSettings>(configuration);
            var docSettings    = GetSettings <IDocSettings>(configuration);
            var logger         = TypeResolver.Resolve <IMarkDocLogger>();

            var memberProcess        = new DefiniteProcess("Assembly resolver", memberSettings.Paths.Count);
            var documentationProcess = new DefiniteProcess("Documentation resolver", docSettings.Paths.Count);
            var printerProcess       = new IndefiniteProcess("Printer");

            var processes = new IProcess[] { memberProcess, documentationProcess, printerProcess };

            return(logger, processes, async _ =>
            {
                var resolver = new Resolver(logger, memberProcess);
                var docResolver = new DocResolver(resolver, docSettings, logger, documentationProcess);

                await Task.WhenAll(resolver.ResolveAsync(memberSettings, globalSettings), docResolver.ResolveAsync())
                .ConfigureAwait(false);

                var linker = new Linker(resolver, linkerSettings);
                var diagrams = linkerSettings.Platform.Equals("3", StringComparison.InvariantCultureIgnoreCase)
          ? new EmptyDiagramResolver()
          : new MermaidResolver(linker) as IDiagramResolver;
                var composer = new TypeComposer(new Creator(false, linker.GetRawUrl()), docResolver, resolver, linker, diagrams);
                var printer = new PrinterMarkdown(composer, linker, printerProcess);

                await printer.Print(resolver.Types.Value.Values.SelectMany(Linq.XtoX), globalSettings.OutputPath)
                .ConfigureAwait(false);
            });
        }
Пример #2
0
 private static DocumentId ResolveForScript(DocResolver resolver, string relativePath)
 {
     if (resolver.TryResolveDocumentId(relativePath, out var id))
     {
         return(id);
     }
     throw new DiffyException($"Could not find {relativePath} in {resolver.Project.Name}", exitStatus: 12);
 }
Пример #3
0
 public static TsMethodGenInfo ToTsMethodGenInfo(MethodBase methodBase, bool isGenericTypeDefinition, bool skipExtentionMethodThis)
 {
     return(new TsMethodGenInfo()
     {
         Name = methodBase.IsConstructor ? "constructor" : methodBase.Name,
         Document = DocResolver.GetTsDocument(methodBase),
         ParameterInfos = methodBase.GetParameters()
                          .Skip(skipExtentionMethodThis && isDefined(methodBase, typeof(ExtensionAttribute)) ? 1 : 0)
                          .Select(info => ToTsParameterGenInfo(info, isGenericTypeDefinition)).ToArray(),
         TypeName = methodBase.IsConstructor ? "" : GetTsTypeName(ToConstraintType((methodBase as MethodInfo).ReturnType, isGenericTypeDefinition)),
         IsConstructor = methodBase.IsConstructor,
         IsStatic = methodBase.IsStatic,
     });
 }
Пример #4
0
        public static TsTypeGenInfo ToTsTypeGenInfo(Type type, HashSet <Type> genTypeSet)
        {
            var result = new TsTypeGenInfo()
            {
                Name     = type.Name.Replace('`', '$'),
                Document = DocResolver.GetTsDocument(type),
                Methods  = genTypeSet.Contains(type) ? (type.IsAbstract ? new MethodBase[] { } : type.GetConstructors(Flags).Where(m => !isFiltered(m)).Cast <MethodBase>())
                           .Concat(GetMethodsForTsTypeGen(type, genTypeSet)
                                   .Where(m => !isFiltered(m) && !IsGetterOrSetter(m) && (type.IsGenericTypeDefinition && !m.IsGenericMethodDefinition || Puerts.Utils.IsSupportedMethod(m)))
                                   .Cast <MethodBase>())
                           .Select(m => ToTsMethodGenInfo(m, type.IsGenericTypeDefinition, false)).ToArray() : new TsMethodGenInfo[] { },
                Properties = genTypeSet.Contains(type) ? type.GetFields(Flags).Where(m => !isFiltered(m))
                             .Select(f => new TsPropertyGenInfo()
                {
                    Name = f.Name, Document = DocResolver.GetTsDocument(f), TypeName = GetTsTypeName(f.FieldType), IsStatic = f.IsStatic
                })
                             .Concat(
                    type.GetProperties(Flags).Where(m => m.Name != "Item").Where(m => !isFiltered(m))
                    .Select(p => new TsPropertyGenInfo()
                {
                    Name = p.Name, Document = DocResolver.GetTsDocument(p), TypeName = GetTsTypeName(p.PropertyType), IsStatic = IsStatic(p), HasGetter = p.GetMethod != null && p.GetMethod.IsPublic, HasSetter = p.SetMethod != null && p.SetMethod.IsPublic
                }))
                             .ToArray() : new TsPropertyGenInfo[] { },
                IsGenericTypeDefinition = type.IsGenericTypeDefinition,
                IsDelegate       = (IsDelegate(type) && type != typeof(Delegate)),
                IsInterface      = type.IsInterface,
                Namespace        = type.Namespace,
                ExtensionMethods = GetExtensionMethods(type, genTypeSet).Select(m => ToTsMethodGenInfo(m, type.IsGenericTypeDefinition, true)).ToArray()
            };

            if (result.IsGenericTypeDefinition)
            {
                result.GenericParameters = type.GetGenericArguments().Select(t => t.ToString()).ToArray();
            }

            if (result.IsDelegate)
            {
                if (type == typeof(Delegate) || type == typeof(System.MulticastDelegate))
                {
                    result.DelegateDef = "(...args:any[]) => any";
                }
                else
                {
                    var m         = type.GetMethod("Invoke");
                    var tsFuncDef = "(" + string.Join(", ", m.GetParameters().Select(p => p.Name + ": " + GetTsTypeName(p.ParameterType)).ToArray()) + ") => " + GetTsTypeName(m.ReturnType);
                    result.DelegateDef = tsFuncDef;
                }
            }

            if (type.IsNested)
            {
                List <string> p    = new List <string>();
                Type          temp = type;
                while (temp.IsNested)
                {
                    p.Add(temp.DeclaringType.Name.Replace('`', '$'));
                    temp = temp.DeclaringType;
                }
                p.Reverse();
                if (type.Namespace != null)
                {
                    result.Namespace = type.Namespace + '.' + string.Join(".", p.ToArray());
                }
                else
                {
                    result.Namespace = string.Join(".", p.ToArray());
                }
            }

            if (!type.IsEnum && type.BaseType != null && type != typeof(object) && !result.IsDelegate && !result.IsInterface)
            {
                result.BaseType = new TsTypeGenInfo()
                {
                    Name      = type.BaseType.IsGenericType ? GetTsTypeName(type.BaseType): type.BaseType.Name.Replace('`', '$'),
                    Document  = DocResolver.GetTsDocument(type.BaseType),
                    Namespace = type.BaseType.Namespace
                };
                if (type.BaseType.IsGenericType && type.BaseType.Namespace != null)
                {
                    result.BaseType.Name = result.BaseType.Name.Substring(type.BaseType.Namespace.Length + 1);
                }
            }

            if (type.IsEnum)
            {
                result.IsEnum = true;
                var KeyValues = type.GetFields(BindingFlags.Static | BindingFlags.Public)
                                .Where(f => f.Name != "value__")
                                .Select(f => f.Name + " = " + Convert.ChangeType(f.GetValue(null), Enum.GetUnderlyingType(type))).ToArray();
                result.EnumKeyValues = string.Join(", ", KeyValues);
            }

            return(result);
        }
Пример #5
0
                public static TsTypeGenInfo FromType(Type type, HashSet <Type> genTypeSet)
                {
                    var result = new TsTypeGenInfo()
                    {
                        Name       = type.Name.Replace('`', '$'),
                        Document   = DocResolver.GetTsDocument(type),
                        Methods    = genTypeSet.Contains(type) ? TsMethodGenInfo.FromType(type, genTypeSet) : new TsMethodGenInfo[] { },
                        Properties = genTypeSet.Contains(type) ? type.GetFields(Utils.Flags)
                                     .Where(m => !Utils.IsNotSupportedMember(m, true))
                                     .Where(m => Utils.getBindingMode(m) != BindingMode.DontBinding)
                                     .Select(f => new TsPropertyGenInfo()
                        {
                            Name     = f.Name,
                            Document = DocResolver.GetTsDocument(f),
                            TypeName = Utils.GetTsTypeName(f.FieldType),
                            IsStatic = f.IsStatic
                        })
                                     .Concat(
                            type.GetProperties(Utils.Flags).Where(m => m.Name != "Item")
                            .Where(m => !Utils.IsNotSupportedMember(m, true))
                            .Where(m => Utils.getBindingMode(m) != BindingMode.DontBinding)
                            .Select(p => new TsPropertyGenInfo()
                        {
                            Name      = p.Name,
                            Document  = DocResolver.GetTsDocument(p),
                            TypeName  = Utils.GetTsTypeName(p.PropertyType),
                            IsStatic  = Utils.IsStatic(p),
                            HasGetter = p.GetGetMethod() != null && p.GetGetMethod().IsPublic,
                            HasSetter = p.GetSetMethod() != null && p.GetSetMethod().IsPublic
                        })
                            )
                                     .ToArray() : new TsPropertyGenInfo[] { },
                        IsGenericTypeDefinition = type.IsGenericTypeDefinition,
                        IsDelegate       = (Utils.IsDelegate(type) && type != typeof(Delegate)),
                        IsInterface      = type.IsInterface,
                        Namespace        = type.Namespace,
                        ExtensionMethods = Utils.GetExtensionMethods(type, genTypeSet).Select(m => TsMethodGenInfo.FromMethodBase(m, type.IsGenericTypeDefinition, true)).ToArray()
                    };

                    if (result.IsGenericTypeDefinition)
                    {
                        result.GenericParameters = type.GetGenericArguments().Select(t => t.ToString()).ToArray();
                    }

                    if (result.IsDelegate)
                    {
                        if (type == typeof(Delegate) || type == typeof(System.MulticastDelegate))
                        {
                            result.DelegateDef = "(...args:any[]) => any";
                        }
                        else
                        {
                            var m         = type.GetMethod("Invoke");
                            var tsFuncDef = "(" + string.Join(", ", m.GetParameters().Select(p => p.Name + ": " + Utils.GetTsTypeName(p.ParameterType)).ToArray()) + ") => " + Utils.GetTsTypeName(m.ReturnType);
                            result.DelegateDef = tsFuncDef;
                        }
                    }

                    Type[] interfaces = type.GetInterfaces();
                    if (interfaces != null && interfaces.Length > 0)
                    {
                        List <TsTypeGenInfo> genInfoList = new List <TsTypeGenInfo>();
                        for (int i = 0; i < interfaces.Length; i++)
                        {
                            var interfaceTypeGenInfo = new TsTypeGenInfo()
                            {
                                Name      = interfaces[i].IsGenericType ? Utils.GetTsTypeName(interfaces[i]) : interfaces[i].Name.Replace('`', '$'),
                                Document  = DocResolver.GetTsDocument(interfaces[i]),
                                Namespace = interfaces[i].Namespace
                            };
                            if (interfaces[i].IsNested)
                            {
                                List <string> p    = new List <string>();
                                Type          temp = interfaces[i];
                                while (temp.IsNested)
                                {
                                    p.Add(temp.DeclaringType.Name.Replace('`', '$'));
                                    temp = temp.DeclaringType;
                                }
                                p.Reverse();
                                if (interfaces[i].Namespace != null)
                                {
                                    interfaceTypeGenInfo.Namespace = interfaces[i].Namespace + '.' + string.Join(".", p.ToArray());
                                }
                                else
                                {
                                    interfaceTypeGenInfo.Namespace = string.Join(".", p.ToArray());
                                }
                            }
                            if (interfaces[i].IsGenericType && interfaces[i].Namespace != null)
                            {
                                interfaceTypeGenInfo.Name = interfaceTypeGenInfo.Name.Substring(interfaceTypeGenInfo.Namespace.Length + 1);
                            }
                            genInfoList.Add(interfaceTypeGenInfo);
                        }
                        result.interfaces = genInfoList.ToArray();
                    }

                    if (type.IsNested)
                    {
                        List <string> p    = new List <string>();
                        Type          temp = type;
                        while (temp.IsNested)
                        {
                            p.Add(temp.DeclaringType.Name.Replace('`', '$'));
                            temp = temp.DeclaringType;
                        }
                        p.Reverse();
                        if (type.Namespace != null)
                        {
                            result.Namespace = type.Namespace + '.' + string.Join(".", p.ToArray());
                        }
                        else
                        {
                            result.Namespace = string.Join(".", p.ToArray());
                        }
                    }

                    if (!type.IsEnum && type.BaseType != null && type != typeof(object) && !result.IsDelegate && !result.IsInterface)
                    {
                        result.BaseType = new TsTypeGenInfo()
                        {
                            Name      = type.BaseType.IsGenericType ? Utils.GetTsTypeName(type.BaseType) : type.BaseType.Name.Replace('`', '$'),
                            Document  = DocResolver.GetTsDocument(type.BaseType),
                            Namespace = type.BaseType.Namespace
                        };
                        if (type.BaseType.IsNested)
                        {
                            List <string> p    = new List <string>();
                            Type          temp = type.BaseType;
                            while (temp.IsNested)
                            {
                                p.Add(temp.DeclaringType.Name.Replace('`', '$'));
                                temp = temp.DeclaringType;
                            }
                            p.Reverse();
                            if (type.BaseType.Namespace != null)
                            {
                                result.BaseType.Namespace = type.BaseType.Namespace + '.' + string.Join(".", p.ToArray());
                            }
                            else
                            {
                                result.BaseType.Namespace = string.Join(".", p.ToArray());
                            }
                        }
                        if (type.BaseType.IsGenericType && type.BaseType.Namespace != null)
                        {
                            result.BaseType.Name = result.BaseType.Name.Substring(result.BaseType.Namespace.Length + 1);
                        }
                    }

                    if (type.IsEnum)
                    {
                        result.IsEnum = true;
                        var KeyValues = type.GetFields(BindingFlags.Static | BindingFlags.Public)
                                        .Where(f => f.Name != "value__")
                                        .Select(f => f.Name + " = " + Convert.ChangeType(f.GetValue(null), Enum.GetUnderlyingType(type))).ToArray();
                        result.EnumKeyValues = string.Join(", ", KeyValues);
                    }

                    return(result);
                }