Example #1
0
        public static MarkdownableSharpType[] LoadFromAssembly(CSharpLibrary library, string dllPath, string pattern, ILogger logger)
        {
            var xmlPath = Path.Combine(Directory.GetParent(dllPath).FullName, Path.GetFileNameWithoutExtension(dllPath) + ".xml");

            XmlDocumentComment[] comments = new XmlDocumentComment[0];
            if (File.Exists(xmlPath))
            {
                comments = VSDocParser.ParseXmlComment(XDocument.Parse(File.ReadAllText(xmlPath)));
            }
            var commentsLookup = comments.ToLookup(x => x.ClassName);

            try {
                var assembly = AssemblyDefinition.ReadAssembly(dllPath);
                var types    = assembly.Modules.SelectMany(m => m.Types);

                return(types
                       .Where(x => x != null)
                       .Where(x => x.IsPublic && !x.IsDelegate() && !x.HasObsoleteAttribute())
                       .Where(x => IsRequiredNamespace(x, pattern))
                       .Select(x => new MarkdownableSharpType(library, x, comments.Where(c => c.ClassName == x.FullName)))
                       .ToArray());
            }
            catch (Exception ex) {
                logger.LogWarning("Could not load assembly. \n" + ex.Message);
                return(Array.Empty <MarkdownableSharpType>());
            }
        }
Example #2
0
 public MarkdownableSharpType(CSharpLibrary library, NugetPackage package, TypeDefinition type, string[] targetFrameworks, IEnumerable <XmlDocumentComment> comments)
 {
     _library         = library;
     _package         = package;
     _type            = type;
     Comments         = comments;
     TargetFrameworks = targetFrameworks;
 }
Example #3
0
        public static string ToMarkdownMethodInfo(CSharpLibrary lib, MethodDefinition methodInfo)
        {
            var isExtension = methodInfo.HasExtensionAttribute();

            var seq = methodInfo.Parameters.Select(x => {
                var suffix = x.HasDefault ? (" = " + (x.Constant ?? $"<span style='color: blue'>null</span>")) : "";
                return(ToMarkdownTypeReference(lib, x.ParameterType, isParam: true) + " " + x.Name + suffix);
            });

            var beautifulMethodName = methodInfo.IsConstructor ? methodInfo.DeclaringType.Name : methodInfo.Name;
            var index = beautifulMethodName.IndexOf("`");

            if (index > 0)
            {
                beautifulMethodName = beautifulMethodName.Remove(index);
            }

            return(beautifulMethodName + "(" + (isExtension ? "<span style='color: blue'>this</span> " : "") + string.Join(", ", seq) + ")");
        }
Example #4
0
        private void LoadAssemblies()
        {
            Regex fileRegex = (!string.IsNullOrEmpty(_fileRegexPattern))
                    ? new Regex(_fileRegexPattern)
                    : null;

            //Finds all dll files with current pattern
            Func <string, bool> isFileToProcess = (s) =>
            {
                if (!s.EndsWith(".dll"))
                {
                    return(false);
                }

                if (fileRegex != null)
                {
                    var fileName = s.Substring(s.LastIndexOf("\\") + 1);
                    if (!fileRegex.IsMatch(fileName))
                    {
                        return(false);
                    }
                }

                return(true);
            };

            var library = new CSharpLibrary();

            library.RootPath = _aistantSettings?.Section?.Uri ?? "";

            var assemblyFiles = Directory.GetFiles(_srcPath).Where(isFileToProcess).ToList();

            foreach (var assemblyFilePath in assemblyFiles)
            {
                _logger.LogInformation($"Loading assembly {assemblyFilePath}...");
                _types.AddRange(MarkdownCSharpGenerator.LoadFromAssembly(library, assemblyFilePath, _nameSpaceRegexPattern, _logger));
            }
            foreach (var type in _types)
            {
                library.Types.TryAdd(type.ClrType.FullName, type);
            }
        }
