Example #1
0
        public static ModuleReader Read(ModuleDefinition module)
        {
            var reader = new ModuleReader();

            if (module.AssemblyReferences.All(reference => reference.Name != "Stiletto"))
            {
                return reader;
            }

            var allTypes = module.GetTypes();

            foreach (var t in allTypes)
            {
                if (IsModule(t))
                {
                    reader.ModuleTypes.Add(t);
                }

                if (IsInject(t))
                {
                    reader.InjectTypes.Add(t);
                }
            }

            return reader;
        }
Example #2
0
        public ModuleProcessor(
            IErrorReporter errorReporter,
            ModuleDefinition moduleDefinition,
            References references,
            IEnumerable <ModuleReader> subModules)
        {
            this.errorReporter    = Conditions.CheckNotNull(errorReporter, "errorReporter");
            this.moduleDefinition = Conditions.CheckNotNull(moduleDefinition, "moduleDefinition");
            this.references       = references;
            this.submodules       = subModules
                                    .Append(ModuleReader.Read(moduleDefinition))
                                    .Where(reader => reader.UsesStiletto);

            moduleGenerators   = new List <ModuleGenerator>();
            injectGenerators   = new List <InjectBindingGenerator>();
            lazyGenerators     = new List <LazyBindingGenerator>();
            providerGenerators = new List <ProviderBindingGenerator>();
            baseGeneratorQueue = new Queue <TypeDefinition>();
        }
Example #3
0
        private IList <ModuleProcessor> GatherModulesNeedingProcessing()
        {
            var processors = new List <ModuleProcessor>();

            if (!IsModuleProcessable(ModuleDefinition))
            {
                return(processors);
            }

            var moduleReaders      = new List <ModuleReader>();
            var stilettoReferences = StilettoReferences.Create(AssemblyResolver);
            var references         = new References(ModuleDefinition, stilettoReferences);

            var copyLocalAssemblies = new Dictionary <string, bool>(StringComparer.Ordinal);
            var localDebugFiles     = new Queue <string>();

            foreach (var copyLocal in ReferenceCopyLocalPaths)
            {
                if (copyLocal.EndsWith(".pdb") || copyLocal.EndsWith(".mdb"))
                {
                    // We'll come back to the debug files after we have a complete
                    // list of local assemblies.
                    localDebugFiles.Enqueue(copyLocal);
                    continue;
                }

                if (copyLocal.EndsWith(".exe") || copyLocal.EndsWith(".dll"))
                {
                    copyLocalAssemblies[copyLocal] = false;
                }
            }

            // Check which assemblies have debug symbols and, consequently,
            // for which assemblies we will attempt to read and write such symbols.
            while (localDebugFiles.Count > 0)
            {
                var pdb     = localDebugFiles.Dequeue();
                var rawPath = Path.Combine(Path.GetDirectoryName(pdb), Path.GetFileNameWithoutExtension(pdb));
                var dll     = rawPath + ".dll";
                var exe     = rawPath + ".exe";

                if (copyLocalAssemblies.ContainsKey(dll))
                {
                    copyLocalAssemblies[dll] = true;
                }

                if (copyLocalAssemblies.ContainsKey(exe))
                {
                    copyLocalAssemblies[exe] = true;
                }
            }

            foreach (var pathAndHasPdb in copyLocalAssemblies)
            {
                var path     = pathAndHasPdb.Key;
                var hasPdb   = pathAndHasPdb.Value;
                var assembly = AssemblyDefinition.ReadAssembly(path, new ReaderParameters {
                    ReadSymbols = hasPdb
                });

                // TODO: Figure out how to differentiate between third-party libs and client code.
                if (assembly.Name.HasPublicKey)
                {
                    LogWarning("Assembly " + assembly.Name + " is strong-named and will not be processed.");
                    continue;
                }

                dependencies[path] = Tuple.Create(assembly, hasPdb);

                foreach (var module in assembly.Modules)
                {
                    if (!IsModuleProcessable(module))
                    {
                        continue;
                    }

                    if (module.IsMain)
                    {
                        var importedCtor       = module.Import(references.InternalsVisibleToAttribute);
                        var internalsVisibleTo = new CustomAttribute(importedCtor);
                        internalsVisibleTo.ConstructorArguments.Add(new CustomAttributeArgument(module.TypeSystem.String, ModuleDefinition.Assembly.Name.Name));
                        module.Assembly.CustomAttributes.Add(internalsVisibleTo);
                    }

                    moduleReaders.Add(ModuleReader.Read(module));
                    AddModuleToAssemblyDictionary(module, null);
                }
            }

            AddModuleToAssemblyDictionary(ModuleDefinition, null);
            var mainModuleProcessor = new ModuleProcessor(
                errorReporter,
                ModuleDefinition,
                references,
                moduleReaders);

            processors.Add(mainModuleProcessor);

            return(processors);
        }