寻找在引入新警告时破坏构建的构建活动
-
09-12-2019 - |
题
我们试图清理一大堆棕色字段代码,与此同时,一个团队正在添加新的功能。我们希望确保从任何编译器/代码分析或其他警告中清除已更改和新代码,但是从清理当前解决方案开始有太多的代码。
我们使用的是TFS2010。
于是提出了以下:
- 编写/选择一个生成活动,该活动将生成中的警告列表与该签入更改的代码行进行比较。
- 如果警告提供了行号,并且该行号已更改,则生成失败。
我明白这不会找到所有新的警告,并且在代码的其他部分引入的东西不会被标记,但它至少是一些东西。
提出的另一种选择:
- 将先前已知良好构建版本的警告列表与此构建版本的列表进行比较。如果有新的警告(跟踪文件名级别),则失败构建。
任何已知的行动在那里,可能提供上述功能?
可以对代码复盖率报告采取行动的任何类似操作?
解决方案
下面的活动只是一个基本的方法,它返回 false
如果当前构建的警告少于或等于上次构建的警告,并且 true
如果他们复活了。
另一个可以定位的活动 新的 警告和/或在代码中显示它们的位置显然会更好,但我认为这可能是一个有趣的起点:
using System;
using System.Activities;
using Microsoft.TeamFoundation.Build.Client;
using Microsoft.TeamFoundation.Build.Workflow.Activities;
namespace CheckWarnings
{
[BuildActivity(HostEnvironmentOption.Agent)]
public sealed class CheckWarnings : CodeActivity<bool>
{
[RequiredArgument]
public InArgument<IBuildDetail> CurrentBuild { get; set; } //buildDetail
public InArgument<string> Configuration { get; set; } //platformConfiguration.Configuration
public InArgument<string> Platform { get; set; } //platformConfiguration.Platform
protected override bool Execute(CodeActivityContext context)
{
IBuildDetail currentBuildDetail = context.GetValue(CurrentBuild);
string currentConfiguration = context.GetValue(Configuration);
string currentPlatform = context.GetValue(Platform);
Uri lastKnownGoodBuildUri = currentBuildDetail.BuildDefinition.LastGoodBuildUri;
IBuildDetail lastKnownGoodBuild = currentBuildDetail.BuildServer.GetBuild(lastKnownGoodBuildUri);
int numOfCurrentWarnings = GetNumberOfWarnings(currentBuildDetail, currentConfiguration, currentPlatform);
context.TrackBuildMessage("Current compile presents " + numOfCurrentWarnings + " warnings.", BuildMessageImportance.Normal);
int numOfLastGoodBuildWarnings = GetNumberOfWarnings(lastKnownGoodBuild, currentConfiguration,
currentPlatform);
context.TrackBuildMessage("Equivalent last good build compile presents " + numOfLastGoodBuildWarnings + " warnings.", BuildMessageImportance.Normal);
if (numOfLastGoodBuildWarnings < numOfCurrentWarnings)
{
return true;
}
return false;
}
private static int GetNumberOfWarnings(IBuildDetail buildDetail, string configuration, string platform)
{
var buildInformationNodes =
buildDetail.Information.GetNodesByType("ConfigurationSummary");
foreach (var buildInformationNode in buildInformationNodes)
{
string localPlatform, numOfWarnings;
string localConfiguration = localPlatform = numOfWarnings = "";
foreach (var field in buildInformationNode.Fields)
{
if (field.Key == "Flavor")
{
localConfiguration = field.Value;
}
if (field.Key == "Platform")
{
localPlatform = field.Value;
}
if (field.Key == "TotalCompilationWarnings")
{
numOfWarnings = field.Value;
}
}
if(localConfiguration == configuration && localPlatform == platform)
{
return Convert.ToInt32((numOfWarnings));
}
}
return 0;
}
}
}
请注意,此活动不提供异常处理,应该进一步细化,以防您的构建定义构建多个解决方案。
它需要三个输入args(buildDetail
, platformConfiguration.Configuration
和 platformConfiguration.Platform
),并应直接放在 Run MSBuild
活动。
不隶属于 StackOverflow