Example #5
0
        public static MarkdownableSharpType[] LoadFromPackage(CSharpLibrary library, NugetPackage package, string pattern, ILogger logger)
        {
            var result = new List <MarkdownableSharpType>();
            var groups = package.Assemblies.GroupBy(a => a.Assembly.Name.Name);

            foreach (var g in groups)
            {
                var dict = new Dictionary <string, (TypeDefinition Type, IEnumerable <XmlDocumentComment> Comments, List <string> Targets)>();
                foreach (var pasm in g.OrderBy(p => p.TargetFramework))
                {
                    var types = pasm.Assembly.Modules.SelectMany(m => m.Types)
                                .SelectMany(t => t.NestedTypes.Where(t => t.IsNestedPublic).Concat(new[] { t }))
                                .Where(t => (t.IsPublic || t.IsNestedPublic) && !t.IsDelegate() && !t.HasObsoleteAttribute())
                                .Where(t => IsRequiredNamespace(t, pattern))
                                .ToList();
                    foreach (var type in types)
                    {
                        if (dict.TryGetValue(type.FullName, out var typeInfo))
                        {
                            typeInfo.Targets.Add(pasm.TargetFramework);
                        }
                        else
                        {
                            var fullTypeName = type.FullName.Replace("/", ".");
                            dict.Add(type.FullName, (type, pasm.Comments?.Where(c => c.ClassName == type.ToString() || c.ClassName == fullTypeName) ?? Enumerable.Empty <XmlDocumentComment>(),
                                                     new List <string>()
                            {
                                pasm.TargetFramework
                            }));
                        }
                    }
                }

                foreach (var typeInfo in dict.Values)
                {
                    result.Add(new MarkdownableSharpType(library, package, typeInfo.Type, typeInfo.Targets.ToArray(), typeInfo.Comments));
                }
            }

            return(result.ToArray());
        }
Example #6
0
        private void LoadPackages()
        {
            Regex fileRegex = (!string.IsNullOrEmpty(_fileRegexPattern))
                  ? new Regex(_fileRegexPattern)
                  : null;

            var library = new CSharpLibrary();

            library.RootPath = _aistantSettings?.Section?.Uri ?? "";
            var packagesFiles = Directory.GetFiles(_packagesPath, "*.nupkg");

            foreach (var packageFilePath in packagesFiles)
            {
                _logger.LogInformation($"Loading package {packageFilePath}...");
                var package = NugetPackage.Load(packageFilePath, fileRegex);
                library.Packages.Add(package);
                _types.AddRange(MarkdownCSharpGenerator.LoadFromPackage(library, package, _nameSpaceRegexPattern, _logger));
            }
            foreach (var type in _types)
            {
                library.Types.TryAdd(type.ClrType.FullName, type);
            }
        }
Example #7
0
 public MarkdownableSharpType(CSharpLibrary library, TypeDefinition type, IEnumerable <XmlDocumentComment> comments) : this(library, null, type, new string[] { }, comments)
 {
 }
Example #8
0
        public static string ToMarkdownTypeReference(CSharpLibrary lib, TypeReference t, bool isParam = false)
        {
            if (t == null)
            {
                return("");
            }
            if (t.FullName == "System.Void")
            {
                return("`void`");
            }
            if (t.FullName == "System.Object")
            {
                return("`object`");
            }
            if (t.FullName == "System.Boolean")
            {
                return("`bool`");
            }
            if (t.FullName == "System.String")
            {
                return("`string`");
            }
            if (t.FullName == "System.Int32")
            {
                return("`int`");
            }
            if (t.FullName == "System.Int64")
            {
                return("`long`");
            }
            if (t.FullName == "System.Double")
            {
                return("`double`");
            }

            var hasMdType = lib.Types.TryGetValue(t.FullName, out var mdType);

            string name;

            if (!t.IsGenericInstance && !t.HasGenericParameters)
            {
                name = (t.IsNested && !isParam) ? $"{t.DeclaringType.Name}.{t.Name}" : t.Name;
                if (hasMdType)
                {
                    return(MarkdownBuilder.MarkdownUrl(name, mdType.GetPath()));
                }

                return(MarkdownBuilder.MarkdownCodeQuote(name));
            }

            string innerFormat = "";

            if (t is GenericInstanceType genType)
            {
                var args = genType.GenericArguments.ToArray();
                innerFormat = string.Join(", ", args.Select(x => ToMarkdownTypeReference(lib, x, isParam: true)));
            }
            else
            {
                innerFormat = string.Join(", ", t.GenericParameters.Select(x => x.Name));
            }

            name = Regex.Replace(t.Name, @"`.+$?", "");
            if (hasMdType)
            {
                name = MarkdownBuilder.MarkdownUrl(name, mdType.GetPath());
            }
            else
            {
                name = MarkdownBuilder.MarkdownCodeQuote(name);
            }

            return(name + "&lt;" + innerFormat + "&gt;");
        }