Delphi および C# プロジェクト、単体テストなどの番号を含む MSBuild スクリプトがあります。
問題は、警告が発生した場合にビルドに失敗したことをマークする方法です (リリース ビルドではなく、テスト目的で)。カスタム タスクで LogWarning の代わりに LogError を使用することは、適切なオプションではないようです。これは、ビルドが可能な限り (実際のエラーが発生するまで) できるだけ多くの警告を報告するようにテストする必要があるためです (ビルド プロジェクトは CruiseControl.NET で使用されています)。 )。
おそらく、解決策は警告フラグを内部に保存する独自のロガーを作成することですが、ビルドの最後にこのフラグを読み取る方法があるかどうかわかりませんか?
PS 警告を受け取った直後にビルドを失敗しても問題ありませんが (Delphi コンパイラの出力はカスタム タスクによって処理され、C# では /warnaserror を使用できます)、望ましい動作は「すべてをビルドし、すべての警告を収集し、ビルドを失敗させます」です。最初の警告だけでなく、すべての警告について報告します。
PPS 警告の数ではなく、その存在のフラグだけが本当に必要な限り、シグナル伝達メカニズムを簡素化し、共有メモリの代わりに単純な Mutex を使用することにしました。コードは以下のとおりです。
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++;
}
}
}