Reputation:
I have integrated StyleCop into a build pipeline using StyleCop.MSBuild nuget package. It works great, but it has some limitations. I can fail a build by treating StyleCop violations as errors. This does the trick, but it means that I can't have any violations at all.
What we would like to do, is gradually introduce StyleCop into the build pipeline and allow developers to have 10 or maybe even 20 StyleCop violations in some projects.
I tried setting StyleCopMaxViolationCount to 1, hoping that build would fail if 1 violation is picked up, but that didn't work - build succeeded and the warning was displayed.
<StyleCopMaxViolationCount Condition="'$(StyleCopMaxViolationCount)' == ''">1</StyleCopMaxViolationCount>
Upvotes: 0
Views: 438
Reputation: 342
Some time ago I did the very same thing, good thing it worked for us!
I didn't find an easy (straightforward) way of doing it. So I first ran a task directly from StyleCop.dll (note it was version 4.6 then) and put results in file. Then I just wrote a simple MSBuild task to find number of errors put into that file:
public class AnalyzeStylecopResults : ITask
{
private IBuildEngine engine;
public IBuildEngine BuildEngine
{
get { return engine; }
set { engine = value; }
}
private ITaskHost host;
public ITaskHost HostObject
{
get { return host; }
set { host = value; }
}
public bool Execute()
{
XDocument xdoc = XDocument.Load(StylecopResultsFile);
_violationCount = xdoc.Descendants().Count();
return true;
}
private string _stylecopResultsFile;
[Required]
public string StylecopResultsFile
{
get { return _stylecopResultsFile; }
set { _stylecopResultsFile = value; }
}
int _violationCount;
[Output]
public int ViolationCount
{
get { return _violationCount; }
set { _violationCount = value; }
}
}
Then I created msbuild file (stylecopvalidate.msbuild) like this:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
<UsingTask AssemblyFile="StylecopAnalyzerTasks.dll" TaskName="StylecopAnalyzerTasks.AnalyzeStylecopResults" />
<Target Name="StyleCop">
<AnalyzeStylecopResults StylecopResultsFile="$(CCNetWorkingDirectory)\stylecop-results.xml">
<Output TaskParameter="ViolationCount" PropertyName="ViolationCount" />
</AnalyzeStylecopResults>
<Error Text="Violations count in results file: $(ViolationCount) but max allowed: $(MaxViolationCount)" Condition=" $(ViolationCount) > $(MaxViolationCount) " />
</Target>
</Project>
And finally called this from msbuild task:
<msbuild>
<description>Stylecop task</description>
<executable>$(MSBuild4Path)</executable>
<projectFile>path\to\stylecopvalidate.msbuild</projectFile>
<timeout>120</timeout>
<logger>$(MSBuildLoggerPath)</logger>
</msbuild>
Hope it helps, unfortunately I don't have any more "recent" version.
Upvotes: 1