Ejemplo n.º 1
0
        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;
        }
Ejemplo n.º 3
0
        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 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);
            }
        }