PageRenderTime 82ms CodeModel.GetById 40ms app.highlight 5ms RepoModel.GetById 35ms app.codeStats 0ms

/src/LinFu.AOP.Tasks/PostWeaveTask.cs

http://github.com/philiplaureano/LinFu
C# | 117 lines | 54 code | 19 blank | 44 comment | 6 complexity | c0b34672c6798183a51bdd000d6e5868 MD5 | raw file
  1using System;
  2using System.IO;
  3using LinFu.AOP.Cecil.Extensions;
  4using LinFu.Reflection.Emit;
  5using Microsoft.Build.Framework;
  6using Microsoft.Build.Utilities;
  7using Mono.Cecil;
  8
  9namespace LinFu.AOP.Tasks
 10{
 11    /// <summary>
 12    ///     Represents an MSBuild task for LinFu.AOP that allows users to inject an aspect framework into their applications
 13    ///     at postbuild time.
 14    /// </summary>
 15    public class PostWeaveTask : Task
 16    {
 17        /// <summary>
 18        ///     Gets or sets the value indicating the full path and filename of the target assembly.
 19        /// </summary>
 20        /// <value>The target assembly filename.</value>
 21        [Required]
 22        public string TargetFile { get; set; }
 23
 24        /// <summary>
 25        ///     Gets or sets the value indicating the full path and filename of the output assembly.
 26        /// </summary>
 27        /// <value>The output assembly filename.</value>
 28        /// <remarks>
 29        ///     This field is optional; if blank, the default value will be the same value as the <see cref="TargetFile" />
 30        ///     property.
 31        /// </remarks>
 32        public string OutputFile { get; set; }
 33
 34        /// <summary>
 35        ///     Gets or sets the value indicating whether or not third party method calls should be intercepted in the target
 36        ///     assembly.
 37        /// </summary>
 38        /// <value>A boolean value indicating whether or not third party method call interception should be enabled.</value>
 39        public bool InterceptAllMethodCalls { get; set; }
 40
 41        /// <summary>
 42        ///     Gets or sets the value indicating whether or not method bodies should be intercepted in the target assembly.
 43        /// </summary>
 44        /// <value>A boolean value indicating whether or not method body interception should be enabled.</value>
 45        public bool InterceptAllMethodBodies { get; set; }
 46
 47        /// <summary>
 48        ///     Gets or sets the value indicating whether or not new instances should be intercepted in the target assembly.
 49        /// </summary>
 50        /// <value>A boolean value indicating whether or not new instance interception should be enabled.</value>
 51        public bool InterceptAllNewInstances { get; set; }
 52
 53        /// <summary>
 54        ///     Gets or sets the value indicating whether or not field reads and writes should be intercepted in the target
 55        ///     assembly.
 56        /// </summary>
 57        /// <value>A boolean value indicating whether or not field reads and writes should be enabled.</value>
 58        public bool InterceptAllFields { get; set; }
 59
 60        /// <summary>
 61        ///     Gets or sets the value indicating whether or not thrown exceptions should be intercepted in the target assembly.
 62        /// </summary>
 63        /// <value>A boolean value indicating whether or not exception interception should be enabled.</value>
 64        public bool InterceptAllExceptions { get; set; }
 65
 66        /// <summary>
 67        ///     Executes the postweaver.
 68        /// </summary>
 69        /// <returns>Returns <c>true</c> if the operation succeeded. Otherwise, it will return <c>false</c>.</returns>
 70        public override bool Execute()
 71        {
 72            // The output file name will be the same as the target
 73            // file by default 
 74            var outputFile = OutputFile;
 75            if (string.IsNullOrEmpty(outputFile))
 76                outputFile = TargetFile;
 77
 78            var result = true;
 79            try
 80            {
 81                Log.LogMessage("PostWeaving Assembly '{0}' -> '{1}'", TargetFile, outputFile);
 82
 83                var assembly = AssemblyDefinition.ReadAssembly(TargetFile);
 84
 85                var filenameWithoutExtension = Path.GetFileNameWithoutExtension(TargetFile);
 86                var pdbFileName = string.Format("{0}.pdb", filenameWithoutExtension);
 87                var pdbExists = File.Exists(pdbFileName);
 88
 89                var module = assembly.MainModule;
 90
 91                if (InterceptAllMethodCalls)
 92                    assembly.InterceptAllMethodCalls();
 93
 94                if (InterceptAllNewInstances)
 95                    assembly.InterceptAllNewInstances();
 96
 97                if (InterceptAllMethodBodies)
 98                    assembly.InterceptAllMethodBodies();
 99
100                if (InterceptAllFields)
101                    assembly.InterceptAllFields();
102
103                if (InterceptAllExceptions)
104                    assembly.InterceptAllExceptions();
105
106                assembly.Save(outputFile);
107            }
108            catch (Exception ex)
109            {
110                Log.LogErrorFromException(ex);
111                result = false;
112            }
113
114            return result;
115        }
116    }
117}