code_junkie

MSBuild: How to obtain number of warnings raised?

Nov 11th, 2011
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.83 KB | None | 0 0
  1. using System;
  2. using Microsoft.Build.Framework;
  3. using Microsoft.Build.Utilities;
  4. using System.Threading;
  5.  
  6. namespace Intrahealth.Build.WarningLogger
  7. {
  8. public sealed class WarningLoggerCheck : Task
  9. {
  10. public override bool Execute()
  11. {
  12. Log.LogMessage("WarningLoggerCheck:" + mutexName + "...");
  13. result = false;
  14. Mutex m = null;
  15. try
  16. {
  17. m = Mutex.OpenExisting(mutexName);
  18. }
  19. catch (WaitHandleCannotBeOpenedException)
  20. {
  21. result = true;
  22. }
  23. catch (Exception)
  24. {
  25. }
  26.  
  27. if (result)
  28. Log.LogMessage("WarningLoggerCheck PASSED");
  29. else
  30. Log.LogError("Build log contains warnings. Build is FAILED");
  31.  
  32. return result;
  33. }
  34.  
  35. private bool result = true;
  36. [Output]
  37. public bool Result
  38. {
  39. get { return result; }
  40. }
  41.  
  42. private string mutexName = "WarningLoggerMutex";
  43. public string MutexName
  44. {
  45. get { return mutexName; }
  46. set { mutexName = value ?? "WarningLoggerMutex"; }
  47. }
  48. }
  49.  
  50. public class WarningLogger : Logger
  51. {
  52. internal static int warningsCount = 0;
  53. private string mutexName = String.Empty;
  54. private Mutex mutex = null;
  55.  
  56. public override void Initialize(IEventSource eventSource)
  57. {
  58. eventSource.WarningRaised += new BuildWarningEventHandler(eventSource_WarningRaised);
  59. }
  60.  
  61. private void SetMutex()
  62. {
  63. if (mutexName == String.Empty)
  64. {
  65. mutexName = "WarningLoggerMutex";
  66. if (this.Parameters != null && this.Parameters != String.Empty)
  67. {
  68. mutexName = this.Parameters;
  69. }
  70. }
  71.  
  72. mutex = new Mutex(false, mutexName);
  73. }
  74.  
  75. void eventSource_WarningRaised(object sender, BuildWarningEventArgs e)
  76. {
  77. if (e.Message != null && e.Message.Contains("MSB3146"))
  78. return;
  79. if (e.Code != null && e.Code.Equals("MSB3146"))
  80. return;
  81.  
  82. if (warningsCount == 0)
  83. SetMutex();
  84. warningsCount++;
  85. }
  86. }
  87. }
  88.  
  89. namespace SampleLogger
  90. {
  91. using System;
  92. using Microsoft.Build.Utilities;
  93. using Microsoft.Build.Framework;
  94. using DM.SharedMemory;
  95.  
  96. public class MySimpleLogger : Logger
  97. {
  98. private Segment s;
  99. private int warningCount;
  100.  
  101. public override void Initialize(IEventSource eventSource)
  102. {
  103. eventSource.WarningRaised += new BuildWarningEventHandler(eventSource_WarningRaised);
  104.  
  105. this.s = new Segment("MSBuildMetadata", SharedMemoryCreationFlag.Create, 65535);
  106. this.s.SetData(this.warningCount.ToString());
  107. }
  108.  
  109. void eventSource_WarningRaised(object sender, BuildWarningEventArgs e)
  110. {
  111. this.warningCount++;
  112. this.s.SetData(this.warningCount.ToString());
  113. }
  114.  
  115. public override void Shutdown()
  116. {
  117. this.s.Dispose();
  118. base.Shutdown();
  119. }
  120. }
  121. }
  122.  
  123. namespace SampleTasks
  124. {
  125. using System;
  126. using Microsoft.Build.Utilities;
  127. using Microsoft.Build.Framework;
  128. using DM.SharedMemory;
  129.  
  130. public class BuildMetadata : Task
  131. {
  132. public int warningCount;
  133.  
  134. [Output]
  135. public int WarningCount
  136. {
  137. get
  138. {
  139. Segment s = new Segment("MSBuildMetadata", SharedMemoryCreationFlag.Attach, 0);
  140. int warningCount = Int32.Parse(s.GetData() as string);
  141. return warningCount;
  142. }
  143. }
  144.  
  145. public override bool Execute()
  146. {
  147. return true;
  148. }
  149. }
  150. }
  151.  
  152. <?xml version="1.0" encoding="UTF-8"?>
  153. <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Main">
  154. <UsingTask TaskName="BuildMetadata" AssemblyFile="F:tempSampleLoggerbindebugSampleTasks.dll" />
  155.  
  156. <Target Name="Main">
  157. <Warning Text="Sample warning #1" />
  158. <Warning Text="Sample warning #2" />
  159.  
  160. <BuildMetadata>
  161. <Output
  162. TaskParameter="WarningCount"
  163. PropertyName="WarningCount" />
  164. </BuildMetadata>
  165.  
  166. <Error Text="A total of $(WarningCount) warning(s) were raised." Condition="$(WarningCount) > 0" />
  167. </Target>
  168. </Project>
  169.  
  170. c:WINDOWSMicrosoft.NETFrameworkv2.0.50727MSBuild test.xml /logger:SampleLogger.dll
  171.  
  172. Microsoft (R) Build Engine Version 2.0.50727.3053
  173. [Microsoft .NET Framework, Version 2.0.50727.3053]
  174. Copyright (C) Microsoft Corporation 2005. All rights reserved.
  175.  
  176. Build started 30-09-2008 13:04:39.
  177. __________________________________________________
  178. Project "F:tempSampleLoggerbindebugtest.xml" (default targets):
  179.  
  180. Target Main:
  181. F:tempSampleLoggerbindebugtest.xml : warning : Sample warning #1
  182. F:tempSampleLoggerbindebugtest.xml : warning : Sample warning #2
  183. F:tempSampleLoggerbindebugtest.xml(15,3): error : A total of 2 warning(s) were raised.
  184. Done building target "Main" in project "test.xml" -- FAILED.
  185.  
  186. Done building project "test.xml" -- FAILED.
  187.  
  188. Build FAILED.
  189. F:tempSampleLoggerbindebugtest.xml : warning : Sample warning #1
  190. F:tempSampleLoggerbindebugtest.xml : warning : Sample warning #2
  191. F:tempSampleLoggerbindebugtest.xml(15,3): error : A total of 2 warning(s) were raised.
  192. 2 Warning(s)
  193. 1 Error(s)
  194.  
  195. Time Elapsed 00:00:00.01
  196.  
  197. msbuild.exe %~nx1 /t:Rebuild /p:Configuration=Release >> %MrB-BUILDLOG%
  198. findstr /r /c:"[1-9][0-9]* Error(s)" >> %MrB-BUILDLOG%
  199. if not errorlevel 1 (
  200. echo ERROR: sending notification email for build errors in '%~nx1'. >> %MrB-BUILDLOG%
  201. ) else (
  202. findstr /r /c:"[1-9][0-9]* Warning(s)" >> %MrB-BUILDLOG%
  203. if not errorlevel 1 (
  204. echo ERROR: sending notification email for build warnings in '%~nx1'. >>
Add Comment
Please, Sign In to add comment