Visual studio 2012 共享代码分析规则和字典

Visual studio 2012 共享代码分析规则和字典,visual-studio-2012,code-analysis,fxcop,Visual Studio 2012,Code Analysis,Fxcop,我们将转到Visual Studio Professional 2012,现在可以访问代码分析 之前,我们使用FxCop并传递了参数SourceControlPath\OurFxCopSharedProject.FxCop。我们还将CustomDictionary.xml添加到包含FxCop.exe的(源代码控制)文件夹中。这非常有效,因为我们使用了许多特定于行业的术语,并使我们的分析规则标准化 在VS 2012中,似乎和 是否有办法将现有项目和新项目默认为我们的标准.ruleset和Custo

我们将转到Visual Studio Professional 2012,现在可以访问代码分析

之前,我们使用FxCop并传递了参数
SourceControlPath\OurFxCopSharedProject.FxCop
。我们还将
CustomDictionary.xml
添加到包含FxCop.exe的(源代码控制)文件夹中。这非常有效,因为我们使用了许多特定于行业的术语,并使我们的分析规则标准化

在VS 2012中,似乎和


是否有办法将现有项目和新项目默认为我们的标准
.ruleset
CustomDictionary.xml
(而不是Microsoft最低推荐规则和标准字典)?我们有数百个解决方案和更多的项目。

是的,至少你可以为VS2010提供解决方案,我认为它对VS2012也同样有效

我遵循中描述的细节,这对我来说非常有效

VS2010中还存在一个问题,即为构建的第一个项目配置的规则集成为所有项目的规则集,而不管配置了什么。这对我来说效果不好,因为我放松了单元测试项目的规则。我认为(希望)它在VS2012中得到了修复,但如果不是,这个过程也提供了一个解决方法


更新

这个博客已经不存在了。我在下面找到了

样品溶液仍然可以在以下位置找到:

一切功劳归于


不使用Visual Studio 2010的代码分析

这篇文章提供了如何将VisualStudio2010的代码分析集成到构建中的分步说明。如果您还没有这样做,请阅读我的第一篇文章,了解将要实现的目标。如果愿意,可以按照我上一篇文章中的说明集成VisualStudio2008的代码分析。虽然您需要安装VisualStudio2010的计算机,但在正确设置项目后,构建项目不需要这样做。我假设Visual Studio安装在默认位置-根据需要调整路径

假设我们从以下目录结构开始:

Project 
    Lib 
    Src
步骤1:复制代码分析工具

C:\Program Files\Microsoft Visual Studio 10.0\Team Tools\Static Analysis Tools\FxCop
的全部内容复制到
Lib\code Analysis

C:\Program Files\MSBuild\Microsoft\VisualStudio\v10.0\CodeAnalysis
的全部内容复制到
Lib\code Analysis

将以下文件复制到
Lib\code Analysis

  • C:\Windows\Microsoft.NET\assembly\GAC\u MSIL\Microsoft.VisualC\v4.0\u 10.0.0.0\u b03f5f7f11d50a3a\Microsoft.VisualC.Dll
  • C:\Windows\Microsoft.NET\assembly\GAC\U MSIL\Microsoft.VisualStudio.CodeAnalysis.Sdk\v4.0\U 10.0.0.0\Uuu b03f5f7f11d50a3a\Microsoft.VisualStudio.CodeAnalysis.Sdk.dll
  • C:\Program Files\Microsoft Visual Studio 10.0\DIA SDK\bin\msdia100.dll
    C:\ProgramFiles\Microsoft Visual Studio 10.0\VC\redist\x86\Microsoft.VC100.CRT\msvcp100.dll
  • C:\ProgramFiles\Microsoft Visual Studio 10.0\VC\redist\x86\Microsoft.VC100.CRT\msvcr100.dll
步骤2:创建代码分析目标文件

创建一个名为
codealysis.targets
的文件,并将其放入
Src
目录中。以下是此文件内容的起点。您应该根据需要调整:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        <!--
        Inject our own target before the code analysis runs.
        -->
        <RunCodeAnalysisDependsOn>
            ConfigureCodeAnalysis;
            $(RunCodeAnalysisDependsOn);
        </RunCodeAnalysisDependsOn>

        <!--
        Ensure code analysis is run
        -->
        <RunCodeAnalysis>True</RunCodeAnalysis>

        <!--
        Set this to false if you don't want all code analysis violations to be treated as errors.
        -->
        <CodeAnalysisTreatWarningsAsErrors>True</CodeAnalysisTreatWarningsAsErrors>
        <!--
        This should be set to resolve to the Lib directory, which must contain the code analysis tooling.
        -->
        <PathToLib>$(MSBuildProjectDirectory)\..\..\Lib\</PathToLib>
        <!--
        This should be set to resolve to the directory containing this targets file.
        -->
        <PathToTargets>$(MSBuildProjectDirectory)\..\</PathToTargets>

        <!--
        Setting these properties is required in order for the code analysis targets to execute correctly.
        Without setting these, it will look for the tooling under default installation directories instead
        -->
        <CodeAnalysisTargets>$(PathToLib)\Code Analysis\Microsoft.CodeAnalysis.Targets</CodeAnalysisTargets>
        <CodeAnalysisPath>$(PathToLib)\Code Analysis</CodeAnalysisPath>
        <!--
        Assign default code analysis rules
        -->
        <CodeAnalysisRuleSet>$(PathToTargets)CodeAnalysis.Default.ruleset</CodeAnalysisRuleSet>
    </PropertyGroup>
    <UsingTask AssemblyFile="$(PathToLib)\MSBuildSdcTasks\Microsoft.Sdc.Tasks.dll" TaskName="StringComparison"/>
    <Target Name="ConfigureCodeAnalysis">
        <!--
        Assume that any projects with ".Tests" in their names are test projects
        -->
        <StringComparison Comparison="Contains" Param1="$(AssemblyName)" Param2=".Tests">
            <Output TaskParameter="Result" PropertyName="IsTestProject"/>
        </StringComparison>
        <!--
        Assign different rules for test projects (more relaxed)
        -->
        <CreateProperty Condition="$(IsTestProject)" Value="$(PathToTargets)CodeAnalysis.Tests.ruleset">
            <Output TaskParameter="Value" PropertyName="CodeAnalysisRuleSet"/>
        </CreateProperty>
    </Target>
</Project>
注意:在导入Microsoft.CSharp.targets之前而不是之后插入,这一点非常重要

第5步:根据需要调整

您可能希望调整
代码分析.targets
代码分析.Default.ruleset
代码分析.Tests.ruleset
文件,以更改启用的规则、使用规则集的条件等。如上所述,您可以使用VS2010创建和编辑
规则集
文件

代码分析现在已与您的项目集成。无论是Visual Studio、命令行还是构建服务器,从何处构建都是不相关的。在所有情况下,都会对项目执行代码分析。您可以下载一个示例解决方案,在下面展示所有这些功能

故障排除

如果获得
CA0001 Phx.FatalError
,则可能需要在生成计算机上注册
msdia100.dll

regsvr32 msdia100.dll
如果构建用户有足够的权限,您也可以将其合并到构建脚本中

regsvr32 msdia100.dll