6

有一个 MSBuild 脚本,其中包括 Delphi 和 C# 项目、单元测试等的编号。

问题是:如果出现警告,如何标记构建失败(出于测试目的,而不是发布构建)?在自定义任务中使用 LogError 而不是 LogWarning 似乎不是一个好的选择,因为构建应该尽可能多地测试(直到真正的错误)一次报告尽可能多的警告(构建项目在 CruiseControl.NET 中使用)。

可能是,解决方案是创建我自己的记录器,在里面存储警告标志,但我找不到在构建结束时是否有办法读取这个标志?

PS 收到警告后立即构建失败没有问题(Delphi 编译器输出由自定义任务处理,/warnaserror 可用于 C#),但期望的行为是“构建所有内容;收集所有警告;构建失败" 报告所有警告,而不仅仅是第一个警告。

PPS 至于我真的不需要警告的数量,而只是他们存在的标志,我决定简化信号机制,并使用微不足道的互斥体而不是共享内存。代码如下:

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++;
        }
    }
}
4

3 回答 3

7

AFAIK MSBuild 没有内置支持来检索构建脚本给定点的警告计数。但是,您可以按照以下步骤来实现此目标:

  1. 创建一个自定义记录器,用于监听警告事件并计算警告次数
  2. 创建一个公开 [Output] WarningCount 属性的自定义任务
  3. 自定义任务以某种方式从自定义记录器获取警告计数的值

最困难的步骤是第 3 步。为此有几个选项,您可以在 IPC - Inter Process Comunication 下自由搜索它们。遵循如何实现这一目标的工作示例。每个项目都是一个不同的类库

共享内存

http://weblogs.asp.net/rosherove/archive/2003/05/01/6295.aspx

我为作为一个更大项目的一部分的命名共享内存创建了一个包装器。它基本上允许在共享内存中存储和检索序列化类型和对象图(包括您期望的跨进程)。更大的项目是否完成是另一回事;-)。

采样记录器

实现跟踪警告计数的自定义记录器。

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();
        }
    }
}

示例任务

实现读取 MSbuild 项目中引发的警告数量的自定义任务。自定义任务从类库SampleLogger中实现的自定义记录器写入的共享内存中读取。

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:\temp\SampleLogger\bin\debug\SampleTasks.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:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MSBuild 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:\temp\SampleLogger\bin\debug\test.xml" (default targets):

Target Main:
    F:\temp\SampleLogger\bin\debug\test.xml : warning : Sample warning #1
    F:\temp\SampleLogger\bin\debug\test.xml : warning : Sample warning #2
    F:\temp\SampleLogger\bin\debug\test.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:\temp\SampleLogger\bin\debug\test.xml : warning : Sample warning #1
F:\temp\SampleLogger\bin\debug\test.xml : warning : Sample warning #2
F:\temp\SampleLogger\bin\debug\test.xml(15,3): error : A total of 2 warning(s) were raised.
    2 Warning(s)
    1 Error(s)

Time Elapsed 00:00:00.01
于 2008-09-30T11:46:49.513 回答
1

C# 编译器 (csc.exe) 有一个 /warnaserror 开关,会将警告视为错误并导致构建失败。这也可用作 .csproj 文件中的设置。我认为德尔福有类似的能力。

于 2008-09-30T10:31:24.220 回答
1
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'. >>

%MrB-BUILDLOG% ) else ( echo 成功构建 '%~nx1'。>> %MrB-BUILDLOG% ) )

于 2008-09-30T10:39:24.357 回答