private void Load(ConfigSourceGroup group, ConfigSource configSource) { NAssemblySource assemblySource = null; if (configSource.AssemblyPath == null) { Logger.Error("Assembly path not specified for <source>. Cannot be null"); return; } // Check Parameters if (!File.Exists(configSource.AssemblyPath)) { Logger.Error("Assembly file [{0}] not found", configSource.AssemblyPath); return; } var extension = Path.GetExtension(configSource.AssemblyPath); if (extension != null && (extension.ToLower() == ".dll" || extension.ToLower() == ".exe")) { assemblySource = LoadAssembly(group, configSource.AssemblyPath); AssemblySources.Add(assemblySource); } else { Logger.Fatal("Invalid Assembly source [{0}]. Must be either an Assembly", configSource.AssemblyPath); } // If documentation path is null, use by default the assembly path if (configSource.DocumentationPath == null) { configSource.DocumentationPath = Path.ChangeExtension(configSource.AssemblyPath, ".xml"); } if (!File.Exists(configSource.DocumentationPath)) { Logger.Error("Documentation file [{0}] not found", configSource.DocumentationPath); return; } extension = Path.GetExtension(configSource.DocumentationPath); if (extension != null && extension.ToLower() == ".xml") { if (assemblySource == null) { assemblySource = new NAssemblySource(); AssemblySources.Add(assemblySource); } assemblySource.Document = LoadAssemblyDocumentation(configSource.DocumentationPath); } else { Logger.Fatal("Invalid Assembly source [{0}]. Must be either a Xml comment file", configSource.DocumentationPath); } }
private void Load(ConfigSource configSource) { NAssemblySource assemblySource = null; if (configSource.AssemblyPath != null) { // Check Parameters if (!File.Exists(configSource.AssemblyPath)) { Logger.Error("Assembly file [{0}] not found", configSource.AssemblyPath); return; } var extension = Path.GetExtension(configSource.AssemblyPath); if (extension != null && (extension.ToLower() == ".dll" || extension.ToLower() == ".exe")) { assemblySource = LoadAssembly(configSource.AssemblyPath); assemblySource.MergeGroup = configSource.MergeGroup; AssemblySources.Add(assemblySource); } else { Logger.Fatal("Invalid Assembly source [{0}]. Must be either an Assembly", configSource.AssemblyPath); } } if (configSource.DocumentationPath != null) { if (!File.Exists(configSource.DocumentationPath)) { Logger.Error("Documentation file [{0}] not found", configSource.DocumentationPath); return; } var extension = Path.GetExtension(configSource.DocumentationPath); if (extension != null && extension.ToLower() == ".xml") { if (assemblySource == null) { assemblySource = new NAssemblySource(); AssemblySources.Add(assemblySource); } assemblySource.Document = LoadAssemblyDocumentation(configSource.DocumentationPath); } else { Logger.Fatal("Invalid Assembly source [{0}]. Must be either a Xml comment file", configSource.DocumentationPath); } } }
private NAssemblySource LoadAssembly(string source) { var dirPath = Path.GetDirectoryName(source); if (!searchPaths.Contains(dirPath)) { searchPaths.Add(dirPath); AddSearchDirectory(dirPath); } var parameters = new ReaderParameters(ReadingMode.Immediate) { AssemblyResolver = this }; var assemblyDefinition = AssemblyDefinition.ReadAssembly(source, parameters); var assemblySource = new NAssemblySource(assemblyDefinition) { Filename = source }; return(assemblySource); }
private NAssemblySource LoadAssembly(ConfigSourceGroup group, string source) { var dirPath = Path.GetDirectoryName(source); var assemblyResolver = new DefaultAssemblyResolver(); // Remove any default search path assemblyResolver.RemoveSearchDirectory("."); assemblyResolver.RemoveSearchDirectory("bin"); // Search from assembly directory assemblyResolver.AddSearchDirectory(dirPath); // Add additional search directory foreach (var searchDirectory in group.SearchDirectories) { assemblyResolver.AddSearchDirectory(searchDirectory); } var parameters = new ReaderParameters(ReadingMode.Immediate) { AssemblyResolver = assemblyResolver }; assemblyResolver.ResolveFailure += (sender, reference) => { var searchDirectories = assemblyResolver.GetSearchDirectories(); foreach (var directory in searchDirectories) { var tryPath = Path.Combine(directory, reference.Name + ".winmd"); if (!File.Exists(tryPath)) { continue; } try { var winmdAssembly = AssemblyDefinition.ReadAssembly(tryPath, parameters); if (winmdAssembly != null) { return(winmdAssembly); } } catch { // Failed... fall thru and try the next one. } } // Log an error if we can't find the assembly. Mono.Cecil will throw an exception just after returning from // this callback Logger.Error("Failed to resolve {0}", reference.FullName); return(null); }; var assemblyDefinition = AssemblyDefinition.ReadAssembly(source, parameters); var assemblySource = new NAssemblySource(assemblyDefinition) { Filename = source, MergeGroup = group.MergeGroup }; return(assemblySource); }
/// <summary> /// Loads from an assembly source definition all types to document. /// </summary> /// <param name="assemblySource">The assembly source definition.</param> /// <param name="memberRegistry">The member registry to populate with types.</param> /// <returns> /// An assembly documentator that contains all documented types, methods. /// </returns> public void LoadFrom(NAssemblySource assemblySource, MemberRegistry memberRegistry) { CurrentMergeGroup = assemblySource.MergeGroup; _source = assemblySource; _registry = memberRegistry; var assemblyDefinition = (AssemblyDefinition)assemblySource.Assembly; var assemblyName = assemblyDefinition.Name.Name; var assemblyId = "A:" + assemblyName; var assembly = (NAssembly)_registry.FindById(assemblyId); // If new assembly if (assembly == null) { assembly = new NAssembly { Name = assemblyName }; assembly.FullName = assembly.Name; assembly.Id = assemblyId; assembly.PageId = PageIdFunction(assembly); assembly.PageTitle = assembly.Name + " " + assembly.Category; assembly.Version = assemblyDefinition.Name.Version.ToString(); assembly.FileName = Path.GetFileName(Utility.GetProperFilePathCapitalization(assemblySource.Filename)); // Apply documentation from AssemblyDoc special class assembly.DocNode = _source.Document.FindMemberDoc("T:" + assembly.Name + "." + AssemblyDocClass); _registry.Register(assembly); } assembly.SetApiGroup(CurrentMergeGroup, true); CurrentAssembly = assembly; Logger.Message("Processing assembly [{0}] [{1}]", assembly.FullName, CurrentMergeGroup); // Process namespaces // Namespaces are created only if a type is actually public foreach (var module in assemblyDefinition.Modules) { foreach (var type in module.Types) { // Todo add configurable filter if (!type.IsPublic) { continue; } // Skip empty namespaces and special <Module> if (string.IsNullOrEmpty(type.Namespace) || type.Namespace.StartsWith("<")) { continue; } // Create naemespace var parentNamespace = AddNamespace(assembly, type.Namespace); parentNamespace.SetApiGroup(CurrentMergeGroup, true); AddType(parentNamespace, type); } } //// Remove empty namespaces //foreach (var removeNamespace in namespaces.Values) //{ // if (removeNamespace.Types.Count == 0) // assembly.Namespaces.Remove(removeNamespace); //} // Sort namespace in alphabetical order //@assembly.Namespaces.Sort((left, right) => left.PageId.CompareTo(right.PageId)); //foreach (var namespaceName in @assembly.Namespaces) // namespaces[namespaceName].Types.Sort((left, right) => left.PageId.CompareTo(right.PageId)); }