コード例 #1
0
        static TypeCache CacheTypes(BaseModuleWeaver weaver, MockAssemblyResolver assemblyResolver)
        {
            var typeCache = new TypeCache(assemblyResolver.Resolve);

            typeCache.BuildAssembliesToScan(weaver);
            return(typeCache);
        }
コード例 #2
0
        static TypeCache CacheTypes(BaseModuleWeaver weaver, MockAssemblyResolver assemblyResolver)
        {
            var definitions = weaver.GetAssembliesForScanning()
                              .Select(assemblyResolver.Resolve)
                              .Where(definition => definition != null);

            var typeCache = new TypeCache();

            typeCache.Initialise(definitions);
            return(typeCache);
        }
コード例 #3
0
        public static TestResult ExecuteTestRun(this BaseModuleWeaver weaver, string assemblyPath)
        {
            assemblyPath = Path.Combine(CodeBaseLocation.CurrentDirectory, assemblyPath);
            var fodyTempDir = Path.Combine(Path.GetDirectoryName(assemblyPath), "fodytemp");

            Directory.CreateDirectory(fodyTempDir);
            var targetAssemblyPath = Path.Combine(fodyTempDir, Path.GetFileName(assemblyPath));
            var targetSymbolsPath  = Path.ChangeExtension(targetAssemblyPath, "pdb");
            var symbolsPath        = Path.ChangeExtension(assemblyPath, "pdb");

            File.Copy(assemblyPath, targetAssemblyPath, true);
            File.Copy(symbolsPath, targetSymbolsPath, true);

            using (var assemblyResolver = new MockAssemblyResolver())
            {
                var typeCache = CacheTypes(weaver, assemblyResolver);

                var testStatus = new TestResult();
                weaver.LogDebug        = text => testStatus.AddMessage(text, MessageImportanceDefaults.Debug);
                weaver.LogInfo         = text => testStatus.AddMessage(text, MessageImportanceDefaults.Info);
                weaver.LogMessage      = (text, messageImportance) => testStatus.AddMessage(text, messageImportance);
                weaver.LogWarning      = text => testStatus.AddWarning(text, null);
                weaver.LogWarningPoint = (text, sequencePoint) => testStatus.AddWarning(text, sequencePoint);
                weaver.LogError        = text => testStatus.AddError(text, null);
                weaver.LogErrorPoint   = (text, sequencePoint) => testStatus.AddError(text, sequencePoint);

                weaver.FindType        = typeCache.FindType;
                weaver.ResolveAssembly = assemblyResolver.Resolve;

                var readerParameters = new ReaderParameters(ReadingMode.Immediate)
                {
                    AssemblyResolver     = assemblyResolver,
                    SymbolReaderProvider = new SymbolReaderProvider(),
                    ReadWrite            = true,
                    ReadSymbols          = true
                };

                using (var moduleDefinition = ModuleDefinition.ReadModule(targetAssemblyPath, readerParameters))
                {
                    weaver.ModuleDefinition = moduleDefinition;

                    weaver.Execute();

                    moduleDefinition.Write();
                }

                PeVerifier.ThrowIfDifferent(assemblyPath, targetAssemblyPath, new[] { "0x80070002" });
                testStatus.Assembly = Assembly.Load(File.ReadAllBytes(targetAssemblyPath));
                return(testStatus);
            }
        }
コード例 #4
0
ファイル: ReferenceCleaner.cs プロジェクト: zhaoqinghai/Fody
        public static void CleanReferences(ModuleDefinition module, BaseModuleWeaver weaver, Action <string> log)
        {
            if (!weaver.ShouldCleanReference)
            {
                return;
            }

            var weaverName        = weaver.GetType().Assembly.GetName().Name.ReplaceCaseless(".Fody", "");
            var referenceToRemove = module.AssemblyReferences
                                    .FirstOrDefault(x => x.Name == weaverName);

            if (referenceToRemove == null)
            {
                log($"\tNo reference to '{weaverName}' found. References not modified.");
                return;
            }

            module.AssemblyReferences.Remove(referenceToRemove);
            log($"\tRemoving reference to '{weaverName}'.");
        }
