/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

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