PageRenderTime 55ms CodeModel.GetById 49ms app.highlight 3ms RepoModel.GetById 2ms app.codeStats 0ms

/src/PostWeaver/Program.cs

http://github.com/philiplaureano/LinFu
C# | 131 lines | 106 code | 25 blank | 0 comment | 21 complexity | 939a4841ff92df6371993b456bbb7586 MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using System.IO;
  4using System.Linq;
  5using LinFu.AOP.Cecil;
  6using LinFu.AOP.Cecil.Extensions;
  7using LinFu.AOP.Cecil.Interfaces;
  8using LinFu.AOP.Interfaces;
  9using LinFu.Reflection;
 10using LinFu.Reflection.Emit;
 11using Mono.Cecil;
 12
 13namespace PostWeaver
 14{
 15    internal class Program
 16    {
 17        private static void Main(string[] args)
 18        {
 19            if (args == null || args.Length == 0)
 20            {
 21                ShowHelp();
 22                return;
 23            }
 24
 25            var inputFile = args[0];
 26            if (!File.Exists(inputFile))
 27                throw new FileNotFoundException(inputFile);
 28
 29            var targetFile = inputFile;
 30            var assembly = AssemblyDefinition.ReadAssembly(targetFile);
 31            var targetDirectory = Path.GetDirectoryName(targetFile);
 32
 33            var filenameWithoutExtension = Path.GetFileNameWithoutExtension(targetFile);
 34            var pdbFileName = string.Format("{0}.pdb", filenameWithoutExtension);
 35            var pdbExists = File.Exists(pdbFileName);
 36
 37            var module = assembly.MainModule;
 38
 39            InterceptMethodCalls(assembly, targetDirectory);
 40            InterceptNewInstances(assembly, targetDirectory);
 41            InterceptMethodBodies(assembly, targetDirectory);
 42            InterceptFields(assembly, targetDirectory);
 43            InterceptExceptions(assembly, targetDirectory);            
 44
 45            assembly.Save(targetFile);
 46
 47            Console.WriteLine("PostWeaving Assembly '{0}' -> '{0}'", targetFile);
 48        }
 49
 50        private static void InterceptExceptions(AssemblyDefinition assembly, string targetDirectory)
 51        {
 52            var methodFilter = LoadFirstInstanceOf<IMethodFilter>(targetDirectory);
 53
 54            if (methodFilter != null)
 55            {
 56                assembly.InterceptExceptions(methodFilter);
 57                return;
 58            }
 59
 60            assembly.InterceptAllExceptions();
 61        }
 62
 63        private static void InterceptFields(AssemblyDefinition assembly, string targetDirectory)
 64        {
 65            var fieldFilter = LoadFirstInstanceOf<IFieldFilter>(targetDirectory);
 66            var hostTypeFilter = LoadFirstInstanceOf<ITypeFilter>(targetDirectory);
 67
 68            if (fieldFilter != null && hostTypeFilter != null)
 69            {
 70                assembly.InterceptFields(hostTypeFilter, fieldFilter);
 71                return;
 72            }
 73
 74            assembly.InterceptAllFields();
 75        }
 76
 77        private static void InterceptMethodBodies(AssemblyDefinition assembly, string targetDirectory)
 78        {
 79            var methodFilter = LoadFirstInstanceOf<IMethodFilter>(targetDirectory);
 80            if (methodFilter != null)
 81            {
 82                assembly.InterceptMethodBody(methodFilter);
 83                return;
 84            }
 85
 86            assembly.InterceptAllMethodBodies();
 87        }
 88
 89        private static void InterceptNewInstances(AssemblyDefinition assembly, string targetDirectory)
 90        {
 91            var newInstanceFilter = LoadFirstInstanceOf<INewInstanceFilter>(targetDirectory);
 92            var methodFilter = LoadFirstInstanceOf<IMethodFilter>(targetDirectory);
 93
 94            if (newInstanceFilter != null && methodFilter != null)
 95            {
 96                assembly.InterceptNewInstances(newInstanceFilter, methodFilter);
 97                return;
 98            }
 99
100            assembly.InterceptAllNewInstances();
101        }
102
103        private static void InterceptMethodCalls(AssemblyDefinition assembly, string sourceDirectory)
104        {
105            var methodCallFilter = LoadFirstInstanceOf<IMethodCallFilter>(sourceDirectory);
106            var hostMethodFilter = LoadFirstInstanceOf<IMethodFilter>(sourceDirectory);
107
108            if (methodCallFilter != null && hostMethodFilter != null)
109            {
110                assembly.InterceptMethodCalls(methodCallFilter, hostMethodFilter);
111                return;
112            }
113
114            assembly.InterceptAllMethodCalls();
115        }
116
117        private static T LoadFirstInstanceOf<T>(string sourceDirectory)
118            where T : class
119        {
120            var items = new List<T>();
121            items.LoadFrom(sourceDirectory, "*.filters.dll");
122
123            return items.FirstOrDefault();
124        }
125
126        private static void ShowHelp()
127        {
128            Console.WriteLine("PostWeaver syntax: PostWeaver [filename]");
129        }
130    }
131}