如何自动化 Visual Studio 的代码指标功能

发布于 2024-07-10 17:07:12 字数 222 浏览 4 评论 0原文

我想自动化在 .NET 解决方案上收集代码指标的过程。 有没有办法让msbuild运行VS2008开发版中包含的Code Metrics功能?

我可能最终会使用 SourceMonitor,但我想知道是否有办法使用来自命令行的 VS Code 指标引擎。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

羁拥 2024-07-17 17:07:13

最后,微软为我们提供了 一种使用新的“电动工具"。

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

可是我不能没有你 2024-07-17 17:07:13

这就是我的公司使用 MSBuild 自动化 FxCop 的方式:

<!-- 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=""$(FxCopCmd)" /p:"$(BuildDirectory)\FxCop\RuleSet.FxCop" /out:$(BuildResults)\FxCop.xml /summary /verbose /f:$(Binaries)\@(CodeAnalysis, ' /f:$(Binaries)\')" />

<!-- Once to report with the build results. -->
<Exec Command=""$(FxCopCmd)" /p:"$(BuildDirectory)\FxCop\RuleSet.FxCop" /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="<body\s"
            ReplacementText="<body onLoad="ExpandAll();" "
            Files="$(BuildResults)\FxCop.html" />
</Target>

然后,您可以编写一些 C# 代码来使用输出文件:

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

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=""$(FxCopCmd)" /p:"$(BuildDirectory)\FxCop\RuleSet.FxCop" /out:$(BuildResults)\FxCop.xml /summary /verbose /f:$(Binaries)\@(CodeAnalysis, ' /f:$(Binaries)\')" />

<!-- Once to report with the build results. -->
<Exec Command=""$(FxCopCmd)" /p:"$(BuildDirectory)\FxCop\RuleSet.FxCop" /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="<body\s"
            ReplacementText="<body onLoad="ExpandAll();" "
            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);
}
听风吹 2024-07-17 17:07:13

jgwood - 我相信他指的是代码度量(圈复杂度等)而不是 FxCop。 我也一直在寻找解决方案,因为 FxCop 的复杂性规则具有硬编码阈值。 听起来 VS2008 中还没有用于指标的命令行或 API(根据 代码分析团队博客上的这篇文章) - 希望他们会发布一个强大的工具。

您是否查看过 NDepend

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文