Exemple #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>());
            }
        }
        public static MarkdownableSharpType[] Load(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 = Assembly.LoadFrom(dllPath);

                Type[] types = Type.EmptyTypes;

                try {
                    types = assembly.GetTypes();
                }
                catch (ReflectionTypeLoadException ex) {
                    types = ex.Types.Where(t => t != null).ToArray();
                }

                return(types
                       .Where(x => x != null)
                       .Where(x => x.IsPublic && !typeof(Delegate).IsAssignableFrom(x) && !x.GetCustomAttributes <ObsoleteAttribute>().Any())
                       .Where(x => IsRequiredNamespace(x, pattern))
                       .Select(x => new MarkdownableSharpType(x, commentsLookup))
                       .ToArray());
            }
            catch (Exception ex) {
                logger.LogWarning("Could not load assembly. \n" + ex.Message);
                return(Array.Empty <MarkdownableSharpType>());
            }
        }
Exemple #3
0
        public static NugetPackage Load(string packageFile, Regex pattern)
        {
            var assemblies   = new List <PackageAssembly>();
            var assemblyDocs = new Dictionary <string, XmlDocumentComment[]>(StringComparer.InvariantCultureIgnoreCase);

            ZipArchiveEntry nuspec = null;

            using (var stream = File.OpenRead(packageFile))
                using (var archive = new ZipArchive(stream, ZipArchiveMode.Read))
                {
                    foreach (var e in archive.Entries.Where(x => x.Name != "_._").OrderBy(x => x.FullName))
                    {
                        var n     = e.FullName;
                        var isLib = n.StartsWith("lib/", StringComparison.InvariantCultureIgnoreCase);
                        if (isLib && (n.EndsWith(".dll", StringComparison.InvariantCultureIgnoreCase) ||
                                      n.EndsWith(".xml", StringComparison.InvariantCultureIgnoreCase)))
                        {
                            var parts = n.Split('/', StringSplitOptions.RemoveEmptyEntries);
                            var tfm   = (parts.Length >= 3) ? Uri.UnescapeDataString(parts[1].Trim().ToLowerInvariant()) : "";

                            if (n.EndsWith(".xml", StringComparison.InvariantCultureIgnoreCase))
                            {
                                try {
                                    var asmName  = Path.GetFileNameWithoutExtension(n);
                                    var comments = VSDocParser.ParseXmlComment(XDocument.Load(e.Open()));
                                    if (!assemblyDocs.ContainsKey(asmName))
                                    {
                                        assemblyDocs[asmName] = comments;
                                    }
                                    else
                                    {
                                        assemblyDocs[asmName] = assemblyDocs[asmName].Concat(comments).ToArray();
                                    }
                                }
                                catch {
                                    //Just ignore the exceptions
                                }
                            }
                            else
                            {
                                if (pattern == null || pattern.IsMatch(Path.GetFileNameWithoutExtension(e.Name)))
                                {
                                    using (var ms = new MemoryStream()) {
                                        e.Open().CopyTo(ms);
                                        ms.Position = 0;

                                        var ad = AssemblyDefinition.ReadAssembly(ms);
                                        assemblies.Add(new PackageAssembly(tfm, ad));
                                    }
                                }
                            }
                        }
                        else if (n.EndsWith(".nuspec", StringComparison.InvariantCultureIgnoreCase))
                        {
                            nuspec = e;
                        }
                        else
                        {
                            // nothing to do
                        }
                    }

                    foreach (var asm in assemblies)
                    {
                        if (assemblyDocs.TryGetValue(asm.Assembly.Name.Name, out var comments))
                        {
                            asm.Comments = comments;
                        }
                    }

                    return(new NugetPackage(assemblies, nuspec));
                }
        }