How to automate Visual Studio's Code Metrics feature
Asked Answered
T

3

11

I want to automate the process of gathering code metrics on a .NET solution. Is there any way of getting msbuild to run the Code Metrics feature included in VS2008 Development Edition?

I may end up using SourceMonitor, but I would like to know if there is a way to use the VS code metrics engine from the command line.

Tyranny answered 8/12, 2008 at 15:25 Comment(0)
T
9

Finally, Microsoft have provided us with a way to automate the Visual Studio code metrics feature using a new "power tool".

Tyranny answered 10/2, 2011 at 17:26 Comment(2)
I wanted to run my C++ Solution file (vs 2010) from command for code metric analysis.Will this work for C++ Project ?Cloudburst
The 'power tool' is missingArbutus
C
5

This is how my company has automated FxCop using MSBuild:

<!-- The directory where FxCop is installed. -->
<FxCopDirectory>C:\Program Files\Microsoft FxCop 1.36</FxCopDirectory>

<!-- The FxCop console executable.. -->
<FxCopCmd>$(FxCopDirectory)\FxCopCmd</FxCopCmd>

<Target Name="CodeAnalysis>
<!-- Once to get XML for metrics. -->
<Exec Command="&quot;$(FxCopCmd)&quot; /p:&quot;$(BuildDirectory)\FxCop\RuleSet.FxCop&quot; /out:$(BuildResults)\FxCop.xml /summary /verbose /f:$(Binaries)\@(CodeAnalysis, ' /f:$(Binaries)\')" />

<!-- Once to report with the build results. -->
<Exec Command="&quot;$(FxCopCmd)&quot; /p:&quot;$(BuildDirectory)\FxCop\RuleSet.FxCop&quot; /out:$(BuildResults)\FxCop.html /summary /verbose /applyoutXsl:$(MSBuildTasks)\CodeAnalysisReport.xsl /f:$(Binaries)\@(CodeAnalysis, ' /f:$(Binaries)\')" />

<!-- Update the FxCop report so that it is fully expanded by default. -->
<FileUpdate Regex="&lt;body\s"
            ReplacementText="&lt;body onLoad=&quot;ExpandAll();&quot; "
            Files="$(BuildResults)\FxCop.html" />
</Target>

Then, you can write some C# code to consume the output file:

/// <summary>
/// Gather metrics for code analysis.
/// </summary>
private static void GatherCodeAnalysisMetrics()
{
    string file = @"$(BuildResults)\FxCop.xml";
    if (!File.Exists(file)) return;
    System.Xml.XmlDocument document = new System.Xml.XmlDocument();
    document.Load(file);
    System.Xml.XmlNodeList list = document.SelectNodes("//Message");
    codeAnalysisWarnings = list.Count;

    Console.WriteLine("Code analysis warnings: " + codeAnalysisWarnings);
}
Coiffeur answered 7/1, 2009 at 21:28 Comment(0)
P
2

jgwood - I believe he's referring to Code Metrics (cyclomatic complexity, etc.) and not FxCop. I have been looking for a solution for this as well, as the FxCop rule for complexity has hardcoded threshholds. It sounds like there's no command-line or API for the metrics in VS2008 yet (per this post on the Code Analysis Team Blog) - hopefully they'll release a powertool.

Have you looked at NDepend for this?

Pokelogan answered 20/1, 2009 at 14:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.