コード例 #5
0
        public static TestResult ExecuteTestRun(
            this BaseModuleWeaver weaver,
            string assemblyPath,
            bool runPeVerify = true,
            Action <ModuleDefinition> afterExecuteCallback  = null,
            Action <ModuleDefinition> beforeExecuteCallback = null,
            string assemblyName = null,
            IEnumerable <string> ignoreCodes = null)
        {
            assemblyPath = Path.Combine(CodeBaseLocation.CurrentDirectory, assemblyPath);
            var fodyTempDir = Path.Combine(Path.GetDirectoryName(assemblyPath), "fodytemp");

            Directory.CreateDirectory(fodyTempDir);

            IoHelper.PurgeDirectory(fodyTempDir);

            string targetFileName;

            if (assemblyName == null)
            {
                assemblyName   = Path.GetFileNameWithoutExtension(assemblyPath);
                targetFileName = Path.GetFileName(assemblyPath);
            }
            else
            {
                targetFileName = assemblyName + ".dll";
            }

            var targetAssemblyPath = Path.Combine(fodyTempDir, targetFileName);

            using (var assemblyResolver = new MockAssemblyResolver())
            {
                var typeCache = CacheTypes(weaver, assemblyResolver);

                var testStatus = new TestResult();
                weaver.LogDebug         = text => testStatus.AddMessage(text, MessageImportanceDefaults.Debug);
                weaver.LogInfo          = text => testStatus.AddMessage(text, MessageImportanceDefaults.Info);
                weaver.LogMessage       = (text, messageImportance) => testStatus.AddMessage(text, messageImportance);
                weaver.LogWarning       = text => testStatus.AddWarning(text, null);
                weaver.LogWarningPoint  = (text, sequencePoint) => testStatus.AddWarning(text, sequencePoint);
                weaver.LogError         = text => testStatus.AddError(text, null);
                weaver.LogErrorPoint    = (text, sequencePoint) => testStatus.AddError(text, sequencePoint);
                weaver.AssemblyFilePath = assemblyPath;
                weaver.FindType         = typeCache.FindType;
                weaver.TryFindType      = typeCache.TryFindType;
                weaver.ResolveAssembly  = assemblyResolver.Resolve;
                var readerParameters = new ReaderParameters
                {
                    AssemblyResolver     = assemblyResolver,
                    SymbolReaderProvider = new SymbolReaderProvider(),
                    ReadWrite            = false,
                    ReadSymbols          = true,
                };

                using (var module = ModuleDefinition.ReadModule(assemblyPath, readerParameters))
                {
                    module.Assembly.Name.Name = assemblyName;
                    weaver.ModuleDefinition   = module;
                    weaver.TypeSystem         = new TypeSystem(typeCache.FindType, module);
                    beforeExecuteCallback?.Invoke(module);

                    weaver.Execute();
                    ReferenceCleaner.CleanReferences(module, weaver, weaver.LogDebug);

                    afterExecuteCallback?.Invoke(module);

                    var writerParameters = new WriterParameters
                    {
                        WriteSymbols = true
                    };

                    module.Write(targetAssemblyPath, writerParameters);
                }

                if (runPeVerify && IsWindows())
                {
                    List <string> ignoreList;
                    if (ignoreCodes == null)
                    {
                        ignoreList = new List <string>();
                    }
                    else
                    {
                        ignoreList = ignoreCodes.ToList();
                    }

                    PeVerifier.ThrowIfDifferent(assemblyPath, targetAssemblyPath, ignoreList, Path.GetDirectoryName(assemblyPath));
                }

                testStatus.Assembly     = Assembly.Load(File.ReadAllBytes(targetAssemblyPath));
                testStatus.AssemblyPath = targetAssemblyPath;
                return(testStatus);
            }
        }
コード例 #6
0
ファイル: TypeCache.cs プロジェクト: zhouji0212/Fody
 public void BuildAssembliesToScan(BaseModuleWeaver weaver)
 {
     BuildAssembliesToScan(new[] { weaver });
 }