Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using Microsoft.Build.Framework;
- using Microsoft.Build.Utilities;
- using System.Threading;
- namespace Intrahealth.Build.WarningLogger
- {
- public sealed class WarningLoggerCheck : Task
- {
- public override bool Execute()
- {
- Log.LogMessage("WarningLoggerCheck:" + mutexName + "...");
- result = false;
- Mutex m = null;
- try
- {
- m = Mutex.OpenExisting(mutexName);
- }
- catch (WaitHandleCannotBeOpenedException)
- {
- result = true;
- }
- catch (Exception)
- {
- }
- if (result)
- Log.LogMessage("WarningLoggerCheck PASSED");
- else
- Log.LogError("Build log contains warnings. Build is FAILED");
- return result;
- }
- private bool result = true;
- [Output]
- public bool Result
- {
- get { return result; }
- }
- private string mutexName = "WarningLoggerMutex";
- public string MutexName
- {
- get { return mutexName; }
- set { mutexName = value ?? "WarningLoggerMutex"; }
- }
- }
- public class WarningLogger : Logger
- {
- internal static int warningsCount = 0;
- private string mutexName = String.Empty;
- private Mutex mutex = null;
- public override void Initialize(IEventSource eventSource)
- {
- eventSource.WarningRaised += new BuildWarningEventHandler(eventSource_WarningRaised);
- }
- private void SetMutex()
- {
- if (mutexName == String.Empty)
- {
- mutexName = "WarningLoggerMutex";
- if (this.Parameters != null && this.Parameters != String.Empty)
- {
- mutexName = this.Parameters;
- }
- }
- mutex = new Mutex(false, mutexName);
- }
- void eventSource_WarningRaised(object sender, BuildWarningEventArgs e)
- {
- if (e.Message != null && e.Message.Contains("MSB3146"))
- return;
- if (e.Code != null && e.Code.Equals("MSB3146"))
- return;
- if (warningsCount == 0)
- SetMutex();
- warningsCount++;
- }
- }
- }
- namespace SampleLogger
- {
- using System;
- using Microsoft.Build.Utilities;
- using Microsoft.Build.Framework;
- using DM.SharedMemory;
- public class MySimpleLogger : Logger
- {
- private Segment s;
- private int warningCount;
- public override void Initialize(IEventSource eventSource)
- {
- eventSource.WarningRaised += new BuildWarningEventHandler(eventSource_WarningRaised);
- this.s = new Segment("MSBuildMetadata", SharedMemoryCreationFlag.Create, 65535);
- this.s.SetData(this.warningCount.ToString());
- }
- void eventSource_WarningRaised(object sender, BuildWarningEventArgs e)
- {
- this.warningCount++;
- this.s.SetData(this.warningCount.ToString());
- }
- public override void Shutdown()
- {
- this.s.Dispose();
- base.Shutdown();
- }
- }
- }
- namespace SampleTasks
- {
- using System;
- using Microsoft.Build.Utilities;
- using Microsoft.Build.Framework;
- using DM.SharedMemory;
- public class BuildMetadata : Task
- {
- public int warningCount;
- [Output]
- public int WarningCount
- {
- get
- {
- Segment s = new Segment("MSBuildMetadata", SharedMemoryCreationFlag.Attach, 0);
- int warningCount = Int32.Parse(s.GetData() as string);
- return warningCount;
- }
- }
- public override bool Execute()
- {
- return true;
- }
- }
- }
- <?xml version="1.0" encoding="UTF-8"?>
- <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Main">
- <UsingTask TaskName="BuildMetadata" AssemblyFile="F:tempSampleLoggerbindebugSampleTasks.dll" />
- <Target Name="Main">
- <Warning Text="Sample warning #1" />
- <Warning Text="Sample warning #2" />
- <BuildMetadata>
- <Output
- TaskParameter="WarningCount"
- PropertyName="WarningCount" />
- </BuildMetadata>
- <Error Text="A total of $(WarningCount) warning(s) were raised." Condition="$(WarningCount) > 0" />
- </Target>
- </Project>
- c:WINDOWSMicrosoft.NETFrameworkv2.0.50727MSBuild test.xml /logger:SampleLogger.dll
- Microsoft (R) Build Engine Version 2.0.50727.3053
- [Microsoft .NET Framework, Version 2.0.50727.3053]
- Copyright (C) Microsoft Corporation 2005. All rights reserved.
- Build started 30-09-2008 13:04:39.
- __________________________________________________
- Project "F:tempSampleLoggerbindebugtest.xml" (default targets):
- Target Main:
- F:tempSampleLoggerbindebugtest.xml : warning : Sample warning #1
- F:tempSampleLoggerbindebugtest.xml : warning : Sample warning #2
- F:tempSampleLoggerbindebugtest.xml(15,3): error : A total of 2 warning(s) were raised.
- Done building target "Main" in project "test.xml" -- FAILED.
- Done building project "test.xml" -- FAILED.
- Build FAILED.
- F:tempSampleLoggerbindebugtest.xml : warning : Sample warning #1
- F:tempSampleLoggerbindebugtest.xml : warning : Sample warning #2
- F:tempSampleLoggerbindebugtest.xml(15,3): error : A total of 2 warning(s) were raised.
- 2 Warning(s)
- 1 Error(s)
- Time Elapsed 00:00:00.01
- msbuild.exe %~nx1 /t:Rebuild /p:Configuration=Release >> %MrB-BUILDLOG%
- findstr /r /c:"[1-9][0-9]* Error(s)" >> %MrB-BUILDLOG%
- if not errorlevel 1 (
- echo ERROR: sending notification email for build errors in '%~nx1'. >> %MrB-BUILDLOG%
- ) else (
- findstr /r /c:"[1-9][0-9]* Warning(s)" >> %MrB-BUILDLOG%
- if not errorlevel 1 (
- echo ERROR: sending notification email for build warnings in '%~nx1'. >>
Add Comment
Please, Sign In to add comment