예제 #1
0
        public void AddDependency(string assembly)
        {
            if (String.IsNullOrEmpty(assembly))
            {
                return;
            }
            if (_dependencies == null)
            {
                _dependencies = new DependencyContent();
            }

            _dependencies.Add(new DependencyItem(
                                  Path.GetFileName(assembly), assembly));
        }
        private void Resolve(BuildLogger logger, AssemblyNameReference nameRef,
                             IAssemblyResolver resolver, BuildFrameworkKind frameworkKind)
        {
            if (nameRef.Name.StartsWith("mscorlib",
                                        StringComparison.OrdinalIgnoreCase))
            {
                if (frameworkKind == BuildFrameworkKind.DotNet)
                {
                    return;
                }
            }
            if (_dictionary.ContainsKey(nameRef.FullName))
            {
                return;
            }

            _dictionary.Add(nameRef.FullName, true);

            ModuleDefinition refModDef = null;

            try
            {
                AssemblyDefinition refAsmDef = resolver.Resolve(nameRef);
                if (refAsmDef == null)
                {
                    return;
                }

                refModDef = refAsmDef.MainModule;
                if (refModDef == null)
                {
                    return;
                }

                string qualifiedName = refModDef.FullyQualifiedName;

                // Prevent adding assemblies in the GAC...
                if (String.IsNullOrEmpty(qualifiedName) || qualifiedName.IndexOf(
                        "GAC_MSIL", StringComparison.OrdinalIgnoreCase) >= 0)
                {
                    return;
                }
                else if (qualifiedName.IndexOf("GAC_32", StringComparison.OrdinalIgnoreCase) >= 0)
                {
                    return;
                }
                else if (qualifiedName.IndexOf("GAC_64", StringComparison.OrdinalIgnoreCase) >= 0)
                {
                    return;
                }

                // NOTE: We do not filter the reference assemblies, which are
                // dependent assemblies at this stage, since we will not be
                // able to provide the assembly redirection support. This is
                // done later at the copying stage.
                DependencyItem item = new DependencyItem(Path.GetFileName(
                                                             qualifiedName), qualifiedName);

                _resolveContent.Add(item);

                if (logger != null)
                {
                    logger.WriteLine("Referenced Assembly: " + item.Name, BuildLoggerLevel.Info);
                }

                AssemblyDefinition foundAsmDef = AssemblyDefinition.ReadAssembly(
                    qualifiedName);

                if (nameRef.Version != foundAsmDef.Name.Version)
                {
                    if (logger != null)
                    {
                        logger.WriteLine(String.Format(
                                             "Creating Redirecting: '{0}' from version '{1}' to '{2}'",
                                             item.Name, nameRef.Version, foundAsmDef.Name.Version),
                                         BuildLoggerLevel.Info);
                    }

                    item.StrongName = foundAsmDef.FullName;

                    // For the redirection...
                    item.Redirected      = true;
                    item.RedirectVersion = nameRef.Version;
                    item.SetPublicKeyToken(nameRef.PublicKeyToken);
                    if (!String.IsNullOrEmpty(nameRef.Culture))
                    {
                        item.RedirectCulture = new CultureInfo(nameRef.Culture);
                    }
                }
            }
            catch (AssemblyResolutionException ex)
            {
                AssemblyNameReference exAsmRef = ex.AssemblyReference;
                if (exAsmRef != null && logger != null)
                {
                    logger.WriteLine("Could not resolve the reference dependency: "
                                     + exAsmRef.FullName, BuildLoggerLevel.Error);
                }

                return;
            }

            IList <AssemblyNameReference> asmRefs = refModDef.AssemblyReferences;

            if (asmRefs != null && asmRefs.Count != 0)
            {
                for (int i = 0; i < asmRefs.Count; i++)
                {
                    AssemblyNameReference nextAsmRef = asmRefs[i];

                    if (!_dictionary.ContainsKey(nextAsmRef.FullName))
                    {
                        this.Resolve(logger, nextAsmRef, resolver,
                                     frameworkKind);
                    }
                }
            }
        }